6 C
London
Tuesday, March 11, 2025
HomeStatistics TutorialRHow to Calculate WMAPE in R (With Example)

How to Calculate WMAPE in R (With Example)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Additional Resources

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

How to Calculate MAPE in R
How to Calculate SMAPE in R
How to Calculate RMSE in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories