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

The Weighted Absolute Percentage Error (WAPE) is a common validation measure for regression models and an extension of the Mean Absolute Percentage Error (MAPE). But, **how do you calculate the WAPE?**

**In R, you calculate the Weighted Absolute Error Percentage (WAPE) by either using basic R code or using the WAPE() function from the deepANN package. If you choose the WAPE() function, you only need to provide the predicted and realized values and, as a result, it returns the Weighted Absolute Percentage Error.**

In this article, we demonstrate how to use the WAPE() function, as well as simple R code to find the WAPE of your regression model. We support the explanations with examples that you can use in your own project.

However, we first take a look at the definition and formula of the Weighted Absolute Percentage Error. Moreover, we discuss the **differences between the WAPE, MAPE, and WMAPE**.

## The Weighted Absolute Percentage Error (WAPE)

### Defintion & Formula

**The Weighted Absolute Percentage Error (WAPE), or MAD/mean ratio, is the sum of the absolute error normalized by the sum of the realized values. **

So, by definition, the WAPE gives more importance to errors (i.e., the difference between realized and predicted values) of observations with a high realized value. Therefore, the WAPE is a convenient metric to prioritize observations with a high realized value. (See the example below)

Furthermore, the WAPE equally penalizes over-estimating and under-estimating.

**Formula WAPE:**

, where:

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

In fact, **the WAPE is an extension of the Mean Absolute Percentage Error (MAPE)**. Instead of weighing the sum of the absolute error by the total number of observations, the WAPE uses the realized value of each observation as the weighting factor.

See the formulas below:

### Difference between WAPE, MAPE, & WMAPE

Although the main purpose of this article is to discuss the WAPE, we should also briefly discuss the MAPE and WMAPE.

**The Weighted Absolute Percentage Error (WAPE), the Mean Absolute Percentage Error (MAPE), and Weighted Mean Absolute Percentage Error (WMAPE) look all quite similar, but there are differences.** We provide the table below so you can pick the right metric for your purpose.

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

Penalizes Over-/Under-Estimation Similarly | X | X | X |

Weight: Number of Observations | X | ||

Weight: Realized Values | X | ||

Weight: Realized Value x Other Scalar Value | X |

So, **the main difference between the MAPE, WAPE, and WMAPE metrics is how they weigh each observation** (i.e., give importance). The MAPE considers all observations equally important, whereas the WAPE and WMAPE multiply each observation by the realized value or the realized value times another value, respectively.

We have created a separate article where we discuss the WMAPE in more detail and provide examples. **Note that; in some definitions, the WAPE and WMAPE are considered the same.**

The example below calculate these 3 metrics for a small dataset. Although the absolute error for the observations is the same, the MAPE, WAPE, and WMAPE differ.

## Two Ways to How to Calculate the Weighted Absolute Percentage Error in R

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

### 1. Calculate the WAPE with Basic R Code

**The first way to calculate the Weighted Absolute Percentage Error in R is by writing your only code. You only need the SUM() and ABS() functions to find the WAPE.**

First, you use the ABS() function to calculate the absolute difference between the realized and predicted values. Then, you apply the SUM() function to compute the total absolute error. Finally, you divide the total absolute error by the sum of the realized values.

**Syntax:**

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

**Example:**

In the example below, the variables *y* and *y_hat* represent the realized and predicted values, respectively.

```
y <- c(10,5)
y_hat <- c(9,4)
sum(abs(y-y_hat))/sum(y)*100
```

### 2. Calculate the WAPE with the WAPE() Function

The second method to calculate the WAPE is by using a function. For example, the WAPE() function from the *deepANN* package.

The WAPE() function is a simple and intuitive function. You only need to provide the realized and predicted values and, as a result, it returns the Weighted Absolute Percentage Error.

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

**Syntax**

wape(actuals,preds)

**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)
wape(actuals = y, preds = y_hat)
```