使用vars包和预测函数绘制未来的观测值

时间:2015-10-08 11:11:04

标签: r statistics forecasting

在以下网站(https://www.otexts.org/fpp/9/2)上,创建了一个Vector自动回归。

library(vars)
VARselect(usconsumption, lag.max=8, type="const")$selection


var <- VAR(usconsumption, p=3, type="const")
serial.test(var, lags.pt=10, type="PT.asymptotic")


summary(var)

代码相当简单,但在创建预测时:

fcst <- forecast(var)
plot(fcst, xlab="Year")

forecast功能不起作用。但是,使用predict函数不会产生与预测函数相同的图。

有任何建议,如何从提供的link获取情节?

感谢您的回复!

1 个答案:

答案 0 :(得分:2)

你的意思是“forecast功能不起作用”?我得到你想要的情节,预测功能给出完全相同的预测:

> fcst <- forecast(var, h=3)
> pred <- predict(var, n.ahead=3)
> fcst
consumption 
        Point Forecast       Lo 80    Hi 80      Lo 95    Hi 95
2011 Q1      0.7421472 -0.06576279 1.550057 -0.4934445 1.977739
2011 Q2      0.7980532 -0.03433474 1.630441 -0.4749743 2.071081
2011 Q3      0.7921921 -0.06526268 1.649647 -0.5191718 2.103556

income 
        Point Forecast      Lo 80    Hi 80      Lo 95    Hi 95
2011 Q1      0.8208025 -0.2748906 1.916496 -0.8549154 2.496520
2011 Q2      0.7188650 -0.4437286 1.881459 -1.0591685 2.496898
2011 Q3      0.8241248 -0.3388670 1.987117 -0.9545177 2.602767
> pred
$consumption
          fcst      lower    upper       CI
[1,] 0.7421472 -0.4934445 1.977739 1.235592
[2,] 0.7980532 -0.4749743 2.071081 1.273028
[3,] 0.7921921 -0.5191718 2.103556 1.311364

$income
          fcst      lower    upper       CI
[1,] 0.8208025 -0.8549154 2.496520 1.675718
[2,] 0.7188650 -1.0591685 2.496898 1.778034
[3,] 0.8241248 -0.9545177 2.602767 1.778643