How to Calculate SMAPE in R

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

How to Calculate SMAPE in R?, SMAPE indicates the symmetric mean absolute percentage error.SMAPE is mainly used to measure the predictive accuracy of models.

Mathematical formula is

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

where:

Σ indicates “sum”

n – indicates sample size

actual – indicates the actual data value

forecast – indicates the forecasted data value

How to perform ANCOVA in R » Quick Guide »

When coming to inference similar to other cases, the smaller the value for SMAPE the better the predictive accuracy of a given model.

Here we are going to describe two different approaches in R.

How to Calculate SMAPE in R

Approach 1: Use Metrics Package

We can make use of Metrics library and smape() function in R for SMAPE calculation.

Compare data frames in R-Quick Guide »

library(Metrics)

define actual values

actual <- c(15, 16, 14, 15, 18, 22, 20)

define forecasted values

forecast <- c(15, 14, 14, 14, 17, 16, 18)

calculate SMAPE

smape(actual, forecast)

[1] 0.09721348

It indicates that the symmetric mean absolute percentage error for this model is 9.72%.

Timeseries analysis in R » Decomposition, & Forecasting »

Approach 2: Function

The above-mentioned approach is one of the easiest ways to calculate SMAPE, however, we can define our own function for SMAPE calculation.

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

The above function will be helpful for SMAPE calculation between a vector of actual values and forecasted values:

define actual values

actual <- c(15, 16, 14, 15, 18, 22, 20)

define forecasted values

forecast <- c(15, 14, 14, 14, 17, 16, 18)

calculate SMAPE

smape(actual, forecast)
[1] 9.721348

As we got earlier the SMAPE value is 9.72%.

When we need to compare different models SMAPE value will be more useful.

Linear Discriminant Analysis in R » LDA Prediction »

The post How to Calculate SMAPE in R appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)