| 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.2.0 |
| Built: | 2026-05-14 08:45:18 UTC |
| Source: | https://github.com/nfidd/nfidd |
Simulate symptom onset and hospitalization times from infection times
add_delays( infection_times, onset_fun = rgamma, onset_params = list(shape = 5, rate = 1), hosp_fun = rlnorm, hosp_params = list(meanlog = 1.75, sdlog = 0.5), hosp_prob = 0.3 )add_delays( infection_times, onset_fun = rgamma, onset_params = list(shape = 5, rate = 1), hosp_fun = rlnorm, hosp_params = list(meanlog = 1.75, sdlog = 0.5), hosp_prob = 0.3 )
infection_times |
A data frame containing a column of infection times |
onset_fun |
Function for onset delay distribution (default rgamma) |
onset_params |
List of parameters for onset distribution |
hosp_fun |
Function for hospitalization delay distribution (default rlnorm) |
hosp_params |
List of parameters for hospitalization distribution |
hosp_prob |
Probability of hospitalization (default 0.3) |
A data frame with columns for infection time, onset time, and hospitalization time (with (1-hosp_prob) of hospitalizations set to NA)
# Default parameters delayed_infections <- add_delays(infection_times) head(delayed_infections) # Change delay parameters delayed_infections_long <- add_delays( infection_times, hosp_params = list(meanlog = 2.0, sdlog = 0.5) ) # Use different distributions delayed_infections_gamma <- add_delays( infection_times, hosp_fun = rgamma, hosp_params = list(shape = 2, rate = 0.5) )# Default parameters delayed_infections <- add_delays(infection_times) head(delayed_infections) # Change delay parameters delayed_infections_long <- add_delays( infection_times, hosp_params = list(meanlog = 2.0, sdlog = 0.5) ) # Use different distributions delayed_infections_gamma <- add_delays( infection_times, hosp_fun = rgamma, hosp_params = list(shape = 2, rate = 0.5) )
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 assumes that symptom onsets are reported with a delay. It reduces the number of symptom onsets by this delay, assuming that the last element of the symptom onset vector is the day of reporting.
condition_onsets_by_report(onsets, delay)condition_onsets_by_report(onsets, delay)
onsets |
vector of symptom onsets |
delay |
vector representing the cumulative distribution function (CDF) of the (deiscrete) delay distribution. The first element corresponds to a delay of 0, i.e. symptom onsets reported on the same day. |
A vector of symptom onsets reduced by the delay distribution.
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_timesinfection_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_forecastsmech_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 either a specified Stan model from the NFIDD package or a custom Stan file provided by the user.
nfidd_cmdstan_model( model_name = NULL, model_file = NULL, include_paths = getOption("nfidd.stan_path", nfidd::nfidd_stan_path()), ... )nfidd_cmdstan_model( model_name = NULL, model_file = NULL, include_paths = getOption("nfidd.stan_path", nfidd::nfidd_stan_path()), ... )
model_name |
Character string specifying which Stan model to use from the NFIDD package. Ignored if model_file is provided. |
model_file |
Character string specifying the path to a custom Stan file. If provided, this takes precedence over model_name. |
include_paths |
Character vector of paths to include for Stan compilation. Defaults to the result of 'nfidd_stan_path()' or can be overridden using the R option "nfidd.stan_path". |
... |
Additional arguments passed to cmdstanr::cmdstan_model(). |
A CmdStanModel object.
Other modelhelpers:
nfidd_sample()
if (!is.null(cmdstanr::cmdstan_version(error_on_NA = FALSE))) { # Using a model from the NFIDD package model <- nfidd_cmdstan_model("simple-nowcast", compile = FALSE) model }if (!is.null(cmdstanr::cmdstan_version(error_on_NA = FALSE))) { # Using a model from the NFIDD package 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 wraps the cmdstanr sample method with optimized defaults for course use to speed up model fitting. All cmdstanr sample arguments can still be overridden for experimentation.
nfidd_sample( model, iter_warmup = 500, iter_sampling = 500, parallel_chains = 4, save_warmup = FALSE, ... )nfidd_sample( model, iter_warmup = 500, iter_sampling = 500, parallel_chains = 4, save_warmup = FALSE, ... )
model |
A CmdStanModel object to sample from. |
iter_warmup |
Integer, number of warmup iterations per chain. Defaults to 500 (reduced from cmdstanr default of 1000) for course speed. |
iter_sampling |
Integer, number of sampling iterations per chain. Defaults to 500 (reduced from cmdstanr default of 1000) for course speed. |
parallel_chains |
Integer, number of chains to run in parallel. Defaults to 4 for course speed. |
save_warmup |
Logical, whether to save warmup samples. Defaults to FALSE for course speed. |
... |
Additional arguments passed to the model's sample method. All cmdstanr sample arguments are supported. |
A CmdStanMCMC object containing the posterior samples.
Other modelhelpers:
nfidd_cmdstan_model()
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_forecastsrw_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_forecastsstat_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
Extract mean and standard deviation from lognormal distribution parameters in a posterior or draws object.
summarise_lognormal(posterior_obj)summarise_lognormal(posterior_obj)
posterior_obj |
A posterior or draws object containing lognormal parameters |
This function extracts lognormal parameters from a posterior object (expcting them to be called 'meanlog' and 'sdlog'and calculates the mean and standard deviation on the natural scale using:
mean = exp(meanlog + 0.5 * sdlog^2)
sd = exp(meanlog + 0.5 * sdlog^2) * sqrt(exp(sdlog^2) - 1)
A summary of the mean and standard deviation of the lognormal distribution
## Not run: # After fitting a lognormal model with cmdstan res |> summarise_lognormal() ## End(Not run)## Not run: # After fitting a lognormal model with cmdstan res |> summarise_lognormal() ## End(Not run)