as_forecast_sample: Convert EpiNow2 model output to a 'forecast_sample' object

as_forecast_sampleR Documentation

Convert EpiNow2 model output to a forecast_sample object

Description

[Experimental] Convert outputs of EpiNow2 fitting and forecasting functions to forecast_sample objects via scoringutils::as_forecast_sample() for evaluating predictive performance. Methods are provided for objects returned by epinow(), estimate_infections(), forecast_secondary(), and estimate_truncation().

These methods extract sample-level posterior predictions via get_predictions() with format = "sample", merge them with the supplied observations on date, and pass the result to scoringutils::as_forecast_sample().

scoringutils is an optional dependency; calling these methods without it installed gives an informative error.

Usage

## S3 method for class 'estimate_infections'
as_forecast_sample(data, observations, horizon = 0, ...)

## S3 method for class 'epinow'
as_forecast_sample(data, observations, horizon = 0, ...)

## S3 method for class 'forecast_secondary'
as_forecast_sample(data, observations, horizon = 0, ...)

## S3 method for class 'estimate_truncation'
as_forecast_sample(data, observations, horizon = -Inf, ...)

Arguments

data

Output of epinow(), estimate_infections(), forecast_secondary(), or estimate_truncation().

observations

A ⁠<data.frame>⁠ of observed values to score against. Must contain a date column. For epinow() and estimate_infections() objects must also contain a confirm column; for forecast_secondary() objects a secondary column; for estimate_truncation() objects a confirm column representing the latest, least-truncated observations.

horizon

Numeric scalar lower bound on the horizon column of get_predictions() output. Predictions with a horizon value at or above this bound are retained. Defaults to 0 for epinow(), estimate_infections() and forecast_secondary() (i.e. forecast period only) and to -Inf for estimate_truncation() (keep all reconstructed horizons). Pass horizon = -Inf to disable filtering.

...

Additional arguments passed to scoringutils::as_forecast_sample(). forecast_unit is set automatically from the object class (forecast_date, date, horizon, plus dataset for estimate_truncation()) and cannot be overridden.

Value

A forecast_sample object as returned by scoringutils::as_forecast_sample(). Rows for which observations does not provide a value on the corresponding date are dropped.

See Also

get_predictions() for the underlying sample extraction.

Examples



library(scoringutils)

# samples and calculation time have been reduced for this example
# for real analyses, use at least samples = 2000
fit <- estimate_infections(example_confirmed[1:40],
  generation_time = gt_opts(example_generation_time),
  delays = delay_opts(example_incubation_period + example_reporting_delay),
  rt = rt_opts(prior = LogNormal(mean = 2, sd = 0.2)),
  stan = stan_opts(samples = 100, warmup = 200)
)

forecast_obj <- as_forecast_sample(fit, observations = example_confirmed)
score(forecast_obj)



EpiNow2 documentation built on June 17, 2026, 1:07 a.m.