rt_forecast: Estimate and forecast the time-varying effective reproduction...

View source: R/rt_forecast.R

rt_forecastR Documentation

Estimate and forecast the time-varying effective reproduction number

Description

Fits filtered (real-time/causal) and smoothed (retrospective) R_t estimates to a case-count time series using the EpiFilter algorithm (Parag 2021), computes one-step-ahead in-sample predictions (a model adequacy check), a genuine out-of-sample one-step forecast with predictive intervals, and the elimination probability P(R_t < 1) at every time point. Disease-agnostic: supply the generation interval for your own pathogen via mean_GI/var_GI.

Usage

rt_forecast(
  time,
  cases,
  mean_GI,
  var_GI,
  Rmin = 0.01,
  Rmax = 10,
  grid_size = 200,
  eta = 0.1,
  ci_level = 0.025,
  quantile_levels = c(0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45),
  maxI = NULL
)

Arguments

time

numeric vector of time indices (e.g. epi weeks)

cases

numeric vector of case counts, same length as time

mean_GI

mean generation interval, in the same time units as time (e.g. weeks if your data is weekly)

var_GI

variance of the generation interval, same time units

Rmin, Rmax

bounds of the grid searched over for R_t (default 0.01, 10)

grid_size

number of points in the R_t grid (default 200)

eta

diffusion (state) noise controlling smoothness of R_t (default 0.1)

ci_level

tail probability defining the reported main credible interval (default 0.025, i.e. a 95% interval)

quantile_levels

additional quantile levels (each in (0, 0.5)) to compute for the out-of-sample forecast, used by wis(). Default adds the levels a standard 11-interval WIS wants; set to NULL to skip and only compute the 50%/95% forecast interval.

maxI

upper bound of the internal prediction grid; if NULL (default) it's set automatically to 3 * max(cases) (minimum 50)

Details

Unlike a script, this returns an object rather than writing files - use plot.rtforecast() if you want the plots run_rtforecast.R used to write directly.

Value

an object of class "rtforecast": a list with elements results (per-time filtered and smoothed R_t estimates with 95% credible intervals, and elimination probability P(R_t < 1)), predictions (in-sample one-step-ahead predictions with 50%/95% intervals), forecast (a one-row data frame: the out-of-sample forecast one step beyond the data, with 50%/95% intervals and the R_t used), forecast_quantiles (a named list of c(lo, hi) pairs for that same forecast at each requested level, for use with wis(); NULL if quantile_levels = NULL), call_args (the settings used, including the resolved maxI), and raw (the underlying filter/smoother objects, for advanced use).

Examples

data(measles_cdmx)
fit <- rt_forecast(measles_cdmx$time, measles_cdmx$cases,
                    mean_GI = 11/7, var_GI = (4/7)^2)
head(fit$results)
fit$forecast

RtForecastR documentation built on Nov. 25, 2026, 9:09 a.m.