简体   繁体   中英

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. (ie 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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM