Forecast infections from a given fit and trajectory of the time-varying reproduction number
Source:R/simulate_infections.R
forecast_infections.Rd
This function simulates infections using an existing fit to observed cases
but with a modified time-varying reproduction number. This can be used to
explore forecast models or past counterfactuals. Simulations can be run in
parallel using future::plan()
.
Usage
forecast_infections(
estimates,
R = NULL,
model = NULL,
samples = NULL,
batch_size = 10,
backend = "rstan",
verbose = interactive()
)
Arguments
- estimates
The
estimates
element of anepinow()
run that has been done with output = "fit", or the result ofestimate_infections()
withreturn_fit
set to TRUE.- R
A numeric vector of reproduction numbers; these will overwrite the reproduction numbers contained in
estimates
, except elements set to NA. Alternatively accepts a<data.frame>
containing at leastdate
andvalue
(integer) variables and optionallysample
. More (or fewer) days than in the original fit can be simulated.- model
A compiled stan model as returned by
rstan::stan_model()
.- samples
Numeric, number of posterior samples to simulate from. The default is to use all samples in the
estimates
input.- batch_size
Numeric, defaults to 10. Size of batches in which to simulate. May decrease run times due to reduced IO costs but this is still being evaluated. If set to NULL then all simulations are done at once.
- backend
Character string indicating the backend to use for fitting stan models. Supported arguments are "rstan" (default) or "cmdstanr".
- verbose
Logical defaults to
interactive()
. If theprogressr
package is available, a progress bar will be shown.
Value
A list of output as returned by estimate_infections()
but based on
results from the specified scenario rather than fitting.
Examples
# \donttest{
# set number of cores to use
old_opts <- options()
options(mc.cores = ifelse(interactive(), 4, 1))
# get example case counts
reported_cases <- example_confirmed[1:50]
# fit model to data to recover Rt estimates
est <- estimate_infections(reported_cases,
generation_time = generation_time_opts(example_generation_time),
delays = delay_opts(example_incubation_period + example_reporting_delay),
rt = rt_opts(prior = LogNormal(mean = 2, sd = 0.1), rw = 7),
obs = obs_opts(scale = Normal(mean = 0.1, sd = 0.01)),
gp = NULL, horizon = 0
)
#> Warning: The `horizon` argument of `estimate_infections()` is deprecated as of EpiNow2
#> 1.7.0.
#> ℹ Please use the `forecast` argument instead.
#> ℹ The `horizon` argument passed to `estimate_infections()` will override any
#> `horizon` argument passed via `forecast_opts()`.
# update Rt trajectory and simulate new infections using it
R <- c(rep(NA_real_, 26), rep(0.5, 10), rep(0.8, 14))
sims <- forecast_infections(est, R)
plot(sims)
# with a data.frame input of samples
R_dt <- data.frame(
date = seq(
min(summary(est, type = "parameters", param = "R")$date),
by = "day", length.out = length(R)
),
value = R
)
sims <- forecast_infections(est, R_dt)
plot(sims)
#' # with a data.frame input of samples
R_samples <- summary(est, type = "samples", param = "R")
R_samples <- R_samples[,
.(date, sample, value)][sample <= 1000][date <= "2020-04-10"
]
R_samples <- R_samples[date >= "2020-04-01", value := 1.1]
sims <- forecast_infections(est, R_samples)
plot(sims)
options(old_opts)
# }