How to Calculate SMAPE in R
SMAPE stands for symmetric mean absolute percentage error. It is an accuracy measure and is used to determine the predictive accuracy of models that are based on relative errors. The relative error is computed as:
relative error = x / y
Where x is the absolute error and y is the magnitude of exact value
SMAPE has both lower bound and upper bound values (in contrast to the mean absolute percentage error). SMAPE can be calculated easily using the following formula:
SMAPE = (1 / n) * Σ(|forecast_value – actual_value| / ((|actual_value| + |forecast_value|) / 2) * 100
Here,
Σ : a symbol that means “sum”
|| : represent the absolute value
n : It represents the sample size
actual_value: It represents the actual data value
forecast_value : It represents the forecasted data value
This article focuses on how we can calculate the SMAPE in R:
Calculating SMAPE in R:
There are two ways using which we can calculate SMAPE in R. These methods are described below in detail:
Method 1: Using Metrics Package
R provides us smape() function defined under the Metrics package using which we can calculate SMAPE. The function has the following syntax:
Syntax:
smape(actual, forecast)
Parameters:
- actual: It represents a vector containing actual values.
- forecasted: It represents a vector containing forecasted values
Return Type:
Returns the SMAPE error between actual and predicted values.
Example:
In this program, we have created two vectors; actual and forecast for the model. Then using smape() function we are calculating symmetric mean absolute percentage error.
R
# Importing library library (Metrics) # Defining a vector containing actual values actual <- c (10, 33, 42, 18, 19, 21, 22) # Defining a vector containing forecasted # values forecast <- c (16, 19, 24, 27, 25, 36, 48) # Determine SMAPE smape (actual, forecast) |
Output:
Thus symmetric mean absolute percentage error for this model comes out to be equal to 49.819%.
Method 2: Using a custom function
Another method is to create our own function that calculates the symmetric mean absolute percentage error using the formula that was described before.
Example:
In this program, we have created a custom function computeSmape() that takes actual and forest vectors as input. Using the formula, we are calculating smape. At last, we have called this function by passing vectors as parameters.
R
# Custom function to calculate SMAPE # for the provided model computeSmape <- function (actual, forecast) { return (1 / length (actual) * sum (2 * abs (forecast - actual) / ( abs (actual) + abs (forecast)) * 100)) } # Defining a vector containing actual values actual <- c (10, 33, 42, 18, 19, 21, 22) # Defining a vector containing forecasted values forecast <- c (16, 19, 24, 27, 25, 36, 48) # Determine SMAPE computeSmape (actual, forecast) |
Output:
Thus symmetric mean absolute percentage error for this model comes out to be equal to 49.819% .
Contact Us