pinball loss

pinball.numeric R Documentation

Description

The pinball()-function computes the pinball loss between the observed and predicted <numeric> vectors. The weighted.pinball() function computes the weighted Pinball Loss.

Usage

## S3 method for class 'numeric'
pinball(actual, predicted, alpha = 0.5, deviance = FALSE, ...)

## S3 method for class 'numeric'
weighted.pinball(actual, predicted, w, alpha = 0.5, deviance = FALSE, ...)

pinball(...)

weighted.pinball(...)

Arguments

actual

A <numeric>-vector of length \(n\). The observed (continuous) response variable.

predicted

A <numeric>-vector of length \(n\). The estimated (continuous) response variable.

alpha

A <numeric>-value of length \(1\) (default: \(0.5\)). The slope of the pinball loss function.

deviance

A <logical>-value of length 1 (default: FALSE). If TRUE the function returns the \(D^2\) loss.

Arguments passed into other methods.

w

A <numeric>-vector of length \(n\). The weight assigned to each observation in the data.

Value

A <numeric> vector of length 1.

Calculation

The metric is calculated as,

\[\text{PinballLoss}_{\text{unweighted}} = \frac{1}{n} \sum_{i=1}^{n} \left[ \alpha \cdot \max(0, y_i - \hat{y}_i) - (1 - \alpha) \cdot \max(0, \hat{y}_i - y_i) \right]\]

where \(y_i\) is the actual value, \(\hat{y}_i\) is the predicted value and \(\alpha\) is the quantile level.

Examples

# 1) fit a linear
# regression
model <- lm(
  mpg ~ .,
  data = mtcars
)

# 1.1) define actual
# and predicted values
# to measure performance
actual    <- mtcars$mpg
predicted <- fitted(model)

# 2) evaluate in-sample model
# performance using Pinball Loss
cat(
  "Pinball Loss", pinball(
    actual    = actual,
    predicted = predicted,
  ),
  "Pinball Loss (weighted)", weighted.pinball(
    actual    = actual,
    predicted = predicted,
    w         = mtcars$mpg/mean(mtcars$mpg)
  ),
  sep = "\n"
)