How to Calculate WMAPE in R (With Example)

WMAPE (Weighted Mean Absolute Percentage Error) is a measure of accuracy used to evaluate the performance of a forecasting model. It is calculated by taking the weighted average of the absolute percentage errors of the forecasted values over the actual values in R. To calculate WMAPE in R, you need to first calculate the absolute percentage errors for each value and then find the weighted average using the weights assigned to each value. The example below shows how to calculate WMAPE in R. In this example, we have a vector of actual values (Actual) and a vector of forecasted values (Forecast). The weights assigned to each value can be found in the Weight vector. We then use the weighted.mean() function in R to calculate the WMAPE. The final result is the WMAPE of 0.098.


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.

The following tutorials explain how to perform other common tasks in R:

x