How can SMAPE (Symmetric Mean Absolute Percentage Error) be calculated in R?

SMAPE (Symmetric Mean Absolute Percentage Error) is a commonly used measure of accuracy for forecasting and prediction models. It takes into account the percentage difference between actual and predicted values, making it suitable for evaluating models with different units of measurement. In R, SMAPE can be calculated by taking the absolute difference between the actual and predicted values, dividing it by the sum of the absolute values of the actual and predicted values, and multiplying it by 200. This value is then averaged over the total number of observations to obtain the final SMAPE score. This calculation can be easily implemented using built-in functions in R, making it a convenient and efficient tool for evaluating the accuracy of forecasting models.

Calculate SMAPE in R


The symmetric mean absolute percentage error (SMAPE) is used to measure the predictive accuracy of models. It is calculated as:

SMAPE = (1/n) * Σ(|forecast – actual| / ((|actual| + |forecast|)/2) * 100

where:

  • Σ – a symbol that means “sum”
  • n – sample size
  • actual – the actual data value
  • forecast – the forecasted data value

The smaller the value for SMAPE, the better the predictive accuracy of a given model.

This tutorial explains two different methods you can use to calculate SMAPE in R.

Method 1: Use smape() from the Metrics Package

One way to calculate SMAPE in R is to use the smape() function from the Metrics package:

library(Metrics)

#define actual values
actual <- c(12, 13, 14, 15, 15, 22, 27)

#define forecasted values
forecast <- c(11, 13, 14, 14, 15, 16, 18)

#calculate SMAPE
smape(actual, forecast)

[1] 0.1245302

We can see that the symmetric mean absolute percentage error for this model is 12.45%.

Method 2: Write Your Own Function

Another way to calculate SMAPE is to create our own function as follows:

find_smape <- function(a, f) {
  return (1/length(a) * sum(2*abs(f-a) / (abs(a)+abs(f))*100))
}

We can then use this function to calculate the SMAPE between a vector of actual values and forecasted values:

#define actual values
actual <- c(12, 13, 14, 15, 15,22, 27)

#define forecasted values
forecast <- c(11, 13, 14, 14, 15, 16, 18)

#calculate SMAPE
find_smape(actual, forecast)

[1] 12.45302

Once again the SMAPE turns out to be 12.45%, which matches the results from the previous example.

Additional Resources

x