用已估计的 AR 模型进行简单预测
既然您已经使用 arima() 对数据建模,现在就可以基于该模型进行简单预测了。predict() 函数可用于对已估计的 AR 模型进行预测。在由 predict() 生成的对象中,$pred 是预测值,$se 是该预测的标准误。
若要对末次观测之后的多个时期进行预测,您可以在 predict() 中使用 n.ahead 参数。该参数用于设定预测区间(h),也就是要预测的期数。预测将从观测时间序列的末尾开始,递归地产生从 1 步到 h 步的前向预测。
本练习中,您将对 Nile 数据使用 AR 模型进行简单预测。该数据记录了 1871 年到 1970 年间尼罗河的年流量观测。
本练习是课程的一部分
R 中的时间序列分析
练习说明
- 使用
arima()为Nile时间序列拟合一个 AR 模型,并将结果保存为AR_fit。 - 使用
predict()预测 1971 年尼罗河的流量。 - 使用
predict_AR配合$pred[1]获取 1 步预测值。 - 再次调用
predict(),生成从 1 步到 10 步(1971 到 1980 年)的前向预测。为此,将n.ahead设为10。 - 运行预先编写的代码,将
Nile数据、预测结果以及 95% 预测区间一并绘图。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Fit an AR model to Nile
AR_fit <- arima(___, order = ___)
print(AR_fit)
# Use predict() to make a 1-step forecast
predict_AR <- predict(___)
# Obtain the 1-step forecast using $pred[1]
# Use predict to make 1-step through 10-step forecasts
predict(___, n.ahead = ___)
# Run to plot the Nile series plus the forecast and 95% prediction intervals
ts.plot(Nile, xlim = c(1871, 1980))
AR_forecast <- predict(AR_fit, n.ahead = 10)$pred
AR_forecast_se <- predict(AR_fit, n.ahead = 10)$se
points(AR_forecast, type = "l", col = 2)
points(AR_forecast - 2*AR_forecast_se, type = "l", col = 2, lty = 2)
points(AR_forecast + 2*AR_forecast_se, type = "l", col = 2, lty = 2)