Title: | Material to support course on nowcasting and forecasting of infectious disease dynamics |
---|---|
Description: | Resources to support a short course on nowcasting and forecasting of infectious disease dynamics. |
Authors: | NFIDD course contributors [cre, aut] |
Maintainer: | NFIDD course contributors <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0 |
Built: | 2024-12-05 05:30:54 UTC |
Source: | https://github.com/nfidd/nfidd |
Simulate symptom onset and hospitalization times from infection times
add_delays(infection_times)
add_delays(infection_times)
infection_times |
A data frame containing a column of infection times |
A data frame with columns for infection time, onset time, and hospitalization time (with 70
delayed_infections <- add_delays(infection_times) head(delayed_infections)
delayed_infections <- add_delays(infection_times) head(delayed_infections)
This function discretises a continuous delay distribution into a probability mass function (PMF) over discrete days.
censored_delay_pmf(rgen, max, n = 1e+06, ...)
censored_delay_pmf(rgen, max, n = 1e+06, ...)
rgen |
A function that generates random delays, e.g., 'rgamma', 'rlnorm'. |
max |
The maximum delay. |
n |
The number of replicates to simulate. Defaults to '1e+6'. |
... |
Additional parameters of the delay distribution. |
A vector of probabilities corresponding to discrete indices from '0' to 'max', representing the discretised delay distribution.
censored_delay_pmf(rgen = rgamma, max = 14, shape = 5, rate = 1)
censored_delay_pmf(rgen = rgamma, max = 14, shape = 5, rate = 1)
This function convolves a time series with a delay distribution given as a probability mass function (PMF).
convolve_with_delay(ts, delay_pmf)
convolve_with_delay(ts, delay_pmf)
ts |
A vector of the time series to convolve. |
delay_pmf |
The probability mass function of the delay, given as a vector of probabilities, corresponding to discrete indices 0, 1, 2 of the discretised delay distribution. |
A vector of the convolved time series.
convolve_with_delay(ts = c(10, 14, 10, 10), delay_pmf = c(0.1, 0.6, 0.3))
convolve_with_delay(ts = c(10, 14, 10, 10), delay_pmf = c(0.1, 0.6, 0.3))
This function generates a geometric differenced autoregressive process.
geometric_diff_ar(init, noise, std, damp)
geometric_diff_ar(init, noise, std, damp)
init |
The initial value. |
noise |
A vector of steps (on the standard normal scale). |
std |
The step size of the random walk. |
damp |
A damping parameter. |
A vector of the generated geometric differenced autoregressive process.
geometric_diff_ar(init = 1, noise = rnorm(100), std = 0.1, damp = 0.1)
geometric_diff_ar(init = 1, noise = rnorm(100), std = 0.1, damp = 0.1)
This function generates a geometric random walk.
geometric_random_walk(init, noise, std)
geometric_random_walk(init, noise, std)
init |
The initial value. |
noise |
A vector of steps (on the standard normal scale). |
std |
The step size of the random walk. |
A vector of the generated geometric random walk.
geometric_random_walk(init = 1, noise = rnorm(100), std = 0.1)
geometric_random_walk(init = 1, noise = rnorm(100), std = 0.1)
A dataset containing random infection times from a branching process model, generated using the code in data-raw/epicurve.r
infection_times
infection_times
A data frame with a single column
the times at which individuals were infected (and became infectious)
Convert infection times to a daily time series
make_daily_infections(infection_times)
make_daily_infections(infection_times)
infection_times |
A data frame containing a column of infection times |
A data frame with columns infection_day and infections, containing the daily count of infections
make_daily_infections(infection_times)
make_daily_infections(infection_times)
Generate a probability mass function for the generation time
make_gen_time_pmf(max = 14, shape = 4, rate = 1)
make_gen_time_pmf(max = 14, shape = 4, rate = 1)
max |
Maximum delay to consider |
shape |
Shape parameter for the gamma distribution |
rate |
Rate parameter for the gamma distribution |
A vector of probabilities representing the generation time PMF
Generate a probability mass function for the incubation period
make_ip_pmf(max = 14, shape = 5, rate = 1)
make_ip_pmf(max = 14, shape = 5, rate = 1)
max |
Maximum delay to consider |
shape |
Shape parameter for the gamma distribution |
rate |
Rate parameter for the gamma distribution |
A vector of probabilities representing the incubation period PMF
A dataset containing forecasts from a mechanistic model, generated using the code in data-raw/generate-example-forecasts.r
mech_forecasts
mech_forecasts
A [tibble::tibble()] with a 7 columns
the day for which the forecast was made
an ID of the posterior predictive sample
name of the variable
predicted value
the forecast horizon in days
the day on which the forecast was made (using data up to this day)
the name of the model
This function creates a CmdStanModel object using a specified Stan model from the NFIDD package and optionally includes additional user-specified Stan files.
nfidd_cmdstan_model(model_name, include_paths = nfidd::nfidd_stan_path(), ...)
nfidd_cmdstan_model(model_name, include_paths = nfidd::nfidd_stan_path(), ...)
model_name |
Character string specifying which Stan model to use. |
include_paths |
Character vector of paths to include for Stan compilation. Defaults to the result of 'nfidd_stan_path()'. |
... |
Additional arguments passed to cmdstanr::cmdstan_model(). |
A CmdStanModel object.
if (!is.null(cmdstanr::cmdstan_version(error_on_NA = FALSE))) { model <- nfidd_cmdstan_model("simple-nowcast", compile = FALSE) model }
if (!is.null(cmdstanr::cmdstan_version(error_on_NA = FALSE))) { model <- nfidd_cmdstan_model("simple-nowcast", compile = FALSE) model }
Load Stan functions as a string
nfidd_load_stan_functions( functions = NULL, stan_path = nfidd::nfidd_stan_path(), wrap_in_block = FALSE, write_to_file = FALSE, output_file = "nfidd_functions.stan" )
nfidd_load_stan_functions( functions = NULL, stan_path = nfidd::nfidd_stan_path(), wrap_in_block = FALSE, write_to_file = FALSE, output_file = "nfidd_functions.stan" )
functions |
Character vector of function names to load. Defaults to all functions. |
stan_path |
Character string, the path to the Stan code. Defaults to the path to the Stan code in the nfidd package. |
wrap_in_block |
Logical, whether to wrap the functions in a 'functions' block. Default is FALSE. |
write_to_file |
Logical, whether to write the output to a file. Default is FALSE. |
output_file |
Character string, the path to write the output file if write_to_file is TRUE. Defaults to "nfidd_functions.stan". |
A character string containing the requested Stan functions
Other stantools:
nfidd_stan_function_files()
,
nfidd_stan_functions()
,
nfidd_stan_path()
This function retrieves Stan files from a specified directory, optionally filtering for files that contain specific functions.
nfidd_stan_function_files( functions = NULL, stan_path = nfidd::nfidd_stan_path() )
nfidd_stan_function_files( functions = NULL, stan_path = nfidd::nfidd_stan_path() )
functions |
Character vector of function names to search for. If NULL, all Stan files are returned. |
stan_path |
Character string specifying the path to the directory containing Stan files. Defaults to the Stan path of the nfidd package. |
A character vector of file paths to Stan files.
Other stantools:
nfidd_load_stan_functions()
,
nfidd_stan_functions()
,
nfidd_stan_path()
This function reads all Stan files in the specified directory and extracts the names of all functions defined in those files.
nfidd_stan_functions(stan_path = nfidd::nfidd_stan_path())
nfidd_stan_functions(stan_path = nfidd::nfidd_stan_path())
stan_path |
Character string specifying the path to the directory containing Stan files. Defaults to the Stan path of the nfidd package. |
A character vector containing unique names of all functions found in the Stan files.
Other stantools:
nfidd_load_stan_functions()
,
nfidd_stan_function_files()
,
nfidd_stan_path()
This function finds all available Stan models in the NFIDD package and returns their names without the .stan extension.
nfidd_stan_models(stan_path = nfidd::nfidd_stan_path())
nfidd_stan_models(stan_path = nfidd::nfidd_stan_path())
stan_path |
Character string specifying the path to Stan files. Defaults to the result of 'nfidd_stan_path()'. |
A character vector of available Stan model names.
nfidd_stan_models()
nfidd_stan_models()
Get the path to Stan code
nfidd_stan_path()
nfidd_stan_path()
A character string with the path to the Stan code
Other stantools:
nfidd_load_stan_functions()
,
nfidd_stan_function_files()
,
nfidd_stan_functions()
This function simulates infections using the renewal equation.
renewal(I0, R, gen_time)
renewal(I0, R, gen_time)
I0 |
The initial number of infections. |
R |
The reproduction number, given as a vector with one entry per time point. |
gen_time |
The generation time distribution, given as a vector with one entry per day after infection (the first element corresponding to one day after infection). |
A vector of simulated infections over time.
renewal( I0 = 5, R = c(rep(3, 4), rep(0.5, 5)), gen_time = c(0.1, 0.2, 0.3, 0.2, 0.1) )
renewal( I0 = 5, R = c(rep(3, 4), rep(0.5, 5)), gen_time = c(0.1, 0.2, 0.3, 0.2, 0.1) )
A dataset containing forecasts from a semi-mechanistic model (using a geometric random walk prior on the reproduction number), generated using the code in data-raw/generate-example-forecasts.r
rw_forecasts
rw_forecasts
A [tibble::tibble()] with a 7 columns
the day for which the forecast was made
an ID of the posterior predictive sample
name of the variable
predicted value
the forecast horizon in days
the day on which the forecast was made (using data up to this day)
the name of the model
Simulate symptom onsets from daily infection counts
simulate_onsets( inf_ts, gen_time_pmf = make_gen_time_pmf(), ip_pmf = make_ip_pmf() )
simulate_onsets( inf_ts, gen_time_pmf = make_gen_time_pmf(), ip_pmf = make_ip_pmf() )
inf_ts |
A data frame containing columns infection_day and infections, as returned by 'make_daily_infections()'. |
gen_time_pmf |
A vector of probabilities representing the generation time PMF, as returned by 'make_gen_time_pmf()'. |
ip_pmf |
A vector of probabilities representing the incubation period PMF, as returned by 'make_ip_pmf()'. |
A data frame with columns day, onsets, and infections containing the daily count of symptom onsets and infections
gt_pmf <- make_gen_time_pmf() ip_pmf <- make_ip_pmf() simulate_onsets(make_daily_infections(infection_times), gt_pmf, ip_pmf)
gt_pmf <- make_gen_time_pmf() ip_pmf <- make_ip_pmf() simulate_onsets(make_daily_infections(infection_times), gt_pmf, ip_pmf)
A dataset containing forecasts from a semi-mechanistic model (using an autoregressive prior for reproduction number), generated using the code in data-raw/generate-example-forecasts.r
stat_forecasts
stat_forecasts
A [tibble::tibble()] with a 7 columns
the day for which the forecast was made
an ID of the posterior predictive sample
name of the variable
predicted value
the forecast horizon in days
the day on which the forecast was made (using data up to this day)
the name of the model