Symmetric Mean Absolute Percentage Error

smape.numeric R Documentation

Description

The smape()-function computes the symmetric mean absolute percentage error between the observed and predicted <numeric> vectors. The weighted.smape() function computes the weighted symmetric mean absolute percentage error.

Usage

## S3 method for class 'numeric'
smape(actual, predicted, ...)

## S3 method for class 'numeric'
weighted.smape(actual, predicted, w, ...)

## Generic S3 method
smape(
 actual,
 predicted,
 ...
)

## Generic S3 method
weighted.smape(
 actual,
 predicted,
 w,
 ...
)

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.

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.

Definition

The metric is calculated as follows,

\[ \sum_i^n \frac{1}{n} \frac{|y_i - \upsilon_i|}{\frac{|y_i|+|\upsilon_i|}{2}} \]

where \(y_i\) and \(\upsilon_i\) is the actual and predicted values respectively.

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 Symmetric Mean Absolute Percentage Error (MAPE)
cat(
  "Symmetric Mean Absolute Percentage Error", mape(
    actual    = actual,
    predicted = predicted,
  ),
  "Symmetric Mean Absolute Percentage Error (weighted)", weighted.mape(
    actual    = actual,
    predicted = predicted,
    w         = mtcars$mpg/mean(mtcars$mpg)
  ),
  sep = "\n"
)