user3011348
user3011348

Reputation: 21

Error with forecast.Arima with xreg

I am trying to fit a regression model with ARMA errors using the arima() and forecast.Arima() functions in the forecast library. (i.e. the closest thing to an ARMAX model that I can fit using the arima() function)

My code:


library(forecast)
data <-read.csv(filename,stringsAsFactors=FALSE)
data.ts<-ts(data$result,frequency=24,start=c(1,1),end=c(7,24))
input.ts<-ts(data$input,frequency=24,start=c(1,1),end=c(7,24))
data.fit <- arima(window(data.ts,start=c(1,1),end=c(5,24)), 
                  order=c(2,0,3), seasonal =list(order = c(1, 0, 1), period = 24),      
                  xreg=window(input.ts,start=c(1,1),end=c(5,24)))
data.forecast <-forecast.Arima(data.fit,
                               xreg=window(input.ts,start=c(6,1),end=c(7,24)))

However, I get the following error when including the xreg factor in the forecast.Arima() function:

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

I don't understand why I get this error. I have included the future values of xreg in forecast.Arima() function, and the input time series is the exact same in the arima() function, just at a different window.

What should be the type of xreg? I have tried coercing the xreg time series object into a data frame and numeric vector with no success.

Upvotes: 2

Views: 5651

Answers (2)

hrbs
hrbs

Reputation: 26

I was getting the same error running a model with error following an AR1 with the arima function:

arima(y, xreg=cbind(x1, x2, x3, x4), order=c(1,0,0))

I solved that using the auto.arima function. In order to get always a model with AR1 error, I conditioned the parameters using the code below:

auto.arima(y, xreg=cbind(x1, x2, x3, x4), max.p = 1, max.q = 0, max.P = 0, max.Q = 0, max.order = 0, max.d = 0, max.D = 0, start.p = 1)

Upvotes: 0

user3099750
user3099750

Reputation: 21

I was having the same problem. I think it stems from the fact that there actually are two different arima functions: arima in the basic stats package, and Arima in the forecast package. What you want to do is to first fit your model using the Arima function. It worked for me.

Upvotes: 1

Related Questions