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 函数。)