Learn R Programming

⚠️There's a newer version (1.7.1) of this package.Take me there.

EpiNow2: Estimate real-time case counts and time-varying epidemiological parameters

Summary

{EpiNow2} estimates the time-varying reproduction number, growth rate, and doubling time using a range of open-source tools (Abbott et al.), and current best practices (Gostic et al.). It aims to help users avoid some of the limitations of naive implementations in a framework that is informed by community feedback and is actively supported.

Forecasting is also supported for the time-varying reproduction number, infections, and reported cases using the same generative process approach as used for estimation.

More details

{EpiNow2} estimates the time-varying reproduction number on cases by date of infection (using a similar approach to that implemented in {EpiEstim}). True infections, treated as latent and unobserved, are estimated and then mapped to observed data (for example cases by date of report) via one or more delay distributions (in the examples in the package documentation these are an incubation period and a reporting delay) and a reporting model that can include weekly periodicity.

Uncertainty is propagated from all inputs into the final parameter estimates, helping to mitigate spurious findings. This is handled internally. The time-varying reproduction estimates and the uncertain generation time also give time-varying estimates of the rate of growth.

Models provided

{EpiNow2} provides three models:

  • estimate_infections(): Reconstruct cases by date of infection from reported cases.

  • estimate_secondary(): Estimate the relationship between primary and secondary observations, for example, deaths (secondary) based on hospital admissions (primary), or bed occupancy (secondary) based on hospital admissions (primary).

  • estimate_truncation(): Estimate a truncation distribution from multiple snapshots of the same data source over time. For more flexibility, check out the {epinowcast} package.

The default model in estimate_infections() uses a non-stationary Gaussian process to estimate the time-varying reproduction number and infer infections. Other options, which generally reduce runtimes at the cost of the granularity of estimates or real-time performance, include:

  • A stationary Gaussian process (faster to estimate but currently gives reduced performance for real time estimates).
  • User specified breakpoints.
  • A fixed reproduction number.
  • A piecewise constant, combining a fixed reproduction number with breakpoints.
  • A random walk, combining a fixed reproduction number with regularly spaced breakpoints (i.e weekly).
  • A deconvolution/back-calculation method for inferring infections, followed with calculating the time-varying reproduction number.
  • Adjustment for the remaining susceptible population beyond the forecast horizon.

By default, all these models are fit with MCMC sampling using the rstan R package as the backend. Users can, however, switch to use approximate algorithms like variational inference, the pathfinder algorithm, or Laplace approximation especially for quick prototyping. The latter two methods are provided through the cmdstanr R package, so users will have to install that separately.

The documentation for estimate_infections provides examples of the implementation of the different options available.

{EpiNow2} is designed to be used via a single function call to two functions:

  • epinow(): Estimate Rt and cases by date of infection and forecast these infections into the future.

  • regional_epinow(): Efficiently run epinow() across multiple regions in an efficient manner.

These two functions call estimate_infections(), which works to reconstruct cases by date of infection from reported cases.

For more details on using each function corresponding function documentation.

Installation

Install the released version of the package:

install.packages("EpiNow2")

Install the development version of the package with:

install.packages("EpiNow2", repos = c("https://epiforecasts.r-universe.dev", getOption("repos")))

Alternatively, install the development version of the package with pak as follows (few users should need to do this):

# check whether {pak} is installed
if (!require("pak")) {
  install.packages("pak")
}
pak::pkg_install("epiforecasts/EpiNow2")

If using pak fails, try:

# check whether {remotes} is installed
if (!require("remotes")) {
  install.packages("remotes")
}
remotes::install_github("epiforecasts/EpiNow2")

To build {EpiNow2} from source, users will need to configure their C toolchain. This is because {EpiNow2} implements the underlying models in Stan (a statistical modelling programming language), which is built on C++.

Each operating system has a different set up procedure. Windows users need to install an appropriate version of RTools. Mac users can follow these steps, and Linux users can use this guide. For simple deployment/development a prebuilt docker image is also available (see documentation here).

Resources

Getting Started

The Getting Started vignette (see vignette("EpiNow2")) is your quickest entry point to the package. It provides a quick run through of the two main functions in the package and how to set up them up. It also discusses how to summarise and visualise the results after running the models.

More broadly, users can also learn the details of estimating delay distributions, nowcasting, and forecasting in a structured way through the free and open short-course, “Nowcasting and forecasting infectious disease dynamics”, developed by some authors of this package.

Package website

The package has two websites: one for the stable release version on CRAN, and another for the version in development. These two provide various resources for learning about the package, including the function reference, details about each model (model definition), workflows for each model (usage), and case studies or literature of applications of the package. However, the development website may contain experimental features and information not yet available in the stable release.

End-to-end workflows

The workflow vignette (see vignette("estimate_infections_workflow")) provides guidance on the end-to-end process of estimating reproduction numbers and performing short-term forecasts for a disease spreading in a

Model definitions

In different vignettes we provide the mathematical definition of each model. For example, the model definition vignette for estimate_infections() can be found in vignette("estimate_infections").

Example implementations

A simple example of using the package to estimate a national Rt for Covid-19 can be found here.

Contributing

We welcome all contributions. If you have identified an issue with the package, you can file an issue here. We also welcome additions and extensions to the underlying model either in the form of options or improvements. If you wish to contribute in any form, please follow the package contributing guide.

Contributors

All contributions to this project are gratefully acknowledged using the allcontributors package following the all-contributors specification. Contributions of any kind are welcome!

Code

seabbs, sbfnk, jamesmbaazam, joeHickson, hsbadr, pitmonticone, actions-user, ellisp, jdmunday, pearsonca, JAllen42, kaitejohnson, adamkucharski, andrjohns, Bisaloo, LloydChapman, medewitt, nikosbosse, sophiemeakin

Issue Authors

raulfernandezn, pcarbo, johnaponte, sophie-schiller, munozedg, kathsherratt, yungwai, kgostic, fkrauer, philturk, krageth, tony352, username-rp, HAKGH, AndrewRiceMGW, brynhayder, RichardMN, andrybicio, rhamoonga, furqan915, MFZaini1984, fabsig, affans, GauriSaran, davidvilanova, jrcpulliam, dajmcdon, joshwlambert, avallecam, athowes, lorenzwalthert, nlinton, martinamcm, adrian-lison

Issue Contributors

jhellewell14, thlytras, LizaHadley, ntorresd, SamuelBrand1

Copy Link

Version

Install

install.packages('EpiNow2')

Monthly Downloads

718

Version

1.6.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Sebastian Funk

Last Published

October 31st, 2024

Functions in EpiNow2 (1.6.1)

calc_CrIs

Calculate Credible Intervals
check_reports_valid

Validate data input
check_generation_time

Validate probability distribution for using as generation time
calc_summary_measures

Calculate All Summary Measures
bootstrapped_dist_fit

Fit a Subsampled Bootstrap to Integer Values and Summarise Distribution Parameters
calc_summary_stats

Calculate Summary Statistics
collapse

Collapse nonparametric distributions in a <dist_spec>
create_clean_reported_cases

Create Clean Reported Cases
check_na_setting_against_data

Cross-check treatment of NA in obs_opts() against input data
create_rt_data

Create Time-varying Reproduction Number Data
check_stan_delay

Validate probability distribution for passing to stan
check_sparse_pmf_tail

Check that PMF tail is not sparse
convert_to_natural

Internal function for converting parameters to natural parameters.
create_obs_model

Create Observation Model Settings
create_shifted_cases

Create Delay Shifted Cases
construct_output

Construct Output
create_complete_cases

Create complete cases
discretise

Discretise a <dist_spec>
clean_nowcasts

Clean Nowcasts for a Supplied Date
convolve_and_scale

Convolve and scale a time series
create_gp_data

Create Gaussian Process Data
create_initial_conditions

Create Initial Conditions Generating Function
clean_regions

Clean Regions
convert_to_logmean

Convert mean and sd to log mean for a log normal distribution
dist_fit

Fit an Integer Adjusted Exponential, Gamma or Lognormal distributions
create_stan_args

Create a List of Stan Arguments
create_backcalc_data

Create Back Calculation Data
delay_opts

Delay Distribution Options
estimates_by_report_date

Estimate Cases by Report Date
copy_results_to_latest

Copy Results From Dated Folder to Latest
epinow2_rstan_model

Load an EpiNow2 rstan model.
epinow2_stan_model

Return a stan model object for the appropriate backend
create_stan_data

Create Stan Data Required for estimate_infections
extract_params

Extract parameter names
example_confirmed

Example Confirmed Case Data Set
extract_parameter_samples

Extract Parameter Samples from a Stan Model
epinow2_cmdstan_model

Load and compile an EpiNow2 cmdstanr model
epinow

Real-time Rt Estimation, Forecasting and Reporting
extract_static_parameter

Extract Samples from a Parameter with a Single Dimension
extract_stan_param

Extract a Parameter Summary from a Stan Object
example_generation_time

Example generation time
example_incubation_period

Example incubation period
create_stan_delays

Create delay variables for stan
estimate_truncation

Estimate Truncation of Observed Data
estimate_secondary

Estimate a Secondary Observation from a Primary Observation
extract_inits

Generate initial conditions from a Stan fit
fit_model_with_nuts

Fit a Stan Model using the NUTs sampler
example_truncated

Example Case Data Set with Truncation
fit_model_approximate

Fit a Stan Model using an approximate method
example_reporting_delay

Example reporting delay
convert_to_logsd

Convert mean and sd to log standard deviation for a log normal distribution
estimate_delay

Estimate a Delay Distribution
estimate_infections

Estimate Infections, the Time-Varying Reproduction Number and the Rate of Growth
dist_skel

Distribution Skeleton
create_delay_inits

Create initial conditions for delays
discrete_pmf

Discretised probability mass function
dist_spec

Specify a distribution.
create_future_rt

Construct the Required Future Rt assumption
is_constrained

Check if a <dist_spec> is constrained, i.e. has a finite maximum or nonzero CDF cutoff.
filter_opts

Filter Options for a Target Region
forecast_infections

Forecast infections from a given fit and trajectory of the time-varying reproduction number
extract_parameter

Extract Samples for a Parameter from a Stan model
gt_opts

Generation Time Distribution Options
lapply_func

Choose a parallel or sequential apply function
fit_model

Fit a model using the chosen backend.
extract_CrIs

Extract Credible Intervals Present
expose_stan_fns

Expose internal package stan functions in R
extract_samples

Extract all samples from a stan fit
mean.dist_spec

Returns the mean of one or more delay distribution
growth_to_R

Convert Growth Rates to Reproduction numbers.
forecast_secondary

Forecast Secondary Observations Given a Fit from estimate_secondary
get_regions_with_most_reports

Get Regions with Most Reported Cases
gp_opts

Approximate Gaussian Process Settings
generation_times

Literature Estimates of Generation Times
lognorm_dist_def

Generate a Log Normal Distribution Definition Based on Parameter Estimates
extract_single_dist

Extract a single element of a composite <dist_spec>
get_distribution

Get the distribution of a <dist_spec>
get_pmf

Get the probability mass function of a nonparametric distribution
get_element

Extracts an element of a <dist_spec>
get_dist

Get a Literature Distribution
lower_bounds

Get the lower bounds of the parameters of a distribution
get_seeding_time

Estimate seeding time from delays and generation time
fix_parameters

Fix the parameters of a <dist_spec>
get_regional_results

Get Combined Regional Results
get_parameters

Get parameters of a parametric distribution
get_incubation_period

Get a Literature Distribution for the Incubation Period
format_fit

Format Posterior Samples
incubation_periods

Literature Estimates of Incubation Periods
get_generation_time

Get a Literature Distribution for the Generation Time
gamma_dist_def

Generate a Gamma Distribution Definition Based on Parameter Estimates
fix_dist

Remove uncertainty in the parameters of a <dist_spec>
+.dist_spec

Creates a delay distribution as the sum of two other delay distributions.
plot.dist_spec

Plot PMF and CDF for a dist_spec object
plot_estimates

Plot Estimates
plot.epinow

Plot method for epinow
print.dist_spec

Prints the parameters of one or more delay distributions
natural_params

Get the names of the natural parameters of a distribution
make_conf

Format Credible Intervals
rstan_opts

Rstan Options
plot_summary

Plot a Summary of the Latest Results
get_raw_result

Get a Single Raw Result
rstan_sampling_opts

Rstan Sampling Options
match_output_arguments

Match User Supplied Arguments with Supported Options
get_regions

Get Folders with Results
max.dist_spec

Returns the maximum of one or more delay distribution
plot.estimate_secondary

Plot method for estimate_secondary
plot.estimate_infections

Plot method for estimate_infections
map_prob_change

Categorise the Probability of Change for Rt
rstan_vb_opts

Rstan Variational Bayes Options
summarise_results

Summarise Real-time Results
stan_laplace_opts

Stan Laplace algorithm Options
secondary_opts

Secondary Reports Options
save_estimate_infections

Save Estimated Infections
summary.epinow

Summary output from epinow
sd

Returns the standard deviation of one or more delay distribution
save_input

Save Observed Data
rt_opts

Time-Varying Reproduction Number Options
stan_opts

Stan Options
regional_summary

Regional Summary Output
obs_opts

Observation Model Options
opts_list

Return an _opts List per Region
regional_epinow

Real-time Rt Estimation, Forecasting and Reporting by Region
init_cumulative_fit

Generate initial conditions by fitting to cumulative cases
new_dist_spec

Internal function for generating a dist_spec given parameters and a distribution.
process_region

Process regional estimate
report_cases

Report case counts by date of report
setup_dt

Convert to Data Table
ndist

Calculate the number of distributions in a <dist_spec>
regional_runtimes

Summarise Regional Runtimes
process_regions

Process all Region Estimates
update_secondary_args

Update estimate_secondary default priors
setup_future

Set up Future Backend
plot_CrIs

Plot EpiNow2 Credible Intervals
plot.estimate_truncation

Plot method for estimate_truncation
run_region

Run epinow with Regional Processing Code
simulate_infections

Simulate infections using the renewal equation
sample_approx_dist

Approximate Sampling a Distribution using Counts
simulate_secondary

Simulate secondary observations from primary observations
setup_default_logging

Setup Default Logging
set_dt_single_thread

Set to Single Threading
report_summary

Provide Summary Statistics for Estimated Infections and Rt
setup_logging

Setup Logging
report_plots

Report plots
setup_target_folder

Setup Target Folder for Saving
trunc_opts

Truncation Distribution Options
update_horizon

Updates Forecast Horizon Based on Input Data and Target
stan_pathfinder_opts

Stan pathfinder algorithm Options
summarise_key_measures

Summarise rt and cases
stan_vb_opts

Stan Variational Bayes Options
stan_sampling_opts

Stan Sampling Options
summary.estimate_infections

Summary output from estimate_infections
test_data_complete

Check if data has either explicit NA values or implicit missing dates.
allocate_empty

Allocate Empty Parameters to a List
R_to_growth

Convert Reproduction Numbers to Growth Rates
backcalc_opts

Back Calculation Options
Distributions

Probability distributions
bound_dist

Define bounds of a <dist_spec>
c.dist_spec

Combines multiple delay distributions for further processing
adjust_infection_to_report

Adjust from Case Counts by Infection Date to Date of Report
apply_tolerance

Applies a threshold to all nonparametric distributions in a <dist_spec>
EpiNow2-package

EpiNow2: Estimate Real-Time Case Counts and Time-Varying Epidemiological Parameters
allocate_delays

Allocate Delays into Required Stan Format
apply_default_cdf_cutoff

Apply default CDF cutoff to a <dist_spec> if it is unconstrained
add_day_of_week

Adds a day of the week vector
calc_CrI

Calculate Credible Interval