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