How to use Metrics Package In R Language

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%.

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:

Similar Reads

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:...

Method 2: Using a custom function

...

Contact Us