I tried implementing xreg into my rolling forecast with re-estimation. Unfortunately I'm facing issues with the length of xreg.
# sample data
sample <- ts(rnorm(100, mean = 1000, sd=7), start = c(2012,1), end = c(2019,12), frequency = 12)
external <- ts(mtcars, start = c(2012,1), end = c(2019,12), frequency = 12)
#Define h --> One-step ahead (for a start, later to be increased)
h <- 1
#specify length to forecast
test <- window(sample, start = c(2018,01), end = c(2019,12), frequency = 12)
n <- length(test) - h + 1
#provide total length of regressors available
total_xreg <- ts(external[,c(1,2,3)], start = c(2012,1), end= c(2019,12), frequency = 12)
#create empty matrix
fcmatx <- matrix(0, nrow=n, ncol=h)
# create loop
for(i in 1:n)
{
# x is the target variable, provide training data
x <- window(sample, end= c(2017,12) + (i-1)/12)
# provide xregs for training data
xregs <- window(total_xreg, end = c(2017,12) + (i-1)/12)
# provide new xregs for forecasting, assuming that xreg is available for the forecasting period
xregs2 <- window(total_xreg, start = c(2018,1) + (i-1)/12
# limit xregs2 to show only the first line since we are only forecasting 1 step in advance
xregs3 <- xregs2[1,]
# create auto.arima model
refit.multirex <- auto.arima(x, xreg = xregs)
# forecast using regressors
fcmatx[i,] <- forecast(refit.multirex,
h=h,
xreg = xregs3
)$mean
}
fcmattsx <- ts(fcmatx, start = c(2018,1), frequency = 12)
This results in the following error:
Error in forecast.forecast_ARIMA(refit.multirex, h = h, xreg = xregs3) :
Number of regressors does not match fitted model
h is length 1 and xregs is length 3 because I'm filling in 3 variables but they are all only for one period of time. I tried various adjustments but cannot get it right.
The following line
xregs3 <- xregs2[1,]
returns a vector rather than a matrix. This is the default behaviour in R when you extract a single column or row from a matrix. Change it to
xregs3 <- xregs2[1,,drop=FALSE]
to keep the matrix structure (1x3). Then the forecast()
function will not return an error.
You will get a different error when i=23
because then you have start
after end
when creating xregs2
.