我想使用Plotmo包中的plotmo指令来绘制arima对象我用一个解释变量矩阵X(传递函数)估计arima模型
arima.model< -arima(Y,C(3,1,3),XREG = X)
绘制此对象时,我有下一个错误:
plotmo(arima.model)stats :: predict(Arima.object,data.frame [3,1],type =“response”)
predict.Arima中的错误(list(coef = c(0,0,0.4686819838403672,-0.23337107002535,:'xreg'和'newxreg'具有不同的列数
我该如何解决这个问题?谢谢C
答案 0 :(得分:0)
Plotmo 对于像arima模型这样的时间序列模型,它并不是真正意义上的 并且不支持他们。
然而,如果你只想绘制拟合的模型和未来
值,以下函数将执行它(可能更简单
使用ts.plot
函数的方式:
plarima <- function(ts, ..., n.ahead=1, main=deparse(substitute(ts)))
{
model <- arima(ts, ...)
if(!inherits(model, "Arima"))
stop("this function requires 'arima' from the standard stats package")
# calculations so we can extend the x axis
n <- length(ts)
x <- xy.coords(ts)$x
if(any(is.na(x)))
stop("NA in time")
xdelta <- (x[n] - x[1]) / n
plot(ts + model$residuals, # plot the fit in gray
xlim=c(x[1], x[n] + xdelta * n.ahead),
main=main, col="gray", lwd=3)
lines(ts) # plot the data
# predict n.ahead values and plot them in red
forecast <- predict(model, n.ahead=n.ahead)
lines(x=x[n] + xdelta * (0:n.ahead), y=c(ts[n], forecast$pred), col=2)
legend("topleft", legend=c("data", "fitted", "forecast"),
col=c(1, "gray", 2), lwd=c(1,3,1), lty=1, bg="white")
model # return the arima model
}
例如
plarima(lh, order=c(3,0,0), n.ahead=10)
plarima(USAccDeaths, order=c(0,1,1), seas=list(order=c(0,1,1)), n.ahead=10)
给出以下图表
(我假设你正在使用arima函数 标准统计包。我认为预测包也是 具有arima功能。)