Learn R Programming

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

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Sebastian Funk

Last Published

February 19th, 2025

Functions in EpiNow2 (1.7.1)

calc_summary_measures

Calculate All Summary Measures
clean_regions

Clean Regions
create_backcalc_data

Create Back Calculation Data
convert_to_logmean

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

Create initial conditions for delays
create_shifted_cases

Create Delay Shifted Cases
construct_output

Construct Output
create_rt_data

Create Time-varying Reproduction Number Data
collapse

Collapse nonparametric distributions in a <dist_spec>
create_future_rt

Construct the Required Future Rt assumption
create_gp_data

Create Gaussian Process Data
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
delay_opts

Delay Distribution Options
default_fill_missing_obs

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

Clean Nowcasts for a Supplied Date
check_stan_delay

Validate probability distribution for passing to stan
create_stan_data

Create Stan Data Required for estimate_infections
copy_results_to_latest

Copy Results From Dated Folder to Latest
convert_to_natural

Internal function for converting parameters to natural parameters.
create_stan_args

Create a List of Stan Arguments
dist_fit

Fit an Integer Adjusted Exponential, Gamma or Lognormal distributions
check_reports_valid

Validate data input
check_sparse_pmf_tail

Check that PMF tail is not sparse
create_initial_conditions

Create Initial Conditions Generating Function
create_obs_model

Create Observation Model Settings
dist_skel

Distribution Skeleton
epinow

Real-time Rt Estimation, Forecasting and Reporting
epinow2_stan_model

Return a stan model object for the appropriate backend
create_stan_delays

Create delay variables for stan
epinow2_rstan_model

Load an EpiNow2 rstan model.
estimate_truncation

Estimate Truncation of Observed Data
extract_CrIs

Extract Credible Intervals Present
epinow2_cmdstan_model

Load and compile an EpiNow2 cmdstanr model
estimates_by_report_date

Estimate Cases by Report Date
create_stan_params

Create parameters for stan
estimate_infections

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

Example Confirmed Case Data Set
filter_leading_zeros

Filter leading zeros from a data set.
example_generation_time

Example generation time
extract_params

Extract parameter names
expose_stan_fns

Expose internal package stan functions in R
estimate_secondary

Estimate a Secondary Observation from a Primary Observation
example_truncated

Example Case Data Set with Truncation
extract_single_dist

Extract a single element of a composite <dist_spec>
get_distribution

Get the distribution of a <dist_spec>
==.dist_spec

Compares two delay distributions
estimate_delay

Estimate a Delay Distribution
filter_opts

Filter Options for a Target Region
extract_samples

Extract all samples from a stan fit
extract_inits

Generate initial conditions from a Stan fit
fit_model_with_nuts

Fit a Stan Model using the NUTs sampler
extract_parameter

Extract Samples for a Parameter from a Stan model
forecast_infections

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

Extract a Parameter Summary from a Stan Object
fix_parameters

Fix the parameters of a <dist_spec>
extract_static_parameter

Extract Samples from a Parameter with a Single Dimension
fit_model

Fit a model using the chosen backend.
extract_parameter_samples

Extract Parameter Samples from a Stan Model
fill_missing

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

Fit a Stan Model using an approximate method
is_constrained

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

Convert Growth Rates to Reproduction numbers.
fix_dist

Remove uncertainty in the parameters of a <dist_spec>
forecast_secondary

Forecast Secondary Observations Given a Fit from estimate_secondary
forecast_opts

Forecast options
lapply_func

Choose a parallel or sequential apply function
discretise

Discretise a <dist_spec>
discrete_pmf

Discretised probability mass function
mean.dist_spec

Returns the mean of one or more delay distribution
plot_estimates

Plot Estimates
plot_summary

Plot a Summary of the Latest Results
natural_params

Get the names of the natural parameters of a distribution
get_regional_results

Get Combined Regional Results
get_parameters

Get parameters of a parametric distribution
get_regions

Get Folders with Results
get_raw_result

Get a Single Raw Result
ndist

Calculate the number of distributions in a <dist_spec>
get_element

Extracts an element of a <dist_spec>
get_regions_with_most_reports

Get Regions with Most Reported Cases
format_fit

Format Posterior Samples
example_reporting_delay

Example reporting delay
plot.estimate_secondary

Plot method for estimate_secondary
get_pmf

Get the probability mass function of a nonparametric distribution
new_dist_spec

Internal function for generating a dist_spec given parameters and a distribution.
plot.estimate_infections

Plot method for estimate_infections
max.dist_spec

Returns the maximum of one or more delay distribution
match_output_arguments

Match User Supplied Arguments with Supported Options
process_region

Process regional estimate
process_regions

Process all Region Estimates
example_incubation_period

Example incubation period
plot.dist_spec

Plot PMF and CDF for a dist_spec object
plot.estimate_truncation

Plot method for estimate_truncation
report_summary

Provide Summary Statistics for Estimated Infections and Rt
plot.epinow

Plot method for epinow
setup_dt

Convert to Data Table
save_input

Save Observed Data
sd

Returns the standard deviation of one or more delay distribution
lower_bounds

Get the lower bounds of the parameters of a distribution
rt_opts

Time-Varying Reproduction Number Options
setup_default_logging

Setup Default Logging
gt_opts

Generation Time Distribution Options
make_conf

Format Credible Intervals
gp_opts

Approximate Gaussian Process Settings
update_secondary_args

Update estimate_secondary default priors
get_seeding_time

Estimate seeding time from delays and generation time
stan_laplace_opts

Stan Laplace algorithm Options
simulate_secondary

Simulate secondary observations from primary observations
regional_summary

Regional Summary Output
summarise_key_measures

Summarise rt and cases
summarise_results

Summarise Real-time Results
trunc_opts

Truncation Distribution Options
plot_CrIs

Plot EpiNow2 Credible Intervals
obs_opts

Observation Model Options
opts_list

Forecast optiong
map_prob_change

Categorise the Probability of Change for Rt
regional_epinow

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

Setup Target Folder for Saving
secondary_opts

Secondary Reports Options
report_plots

Report plots
stan_vb_opts

Stan Variational Bayes Options
stan_sampling_opts

Stan Sampling Options
set_dt_single_thread

Set to Single Threading
simulate_infections

Simulate infections using the renewal equation
summary.estimate_infections

Summary output from estimate_infections
summary.epinow

Summary output from epinow
update_horizon

Updates Forecast Horizon Based on Input Data and Target
setup_future

Set up Future Backend
+.dist_spec

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

Prints the parameters of one or more delay distributions
regional_runtimes

Summarise Regional Runtimes
save_estimate_infections

Save Estimated Infections
run_region

Run epinow with Regional Processing Code
stan_opts

Stan Options
stan_pathfinder_opts

Stan pathfinder algorithm Options
setup_logging

Setup Logging
R_to_growth

Convert Reproduction Numbers to Growth Rates
add_day_of_week

Adds a day of the week vector
add_breakpoints

Add breakpoints to certain dates in a data set.
Distributions

Probability distributions
add_horizon

Add missing values for future dates
apply_default_cdf_cutoff

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

Allocate Empty Parameters to a List
allocate_delays

Allocate Delays into Required Stan Format
EpiNow2-package

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

Back Calculation Options
apply_zero_threshold

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

Calculate Credible Intervals
calc_CrI

Calculate Credible Interval
c.dist_spec

Combines multiple delay distributions for further processing
bootstrapped_dist_fit

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

Define bounds of a <dist_spec>
apply_tolerance

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

Calculate Summary Statistics
check_generation_time

Validate probability distribution for using as generation time