
Compute weighted interval score
Source:R/quant-weighted_interval_score.R
weighted_interval_score.RdWeighted interval score (WIS), a well-known quantile-based approximation of the commonly-used continuous ranked probability score (CRPS). WIS is a proper score, and can be thought of as a distributional generalization of absolute error. For example, see Bracher et al. (2020) for discussion in the context of COVID-19 forecasting.
Usage
weighted_interval_score(data, ...)
# S3 method for class 'data.frame'
weighted_interval_score(
data,
truth,
estimate,
quantile_levels = NULL,
na_rm = TRUE,
quantile_estimate_nas = c("impute", "drop", "propagate"),
case_weights = NULL,
...
)
weighted_interval_score_vec(
truth,
estimate,
quantile_levels = NULL,
na_rm = FALSE,
quantile_estimate_nas = c("impute", "drop", "propagate"),
case_weights = NULL,
...
)Arguments
- data
A
data.framecontaining the columns specified by thetruthandestimatearguments.- ...
Not Currently used.
- truth
The column identifier for the true class results (that is a
numeric). This should be an unquoted column name although this argument is passed by expression and supports quasiquotation (you can unquote column names). For_vec()functions, anumericvector.- estimate
The column identifier for the predicted class results (that is also
quantile_pred). As withtruththis can be specified different ways but the primary method is to use an unquoted variable name. For_vec()functions, aquantile_predvector.- quantile_levels
probabilities. If specified, the score will be computed at this set of levels. Otherwise, those present in
xwill be used. Ifquantile_levelsdo not exactly match those available inx, then some quantiles will have implicit missingness. Handling of these is determined byquantile_estimate_nas.- na_rm
logical. If
TRUE, missing values inactualor both implicit and explicit (values ofNApresent inx), will be ignored (dropped) in the calculation of the summary score. IfFALSE(the default), anyNAs will result in the summary beingNA.- quantile_estimate_nas
character. This argument applies only to
x. It handles imputation of individualquantile_levelsthat are necessary to compute a score. Because each element ofxis a hardhat::quantile_pred, it is possible for these to be missing for particularquantile_levels. There are a number of different possibilities for such missingness. The options are as follows:For
"impute", both explicit and implicit missing values will be imputed usinghardhat::impute_quantiles()prior to the calculation of the score. So the score will beNAonly if imputation fails.For
"drop", any explicit missing values will be removed before calculating the score for a particular prediction. This may be reasonable due to the weighting. For example, if the estimate hasquantile_levels = c(.25, .5, .75)but the median isNAfor a particular prediction, it may be reasonable to average the accuracy ofc(.25, .75)for that prediction with others that don't have missingness. This option is only works ifquantile_levels = NULLor is a subset of thequantile_levelsinx.For
"propagate", any missing value predictions will result in that element ofxhaving a score ofNA. Ifna_rm = TRUE, then these will be removed before averaging.
- case_weights
The optional column identifier for case weights. This should be an unquoted column name that evaluates to a numeric column in
data. For_vec()functions, a numeric vector,hardhat::importance_weights(), orhardhat::frequency_weights().
Details
Weighted interval score is a metric that should be minimized. The output ranges from 0 to ∞, with 0 indicating perfect predictions.
Examples
library(hardhat)
quantile_levels <- c(.2, .4, .6, .8)
pred1 <- 1:4
pred2 <- 8:11
preds <- quantile_pred(rbind(pred1, pred2), quantile_levels)
truth <- c(3.3, 7.1)
weighted_interval_score_vec(truth, preds)
#> [1] 1.275
weighted_interval_score_vec(truth, preds, quantile_levels = c(.25, .5, .75))
#> [1] 1.333333
# Missing value behaviours
preds_na <- quantile_pred(rbind(pred1, c(1, 2, NA, 4)), 1:4 / 5)
truth <- c(2.5, 2.5)
weighted_interval_score_vec(truth, preds_na)
#> [1] 0.5
weighted_interval_score_vec(truth, preds_na, quantile_levels = 1:9 / 10)
#> [1] 0.455656
try(weighted_interval_score_vec(
truth,
preds_na,
quantile_levels = 1:9 / 10,
quantile_estimate_nas = "drop"
))
#> Error in weighted_interval_score_vec(truth, preds_na, quantile_levels = 1:9/10, :
#> When `quantile_levels` is not a subset of those available in
#> `estimate`, `quantile_estimate_nas` may not be `'drop'`.
weighted_interval_score_vec(
truth,
preds_na,
quantile_levels = c(2, 3) / 5,
quantile_estimate_nas = "drop"
)
#> [1] 0.4
weighted_interval_score_vec(
truth, preds_na, na_rm = TRUE, quantile_estimate_nas = "propagate"
)
#> [1] 0.5
weighted_interval_score_vec(
truth, preds_na, quantile_estimate_nas = "propagate"
)
#> [1] NA