6
$\begingroup$

Is the below calculation of the Mean Absolute Percentage Error MAPE correct?

I've included a workable example, but really the lines in question are these:

result      <- (((actual-predicted)/actual)-1)*100
result.mean <- rowMeans(result, na.rm = TRUE)

I know this is a simple one, but I'm keen to get it right and any insight would be much appreciated.

library(forecast)
library(vars)

x <- rnorm(70)
y <- rnorm(70)

dx <- cbind(x,y)
dx <- as.ts(dx)


# Forecast Accuracy
j = 12          # Forecast horizon
k = nrow(dx)-j  # length of minimum training set

predicted <- do.call(rbind, lapply(1:j, function(i){
  trainingset <- window(dx, end = k+i-1)
  fit         <- VAR(trainingset, p = 2)                       
  fcast       <- forecast(fit, h = j-i+1)
  `length<-`(fcast$mean$x, j)
}))

actual <- do.call(rbind, lapply(1:j, function(i){
  actual      <- window(dx[,1], start = k+i, end = k+j)
  `length<-`(actual, j)
}))

result <- (((actual-predicted)/actual)-1)*100
result.mean <- rowMeans(result, na.rm = TRUE)

plot(result.mean, type = "l")

MAPE <- mean(result.mean)
MAPE
$\endgroup$

1 Answer 1

7
$\begingroup$

From the name Mean Absolute Percentage Error, there needs to be an absolute value in the calculation of MAPE:

rowMeans(abs((actual-predicted)/actual) * 100)

This matches the formula for MAPE at, for instance, https://en.wikipedia.org/wiki/Mean_absolute_percentage_error

$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.