Skip to contents

Given a forecast and set of observations, compute the log score for every time point. Uses a Kernel Density Estimation (KDE) to interpolate the density at the observation point.

Usage

log_score(fcst, obs, summarize = TRUE, at = NULL, after = NULL, bw = NULL)

Arguments

fcst

A forecast object (see output of create_forecast()).

obs

An observations data frame.

summarize

A boolean, defaults to TRUE. If TRUE, a single number will be returned as the score for the forecast. If FALSE, a data frame with columns named time, val_obs, and score will be returned, containing the scores for each individual time point. This can be used by plotting functions to colour-code observations, for example.

at

(Optional) A time (must be compatible with fcst and obs). If specified, the score for this time point will be returned. Mutually exclusive with after.

after

(Optional) A number. If specified, the score at time fcst$forecast_time + after will be returned. Mutually exclusive with at.

bw

(Optional) The bandwidth for calculating the Kernel Density Estimation (see ?scoringRules::logs_sample). If not provided, a bandwidth will automatically be calculated by scoringRules::logs_sample().

Value

If summarize is FALSE, a data frame containing times, observations, and scores. Otherwise, the score at the time speficied by either at or after.

Examples

df <- data.frame(time=c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3), val=c(1:5, 1:5, 1:5))
# return a data frame
log_score(
  create_forecast(df),
  data.frame(time=1:3, val_obs=c(-1, 2.5, 5)),
  summarize=FALSE
)
#> # A tibble: 3 × 3
#>    time val_obs score
#>   <dbl>   <dbl> <dbl>
#> 1     1    -1   -4.04
#> 2     2     2.5 -1.65
#> 3     3     5   -2.00

# use `at` parameter to specify absolute times
log_score(
  create_forecast(df, forecast_time=1),
  data.frame(time=1:3, val_obs=c(-1, 2.5, 5)),
  at=2
)
#> [1] -1.649454

# use `after` parameter to specify times relative to `forecast_time`
log_score(
  create_forecast(df, forecast_time=1),
  data.frame(time=1:3, val_obs=c(-1, 2.5, 5)),
  after=1
)
#> [1] -1.649454