Skip to contents

Evaluate a Model for Forecasting Rts

Usage

evaluate_model(
  obs_rts = NULL,
  obs_cases = NULL,
  model = NULL,
  horizon = 7,
  samples = 1000,
  timeout = 30,
  bound_rt = TRUE,
  min_points = 3,
  serial_interval = NULL,
  rdist = NULL,
  return_raw = FALSE
)

Arguments

obs_rts

Dataframe of Rt observations to forecast with and score against. Should contain a date and rt column. If multiple samples are included this should be denoted using a numeric sample variable.

obs_cases

Dataframe of case observations to use for case prediction and scoring. Should contain a date and cases column. If multiple samples are included this should be denoted using a numeric sample variable.

model

A model object in the format of bsts_model or fable_model. See the corresponding help files for details.

horizon

Numeric, the time horizon over which to predict.

samples

Numeric, number of samples to take.

timeout

Numeric, timeout of model fitting in seconds. Defaults to 30 seconds.

bound_rt

Logical, defaults to TRUE. Should Rt values be bounded to be greater than or equal to 0.

min_points

Numeric, defaults to 3. The minimum number of time points at which to begin iteratively evaluating the forecast.

serial_interval

A numeric vector describing the probability distribution the serial interval. See EpiNow::covid_serial_interval for an example of the format.

rdist

A function to be used to sample the number of cases. Must take two arguments with the first specfying the number of samples and the second the mean. Defaults to rpois if not supplied

return_raw

Logical, should raw cases and rt forecasts be returned. Defaults to FALSE.

Value

a list of tibbles containing the predicted Rt values (forecast_rts), their scores (rt_scores), as well as predicted cases (forecast_cases) and their scores (case_scores).

Examples

if (FALSE) {
## Evaluate a model based on a single sample of input cases
evaluate_model(EpiSoon::example_obs_rts,
  EpiSoon::example_obs_cases,
  model = function(...) {
    EpiSoon::bsts_model(
      model =
        function(ss, y) {
          bsts::AddSemilocalLinearTrend(ss, y = y)
        }, ...
    )
  },
  horizon = 7, samples = 10,
  serial_interval = example_serial_interval
)


## Samples of observed data
sampled_obs <- EpiSoon::example_obs_rts %>%
  dplyr::mutate(sample = 1) %>%
  dplyr::bind_rows(EpiSoon::example_obs_rts %>%
    dplyr::mutate(sample = 2))

sampled_cases <- EpiSoon::example_obs_cases %>%
  dplyr::mutate(sample = 1) %>%
  dplyr::bind_rows(EpiSoon::example_obs_cases %>%
    dplyr::mutate(sample = 2))


## Evaluate a model across samples
evaluate_model(sampled_obs,
  sampled_cases,
  model = function(...) {
    EpiSoon::bsts_model(
      model =
        function(ss, y) {
          bsts::AddSemilocalLinearTrend(ss, y = y)
        }, ...
    )
  },
  horizon = 7, samples = 10,
  serial_interval = EpiSoon::example_serial_interval
)
}