-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.R
361 lines (346 loc) · 10.6 KB
/
model.R
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#' Default model priors
#'
#' @param individual_variation A vector of length two specifying the
#' default mean and standard deviation of individual-level variation.
#'
#' @param include_descriptive Logical, defaults `FALSE`. Should an extended
#' list of priors be returned including transformed parameters. This is useful
#' when transforming model output and for user exploration.
#'
#' @return A data frame summarising the model priors.
#'
#' @importFrom data.table data.table
#' @family model
#' @export
#' @author Sam Abbott
#' @examples
#' epict_priors()
#'
#' # Also include descriptive parameters
#' epict_priors(include_descriptive = TRUE)
epict_priors <- function(individual_variation = c(0, 0.05),
include_descriptive = FALSE) {
priors <- data.table::data.table(
variable = c(
"t_inf",
"c_thres",
"c_p",
"t_p",
"c_s",
"t_s",
"t_clear",
"inc_mean",
"inc_sd",
"sigma",
"lkj",
"ct_shift",
"ct_scale",
"nat_inc_mean",
"nat_inc_sd"
),
name = c(
"Time between infection and first positive test",
"Ct value at clearance of infection",
"Ct value at peak",
"Time at peak Ct",
"Ct value at switch",
"Time at switch Ct",
"Time at clearance of infection",
"Incubation period (log) mean",
"Incubation period (log) standard deviation",
"Observation standard deviation",
"Lewandowski-Kurowicka-Joe distribution",
"Ct intercept adjustment",
"Ct multiplicative adjustment",
"Incubation period (mean)",
"Incubation period (standard deviation)"
),
detail = c(
"Offset by onset if available",
"Offset by user specified limit of Ct detection",
"Logit scale offset by latent limit of detection/Ct at switch",
"Log scale",
"Logit scale offset by latent limit of detection",
"Log scale relative to the time at peak Ct",
"Relative to time at peak and switch C",
"Parameterised as a log-normal distribution",
"Parameterised as a log-normal distribution",
"Assuming a normal error model",
"Prior used for the individual-level correlation matrix. There is
one parameter which at 1 represents a uniform prior. Smaller values indicate strong correlations, and larger values weaker correlations",
"Cycle threshold intercept adjustment with an intercept set to 0 (i. no
adjustment. This is adjusted using `adjustment_formula()`",
"Cycle threshold gradient adjustment with an intercept set to 1 (i. no
adjustment. This is adjusted using `adjustment_formula()`",
"Incubation period mean on the natural scale",
"Incubation period standard deviation on the natural scale"
),
distribution = c(
"Normal (truncated by onset or 0)",
"Normal (truncated by user specified limit of detection)",
"Normal",
"Normal",
"Normal",
"Normal",
"Normal",
"Normal",
"Zero truncated normal",
"Zero truncated normal",
"Lewandowski-Kurowicka-Joe (LKJ) distribution",
"",
"",
"",
""
),
intercept_mean = c(
5, 10, 0, 1.61, 0, 1.61, 2.3, 1.62, 0.42, 2, 1, NA, NA, NA, NA
),
intercept_sd = c(
5, 10, 1, 0.5, 1, 0.5, 0.5, 0.06, 0.07, 2, NA, NA, NA, NA, NA
)
)
priors[, `:=`(
individual_variation_mean = NA_real_,
individual_variation_sd = NA_real_
)]
params_with_var <- c(
"c_p", "t_p", "t_s", "c_s", "t_clear"
)
priors[
variable %in% params_with_var,
individual_variation_mean := individual_variation[1]
]
priors[
variable %in% params_with_var,
individual_variation_sd := individual_variation[2]
]
if (!include_descriptive) {
priors <- priors[!is.na(intercept_mean)]
}
return(priors[])
}
#' Format observations and model settings for use with stan
#'
#' @param model_opts A list of model options. See [epict_model_opts()]
#' for details
#'
#' @param inference_opts A list of options to use for inference. See
#' [epict_inference_opts()] for details.
#'
#' @return A list as required by stan.
#' @inheritParams epict_obs_as_list
#' @inheritParams epict_onset_obs_as_list
#' @inheritParams epict_model_opts
#' @inheritParams epict_inference_opts
#' @inheritParams epict_formula_as_list
#' @inheritParams epict_population_priors_as_list
#' @inheritParams epict_individual_priors_as_list
#' @inheritParams epict_posterior_as_prior
#' @family model
#' @author Sam Abbott
#' @export
epict_convert_to_list <- function(
obs,
piecewise_formula = epict::piecewise_formula(~1, obs),
adjustment_formula = epict::adjustment_formula(~1, obs),
priors = epict::epict_priors(),
model_opts = epict::epict_model_opts(),
inference_opts = epict::epict_inference_opts()
) {
data <- enw_obs_as_list(obs)
data <- c(data,
epict_onset_obs_as_list(obs, onsets = (model_opts$onsets == 1))
)
data <- c(
data,
epict_formula_as_list(
data,
piecewise_formula = piecewise_formula,
adjustment_formula = adjustment_formula
)
)
data <- c(
data,
model_opts,
inference_opts
)
data <- c(
data,
epict_population_priors_as_list(priors),
epict_individual_priors_as_list(priors)
)
return(data)
}
#' Set up initial conditions for model
#'
#' @param data A list of data as produced by [epict_as_list()].
#'
#' @param scale Numeric defaults to 0.1. The numeric scaling to apply to the
#' standard deviation when sampling. Helpful for reducing numeric instability
#' during warmup.
#'
#' @return A function that when called returns a list of initial conditions
#' for the package stan models.
#'
#' @family model
#' @importFrom purrr map_dbl
#' @importFrom truncnorm rtruncnorm
#' @author Sam Abbott
#' @export
epict_inits <- function(data, scale = 0.1) {
function() {
inits <- list(
t_inf = purrr::map_dbl(
1:data$P,
~ truncnorm::rtruncnorm(
1,
a = -data$onset_time[.],
mean = -data$onset_time[.] + data$t_inf_p[1],
sd = data$t_inf_p[2] * scale
)
),
c_thres = truncnorm::rtruncnorm(
1,
a = data$c_lod,
mean = data$c_lod + data$c_thres_p[1],
sd = data$c_thres_p[2] * scale
),
c_p_int = rnorm(1, data$c_p_p[1], data$c_p_p[2]),
t_p_int = rnorm(1, data$t_p_p[1], data$t_p_p[2]),
t_clear_int = rnorm(1, data$t_clear_p[1], data$t_clear_p[2]),
ind_var = abs(rnorm(data$K, 0, data$ind_var_sd * scale)),
ind_eta = matrix(
rnorm(data$P * data$K, 0, 1), nrow = data$K, ncol = data$P
),
sigma = truncnorm::rtruncnorm(
1, a = 0, mean = data$sigma_p[1], sd = data$sigma_p[2]
)
)
if (data$switch > 0) {
inits$c_s_int <- array(rnorm(1, data$c_s_p[1], data$c_s_p[2]))
inits$t_s_int <- array(rnorm(1, data$t_s_p[1], data$t_s_p[2]))
}
if (data$preds > 0) {
if (data$adj_t_p > 0) {
inits$beta_t_p <- rnorm(
data$preds, data$beta_t_p_m, data$beta_t_p_sd * scale
)
}
if (data$adj_t_s > 0) {
inits$beta_t_s <- rnorm(
data$preds, data$beta_t_s_m, data$beta_t_s_sd * scale
)
}
if (data$adj_t_clear > 0) {
inits$beta_t_clear <- rnorm(
data$preds, data$beta_t_clear_m, data$beta_t_clear_sd * scale
)
}
if (data$adj_c_p > 0) {
inits$beta_c_p <- rnorm(
data$preds, data$beta_c_p_m, data$beta_c_p_sd * scale
)
}
if (data$adj_c_s > 0) {
inits$beta_c_s <- rnorm(
data$preds, data$beta_c_s_m, data$beta_c_s_sd * scale
)
}
if (data$adj_inc_mean > 0) {
inits$beta_inc_mean <- rnorm(
data$preds, data$beta_inc_mean_m, data$beta_inc_mean_sd * scale
)
}
if (data$adj_inc_sd > 0) {
inits$beta_inc_sd <- rnorm(
data$preds, data$beta_inc_sd_m, data$beta_inc_sd_sd * scale
)
}
}
if (data$ct_preds > 0) {
if (data$adj_ct > 0) {
inits$beta_ct_shift <- rnorm(
data$ct_preds, data$beta_ct_shift_m, data$beta_ct_shift_sd * scale
)
inits$beta_ct_scale <- rnorm(
data$ct_preds, data$beta_ct_scale_m, data$beta_ct_scale_sd * scale
)
}
}
if (data$any_onsets == 1) {
inits$inc_mean_int <- rnorm(
1, data$inc_mean_p[1], data$inc_mean_p[2] * scale
)
inits$inc_sd_int <- truncnorm::rtruncnorm(
1, a = 0, mean = data$inc_sd_p[1], sd = data$inc_sd_p[2] * scale
)
}
return(inits)
}
}
#' Load and compile the Cycle thresholds model
#'
#' @param model A character string indicating the path to the model.
#' If not supplied the package default model is used.
#'
#' @param include A character string specifying the path to any stan
#' files to include in the model. If missing the package default is used.
#'
#' @param compile Logical, defaults to `TRUE`. Should the model
#' be loaded and compiled using [cmdstanr::cmdstan_model()].
#'
#' @param threads Logical, defaults to `FALSE`. Should the model compile with
#' support for multi-thread support in chain. Note that this requires the use of
#' the `threads_per_chain` argument when model fitting using [epict()] and is
#' not currently supported in model.
#'
#' @param stanc_options A list of options to pass to the `stanc_options` of
#' [cmdstanr::cmdstan_model()]. By default "01" is passed which specifies simple
#' optimisations should be done by the prior to compilation.
#'
#' @param verbose Logical, defaults to `TRUE`. Should verbose
#' messages be shown.
#'
#' @param ... Additional arguments passed to [cmdstanr::cmdstan_model()].
#'
#' @return A `cmdstanr` model.
#'
#' @family model
#' @export
#' @importFrom cmdstanr cmdstan_model
#' @examplesIf interactive()
#' mod <- epict_model()
#' mod
epict_model <- function(model, include,
compile = TRUE, threads = FALSE,
stanc_options = list("O1"), verbose = TRUE, ...) {
if (missing(model)) {
model <- "stan/model.stan"
model <- system.file(model, package = "epict")
}
if (missing(include)) {
include <- system.file("stan", package = "epict")
}
if (compile) {
if (verbose) {
model <- cmdstanr::cmdstan_model(model,
include_paths = include,
stanc_options = stanc_options,
cpp_options = list(
stan_threads = threads
),
...
)
} else {
suppressMessages(
model <- cmdstanr::cmdstan_model(model,
include_paths = include,
cpp_options = list(
stan_threads = threads
), ...
)
)
}
}
return(model)
}