Daniel James
Daniel James

Reputation: 1433

R: Put me through on the accuracy function on forecast package for time series

I intend to find Root Mean Squared Error for an ARIMA forecast method which I simulated its data. My method is done as follows using R as I followed Rob J. Hyndman approach:

  1. Divide the time series data into train and test sets.
  2. Obtain the best model through auto.arima() function
  3. Forecast for the train data set into the future up to the length of the test set.
  4. Calculate the RMSE of the forecast.

MWE

library(forecast)
n=50
phi <- 0.6
set.seed(106100125)
ar1 <- arima.sim(n, model = list(ar=phi, order = c(1, 0, 0)), sd = 1)

train <- head(ar1, round(length(ar1) * 0.8)) # Train set

test <- tail(ar1, length(ar1) - length(train)) # Test set

nfuture <- forecast(train, model = auto.arima(train), h = length(test))      # makes the `forecast of test set to the future up to length of test set

RMSE <- accuracy(test, nfuture)      # RETURN RMSE

When I call RMSE as I use it in MWE I got 0. But when I called test and nfuture I got

#[1]  1.0470537  0.3984545  0.5811056  2.2703350 -1.0060028 -1.6126040 -0.4329466  2.1523534  1.2588265  0.7308986

and

#[1] 0.55281252 0.42374990 0.32481894 0.24898494 0.19085556 0.14629738 0.11214200 0.08596072 0.06589186 0.05050839

respectively which show both are not similar thus, RMSE can not be 0

Please help me out on what I have done wrong and put me through on what I need to do to make it right.

Upvotes: 1

Views: 1252

Answers (1)

Rob Hyndman
Rob Hyndman

Reputation: 31820

Using your code, the following error is produced:

RMSE <- accuracy(test, nfuture)
#> Error in xx - ff[1:n]: non-numeric argument to binary operator

You have switched the order of the arguments. If you fix that problem, you get the following result

accuracy(nfuture, test)
#>                     ME      RMSE       MAE       MPE     MAPE      MASE
#> Training set 0.1068326 0.7035255 0.5543322 146.47245 194.2587 0.9426693
#> Test set     0.3185452 1.2399912 1.0237739  81.17983  82.4495 1.7409780
#>                   ACF1 Theil's U
#> Training set 0.1696878        NA
#> Test set     0.1777069 0.9050431

Created on 2020-09-24 by the reprex package (v0.3.0)

Upvotes: 3

Related Questions