How to Calculate the Weighted Mean Absolute Percentage Error in R

In this article, we discuss how to calculate the Weighted Mean Absolute Percentage Error in R.

The Weighted Mean Absolute Percentage Error (WMAPE) is a validation metric for regression models and an extension of the Mean Absolute Percentage Error (MAPE). The WMAPE is a convenient metric if you want to express the difference (i.e., error) between actual and predicted values in a percentage, and assign some observations more weight (i.e., importance).

But, how do you calculate the Weighted Mean Absolute Percentage Error (WMAPE) in R?

You find the WMAPE of a regression model in R by either using basic R code or the WMAPE() function from the deepANN package. If you pick the WMAPE() function, you only need to provide the realized and predicted values, and it returns the Weighted Mean Absolute Percentage Error.

In this article, we show how to use the WMAPE() function, as well as how to calculate the Weighted Mean Absolute Percentage Error with plain R code. However, we first take a more detailed look at the formula of the WMAPE.

The Weighted Mean Absolute Percentage Error (WMAPE)

Defintion & Formula

The Weighted Mean Absolute Percentage Error (WMAPE) is the sum of the absolute error normalized by the sum of the realized values times a scalar value (i.e., weight).

Formula WMAPE:

Formula 2 Weighted Mean Absolute Percentage Error

, where:

  • n: represents the number of observations
  • yi: represents the realized value
  • ŷi: represents the predicted value
  • wi: represents the weights

So, by definition, the WMAPE gives more importance to some errors than others errors (i.e., the difference between realized and predicted values). The importance of each error depends on the multiplication of the realized value and its weight.

Difference between WAPE, WMAPE & MAPE

Although the implementation of the WMAPE sounds straightforward, the actual formula might cause some discussion or confusion. This confusion is mainly caused by two other metrics with similar names, namely the Mean Absolute Percentage Error (MAPE) and the Weighted Absolute Percentage Error (WAPE).

In fact, some argue that the WMAPE and WAPE are the same measures. Nevertheless, we consider them as different metrics.

Below we show the formulas of the Mean Absolute Percentage Error (MAPE), the Weighted Absolute Percentage Error (WAPE), and the Weighted Mean Absolute Percentage Error (WMAPE).

Formulae Mean Absolute Percentage Error.
Formulae Weighted Absolute Percentage Error.
Formulae Weighted Mean Absolute Percentage Error.

The table below shows an overview of the differences between the 3 metrics.

MAPEWAPEWMAPE
Weight: Number of ObservationsX
Weight: Realized ValuesX
Weight: Realized Value x Other Scalar ValueX

Numeric examples of the MAPE, WAPE, and WMAPE:

Difference between MAPE, WAPE, and WMAPE.

We have written two other articles where we discuss the MAPE metric and the WAPE metric in more detail.

Two Ways to Calculate the Weighted Mean Absolute Percentage Error (WMAPE)

In the remainder of this article, we use the numbers in the example above and demonstrate 2 ways to calculate the WMAPE in R.

1. Calculate the WMAPE with Basic R Code

These are the steps to calculate the Weighted Mean Absolute Percentage Error using in R if you write your own code:

  1. Sum the absolute error multiplied by its weight of all observations.
  2. Sum the actual value multiplied by its weight of all observations.
  3. Divide the result of Step 1 by the result of Step 2.
  4. Multiply the division by 100.

Syntax:

sum(abs(realized - predicted)*weight) / sum(realized*weight) * 100

Example:

In the example below, the variables y and y_hat represent the realized and predicted values, respectively. The variable w is the weight of each observation.

y <- c(10,5)
y_hat <- c(9,4)
w <- c(95,5)
sum(abs(y-y_hat)*w)/sum(y*w)*100
Calculate the Weighted Mean Absolute Percentage Error (WMAPE) with basic R code.

2. Calculate the WAPE with the WAPE() Function

Instead of writing your own code, you can also use the WMAPE() function from the deepANN package to calculate the Weighted Mean Absolute Percentage Error. To do so, you need to provide 3 parameters:

  1. The actual values.
  2. The predicted value.
  3. The weights.

Currently, the deepANN package is not (yet) available on CRAN. Therefore, you need to download it from Github. Nevertheless, the WAPE() function works, just like the other functions, perfectly fin

Syntax

wape(actuals, preds, weights)

Example:

if (!require("remotes")) install.packages("remotes")
if (!require("stschn/deepANN")) remotes::install_github("stschn/deepANN")
library("deepANN")

y <- c(10,5)
y_hat <- c(9,4)
w <- c(95,5)
wmape(actuals = y, preds = y_hat, weights = w)
Calculate the Weighted Mean Absolute Percentage Error (WMAPE) with a function from a package.