Table of Contents
SMAPE (Symmetric Mean Absolute Percentage Error) in R can be calculated with the forecast package by using the function accuracy() and setting the parameter “rmse” to “SMAPE”. The function takes as input two vectors that represent the predicted and observed values, and returns the computed SMAPE as a single number. The syntax is accuracy(forecast, observed, rmse=”SMAPE”).
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.