問題:
使用forecast.Arima對帶xreg的arima模型進行預測,報xreg Error
1 pre.m4x <- forecast.Arima(m4x, h = 20, xreg = seq(429,448,by = 1))
報錯:
Error in if (ncol(xreg) != ncol(object$call$xreg)) stop("Number of regressors does not match fitted model") : argument is of length zero
原因:
R語言的基礎包stats中包含arima函數,擬合模型時,使用的是stats包中的arima函數。
應該是forecast包對stats中的arima函數支持不夠,所以使用forecast包進行預測會報錯。
正確的做法:
使用Arima函數擬合模型。
Arima()是forecast包中的函數,生成的帶xreg的模型能夠使用forecast.Arima函數進行預測。
參考:http://stackoverflow.com/questions/20086487/error-with-forecast-arima-with-xreg