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:**

, where:

: represents the number of observations*n*: represents the realized value*y*_{i}*ŷ*_{i}: represents the predicted value: represents the weights*w*_{i}

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

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

MAPE | WAPE | WMAPE | |
---|---|---|---|

Weight: Number of Observations | X | ||

Weight: Realized Values | X | ||

Weight: Realized Value x Other Scalar Value | X |

Numeric examples of the 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:**

- Sum the absolute error multiplied by its weight of all observations.
- Sum the actual value multiplied by its weight of all observations.
- Divide the result of Step 1 by the result of Step 2.
- 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
```

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

- The actual values.
- The predicted value.
- 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)
```