Search code examples
rtime-seriesforecastingarima

Specified forecast period not constraining forecast output


I am attempting to build an ARIMA model of a 10-year long time series, and use it to forecast one year into the future. To test my model, I train on 9 years of data, predict 1 year of data, and compare the predicted values to the actual values for that year.

Problem: I tell forecast() to constrain the period of its prediction to 365 i.e. 1 year. But when I plot the output, it appears to output 9 years or an "h=" of roughly 3285. Why is this occurring?

##The time series is 3650 daily observations of rainfall
x <- ts(x$obs, start=c(2007, 10), end=c(2017, 9), frequency = 365)

##create training set - first 9 years of observations
x_train <- subset(x, start = 1, end = 3285)

##test set - last year of observations
x_test <- subset(x, start = 3286, end = 3650)

##fit the model
x_train_fit <- auto.arima(x_train, seasonal=FALSE, xreg=fourier(x_train, K=1))

##forecast using the model 
x_fcast_test <- forecast(x_train_fit,h=365, xreg=fourier(x_train, K=1))
plot(x_fcast_test, col="black") 
lines(x_test,col="red")

enter image description here

Update: Rob Hyndman's answer below is correct. The number of forecast periods will be set to the number of rows of xreg because that is used in favor of h= when xreg is used, so my h= was not being used. Therefore, passing in the entire training set as an exreg created a forecast equal in length to the training set.

x_fcast_test <- forecast(x_train_fit,h=365, xreg=fourier(x_test, K=1))
plot(x_fcast_test, col="black") 
lines(x_test,col="red")

enter image description here


Solution

  • It is always worth reading the help files that are provided. In this case:

    h: Number of periods for forecasting. If xreg is used, h is ignored and the number of forecast periods is set to the number of rows of xreg.

    You passed the training data in the xreg argument, so you get as many forecasts as you have observations.

    Presumably you meant to use the test data for the xreg argument.