2

我想使用 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.426819838403672, -0.23337107002535, : 'xreg' 和 'newxreg' 的列数不同) 中的错误

我该如何解决这个问题?谢谢 C

4

1 回答 1

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)

给出以下图

阴谋

(我假设您正在使用标准 stats 包中的 arima 函数。我认为 forecast 包也有 arima 函数。)

于 2018-03-20T21:15:40.457 回答