# How to Calculate WMAPE in R

The Weighted Mean Absolute Percentage Error (WMAPE) is a popular metric used in the field of forecasting to evaluate the accuracy of predicted values. It is particularly useful when the data series has a mix of high and low values, as the weighting ensures that each data point contributes to the error measure in proportion to its size. This article aims to provide a comprehensive guide on how to calculate WMAPE in R.

1. Basics of WMAPE
3. Calculating WMAPE Step-by-Step
4. Using Built-In Functions
5. Use Cases of WMAPE
7. Conclusion

## 1. Basics of WMAPE

Before we dive into the R code, let’s understand what WMAPE is.

### Formula

The formula for WMAPE is:

Where:

• At is the actual value at time tt
• Ft​ is the forecasted value at time tt
• n is the number of time periods

### Interpretation

A WMAPE closer to 0 indicates a more accurate forecast, whereas higher WMAPE values indicate less accuracy.

## 2. Reading Data into R

You can read data into R from various sources like Excel, CSV, SQL databases, or directly from the web.

For this example, let’s consider a simple dataset:

# Creating the data frame
data <- data.frame(
Actual = c(100, 150, 135, 120),
Forecast = c(110, 145, 130, 125)
)

## 3. Calculating WMAPE Step-by-Step

Here are the steps to calculate WMAPE manually in R:

### Step 1: Calculate the Absolute Errors

Calculate the absolute differences between the actual and forecasted values.

### Step 3: Sum of Absolute Errors

Calculate the sum of absolute errors.

## 5. Use Cases of WMAPE

WMAPE is widely used in:

• Inventory management
• Financial forecasting
• Demand prediction
• Sales projections

1. Easy to interpret
2. Provides a percentage error, which is universal
3. Weights errors based on their size

### Limitations

1. Sensitive to zero or near-zero actual values
2. Doesn’t consider direction of the error (over or under forecasting).

## 7. Conclusion

WMAPE is a useful metric for evaluating the accuracy of forecasted values, particularly when the data has a mix of high and low values. With R, calculating WMAPE can be quite straightforward, either by doing it step-by-step or by using built-in functions. By mastering WMAPE, you’ll be well-equipped to assess the quality of various forecasting models and make informed decisions in your data analysis projects.

Posted in RTagged