How I can calculate MAPE in r with actual e predicted values?

I'm trying to calculare MAPE in R but I have any problems. I have a dataset of food retails from 2017 to 2020 and I split this into training set e and test set. Now, after to calculate forecast value in this way:

tsData_train <- ts(t1[,3], start= 2017, end= 2019, frequency=12)
tsData_test <- ts(t2[,3], start=2019, end= 2020, frequency=12)

#choose best model with arima

a1 <-auto.arima(tsData_train, max.d = 1, D=1)
fitA <- arima(tsData_train, method="ML")
pred <- predict(fitA,n.ahead = 4)
futurVal <- forecast(a1 ,h=24)`

Please, could you tell me if the code is correct. I would like to calculate MAPE manually, my insigh is:


but I don't know which are actual and predicted. Help me, please.

1 answer

  • answered 2020-06-01 10:24 Rémi Coulaud

    The MAPE is the Mean Absolute Persentage Error so it lacks a mean. Moreover :

    MAPE = mean(abs((Y - Yhat)/Y))

    With Y is actual or observed values and Yhat is the predicted values.