-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3a_7_Outcome_model_fit_Vanderbilt_only.Rmd
334 lines (250 loc) · 7.58 KB
/
3a_7_Outcome_model_fit_Vanderbilt_only.Rmd
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
---
author: "Leon Di Stefano"
date: "`r Sys.Date()`"
output:
html_document:
keep_md: false
params:
fit_name: "main_fit_Vanderbilt_only"
outcome_min: 28
outcome_max: 35
title: "`r paste0('Outcome model fit', params$outcome_min, params$outcome_max, params$fit_name, sep = '-')`"
---
```{r}
knitr::opts_chunk$set(echo = TRUE)
require(here)
require(brms)
require(tidybayes)
require(mice)
require(cmdstanr)
here::i_am(file.path("hcq_pooling_analysis", "3a_7_Outcome_model_fit_Vanderbilt_only.Rmd"))
source(here("hcq_pooling_analysis", "common.R"))
set_cmdstan_path(here::here("hcq_pooling_analysis", ".cmdstan", "cmdstan-2.27.0"))
out_stub <- paste(params$outcome_min, params$outcome_max, sep = '-')
output_dir <- here("hcq_pooling_analysis", "output", out_stub)
output_model_dir <- file.path(output_dir, params$fit_name)
if(!dir.exists(output_model_dir)) {
dir.create(output_model_dir, recursive = TRUE)
}
```
```{r}
params
```
Read multiply-imputed data:
```{r}
mice_df_list_all <-
read_rds(here(output_dir, "mice_complete_df_list.rds"))
mice_df_list_orchid_only <-
mice_df_list_all %>%
# FIT ONLY TO ORCHID DATA
map(function(d) { filter(d, siteid == "ORCHID") })
# patients <-
# read_rds(here(output_dir, "patients.rds"))
map(mice_df_list_orchid_only, nrow)
```
Specify the primary model formula:
```{r}
orchid_only_model <-
brms::bf(
niaid_outcome ~
treat*(
sex_model +
splines::ns(age_model, 3) +
splines::ns(bmi_model, 3) +
splines::ns(comorbidity_count, 3) +
niaid_baseline_numeric_model
) +
# (1 + treat || siteid) + ## Exclude site random effects
(1 + treat || niaid_baseline_fct)
)
```
The problem we have is that there are no individuals with outcome NCOSS of 3 in the ORCHID data.
This means that the stan code output by `brm`, as well as the stan data prepared by `brm`, have only 6 levels/5 cutpoints instead of 7 levels/6 cutpoints.
The solution, it would seem, is to
1. manually produce stan code as for the full model, then
2. apply this to manually filtered ORCHID-only stan data.
Manually producing the stan code:
```{r}
(orchid_model_stancode <-
make_stancode(
formula = orchid_only_model,
family = cumulative,
data = mice_df_list_all[[1]],
prior = prior(student_t(3, 0, 10), class = sd)))
```
Manually producing stan data:
```{r}
orchid_model_standata_imp1 <-
make_standata(
formula = orchid_only_model,
family = cumulative,
data = mice_df_list_all[[1]],
prior = prior(student_t(3, 0, 10), class = sd))
```
Note that there are 6 thresholds (correct):
```{r}
orchid_model_standata_imp1$nthres
```
... and the responses go from 1 to 7:
```{r}
orchid_model_standata_imp1$Y %>% table()
```
Whereas if I do the same for the ORCHID data only:
```{r}
orchid_model_standata_imp1_orchid_only <-
make_standata(
formula = orchid_only_model,
family = cumulative,
data = mice_df_list_orchid_only[[1]],
prior = prior(student_t(3, 0, 10), class = sd))
```
```{r}
orchid_model_standata_imp1_orchid_only$nthres
```
... and the responses go from 1 to 6!:
```{r}
orchid_model_standata_imp1_orchid_only$Y %>% table()
```
Cf. the raw data coding:
```{r}
mice_df_list_orchid_only[[1]]$niaid_outcome %>% as.numeric() %>% table()
```
Filtering the "all data" standata object manually:
```{r}
names(orchid_model_standata_imp1)
```
```{r}
orchid_indices_among_nonmissing_y <-
(mice_df_list_all[[1]] %>% filter(!is.na(niaid_outcome)))$siteid == "ORCHID"
c(n_nonmissing = length(orchid_indices_among_nonmissing_y),
n_nonmissing_orchid = sum(orchid_indices_among_nonmissing_y))
```
```{r}
map(orchid_model_standata_imp1, dim)
map(orchid_model_standata_imp1, length)
```
```{r}
orchid_model_standata_imp1_orchid_only_manual <- orchid_model_standata_imp1
orchid_model_standata_imp1_orchid_only_manual$N <-
sum(orchid_indices_among_nonmissing_y)
orchid_model_standata_imp1_orchid_only_manual$Y <-
orchid_model_standata_imp1$Y[orchid_indices_among_nonmissing_y]
orchid_model_standata_imp1_orchid_only_manual$X <-
orchid_model_standata_imp1$X[orchid_indices_among_nonmissing_y,]
orchid_model_standata_imp1_orchid_only_manual$Z_1_1 <-
orchid_model_standata_imp1$Z_1_1[orchid_indices_among_nonmissing_y]
orchid_model_standata_imp1_orchid_only_manual$Z_1_2 <-
orchid_model_standata_imp1$Z_1_2[orchid_indices_among_nonmissing_y]
orchid_model_standata_imp1_orchid_only_manual$J_1 <-
orchid_model_standata_imp1$J_1[orchid_indices_among_nonmissing_y]
attr(orchid_model_standata_imp1_orchid_only_manual, "class") <- "standata"
```
```{r}
map(orchid_model_standata_imp1_orchid_only_manual, dim)
map(orchid_model_standata_imp1_orchid_only_manual, length)
```
Try to fit with this:
```{r}
system.time(
imp_1_orchid_only_fit <- rstan::stan(model_code = orchid_model_stancode, data = orchid_model_standata_imp1_orchid_only_manual))
```
Check that there are 6 cutpoints:
```{r}
bayesplot::mcmc_dens(imp_1_orchid_only_fit, regex_pars = "b_Intercept")
```
Try turning this into a `brmsfit` object:
```{r}
orchid_only_test_brmfit_1 <- brm(
formula = orchid_only_model,
family = cumulative,
data = mice_df_list_all[[1]],
prior = prior(student_t(3, 0, 10), class = sd),
empty = TRUE)
orchid_only_test_brmfit_1$fit <- imp_1_orchid_only_fit
orchid_only_test_brmfit_1 <- rename_pars(orchid_only_test_brmfit_1)
orchid_only_test_brmfit_1
```
Create a function for updating the "standata" to only include ORCHID:
```{r}
filter_standata_to_orchid <- function(old_standata, orchid_indices) {
new_standata <- old_standata
new_standata$N <-
sum(orchid_indices)
new_standata$Y <-
old_standata$Y[orchid_indices]
new_standata$X <-
old_standata$X[orchid_indices,]
new_standata$Z_1_1 <-
old_standata$Z_1_1[orchid_indices]
new_standata$Z_1_2 <-
old_standata$Z_1_2[orchid_indices]
new_standata$J_1 <-
old_standata$J_1[orchid_indices]
attr(new_standata, "class") <- "standata"
return(new_standata)
}
```
Create "standata" objects for each of the imputations:
```{r}
mice_standata_list_orchid <-
map(mice_df_list_all, function (d) {
make_standata(
formula = orchid_only_model,
family = cumulative,
data = d,
prior = prior(student_t(3, 0, 10), class = sd)) %>%
filter_standata_to_orchid(orchid_indices_among_nonmissing_y)
})
```
Model fitting function:
```{r}
ncores <- parallel::detectCores() - 1
fit_orchid_standata <- function(orchid_standata) {
rstan::stan(
model_code = orchid_model_stancode,
data = orchid_standata,
cores = ncores,
iter = 3000,
thin = 3,
control = list(adapt_delta = 0.999),
seed = 20200524)
}
```
Fit each of these models:
```{r}
stan_fit_list_orchid <-
map(mice_standata_list_orchid, fit_orchid_standata)
```
Create `brmfit` objects:
```{r}
brmfit_list <-
map2(stan_fit_list_orchid, mice_df_list_all,
function(stanfit, full_df) {
brm_fit <- brm(
formula = orchid_only_model,
family = cumulative,
data = mice_df_list_all[[1]],
prior = prior(student_t(3, 0, 10), class = sd),
empty = TRUE)
brm_fit$fit <- stanfit
brm_fit <- rename_pars(brm_fit)
return(brm_fit)
}
)
```
Now combine these:
```{r}
brmfit_combined <- combine_models(mlist = brmfit_list, check_data = FALSE)
# The source for brm_multiple does this, and we make use of it
brmfit_combined$rhats <- do.call(rbind, map(brmfit_list, rhat))
```
Fit the primary outcome model:
```{r message=FALSE}
write_rds(brmfit_combined, file.path(output_model_dir, paste0(params$fit_name, ".rds")))
```
```{r}
sessionInfo()
```
```{r}
Sys.time()
```