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.

## Table of Contents

- Basics of WMAPE
- Reading Data into R
- Calculating WMAPE Step-by-Step
- Using Built-In Functions
- Use Cases of WMAPE
- Advantages and Limitations
- 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.

`data$AbsoluteError <- abs(data$Actual - data$Forecast)`

### Step 2: Sum of Actual Values

Calculate the sum of actual values.

`total_actual <- sum(data$Actual)`

### Step 3: Sum of Absolute Errors

Calculate the sum of absolute errors.

`total_absolute_error <- sum(data$AbsoluteError)`

### Step 4: Calculate WMAPE

Now, calculate WMAPE.

`WMAPE <- (total_absolute_error / total_actual) * 100`

## 4. Using Built-In Functions

You can also write a function to streamline the process:

```
calculate_WMAPE <- function(actual, forecast) {
absolute_error <- abs(actual - forecast)
total_actual <- sum(actual)
total_absolute_error <- sum(absolute_error)
WMAPE <- (total_absolute_error / total_actual) * 100
return(WMAPE)
}
# Using the function
WMAPE <- calculate_WMAPE(data$Actual, data$Forecast)
```

## 5. Use Cases of WMAPE

WMAPE is widely used in:

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

## 6. Advantages and Limitations

### Advantages

- Easy to interpret
- Provides a percentage error, which is universal
- Weights errors based on their size

### Limitations

- Sensitive to zero or near-zero actual values
- 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.