knitr::opts_chunk$set(echo = TRUE,
                      fig.width = 7,
                      collapse = TRUE,
                      comment = "#>")
library(scoringutils)
library(magrittr)
library(data.table)
library(ggplot2)
library(knitr)
# number of threads used for data.table computations, update as needed
data.table::setDTthreads(2)

The scoringutils package provides a collection of metrics and proper scoring rules that make it simple to score probabilistic forecasts against observed values. You can find more information in the paper Evaluating Forecasts with scoringutils in R as well as the Metrics-Vignette and the Scoring forecasts directly Vignette.

The scoringutils package offers convenient automated forecast evaluation in a data.table format (using the function score()), but also provides experienced users with a set of reliable lower-level scoring metrics operating on vectors/matriced they can build upon in other applications. In addition it implements a wide range of flexible plots that are able to cover many use cases.

The goal of this package is to provide a tested and reliable collection of metrics that can be used for scoring probabilistic forecasts (forecasts with a full predictive distribution, rather than point forecasts). It has a much stronger focus on convenience than e.g. the scoringRules package, which provides a comprehensive collection of proper scoring rules (also used in scoringutils). In contrast to other packages, scoringutils offers functionality to automatically evaluate forecasts, to visualise scores and to obtain relative scores between models.

Predictions can be handled in various formats: scoringutils can handle probabilistic forecasts in either a sample based or a quantile based format. For more detail on the expected input formats please see below. True values can be integer, continuous or binary.

Input formats

Most of the time, the score() function will be able to do the entire evaluation for you. All you need to do is to pass in a data.frame with the appropriate columns. Which columns are required depends on the format the forecasts come in. The forecast format can either be based on quantiles (see example_quantile for the expected format), based on predictive samples (see example_sample_continuous and example_sample_discrete for the expected format in each case) or in a binary format. The following table gives an overview (pairwise comparisons will be explained in more detail below):

requirements <- data.table(
  Format = c(
    "quantile-based", "sample-based", "binary", "pairwise-comparisons"
  ),
  `Required columns` = c(
    "'observed', 'predicted', 'quantile'",
    "'observed', 'predicted', 'sample'", "'observed', 'predicted'",
    "additionally a column 'model'"
  )
)
kable(requirements)

Additional columns may be present to indicate a grouping of forecasts. For example, we could have forecasts made by different models in various locations at different time points, each for several weeks into the future. scoringutils automatically tries to determine the unit of a single forecast, i.e. the combination of existing columns that are able to uniquely identify a single forecast. It uses all existing columns for this, which can sometimes lead to issues. We therefore recommend using the function set_forecast_unit() to determine the forecast unit manually. The function simply drops unneeded columns, while making sure that some necessary, 'protected columns' like "predicted" or "observed" are retained.

colnames(example_quantile)

set_forecast_unit(
  example_quantile,
  c("location", "target_end_date", "target_type", "horizon", "model")
) %>%
  colnames()

Constructing a forecast object

The function as_forecast() is be used to construct a forecast object and check the input data. It determines the forecast type, creates an object of the appropriate class (forecast_binary, forecast_quantile or forecast_sample), and validates the input data. Objects of class forecast_* have a print method that provides additional information.

head(example_quantile)
forecast_quantile <- example_quantile %>%
  set_forecast_unit(
    c("model", "location", "target_end_date", "forecast_date",
      "target_type", "horizon")
  ) %>%
  as_forecast()

forecast_quantile

If you are unsure what your input data should look like, have a look at the example_quantile, example_sample_discrete, example_sample_continuous and example_binary data sets provided in the package.

The output of as_forecast() can later be directly used as input to score(). This is the recommended workflow (however, score() will run as_forecast() internally if this hasn't happened before).

Note that in the above example some columns contain duplicated information with regards to the forecast unit, e.g. "location" and "location_name", and can be dropped. If we drop essential information, for example, the "target_type" column, we'll get an error informing us that the forecasts aren't uniquely identified any more.

example_quantile %>%
  set_forecast_unit(
    c("location", "target_end_date",
      "forecast_date", "model", "horizon")
  ) %>%
  as_forecast()

The function get_duplicate_forecasts() may help to investigate the issue. When filtering for only a single quantile of the EuroCOVIDhub-ensemble, we can see that there are indeed two forecasts for every date, location and horizon.

duplicates <- example_quantile %>%
  set_forecast_unit(
    c("location", "target_end_date",
      "forecast_date", "model", "horizon")
  ) %>%
  get_duplicate_forecasts()

duplicates[quantile_level == 0.5 & model == "EuroCOVIDhub-ensemble", ] %>%
  head()

Showing available forecasts

The function get_forecast_counts() may also be helpful to determine where forecasts are available. Using the by argument you can specify the level of summary. For example, to see how many forecasts there are per model and target_type, we can run

get_forecast_counts(forecast_quantile, by = c("model", "target_type"))

We see that 'epiforecasts-EpiNow2' has some missing forecasts for the deaths forecast target and that UMass-MechBayes has no case forecasts.

This information can also be visualised using plot():

forecast_quantile %>%
  get_forecast_counts(by = c("model", "forecast_date", "target_type")) %>%
  plot_forecast_counts(x = "forecast_date") +
  facet_wrap(~ target_type)

Scoring and summarising forecasts

Forecasts can easily be scored using the score() function.

For clarity, we suggest setting the forecast unit explicitly and calling as_forecast() explicitly.

scores <- forecast_quantile %>%
  score()

print(scores, 2)

The function score() returns unsumarised scores, which in most cases is not what the user wants. It returns a single score per forecast (as determined by the forecast unit).

A second function, summarise_scores() takes care of summarising these scores to the level specified by the user. The by argument can be used to define the level of summary. By default, by = model and one score per model is returned. Through the by argument we can specify what unit of summary we want. We can also call sumarise_scores() multiple tines, e.g to round your outputs by specifying e.g. signif() as a summary function.

scores %>%
  summarise_scores(by = c("model", "target_type")) %>%
  summarise_scores(fun = signif, digits = 2) %>%
  kable()

Scoring point forecasts

Point forecasts can be scored in a separate format.

suppressMessages(score(as_forecast(example_point))) %>%
  summarise_scores(by = "model", na.rm = TRUE)

Adding empirical coverage

For quantile-based forecasts we are often interested in specific coverage-levels, for example, what percentage of true values fell between all 50% or the 90% prediction intervals. We can add this information using the function get_coverage(). This function also requires a by argument which defines the level of grouping for which the percentage of true values covered by certain prediction intervals is computed.

score(as_forecast(example_quantile)) %>%
  summarise_scores(by = c("model", "target_type")) %>%
  summarise_scores(fun = signif, digits = 2)

Adding relative scores

In order to better compare models against each other we can use relative scores which are computed based on pairwise comparisons (see details below). Relative scores can be added to the evaluation using the function summarise_scores(). This requires a column called 'model' to be present. Pairwise comparisons are computed according to the grouping specified in by: essentially, the data.frame with all scores gets split into different data.frames according to the values specified in by and relative scores are computed for every individual group separately. The baseline argument allows us to specify a baseline that can be used to scale relative scores (all scores are divided by the baseline relative score). For example, to obtain relative scores separately for different forecast targets, we can run

score(as_forecast(example_quantile)) %>%
  add_relative_skill(
    by = c("model", "target_type"),
    baseline = "EuroCOVIDhub-ensemble"
  ) %>%
  summarise_scores(by = c("model", "target_type"))

Visualising scores

Score heatmap

We can also summarise one particular metric across different categories using a simple heatmap:

score(as_forecast(example_sample_continuous)) %>%
  summarise_scores(by = c("model", "location", "target_type")) %>%
  plot_heatmap(x = "location", metric = "bias") +
  facet_wrap(~ target_type)

Weighted interval score components

The weighted interval score can be split up into three components: Over-prediction, under-prediction and dispersion. These can be visualised separately in the following way:

score(as_forecast(example_quantile)) %>%
  summarise_scores(by = c("target_type", "model")) %>%
  plot_wis() +
  facet_wrap(~ target_type, scales = "free")

Calibration

Calibration is a measure statistical consistency between the forecasts and observed values. The most common way of assessing calibration (more precisely: probabilistic calibration) are PIT histograms. The probability integral transform (PIT) is equal to the cumulative distribution function of a forecast evaluated at the observed value. Ideally, pit values should be uniformly distributed after the transformation.

We can compute pit values as such:

example_sample_continuous %>%
  as_forecast() %>%
  get_pit(by = "model")

And visualise the results as such:

example_sample_continuous %>%
  as_forecast() %>%
  get_pit(by = c("model", "target_type")) %>%
  plot_pit() +
  facet_grid(model ~ target_type)

Similarly for quantile-based forecasts:

example_quantile[quantile_level %in% seq(0.1, 0.9, 0.1), ] %>%
  as_forecast() %>%
  get_pit(by = c("model", "target_type")) %>%
  plot_pit() +
  facet_grid(model ~ target_type)
example_quantile %>%
  as_forecast() %>%
  score() %>%
  summarise_scores(by = c("model", "interval_range")) %>%
  plot_interval_coverage()
example_quantile %>%
  as_forecast() %>%
  score() %>%
  summarise_scores(by = c("model", "quantile_level")) %>%
  plot_quantile_coverage()

Pairwise comparisons

Relative scores for different models can be computed using pairwise comparisons, a sort of pairwise tournament where all combinations of two models are compared against each other based on the overlapping set of available forecasts common to both models. Internally, a ratio of the mean scores of both models is computed. The relative score of a model is then the geometric mean of all mean score ratios which involve that model. When a baseline is provided, then that baseline is excluded from the relative scores for individual models (which therefore differ slightly from relative scores without a baseline) and all relative scores are scaled by (i.e. divided by) the relative score of the baseline model.

In scoringutils, pairwise comparisons can be made in two ways: Through the standalone function get_pairwise_comparisons() or from within summarise_scores() which simply adds relative scores to an existing set of scores.

forecast_quantile %>%
  score() %>%
  get_pairwise_comparisons(by = "model", baseline = "EuroCOVIDhub-baseline")
forecast_quantile %>%
  score() %>%
  summarise_scores(by = "model") %>%
  add_relative_skill(baseline = "EuroCOVIDhub-baseline")

If using the get_pairwise_comparisons() function, we can also visualise pairwise comparisons by showing the mean score ratios between models. By default, smaller values are better and the model we care about is showing on the y axis on the left, while the model against it is compared is shown on the x-axis on the bottom. In the example above, the EuroCOVIDhub-ensemble performs best (it only has values smaller 1), while the EuroCOVIDhub-baseline performs worst (and only has values larger than 1). For cases, the UMass-MechBayes model is of course excluded as there are no case forecasts available and therefore the set of overlapping forecasts is empty.

forecast_quantile %>%
  score() %>%
  get_pairwise_comparisons(by = c("model", "target_type")) %>%
  plot_pairwise_comparisons() +
  facet_wrap(~ target_type)

Additional analyses and visualisations

Correlation between scores

It may sometimes be interesting to see how different scores correlate with each other. We can examine this using the function correlation(). When dealing with quantile-based forecasts, it is important to call summarise_scorees() before get_correlations() to summarise over quantiles before computing correlations.

forecast_quantile %>%
  score() %>%
  summarise_scores() %>%
  get_correlations()

Visualising correlations:

forecast_quantile %>%
  score() %>%
  summarise_scores() %>%
  get_correlations(digits = 2) %>%
  plot_correlations()

Converting to quantile-based forecasts

Different metrics are available for different forecasting formats. In some cases, you may for example have forecasts in a sample-based format, but wish to make use of some of the functionality only available to quantile-based forecasts. For example, you may want to use the decomposition of the weighted interval score, or may like to compute interval coverage values.

You can convert your sample-based forecasts into a quantile-based format using the function sample_to_quantile(). There is, however, one caveat: Quantiles will be calculated based on the predictive samples, which may introduce a bias if the number of available samples is small.

example_sample_discrete %>%
  as_forecast() %>%
  sample_to_quantile(
    quantile_level = c(0.01, 0.025, seq(0.05, 0.95, 0.05), 0.975, 0.99)
  ) %>%
  as_forecast() %>%
  score()

Lower level functions

Most of these metrics are available as lower level functions and extensively documented. Have a look at the help files to understand these better.



epiforecasts/scoringutils documentation built on April 23, 2024, 4:56 p.m.