Calculate regression/forecast accuracy and bias metrics
calc_metrics.RdCalculates a set of accuracy (error) and bias metrics between observed values and predictions:
R2\_model: \(1 - (y - y)^2 (y - y)^2\)
RMSE: \(mean((y-y)^2)\)
MAE: \(mean(|y-y|)\)
RMSRE: \(mean(((y-y)/y)^2)\) (optionally in %)
MARE: \(mean(|y-y|/y)\) (optionally in %)
MedBias: \(median(y-y)\)
MedRelBias: \(median((y-y)/y)\) (optionally in %)
Relative metrics (RMSRE/MARE/MedRelBias) use the observed values y in the denominator.
If zeros occur in y, control the behavior via rel_zero:
"omit": drop pairs where \(y=0\) (default for stability)"epsilon": replace \(y\) by \(y + \) with smallepsilon"none": no guard; may yieldInf/NaN
Usage
calc_metrics(
y,
y_pred,
na_rm = TRUE,
percent = FALSE,
rel_zero = c("omit", "epsilon", "none"),
epsilon = 1e-08
)Arguments
- y
Numeric vector of observed values.
- y_pred
Numeric vector of predicted values (same length as
y).- na_rm
Logical; if
TRUE(default), removeNA/NaN/Infafter forming needed quantities for each metric.- percent
Logical; if
TRUE(default), express relative metrics (RMSRE, MARE, MedRelBias) in percentage (i.e., multiplied by 100). IfFALSE, leave as proportions.- rel_zero
Character; handling of zeros in
yfor relative metrics. One of"omit","epsilon","none". See Details.- epsilon
Numeric; small constant added to denominators when
rel_zero = "epsilon". Default1e-8.
Details
Compute common regression metrics (R2, RMSE, MAE, RMSRE, MARE, MedBias, MedRelBias)
R2\_model uses the usual in-sample definition with the sample mean of
y. Ifvar(y) == 0,R2_modelis returned asNA.Bias-type metrics retain the sign (systematic over-/under-prediction), while error-type metrics use absolute value or squares (magnitude only).
Examples
y <- c(10, 12, 9, 15, 0, 8)
y_pred <- c(11, 11, 8, 14, 0.2, 9)
# Default: percent = TRUE, rel_zero = "omit"
calc_metrics(y, y_pred)
#> RMSE MAE RMSRE MARE MedBias MedRelBias Median CV
#> 1 0.9165151 0.8666667 0.0993575 0.09722222 -0.4 -0.06666667 10 0.5321906
# Use proportions (not %) and guard zeros with epsilon
calc_metrics(y, y_pred, percent = FALSE, rel_zero = "epsilon", epsilon = 1e-6)
#> RMSE MAE RMSRE MARE MedBias MedRelBias Median CV
#> 1 0.9165151 0.8666667 81649.66 33333.41 -0.4 0.01666666 10 0.5321906
# No guarding (may produce Inf/NaN if y contains zeros)
calc_metrics(y, y_pred, rel_zero = "none")
#> RMSE MAE RMSRE MARE MedBias MedRelBias Median CV
#> 1 0.9165151 0.8666667 0.0993575 0.09722222 -0.4 -0.06666667 10 0.5321906