epinow: Real-time Rt Estimation, Forecasting and Reporting

View source: R/epinow.R

epinowR Documentation

Real-time Rt Estimation, Forecasting and Reporting


[Stable] This function wraps the functionality of estimate_infections() in order to estimate Rt and cases by date of infection and forecast these infections into the future. In addition to the functionality of estimate_infections() it produces additional summary output useful for reporting results and interpreting them as well as error catching and reporting, making it particularly useful for production use e.g. running at set intervals on a dedicated server.


  generation_time = gt_opts(),
  delays = delay_opts(),
  truncation = trunc_opts(),
  rt = rt_opts(),
  backcalc = backcalc_opts(),
  gp = gp_opts(),
  obs = obs_opts(),
  stan = stan_opts(),
  horizon = 7,
  CrIs = c(0.2, 0.5, 0.9),
  filter_leading_zeros = TRUE,
  zero_threshold = Inf,
  return_output = FALSE,
  output = c("samples", "plots", "latest", "fit", "timing"),
  plot_args = list(),
  target_folder = NULL,
  logs = tempdir(),
  id = "epinow",
  verbose = interactive(),



A ⁠<data.frame>⁠ of confirmed cases (confirm) by date (date). confirm must be numeric and date must be in date format.


A call to gt_opts() (or its alias generation_time_opts()) defining the generation time distribution used. For backwards compatibility a list of summary parameters can also be passed.


A call to delay_opts() defining delay distributions and options. See the documentation of delay_opts() and the examples below for details.


A call to trunc_opts() defining the truncation of the observed data. Defaults to trunc_opts(), i.e. no truncation. See the estimate_truncation() help file for an approach to estimating this from data where the dist list element returned by estimate_truncation() is used as the truncation argument here, thereby propagating the uncertainty in the estimate.


A list of options as generated by rt_opts() defining Rt estimation. Defaults to rt_opts(). Set to NULL to switch to using back calculation rather than generating infections using Rt.


A list of options as generated by backcalc_opts() to define the back calculation. Defaults to backcalc_opts().


A list of options as generated by gp_opts() to define the Gaussian process. Defaults to gp_opts(). Set to NULL to disable the Gaussian process.


A list of options as generated by obs_opts() defining the observation model. Defaults to obs_opts().


A list of stan options as generated by stan_opts(). Defaults to stan_opts(). Can be used to override data, init, and verbose settings if desired.


Numeric, defaults to 7. Number of days into the future to forecast.


Numeric vector of credible intervals to calculate.


Logical, defaults to TRUE. Should zeros at the start of the time series be filtered out.


[Experimental] Numeric defaults to Inf. Indicates if detected zero cases are meaningful by using a threshold number of cases based on the 7-day average. If the average is above this threshold then the zero is replaced using fill.


Logical, defaults to FALSE. Should output be returned, this automatically updates to TRUE if no directory for saving is specified.


A character vector of optional output to return. Supported options are samples ("samples"), plots ("plots"), the run time ("timing"), copying the dated folder into a latest folder (if target_folder is not null, set using "latest"), and the stan fit ("fit"). The default is to return all options.


A list of optional arguments passed to plot.epinow().


Character string specifying where to save results (will create if not present).


Date, defaults to maximum found in the data if not specified.


Character path indicating the target folder in which to store log information. Defaults to the temporary directory if not specified. Default logging can be disabled if logs is set to NULL. If specifying a custom logging setup then the code for setup_default_logging() and the setup_logging() function are a sensible place to start.


A character string used to assign logging information on error. Used by regional_epinow() to assign errors to regions. Alter the default to run with error catching.


Logical, defaults to TRUE when used interactively and otherwise FALSE. Should verbose debug progress messages be printed. Corresponds to the "DEBUG" level from futile.logger. See setup_logging for more detailed logging options.


Deprecated; use data instead.


A list of output from estimate_infections with additional elements summarising results and reporting errors if they have occurred.

See Also

estimate_infections() forecast_infections() regional_epinow()


# set number of cores to use
old_opts <- options()
options(mc.cores = ifelse(interactive(), 4, 1))

# set an example generation time. In practice this should use an estimate
# from the literature or be estimated from data
generation_time <- Gamma(
  shape = Normal(1.3, 0.3),
  rate = Normal(0.37, 0.09),
  max = 14
# set an example incubation period. In practice this should use an estimate
# from the literature or be estimated from data
incubation_period <- LogNormal(
   meanlog = Normal(1.6, 0.06),
   sdlog = Normal(0.4, 0.07),
   max = 14
# set an example reporting delay. In practice this should use an estimate
# from the literature or be estimated from data
reporting_delay <- LogNormal(mean = 2, sd = 1, max = 10)

# example case data
reported_cases <- example_confirmed[1:40]

# estimate Rt and nowcast/forecast cases by date of infection
out <- epinow(
  data = reported_cases,
  generation_time = gt_opts(generation_time),
  rt = rt_opts(prior = list(mean = 2, sd = 0.1)),
  delays = delay_opts(incubation_period + reporting_delay)
# summary of the latest estimates
# plot estimates

# summary of R estimates
summary(out, type = "parameters", params = "R")


epiforecasts/EpiNow2 documentation built on July 13, 2024, 1:03 p.m.