3 Ways to Calculate the Normalized Mean Absolute Error in R

In this article, we demonstrate 3 ways to calculate the Normalized Mean Absolute Error in R.

Although the Normalized Mean Absolute Error (NMAE), or Coefficient of Variance of the MAE, is not used very frequently, it still is a useful metric. The NMAE normalizes the Mean Absolute Error (MAE) which is especially convenient when you want to compare the MAE of models with different scales.

But, how do you calculate the Normalized Mean Absolute Error (NMAE) in R?

The easiest way to calculate the Normalized Mean Absolute Error is a two-step process. First, you calculate the Mean Absolute Error, for example with the MAE() function. Then, you use a normalization method such as the average, range, or interquartile range, to find the Normalized Mean Absolute Error (NMAE).

In this article, we discuss all 3 methods to normalize the Mean Absolute Error with R code and examples.

The Normalized Mean Absolute Error (NMAE)

As mentioned before, the Normalized Mean Absolute Error (NMAE) is a validation metric to compare the Mean Absolute Error (MAE) of (time) series with different scales.

For example, in the situation below, we have 2 series with a different Mean Absolute Error. Therefore, one could conclude that the second model performs worse. However, since the scales of the series differ, the Normalized Mean Absolute Error is the same.

Example Normalized Mean Absolute Error

In the example above, we used the mean of the actual values to normalize the MAE. However, there isn’t one clear definition of the NMAE. Instead, there are 3 commonly used definitions.

1. Normalization of the Mean Absolute Error with the Mean

The most common way to normalize the Mean Absolute Error is by using the mean of the actual values as the denominator.

Definition 1 Normalized Mean Absolute Error (NMAE),

2. Normalization of the Mean Absolute Error with the Range

Another frequently used definition of the Normalized Mean Absolute Error is by dividing the MAE by the range of actual value.

Definition 2 Normalized Mean Absolute Error (NMAE),

3. Normalization of the Mean Absolute Error with the Interquartile Range

Lastly, you can also use the interquartile range to normalize the Mean Absolute Error.

Definition 3 Normalized Mean Absolute Error (NMAE),

It depends on your situation which definition you should use. Please, check this article for more information about how to calculate the Mean Absolute Error (MAE).

3 Easy Ways to Calculate the Normalized Mean Absolute Error

Before we show how to calculate the Normalized Mean Absolute Error using the 3 definitions, we first create two vectors with random numbers. These vectors will represent the actual values and predicted values in our examples.

We use the SAMPLE.INT() function to create these vectors of random numbers.

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

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

In the examples below, we will use the MAE() function from the Metrics package to find the Mean Absolute Error before any normalization. However, if your data contains missing values, we recommend using the MAE() function from the ie2misc package.

1. Calculate the Normalized Mean Absolute Error with the Mean

As mentioned before, calculating the Normalized Mean Absolute Error in R is mostly done by dividing the Mean Absolute Error by the mean. You can use the MAE() function and the MEAN() function to find the Mean Absolute Error and Mean of the actual values, respectively.

Example

library(Metrics)
mae(actual = y, predicted = y_hat) / mean(y)
Calculate the Normalized Mean Absolute Error in R.

2. Calculate the Normalized Mean Absolute Error with the Range

The second way to calculate the NMAE in R is by dividing the Mean Absolute Error by the range of actual values. In others, the difference between the highest and lowest actual value.

If you want to use this definition, you can find the Normalized Mean Absolute Error using the functions MAE(), MAX(), and MIN().

Example

library(Metrics)
mae(actual = y, predicted = y_hat) / (max(y) - min (y))
Calculate the Normalized Mean Absolute Error in R.

Alternatively, you could install the DTWBI package and use the COMPUTE.NMAE() function. This function requires two parameters, namely the actual values and the predicted value, and returns the Normalized Mean Absolute Error.

Example

library(DTWBI)
compute.nmae(y, y_hat)

3. Calculate the Normalized Mean Absolute Error with the Interquartile Range

Lastly, you can normalize the Mean Absolute Error with the interquartile range. In other words, the difference between the 75th and 25th percentiles of your actual data. You can find the interquartile range in R with the IQR() function.

Example

library(Metrics)
mae(actual = y, predicted = y_hat) / IQR(y)
Calculate the Normalized Mean Absolute Error in R.