huber loss

huberloss.numeric R Documentation

Description

The huberloss()-function computes the simple and weighted huber loss between the predicted and observed <numeric> vectors. The weighted.huberloss() function computes the weighted Huber Loss.

Usage

## S3 method for class 'numeric'
huberloss(actual, predicted, delta = 1, ...)

## S3 method for class 'numeric'
weighted.huberloss(actual, predicted, w, delta = 1, ...)

huberloss(...)

weighted.huberloss(...)

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.

delta

A <numeric>-vector of length \(1\) (default: \(1\)). The threshold value for switch between functions (see calculation).

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 follows,

\[ \frac{1}{2} (y - \upsilon)^2 ~for~ |y - \upsilon| \leq \delta \]

and

\[ \delta |y-\upsilon|-\frac{1}{2} \delta^2 ~for~ \text{otherwise} \]

where \(y\) and \(\upsilon\) are the actual and predicted values respectively. If w is not NULL, then all values are aggregated using the weights.

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) calculate the metric
# with delta 0.5
huberloss(
  actual = actual,
  predicted = predicted,
  delta = 0.5
)

# 3) caclulate weighted
# metric using arbitrary weights
w <- rbeta(
  n = 1e3,
  shape1 = 10,
  shape2 = 2
)

huberloss(
  actual = actual,
  predicted = predicted,
  delta = 0.5,
  w     = w
)