[R语言]forecast.Arima中使用xreg报错

问题:

使用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

原文地址:https://www.cnblogs.com/oDoraemon/p/5253340.html