modLmakur
modLmakur

Reputation: 573

Forecasting ARIMA with xreg

I'm trying to forecast time in time out ("TiTo") for someone ordering food at a restaurant using the code below. TiTo is the total time it takes someone from the time they walk through the door to the time they get their food. TimeTT is the time the customer spends talking to the waiter. I believe TimeTT is a predictor of TiTo and I would like to use it as a covariate in the forecast for TiTo. I've read some about ARIMA, and as I understand it you add the predictors to the model in the xreg parameter. I'm thinking of the xreg parameter as something like the independent variable for a regression model, like lm(TiTo ~ TimeTT). Is this the correct way to think of the xreg parameter? Also what does the error message below mean? Do I need to convert TimeTT into a time series to use it in the xreg parameter? I'm new to forecasting so all help is very appreciated.

Forecast Attempt:

OV<-zoo(SampleData$TiTo, order.by=SampleData$DateTime)

eData <- ts(OV, frequency = 24)

Train <-eData[1:15000]

Test <- eData[15001:20809]

Arima.fit <- auto.arima(Train)

Acast<-forecast(Arima.fit, h=5808, xreg = SampleData$TimeTT)

Error:

Error in if (ncol(xreg) != ncol(object$call$xreg)) stop("Number of regressors does not match fitted model") : argument is of length zero

Data:

dput(Train[1:5])
c(1152L, 1680L, 1680L, 968L, 1680L)

dput(SampleData[1,]$TimeTT)
structure(1156L, .Label = c("0.000000", "0.125000", "0.142857",  
"96.750000", "97.800000", "99.000000", "99.600000", "NULL"), class = "factor")

Upvotes: 1

Views: 6535

Answers (1)

Glen Moutrie
Glen Moutrie

Reputation: 325

You need to define the xreg when you estimate the model itself, and these need to be forecasted ahead as well. So this will look something like:

Arima.fit <- auto.arima(Train, xreg = SampleData$TimeTT)
forecast(Arima.fit, h = 508, xreg = NewData$TimeTT)

Here is an example using Arima and xreg from Rob Hyndman (here is the link to the example, but to read more about using contemporaneous covariates in ARIMA models go here), this is analogous to auto.arima.

n <- 2000
m <- 200
y <- ts(rnorm(n) + (1:n)%%100/30, f=m)

library(forecast)
fit <- Arima(y, order=c(2,0,1), xreg=fourier(y, K=4))
plot(forecast(fit, h=2*m, xreg=fourierf(y, K=4, h=2*m)))

Hope this helps.

Upvotes: 3

Related Questions