How to Calculate MAPE (Mean Absolute Percentage Error) in R

Spread the love

Calculating the Mean Absolute Percentage Error (MAPE) is a standard practice in various fields like finance, economics, and data science, for evaluating the accuracy of a forecasting or prediction model. R, being a powerful statistical programming language, offers a plethora of ways to calculate MAPE. This guide will comprehensively cover different approaches to calculate MAPE in R, including base R methods, usage of specialized libraries, and custom functions. We will also delve into what MAPE is, why it is used, and its limitations.

Table of Contents

  1. What is MAPE?
  2. Why Use MAPE?
  3. Limitations of MAPE
  4. Calculating MAPE in R
    1. Using Base R
    2. Using the forecast package
    3. Using the Metrics package
    4. Custom Function Approach
  5. Interpretation of MAPE
  6. Conclusion

1. What is MAPE?

Mean Absolute Percentage Error (MAPE) is a measure used to evaluate the accuracy of a forecasting model. MAPE quantifies the difference between the predicted and the observed values in percentage terms. The formula for MAPE is:

where At is the actual value and Ft is the forecasted value at time t, and n is the total number of observations.

2. Why Use MAPE?

  • Interpretability: MAPE’s output is a percentage, which is easier to understand compared to other metrics like RMSE (Root Mean Square Error).
  • Uniform Scale: Since it is a percentage, it allows for comparisons across different scales and units.

3. Limitations of MAPE

  • Undefined for Zero Values: If the actual value is zero, the MAPE formula becomes undefined.
  • Biased Toward Underprediction: MAPE is often more sensitive to underpredictions than overpredictions.

4. Calculating MAPE in R

4.1 Using Base R

Calculating MAPE using base R involves straightforward manipulation of vectors. Here’s a simple example:

# Define actual and forecasted values
actual <- c(100, 200, 300, 400, 500)
forecast <- c(110, 195, 290, 405, 480)

# Calculate MAPE
mape <- mean(abs((actual - forecast) / actual)) * 100
print(paste("MAPE: ", round(mape, 2), "%"))

4.2 Using the forecast package

The forecast package provides a suite of tools for time series analysis, including a accuracy() function which computes MAPE among other metrics.

First, install and load the forecast package:


Then, calculate MAPE:


# Define actual and forecasted values
actual <- c(100, 200, 300, 400, 500)
forecast <- c(110, 195, 290, 405, 480)

# Convert to time series objects
actual_ts <- ts(actual)
forecast_ts <- ts(forecast)

# Compute accuracy measures
accuracy_measures <- accuracy(forecast_ts, actual_ts)

# Fetch MAPE
mape_value <- accuracy_measures[1, "MAPE"]

# Print MAPE
print(paste("MAPE: ", round(mape_value, 2), "%"))

4.3 Using the Metrics package

The Metrics package specializes in performance metrics, including MAPE. First, install and load the package:


Then calculate MAPE:

mape_metrics <- mape(actual, forecast)
print(paste("MAPE: ", round(mape_metrics, 2), "%"))

4.4 Custom Function Approach

If you require a more flexible approach, you can write a custom function to calculate MAPE.

custom_mape <- function(actual, forecast) {
  return (mean(abs((actual - forecast) / actual), na.rm = TRUE) * 100)

mape_custom <- custom_mape(actual, forecast)
print(paste("MAPE: ", round(mape_custom, 2), "%"))

5. Interpretation of MAPE

A lower MAPE value indicates a better fit of the model to the actual data. However, keep in mind the limitations mentioned earlier when interpreting the results.

6. Conclusion

MAPE is a widely-used and interpretable metric for evaluating the accuracy of forecasting models. R offers various methods for calculating MAPE, each with its advantages and limitations. By understanding the nuances of these methods, you can pick the one that best suits your specific needs.

Posted in RTagged

Leave a Reply