# 3 Ways to Calculate the Mean Absolute Percentage Error in R [Examples]

In this article, we show 3 easy ways to calculate the Mean Absolute Percentage Error (MAPE) in R.

Besides the Mean Absolute Error (MAE), the Mean Absolute Percentage Error (MAPE) is one of the most used metrics to validate model performance. It measures the average absolute error in percentage between predicted values and actual (i.e., observed) values.

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

The easiest way to calculate the Mean Absolute Percentage Error is by using the MAPE() function from either the Metrics or ie2misc package. You only need to provide the predicted and actual values, and the MAPE() function will return the Mean Absolute Percentage Error.

In this article, we discuss and compare the MAPE() function from both packages. Additionally, we will show a third method to calculate the Mean Absolute Percentage Error with just basic R code.

## The Mean Absolute Percentage Error (MAPE)

The Mean Absolute Percentage Error, also known as the Mean Absolute Percentage Deviation (MAPD), is a loss function for model validation commonly used for regression problems.

The MAPE is defined as the average of the absolute error expressed in percentage over a sample, or by the formula:

, where:

• n: represents the number of observations
• yi: represents the actual value
• ŷi: represents the predicted value

Note that, the MAPE is also frequently defined as:

So, before you take the output of any MAPE() function for granted, please check if the implemented definition matches your needs.

As the formula shows, all observations (and hence errors) are equally important while calculating the average. In other words, an error of 1% has the same severity as an error of 10%. If you want to penalize significant errors more, then you need another metric, such as the Root Mean Squared Error (RMSE).

A disadvantage of the Mean Absolute Percentage Error is the fact that the measure can be big when the true values (denominator) are very small. Moreover, the MAPE doesn’t work if the actual values are 0. So, instead of using the MAPE, you could opt for the Mean Absolute Error (MAE).

## 3 Ways to Calculate the Mean Absolute Percentage Error

Before we show 3 methods to find the MAPE, we first create two vectors with random numbers that represent the actual values (y) and the predicted value (y_hat). We use the SAMPLE.INT() function for this purpose.

R Code:

``````set.seed(123)
y <- sample.int(100, 100, replace = TRUE)
y

set.seed(321)
y_hat <- sample.int(100, 100, replace = TRUE)
y_hat``````

### 1. Calculate the Mean Absolute Percentage Error with the MAPE Function from the Metrics Package

The best way to calculate the Mean Absolute Percentage Error in R is with the MAPE() function from the Metrics packages. You only need to provide two parameters, namely the actual values and the predicted values, and the MAPE() function returns the Mean Absolute Percentage Error.

Syntax

`mape(actual, predicted)`

The Metrics package is an extensive R package with many functions to assess model performance. Amongst others, it contains metrics for regression, classification, and time series problems.

You can use the following code to find the Mean Absolute Percentage Error:

``````library(Metrics)
mape(actual = y, predicted = y_hat)``````

The MAPE() function from the Metrics package implements the following formula:

Hence, the result of 2.221 in our example means a Mean Absolute Percentage Error of 222.1%.

### 2. Calculate the Mean Absolute Percentage Error with the MAPE Function from the ie2misc Package

The ie2misc package provides the second method to calculate the Mean Absolute Percentage Error. Like the Metrics package, this function is also called MAPE() and requires the same two parameters (i.e., predicted and actual values).

Syntax

`mape(predicted, observed)`

Example:

``````library(ie2misc)
mape(predicted = y_hat, observed = y)``````

In contrast to the MAPE() function from the Metrics packages, the MAPE() function from the ie2misc package already multiplied the result by 100. Hence, this is a significant difference between the two functions that have the same name.

Another difference between the two MAPE() functions is how they handle missing values. By default, both functions return a missing value if the input vectors contain NA’s. See the example below.

``````library(Metrics)
library(ie2misc)

y_new <- y
y_new <- NA
y_hat_new <- y_hat
y_hat_new <- NA

Metrics::mape(actual = y_new, predicted = y_hat_new)
ie2misc::mape(predicted = y_hat_new, observed = y_new)``````

However, the MAPE() function from the ie2misc package provides a third parameter, namely na.rm, that can be helpful By setting the parameter to TRUE (i.e., na.rm = TRUE), the function omits missing values while calculating the Mean Absolute Percentage Error.

``ie2misc::mape(predicted = y_hat_new, observed = y_new, na.rm = TRUE)``

### 3. Calculate the Mean Absolute Percentage Error with Basic R Functions

Lastly, you can also calculate the Mean Absolute Percentage Error with just basic R code. You only need three functions, namely SUM(), ABS(), and LENGTH(). See the code below.

``sum(abs(y - y_hat)/abs(y))/length(y)``

Although this method requires more R code, it might help people who aren’t familiar with the concept of Mean Absolute Percentage Error to better understand your code.