Skip to contents

[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

# \donttest{
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)
)
#> Returning NA: this distribution has uncertain parameters.
#>  Resolve the uncertainty first with `fix_parameters()`.
#> This message is displayed once every 8 hours.
#> Warning: The largest R-hat is 1.12, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess

forecast_obj <- as_forecast_sample(fit, observations = example_confirmed)
score(forecast_obj)
#> Warning: Predictions appear to be integer-valued.
#> ! The log score uses kernel density estimation, which may not be appropriate
#>   for integer-valued forecasts.
#>  See the scoringRules package for alternatives for discrete probability
#>   distributions.
#>    forecast_date       date horizon  bias      dss     crps overprediction
#>           <Date>     <Date>   <num> <num>    <num>    <num>          <num>
#> 1:    2020-04-01 2020-04-01       0 -0.10 14.32283 263.3088            0.0
#> 2:    2020-04-01 2020-04-02       1 -0.32 14.41590 397.9611            0.0
#> 3:    2020-04-01 2020-04-03       2  0.26 14.72203 400.7056           75.6
#> 4:    2020-04-01 2020-04-04       3 -0.41 14.54437 427.5907            0.0
#> 5:    2020-04-01 2020-04-05       4 -0.08 14.71243 337.8359            0.0
#> 6:    2020-04-01 2020-04-06       5 -0.06 14.45683 292.6633            0.0
#> 7:    2020-04-01 2020-04-07       6 -0.04 14.60560 318.1096            0.0
#> 8:    2020-04-01 2020-04-08       7 -0.20 14.04463 278.6392            0.0
#>    underprediction dispersion log_score      mad ae_median   se_mean
#>              <num>      <num>     <num>    <num>     <num>     <num>
#> 1:            7.42   255.8888  7.987332 1067.472     119.0  10899.36
#> 2:          108.00   289.9611  8.360142 1194.234     562.0 101435.88
#> 3:            0.00   325.1056  8.167756 1309.136     544.5 529052.57
#> 4:          162.64   264.9507  8.324008 1049.681     713.0 146512.87
#> 5:            4.62   333.2159  8.258793 1412.918     124.0  19429.57
#> 6:            7.24   285.4233  8.067615 1084.522     142.0  49119.86
#> 7:            1.82   316.2896  8.154446 1179.408      85.0  41795.71
#> 8:           28.28   250.3592  8.033855 1135.672     225.5   3616.82
# }