Explore scenarios using gridding with sampling for parameters not in the grid. Parameters that
are included in the grid are currently hard coded. Use the future
package to control parallisation
outside of the function.
parameter_sweep(scenarios = NULL, samples = 1, sim_fn = NULL)
a data.frame
: containing all gridded scenarios - see the
examples for the required structure. Defaults to NULL
.
a positive integer
scalar: the number of samples to take.
Defaults to 1
.
a function
: defaults to NULL
. The vectorised model
simulation function - see the examples for usage.
A nested data.table
containing the parameters for each scenario and a nested list of output
from scenario_sim()
.
library(data.table)
# Put parameters that are grouped by disease into this data.table
scenarios <- data.table(
expand.grid(
delay_group = list(data.table(
delay = c("SARS", "Wuhan"),
delay_shape = c(1.651524, 2.305172),
delay_scale = c(4.287786, 9.483875)
)),
k_group = list(data.table(
theta = c("<1%", "15%", "30%"),
k = c(1, 0.88, 0.47)
)),
index_R0 = c(1.5, 2.5, 3.5),
prop.asym = c(0, 0.1),
control_effectiveness = seq(0, 1, 0.2),
num.initial.cases = c(5, 20, 40)
)
)
list_cols <- grep("_group", colnames(scenarios), value = TRUE)
non_list_cols <- setdiff(colnames(scenarios), list_cols)
expanded_groups <- scenarios[, rbindlist(delay_group), by = c(non_list_cols)]
expanded_k <- scenarios[, rbindlist(k_group), by = c(non_list_cols)]
scenarios <- merge(
expanded_groups, expanded_k, by = non_list_cols, allow.cartesian = TRUE
)
scenarios[, scenario := 1:.N]
#> Key: <index_R0, prop.asym, control_effectiveness, num.initial.cases>
#> index_R0 prop.asym control_effectiveness num.initial.cases delay
#> <num> <num> <num> <num> <char>
#> 1: 1.5 0.0 0 5 SARS
#> 2: 1.5 0.0 0 5 SARS
#> 3: 1.5 0.0 0 5 SARS
#> 4: 1.5 0.0 0 5 Wuhan
#> 5: 1.5 0.0 0 5 Wuhan
#> ---
#> 644: 3.5 0.1 1 40 SARS
#> 645: 3.5 0.1 1 40 SARS
#> 646: 3.5 0.1 1 40 Wuhan
#> 647: 3.5 0.1 1 40 Wuhan
#> 648: 3.5 0.1 1 40 Wuhan
#> delay_shape delay_scale theta k scenario
#> <num> <num> <char> <num> <int>
#> 1: 1.651524 4.287786 <1% 1.00 1
#> 2: 1.651524 4.287786 15% 0.88 2
#> 3: 1.651524 4.287786 30% 0.47 3
#> 4: 2.305172 9.483875 <1% 1.00 4
#> 5: 2.305172 9.483875 15% 0.88 5
#> ---
#> 644: 1.651524 4.287786 15% 0.88 644
#> 645: 1.651524 4.287786 30% 0.47 645
#> 646: 2.305172 9.483875 <1% 1.00 646
#> 647: 2.305172 9.483875 15% 0.88 647
#> 648: 2.305172 9.483875 30% 0.47 648
## Parameterise fixed paramters
sim_with_params <- purrr::partial(
ringbp::scenario_sim,
cap_max_days = 365,
cap_cases = 5000,
r0isolated = 0,
disp.iso= 1,
disp.subclin = 0.16,
disp.com = 0.16,
quarantine = FALSE
)
## parameter_sweep uses the future_lapply() function
## Default is to run sequntially on a single core
# future::plan("sequential")
## Set up multicore if using see ?future::plan for details
## Use the workers argument to control the number of cores used.
# future::plan("multiprocess")
## Run parameter sweep
sweep_results <- parameter_sweep(
scenarios,
sim_fn = sim_with_params,
samples = 1
)
sweep_results
#> scenario data sims
#> <int> <list> <list>
#> 1: 526 <data.table[1x9]> [NULL]
#> 2: 642 <data.table[1x9]> [NULL]
#> 3: 45 <data.table[1x9]> [NULL]
#> 4: 402 <data.table[1x9]> [NULL]
#> 5: 22 <data.table[1x9]> [NULL]
#> ---
#> 644: 155 <data.table[1x9]> [NULL]
#> 645: 493 <data.table[1x9]> [NULL]
#> 646: 188 <data.table[1x9]> [NULL]
#> 647: 182 <data.table[1x9]> [NULL]
#> 648: 174 <data.table[1x9]> [NULL]