Skip to content

Commit 7b828c5

Browse files
authored
Merge pull request #163 from mrc-ide/nimue_fitting
v0.6.4 changes for nimue vaccine fitting
2 parents 432119b + 238ca70 commit 7b828c5

File tree

7 files changed

+241
-97
lines changed

7 files changed

+241
-97
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: squire
22
Type: Package
33
Title: SEIR transmission model of COVID-19
4-
Version: 0.6.3
4+
Version: 0.6.4
55
Authors@R: c(
66
person("OJ", "Watson", email = "o.watson15@imperial.ac.uk", role = c("aut", "cre")),
77
person("Patrick", "Walker", email = "patrick.walker06@imperial.ac.uk", role = c("aut")),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# squire 0.6.4
2+
3+
* `pmcmc` changes to adapt for `nimue` class models and vaccines
4+
15
# squire 0.6.3
26

37
* `beta_est` accepts `nimue` models

R/models.R

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,50 @@ explicit_model <- function() {
1313
compare_output(model, pars_obs, data, type=model_class)
1414
}
1515

16+
parameter_func <- function(..., tt_vaccine, max_vaccine) {
17+
18+
# build model parameters with no vaccine being passed through as
19+
# this is not the vaccine model
20+
parameters_explicit_SEEIR(...)
21+
22+
}
23+
24+
run_func <- function(country,
25+
contact_matrix_set,
26+
tt_contact_matrix,
27+
hosp_bed_capacity,
28+
tt_hosp_beds,
29+
ICU_bed_capacity,
30+
tt_ICU_beds,
31+
max_vaccine,
32+
tt_vaccine,
33+
population,
34+
replicates,
35+
day_return,
36+
time_period,
37+
...) {
38+
39+
# build model run with no vaccine being passed through as
40+
# this is not the vaccine model
41+
run_explicit_SEEIR_model(country = country,
42+
contact_matrix_set = contact_matrix_set,
43+
tt_contact_matrix = tt_contact_matrix,
44+
hosp_bed_capacity = hosp_bed_capacity,
45+
tt_hosp_beds = tt_hosp_beds,
46+
ICU_bed_capacity = ICU_bed_capacity,
47+
tt_ICU_beds = tt_ICU_beds,
48+
population = population,
49+
replicates = replicates,
50+
day_return = day_return,
51+
time_period = time_period,
52+
...)
53+
}
54+
55+
1656
explicit_model <- list(odin_model = explicit_SEIR,
1757
generate_beta_func = beta_est_explicit,
18-
parameter_func = parameters_explicit_SEEIR,
19-
run_func = run_explicit_SEEIR_model,
58+
parameter_func = parameter_func,
59+
run_func = run_func,
2060
compare_model = compare_model)
2161
class(explicit_model) <- c(model_class, "stochastic", "squire_model")
2262
explicit_model
@@ -61,10 +101,48 @@ deterministic_model <- function() {
61101
compare_output(model, pars_obs, data, type=model_class)
62102
}
63103

104+
parameter_func <- function(..., tt_vaccine, max_vaccine) {
105+
106+
# build model parameters with no vaccine being passed through as
107+
# this is not the vaccine model
108+
parameters_explicit_SEEIR(...)
109+
}
110+
111+
run_func <- function(country,
112+
contact_matrix_set,
113+
tt_contact_matrix,
114+
hosp_bed_capacity,
115+
tt_hosp_beds,
116+
ICU_bed_capacity,
117+
tt_ICU_beds,
118+
max_vaccine,
119+
tt_vaccine,
120+
population,
121+
replicates,
122+
day_return,
123+
time_period,
124+
...) {
125+
126+
# build model run with no vaccine being passed through as
127+
# this is not the vaccine model
128+
run_deterministic_SEIR_model(country = country,
129+
contact_matrix_set = contact_matrix_set,
130+
tt_contact_matrix = tt_contact_matrix,
131+
hosp_bed_capacity = hosp_bed_capacity,
132+
tt_hosp_beds = tt_hosp_beds,
133+
ICU_bed_capacity = ICU_bed_capacity,
134+
tt_ICU_beds = tt_ICU_beds,
135+
population = population,
136+
replicates = replicates,
137+
day_return = day_return,
138+
time_period = time_period,
139+
...)
140+
}
141+
64142
det_model <- list(odin_model = explicit_SEIR_deterministic,
65143
generate_beta_func = beta_est_explicit,
66-
parameter_func = parameters_explicit_SEEIR,
67-
run_func = run_deterministic_SEIR_model,
144+
parameter_func = parameter_func,
145+
run_func = run_func,
68146
compare_model = compare_model)
69147
class(det_model) <- c(model_class, "deterministic", "squire_model")
70148
det_model

R/pmcmc.R

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#' @param output_proposals Logical indicating whether proposed parameter jumps should be output along with results
3434
#' @param n_chains number of MCMC chains to run
3535
#' @inheritParams calibrate
36+
#' @param date_vaccine_change Date that vaccine doses per day change.
37+
#' Default = NULL.
38+
#' @param baseline_max_vaccine Baseline vaccine doses per day. Default = NULL
39+
#' @param max_vaccine Time varying maximum vaccine doeses per day. Default = NULL.
3640
#' @param Rt_args List of arguments to be passed to \code{evaluate_Rt_pmcmc} for calculating Rt.
3741
#' Current arguments are available in \code{Rt_args_list}
3842
#' @param burnin number of iterations to discard from the start of MCMC run when sampling from the posterior for trajectories
@@ -161,6 +165,9 @@ pmcmc <- function(data,
161165
ICU_bed_capacity = NULL,
162166
baseline_ICU_bed_capacity = NULL,
163167
date_ICU_bed_capacity_change = NULL,
168+
date_vaccine_change = NULL,
169+
baseline_max_vaccine = NULL,
170+
max_vaccine = NULL,
164171
Rt_args = NULL,
165172
burnin = 0,
166173
replicates = 100,
@@ -180,6 +187,11 @@ pmcmc <- function(data,
180187
# assertions & checks
181188
#--------------------
182189

190+
# if nimue keep to 1 step per day
191+
if(inherits(squire_model, "nimue_model")) {
192+
steps_per_day <- 1
193+
}
194+
183195
# we work with pars_init being a list of inital conditions for starting
184196
if(any(c("start_date", "R0") %in% names(pars_init))) {
185197
pars_init <- list(pars_init)
@@ -312,9 +324,6 @@ pmcmc <- function(data,
312324
if(as.Date(tail(date_contact_matrix_set_change,1)) > as.Date(tail(data$date, 1))) {
313325
stop("Last date in date_contact_matrix_set_change is greater than the last date in data")
314326
}
315-
if(as.Date(pars_max$start_date) >= as.Date(head(date_contact_matrix_set_change, 1))) {
316-
stop("First date in date_contact_matrix_set_change is earlier than maximum start date allowed in pars search")
317-
}
318327

319328
# Get in correct format
320329
if(is.matrix(baseline_contact_matrix)) {
@@ -343,9 +352,6 @@ pmcmc <- function(data,
343352
if(as.Date(tail(date_ICU_bed_capacity_change,1)) > as.Date(tail(data$date, 1))) {
344353
stop("Last date in date_ICU_bed_capacity_change is greater than the last date in data")
345354
}
346-
if(as.Date(pars_max$start_date) >= as.Date(head(date_ICU_bed_capacity_change, 1))) {
347-
stop("First date in date_ICU_bed_capacity_change is earlier than maximum start date of epidemic")
348-
}
349355

350356
tt_ICU_beds <- c(0, seq_len(length(date_ICU_bed_capacity_change)))
351357
ICU_bed_capacity <- c(baseline_ICU_bed_capacity, ICU_bed_capacity)
@@ -355,6 +361,33 @@ pmcmc <- function(data,
355361
ICU_bed_capacity <- baseline_ICU_bed_capacity
356362
}
357363

364+
# handle vaccine changes
365+
if(!is.null(date_vaccine_change)) {
366+
367+
assert_date(date_vaccine_change)
368+
assert_vector(max_vaccine)
369+
assert_numeric(max_vaccine)
370+
assert_numeric(baseline_max_vaccine)
371+
372+
if(is.null(baseline_max_vaccine)) {
373+
stop("baseline_max_vaccine can't be NULL if date_vaccine_change is provided")
374+
}
375+
if(as.Date(tail(date_vaccine_change,1)) > as.Date(tail(data$date, 1))) {
376+
stop("Last date in date_vaccine_change is greater than the last date in data")
377+
}
378+
379+
tt_vaccine <- c(0, seq_len(length(date_vaccine_change)))
380+
max_vaccine <- c(baseline_max_vaccine, max_vaccine)
381+
382+
} else {
383+
tt_vaccine <- 0
384+
if(!is.null(baseline_max_vaccine)) {
385+
max_vaccine <- baseline_max_vaccine
386+
} else {
387+
max_vaccine <- 0
388+
}
389+
}
390+
358391
# handle hosp bed changed
359392
if(!is.null(date_hosp_bed_capacity_change)) {
360393

@@ -369,9 +402,6 @@ pmcmc <- function(data,
369402
if(as.Date(tail(date_hosp_bed_capacity_change,1)) > as.Date(tail(data$date, 1))) {
370403
stop("Last date in date_hosp_bed_capacity_change is greater than the last date in data")
371404
}
372-
if(as.Date(pars_max$start_date) >= as.Date(head(date_hosp_bed_capacity_change, 1))) {
373-
stop("First date in date_hosp_bed_capacity_change is earlier than maximum start date of epidemic")
374-
}
375405

376406
tt_hosp_beds <- c(0, seq_len(length(date_hosp_bed_capacity_change)))
377407
hosp_bed_capacity <- c(baseline_hosp_bed_capacity, hosp_bed_capacity)
@@ -403,6 +433,8 @@ pmcmc <- function(data,
403433
tt_hosp_beds = tt_hosp_beds,
404434
ICU_bed_capacity = ICU_bed_capacity,
405435
tt_ICU_beds = tt_ICU_beds,
436+
max_vaccine = max_vaccine,
437+
tt_vaccine = tt_vaccine,
406438
...)
407439

408440
# collect interventions for odin model likelihood
@@ -413,7 +445,9 @@ pmcmc <- function(data,
413445
date_ICU_bed_capacity_change = date_ICU_bed_capacity_change,
414446
ICU_bed_capacity = ICU_bed_capacity,
415447
date_hosp_bed_capacity_change = date_hosp_bed_capacity_change,
416-
hosp_bed_capacity = hosp_bed_capacity)
448+
hosp_bed_capacity = hosp_bed_capacity,
449+
date_vaccine_change = date_vaccine_change,
450+
max_vaccine = max_vaccine)
417451

418452
#----------------..
419453
# Collect Odin and MCMC Inputs
@@ -609,10 +643,13 @@ pmcmc <- function(data,
609643
tt_hosp_beds = tt_hosp_beds,
610644
ICU_bed_capacity = ICU_bed_capacity,
611645
tt_ICU_beds = tt_ICU_beds,
646+
max_vaccine = max_vaccine,
647+
tt_vaccine = tt_vaccine,
612648
population = population,
613649
replicates = 1,
614650
day_return = TRUE,
615-
time_period = nrow(pmcmc_samples$trajectories))
651+
time_period = nrow(pmcmc_samples$trajectories),
652+
...)
616653

617654
# and add the parameters that changed between each simulation, i.e. posterior draws
618655
r$replicate_parameters <- pmcmc_samples$sampled_PMCMC_Results
@@ -1195,6 +1232,7 @@ calc_loglikelihood <- function(pars, data, squire_model, model_params,
11951232
date_contact_matrix_set_change <- interventions$date_contact_matrix_set_change
11961233
date_ICU_bed_capacity_change <- interventions$date_ICU_bed_capacity_change
11971234
date_hosp_bed_capacity_change <- interventions$date_hosp_bed_capacity_change
1235+
date_vaccine_change <- interventions$date_vaccine_change
11981236

11991237
# change betas
12001238
if (is.null(date_R0_change)) {
@@ -1245,6 +1283,19 @@ calc_loglikelihood <- function(pars, data, squire_model, model_params,
12451283
model_params$hosp_beds <- tt_list$change
12461284
}
12471285

1286+
# and vaccine coverage
1287+
if (is.null(date_vaccine_change)) {
1288+
tt_vaccine <- 0
1289+
max_vaccine <- 0
1290+
} else {
1291+
tt_list <- intervention_dates_for_odin(dates = sort(unique(c(start_date,date_vaccine_change))),
1292+
change = interventions$max_vaccine,
1293+
start_date = start_date,
1294+
steps_per_day = round(1/model_params$dt))
1295+
model_params$tt_vaccine <- tt_list$tt
1296+
model_params$max_vaccine <- tt_list$change
1297+
}
1298+
12481299
#--------------------..
12491300
# update new R0s based on R0_change and R0_date_change, and Meff_date_change
12501301
#--------------------..

R/projections.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ projections <- function(r,
107107
# ----------------------------------------------------------------------------
108108
## assertion checks on parameters
109109
# ----------------------------------------------------------------------------
110-
assert_custom_class(r, "squire_simulation")
110+
# assert_custom_class(r, "squire_simulation")
111111
# TODO future asserts if these are our "end" classes
112112
if (is.null(r$output) & is.null(r$scan_results) & is.null(r$pmcmc_results)) {
113113
stop("Model must have been produced either with Squire Default, Scan Grid (calibrate), or pMCMC (pmcmc) Approach")

man/pmcmc.Rd

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)