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.

{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.

{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.

Resources

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.

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.

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

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").

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 allcontributors specification. Contributions of any kind are welcome!

Code

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

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, micahwiesner67

Issue Contributors

jhellewell14, thlytras, LizaHadley, ntorresd, SamuelBrand1

Copy Link

Version

Install

install.packages('EpiNow2')

Monthly Downloads

718

Version

1.7.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Sebastian Funk

Last Published

February 5th, 2025

Functions in EpiNow2 (1.7.0)

apply_zero_threshold

Convert zero case counts to NA (missing) if the 7-day average is above a threshold.
bootstrapped_dist_fit

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

Calculate Summary Statistics
calc_CrIs

Calculate Credible Intervals
c.dist_spec

Combines multiple delay distributions for further processing
check_stan_delay

Validate probability distribution for passing to stan
clean_regions

Clean Regions
calc_summary_measures

Calculate All Summary Measures
check_sparse_pmf_tail

Check that PMF tail is not sparse
check_reports_valid

Validate data input
clean_nowcasts

Clean Nowcasts for a Supplied Date
create_clean_reported_cases

Create Clean Reported Cases
convert_to_logmean

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

Convolve and scale a time series
convert_to_logsd

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

Copy Results From Dated Folder to Latest
construct_output

Construct Output
create_backcalc_data

Create Back Calculation Data
convert_to_natural

Internal function for converting parameters to natural parameters.
collapse

Collapse nonparametric distributions in a <dist_spec>
create_delay_inits

Create initial conditions for delays
create_gp_data

Create Gaussian Process Data
create_future_rt

Construct the Required Future Rt assumption
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
create_obs_model

Create Observation Model Settings
check_generation_time

Validate probability distribution for using as generation time
create_initial_conditions

Create Initial Conditions Generating Function
create_shifted_cases

Create Delay Shifted Cases
create_stan_data

Create Stan Data Required for estimate_infections
create_stan_delays

Create delay variables for stan
estimate_delay

Estimate a Delay Distribution
==.dist_spec

Compares two delay distributions
create_stan_args

Create a List of Stan Arguments
dist_fit

Fit an Integer Adjusted Exponential, Gamma or Lognormal distributions
discretise

Discretise a <dist_spec>
create_stan_params

Create parameters for stan
example_confirmed

Example Confirmed Case Data Set
epinow

Real-time Rt Estimation, Forecasting and Reporting
epinow2_cmdstan_model

Load and compile an EpiNow2 cmdstanr model
example_reporting_delay

Example reporting delay
example_incubation_period

Example incubation period
dist_skel

Distribution Skeleton
default_fill_missing_obs

Temporary function to support the transition to full support of missing data.
dist_spec

Specify a distribution.
delay_opts

Delay Distribution Options
example_generation_time

Example generation time
fill_missing

Fill missing data in a data set to prepare it for use within the package
extract_static_parameter

Extract Samples from a Parameter with a Single Dimension
extract_stan_param

Extract a Parameter Summary from a Stan Object
extract_single_dist

Extract a single element of a composite <dist_spec>
epinow2_rstan_model

Load an EpiNow2 rstan model.
expose_stan_fns

Expose internal package stan functions in R
estimate_truncation

Estimate Truncation of Observed Data
estimate_infections

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

Discretised probability mass function
example_truncated

Example Case Data Set with Truncation
fit_model

Fit a model using the chosen backend.
epinow2_stan_model

Return a stan model object for the appropriate backend
estimate_secondary

Estimate a Secondary Observation from a Primary Observation
fix_parameters

Fix the parameters of a <dist_spec>
get_incubation_period

Get a Literature Distribution for the Incubation Period
extract_parameter_samples

Extract Parameter Samples from a Stan Model
fit_model_approximate

Fit a Stan Model using an approximate method
extract_parameter

Extract Samples for a Parameter from a Stan model
get_parameters

Get parameters of a parametric distribution
extract_CrIs

Extract Credible Intervals Present
extract_inits

Generate initial conditions from a Stan fit
gt_opts

Generation Time Distribution Options
forecast_infections

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

Literature Estimates of Generation Times
forecast_opts

Forecast options
get_element

Extracts an element of a <dist_spec>
extract_samples

Extract all samples from a stan fit
filter_opts

Filter Options for a Target Region
filter_leading_zeros

Filter leading zeros from a data set.
extract_params

Extract parameter names
estimates_by_report_date

Estimate Cases by Report Date
get_pmf

Get the probability mass function of a nonparametric distribution
forecast_secondary

Forecast Secondary Observations Given a Fit from estimate_secondary
fit_model_with_nuts

Fit a Stan Model using the NUTs sampler
fix_dist

Remove uncertainty in the parameters of a <dist_spec>
get_distribution

Get the distribution of a <dist_spec>
is_constrained

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

Get a Literature Distribution
gamma_dist_def

Generate a Gamma Distribution Definition Based on Parameter Estimates
format_fit

Format Posterior Samples
get_generation_time

Get a Literature Distribution for the Generation Time
init_cumulative_fit

Generate initial conditions by fitting to cumulative cases
incubation_periods

Literature Estimates of Incubation Periods
get_regional_results

Get Combined Regional Results
get_regions

Get Folders with Results
make_conf

Format Credible Intervals
plot.estimate_truncation

Plot method for estimate_truncation
map_prob_change

Categorise the Probability of Change for Rt
gp_opts

Approximate Gaussian Process Settings
max.dist_spec

Returns the maximum of one or more delay distribution
get_raw_result

Get a Single Raw Result
match_output_arguments

Match User Supplied Arguments with Supported Options
plot.estimate_infections

Plot method for estimate_infections
plot.estimate_secondary

Plot method for estimate_secondary
plot_CrIs

Plot EpiNow2 Credible Intervals
plot.epinow

Plot method for epinow
regional_summary

Regional Summary Output
regional_epinow

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

Process regional estimate
lapply_func

Choose a parallel or sequential apply function
lognorm_dist_def

Generate a Log Normal Distribution Definition Based on Parameter Estimates
lower_bounds

Get the lower bounds of the parameters of a distribution
plot.dist_spec

Plot PMF and CDF for a dist_spec object
growth_to_R

Convert Growth Rates to Reproduction numbers.
print.dist_spec

Prints the parameters of one or more delay distributions
new_dist_spec

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

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

Report case counts by date of report
save_input

Save Observed Data
save_estimate_infections

Save Estimated Infections
ndist

Calculate the number of distributions in a <dist_spec>
report_plots

Report plots
process_regions

Process all Region Estimates
get_regions_with_most_reports

Get Regions with Most Reported Cases
regional_runtimes

Summarise Regional Runtimes
report_summary

Provide Summary Statistics for Estimated Infections and Rt
rstan_opts

Rstan Options
get_seeding_time

Estimate seeding time from delays and generation time
natural_params

Get the names of the natural parameters of a distribution
plot_estimates

Plot Estimates
opts_list

Forecast optiong
obs_opts

Observation Model Options
mean.dist_spec

Returns the mean of one or more delay distribution
run_region

Run epinow with Regional Processing Code
plot_summary

Plot a Summary of the Latest Results
setup_future

Set up Future Backend
setup_logging

Setup Logging
stan_opts

Stan Options
rt_opts

Time-Varying Reproduction Number Options
sd

Returns the standard deviation of one or more delay distribution
secondary_opts

Secondary Reports Options
summary.estimate_infections

Summary output from estimate_infections
rstan_vb_opts

Rstan Variational Bayes Options
summary.epinow

Summary output from epinow
stan_laplace_opts

Stan Laplace algorithm Options
rstan_sampling_opts

Rstan Sampling Options
trunc_opts

Truncation Distribution Options
setup_forecast

Ensure forecast is properly set up
simulate_secondary

Simulate secondary observations from primary observations
setup_dt

Convert to Data Table
set_dt_single_thread

Set to Single Threading
test_data_complete

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

Stan Sampling Options
sample_approx_dist

Approximate Sampling a Distribution using Counts
stan_pathfinder_opts

Stan pathfinder algorithm Options
setup_target_folder

Setup Target Folder for Saving
simulate_infections

Simulate infections using the renewal equation
update_secondary_args

Update estimate_secondary default priors
update_horizon

Updates Forecast Horizon Based on Input Data and Target
stan_vb_opts

Stan Variational Bayes Options
summarise_key_measures

Summarise rt and cases
setup_default_logging

Setup Default Logging
summarise_results

Summarise Real-time Results
allocate_empty

Allocate Empty Parameters to a List
R_to_growth

Convert Reproduction Numbers to Growth Rates
Distributions

Probability distributions
add_breakpoints

Add breakpoints to certain dates in a data set.
bound_dist

Define bounds of a <dist_spec>
backcalc_opts

Back Calculation Options
calc_CrI

Calculate Credible Interval
add_horizon

Add missing values for future dates
EpiNow2-package

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

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

Allocate Delays into Required Stan Format
apply_tolerance

Applies a threshold to all nonparametric distributions in a <dist_spec>
add_day_of_week

Adds a day of the week vector
apply_default_cdf_cutoff

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