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