I don't quite understand the syntax of how forecast()
applies external regressors in the library(forecast)
in R
.
My fit looks like this:
fit <- auto.arima(Y,xreg=factors)
where Y
is a timeSeries
object 100 x 1 and factors is a timeSeries
object 100 x 5.
When I go to forecast, I apply...
forecast(fit, h=horizon)
And I get an error:
Error in forecast.Arima(fit, h = horizon) : No regressors provided
Does it want me to add back the xregressors from the fit? I thought these were included in the fit
object as fit$xreg
. Does that mean it's asking for future values of the xregressors, or that I should repeat the same values I used in the fit set? The documentation doesn't cover the meaning of xreg
in the forecast step.
I believe all this means I should use
forecast(fit, h=horizon,xreg=factors)
or
forecast(fit, h=horizon,xreg=fit$xreg)
Which gives the same results. But I'm not sure whether the forecast step is interpreting the factors as future values, or appropriately as previous ones. So,
In this case, auto. arima from the forecast package in R allows us to implement a model of this type with relative ease.
To run the forecasting models in 'R', we need to convert the data into a time series object which is done in the first line of code below. The 'start' and 'end' argument specifies the time of the first and the last observation, respectively.
Correct me if I am wrong, but I think you may not completely understand how the ARIMA model with regressors works.
When you forecast with a simple ARIMA model (without regressors), it simply uses past values of your time series to predict future values. In such a model, you could simply specify your horizon, and it would give you a forecast until that horizon.
When you use regressors to build an ARIMA model, you need to include future values of the regressors to forecast. For example, if you used temperature as a regressor, and you were predicting disease incidence, then you would need future values of temperature to predict disease incidence.
In fact, the documentation does talk about xreg
specifically. look up ?forecast.Arima
and look at both the arguments h
and xreg
. You will see that If xreg
is used, then h
is ignored. Why? Because if your function uses xreg
, then it needs them for forecasting.
So, in your code, h
was simply ignored when you included xreg
. Since you just used the values that you used to fit the model, it just gave you all the predictions for the same set of regressors as if they were in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With