Convert forecast output into a matrix

Hi All,

How to convert a forecast object into a matrix? so that I can use the forecast output as a co-variate to the auto.arima function.
When I use the forecast output as one of the co-variate to my auto.arima function it throws an error- Error in forecast.forecast_ARIMA(bestfit, xreg = cbind(fourier.bestfit, :
xreg should be a numeric matrix or a numeric vector)

this is my forecast code

forecast.final<-forecast(bestfit,xreg = cbind(fourier.bestfit,forecast.1),h=30)

where the forecast.1 is the forecast object im trying to convert to a matrix in order to use that as a co-variate to my above forecast code.

Please advise

Thank you

The forecast object is a list. You probably want the point forecast (the mean of the forecast distribution) to use as a covariate, which is stored as mean in the forecast object. So here is some code that forecasts mdeaths as a function of fdeaths.

library(forecast)
fitm <- auto.arima(mdeaths, xreg=fdeaths)
fitf <- auto.arima(fdeaths)
fcf <- forecast(fitf, h=10)
fcm <- forecast(fitm, xreg=fcf$mean)
autoplot(fcm)

Created on 2021-11-24 by the reprex package (v2.0.1)

While that works, it ignores the uncertainty in the forecasts of the covariates. A better approach would take that uncertainty into account by simulating future sample paths of the covariates and using those to simulate future sample paths of the main variable.

1 Like

Thank you Sir. I did that as I happen to find the mean from the forecast object after I post this question and as you rightly said I have predicted the future values of the co-variate first and used that as a regressor to the main variable.

I have been reading your books and it is highly helpful. Thank you for guiding us through.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.