-
Notifications
You must be signed in to change notification settings - Fork 0
/
3a_Outcome_model_fit.Rmd
105 lines (84 loc) · 2.22 KB
/
3a_Outcome_model_fit.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
---
author: "Leon Di Stefano"
date: "`r Sys.Date()`"
output:
html_document:
keep_md: false
params:
fit_name: "main_fit"
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_Outcome_model_fit.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 <-
read_rds(here(output_dir, "mice_complete_df_list.rds"))
patients <-
read_rds(here(output_dir, "patients.rds"))
```
Specify the primary model formula:
```{r}
sap_primary_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) +
(1 + treat || niaid_baseline_fct)
)
```
Have a look at the default priors:
```{r}
get_prior(sap_primary_model, data = mice_df_list[[1]])
```
Fit the primary outcome model:
```{r message=FALSE}
ncores <- parallel::detectCores() - 1
brm_fit <-
brm_multiple(
formula = sap_primary_model,
family = cumulative,
prior = prior(student_t(3, 0, 10), class = sd), # Defaults may have changed—SAP has broader priors
data = mice_df_list,
cores = ncores,
control = list(adapt_delta = 0.999), # helps avoid divergent transitions
iter = 3000,
thin = 3,
backend = 'cmdstanr',
seed = 20200524
# file_refit = "on_change"
)
write_rds(brm_fit, file.path(output_model_dir, paste0(params$fit_name, ".rds")))
```
```{r}
sessionInfo()
```
```{r}
Sys.time()
```