-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpathways_paper_fitting.Rmd
More file actions
259 lines (167 loc) · 6.87 KB
/
pathways_paper_fitting.Rmd
File metadata and controls
259 lines (167 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
---
title: "Patient pathways paper - code for LoS fitting"
author: "Quentin J. Leclerc, Gwenan M. Knight"
date: "`r format(Sys.time(), '%A %d %B %Y')`"
output:
bookdown::pdf_document2:
fig_width: 7.5
fig_height: 4
---
# Setup
We load packages, pathways data, and read in the movement data from Excel.
```{r setup, include = F}
knitr::opts_chunk$set(echo = FALSE, message = F, warning = F)
library(openxlsx)
library(dplyr)
library(linelist)
library(knitr)
library(reshape2)
library(rlist)
library(here)
library(Hmisc)
source(here::here("code", "core_model_functions.R"))
source(here::here("code", "paths_table_to_blocks.R"))
set.seed(3210)
```
```{r load_hospital_data}
#latest version of hosp data can be generated by sourcing the corresponding code:
#(generates a ton of objects, recommend clearing global envir after!)
#source(here::here("code", "import_hosp_data.R"))
hospital_data = read.csv(here::here("data", "uk_hospital_data.csv")) %>%
mutate(date = as.Date(date))
regions_hospital_data = hospital_data %>%
filter(!(geography %in% c("England", "Scotland", "Wales", "Northern Ireland")))
cov_curve = hospital_data %>%
filter(geography == "England") %>%
filter(value_type == "hospital_inc")
```
```{r calc_region_props}
#calculate proportion of cumulative hospitalisations in each region
#used later to calculate fitted weighted average LoS for England
hosp_props = hospital_data %>%
filter(value_type == "hospital_inc") %>%
filter(geography != "England") %>%
group_by(geography) %>%
summarise(n = sum(value)) %>%
ungroup %>%
mutate(n = n/(sum(n)))
```
## Fitting LoS by region
```{r fit_by_region}
all_regions_results = data.frame()
all_regions_true_prev = data.frame()
all_regions_LoS = data.frame()
optim_func_ward = function(par, total_LoS_all, cov_curve, true_prev){
message("Try Ward LoS ", par)
total_LoS_all$Ward$LoS$mean = par
#giving it a spin with total LoS assumptions
res_total_LoS_all = multi_pathways_model(10, cov_curve, total_LoS_all, length(cov_curve))
sq_diff = sum((res_total_LoS_all$Ward - true_prev$Ward)^2, na.rm = T)
sq_diff
}
optim_func_CC = function(par, total_LoS_all, cov_curve, true_prev){
message("Try CC LoS ", par)
total_LoS_all$CC$LoS$mean = par
#giving it a spin with total LoS assumptions
res_total_LoS_all = multi_pathways_model(10, cov_curve, total_LoS_all, length(cov_curve))
sq_diff = sum((res_total_LoS_all$CC - true_prev$CC)^2, na.rm = T)
sq_diff
}
for (i in unique(regions_hospital_data$geography)) {
message("Currently on region ", i)
data_reg = regions_hospital_data %>%
filter(geography == i)
cov_curve = data_reg %>%
filter(value_type == "hospital_inc") %>%
select(value) %>%
pull
true_reg = data.frame(
time = c(1:length(unique(data_reg$date))),
date = unique(data_reg$date),
Ward = data_reg %>% filter(value_type == "hospital_prev") %>% select(value) %>% pull,
CC = data_reg %>% filter(value_type == "icu_prev") %>% select(value) %>% pull,
region = i
)
opt_res_ward = optimize(optim_func_ward, lower = 1, upper = 10,
tol = 0.01, total_LoS_all = total_LoS_all,
cov_curve = cov_curve, true_prev = true_reg)
opt_res_CC = optimize(optim_func_CC, lower = 8, upper = 20,
tol = 0.01, total_LoS_all = total_LoS_all,
cov_curve = cov_curve, true_prev = true_reg)
total_LoS_all$Ward$LoS$mean = opt_res_ward$minimum
total_LoS_all$CC$LoS$mean = opt_res_CC$minimum
#giving it a spin with total LoS assumptions
res_total_LoS_all = multi_pathways_model(100, cov_curve, total_LoS_all, length(cov_curve))
res_total_LoS_all$region = i
res_total_LoS_all$date = true_reg$date
all_regions_results = rbind(all_regions_results, res_total_LoS_all)
all_regions_true_prev = rbind(all_regions_true_prev, true_reg)
res_region_LoS = data.frame(
Ward = opt_res_ward$minimum,
CC = opt_res_CC$minimum,
region = i
)
all_regions_LoS = rbind(all_regions_LoS, res_region_LoS)
}
eng_fitted = c(Hmisc::wtd.mean(all_regions_LoS$Ward, hosp_props$n),
Hmisc::wtd.mean(all_regions_LoS$CC, hosp_props$n),
"England")
saveRDS(all_regions_results, here::here("outputs", "results", "fit_regions_eng.rds"))
all_regions_LoS = rbind(all_regions_LoS, eng_fitted)
write.csv(all_regions_LoS, here::here("outputs", "tables", "best_fit_LoS.csv"), row.names = F)
```
## Fitting prop to CC by region
```{r fit_by_region_proba}
all_regions_results2 = data.frame()
all_regions_true_prev2 = data.frame()
all_regions_proba2 = data.frame()
optim_func2 = function(par, total_LoS_all, cov_curve, true_prev){
message("Try CC proba ", par)
total_LoS_all$CC$proba = par
total_LoS_all$Ward$proba = 1-par
#giving it a spin with total LoS assumptions
res_total_LoS_all = multi_pathways_model(10, cov_curve, total_LoS_all, length(cov_curve))
sq_diff = sum((res_total_LoS_all$Ward - true_prev$Ward)^2, na.rm = T)
sq_diff2 = sum((res_total_LoS_all$CC - true_prev$CC)^2, na.rm = T)
sq_diff + sq_diff2
}
for (i in unique(regions_hospital_data$geography)) {
message("Currently on region ", i)
data_reg = regions_hospital_data %>%
filter(geography == i)
cov_curve = data_reg %>%
filter(value_type == "hospital_inc") %>%
select(value) %>%
pull
true_reg = data.frame(
time = c(1:length(unique(data_reg$date))),
date = unique(data_reg$date),
Ward = data_reg %>% filter(value_type == "hospital_prev") %>% select(value) %>% pull,
CC = data_reg %>% filter(value_type == "icu_prev") %>% select(value) %>% pull,
region = i
)
opt_res = optimize(optim_func2, lower = 0.01, upper = 0.5,
tol = 0.01, total_LoS_all = total_LoS_all,
cov_curve = cov_curve, true_prev = true_reg)
total_LoS_all$CC$proba = opt_res$minimum
total_LoS_all$Ward$proba = 1-opt_res$minimum
#giving it a spin with total LoS assumptions
res_total_LoS_all = multi_pathways_model(100, cov_curve, total_LoS_all, length(cov_curve))
res_total_LoS_all$region = i
res_total_LoS_all$date = true_reg$date
all_regions_results2 = rbind(all_regions_results2, res_total_LoS_all)
all_regions_true_prev2 = rbind(all_regions_true_prev2, true_reg)
res_region_proba = data.frame(
Ward = 1-opt_res$minimum,
CC = opt_res$minimum,
region = i
)
all_regions_proba2 = rbind(all_regions_proba2, res_region_proba)
}
eng_fitted2 = c(Hmisc::wtd.mean(all_regions_proba2$Ward, hosp_props$n),
Hmisc::wtd.mean(all_regions_proba2$CC, hosp_props$n),
"England")
saveRDS(all_regions_results2, here::here("outputs", "results", "fit_regions_eng_proba.rds"))
all_regions_proba2 = rbind(all_regions_proba2, eng_fitted2)
write.csv(all_regions_proba2, here::here("outputs", "tables", "best_fit_proba.csv"), row.names = F)
```