How can I calculate the weighted mean absolute percentage error (WMAPE) in R, and can you provide an example?

How can I calculate the weighted mean absolute percentage error (WMAPE) in R, and can you provide an example?

The weighted mean absolute percentage error (WMAPE) is a measure of accuracy used to evaluate the performance of forecasting models. It takes into account the magnitude of errors and the relative importance of the data points. In R, the WMAPE can be calculated using the “accuracy” function from the “forecast” package. This function requires two arguments: the actual values and the predicted values. Once the WMAPE is calculated, it can be interpreted as the average percentage error of the forecasted values. For example, if the WMAPE is 10%, it means that on average, the forecasted values were off by 10% from the actual values. This measure can be particularly useful in industries such as finance or supply chain management, where accurate forecasting is crucial for decision making.

Calculate WMAPE in R (With Example)


One of the most common metrics used to measure the forecasting accuracy of a model is WMAPE, which stands for weighted mean absolute percentage error.

The formula to calculate WMAPE is as follows:

WMAPE = ( Σ|yi– ŷi|*wi ) / ( Σyi*wi ) * 100

where:

  • Σ – a symbol that means “sum”
  • yi – The actual value of the ith observation
  • ŷi – The predicted value of the ith observation 
  • wi – The weight for the ith observation

We can define the following function to calculate WMAPE in R:

find_WMAPE <- function(y, yhat, w){
  return(sum(abs(y-yhat)*w)/sum(y*w)*100)
}

The following example shows how to use this function in practice.

Example: Calculating WMAPE in R

Suppose we have the following data frame in R that contains information about the actual sales and predicted sales for some retail store:

#create dataset
data <- data.frame(actual=c(23, 37, 44, 47, 48, 48, 46, 43, 32, 27, 26, 24),
                   forecast=c(37, 40, 46, 44, 46, 50, 45, 44, 34, 30, 22, 23))

#view dataset
data

   actual forecast
1      23       37
2      37       40
3      44       46
4      47       44
5      48       46
6      48       50
7      46       45
8      43       44
9      32       34
10     27       30
11     26       22
12     24       23

To compute the WMAPE for the difference in actual vs. forecasted sales, we can define a vector of weights to be used and then use the WMAPE function we defined earlier:

#define function to calculate WMAPE
find_WMAPE <- function(y, yhat, w){
  return(sum(abs(y-yhat)*w)/sum(y*w)*100)
}

#define weights for each month
weights <- c(20, 20, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6)

#calculate WMAPE
find_WMAPE(df$actual, df$predicted, weights)

[1] 13.27635

The WMAPE for this model turns out to be 13.27635%.

That is, the weighted mean absolute percentage error between the forecasted sales values and actual sales values is 13.27635%.

Note that we gave significantly larger weights to the values for January and February in this example.

Depending on your particular problem, you may give larger or smaller weights to different observations depending on the importance of each error in your model.

Cite this article

stats writer (2024). How can I calculate the weighted mean absolute percentage error (WMAPE) in R, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-calculate-the-weighted-mean-absolute-percentage-error-wmape-in-r-and-can-you-provide-an-example/

stats writer. "How can I calculate the weighted mean absolute percentage error (WMAPE) in R, and can you provide an example?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-calculate-the-weighted-mean-absolute-percentage-error-wmape-in-r-and-can-you-provide-an-example/.

stats writer. "How can I calculate the weighted mean absolute percentage error (WMAPE) in R, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-calculate-the-weighted-mean-absolute-percentage-error-wmape-in-r-and-can-you-provide-an-example/.

stats writer (2024) 'How can I calculate the weighted mean absolute percentage error (WMAPE) in R, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-calculate-the-weighted-mean-absolute-percentage-error-wmape-in-r-and-can-you-provide-an-example/.

[1] stats writer, "How can I calculate the weighted mean absolute percentage error (WMAPE) in R, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I calculate the weighted mean absolute percentage error (WMAPE) in R, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top