推定した AR モデルからのシンプルな予測
arima() コマンドでデータをモデリングできたので、モデルに基づいたシンプルな予測を作成してみましょう。推定した AR モデルから予測を行うには、predict() 関数を使います。predict() の返すオブジェクトでは、$pred に予測値、$se に予測の標準誤差が格納されます。
最後の観測期間より先の複数期間について予測するには、predict() の引数 n.ahead を使用します。この引数は予測地平(h)、すなわち予測する期間数を指定します。予測は、観測済み時系列の末尾から 1 期先から h 期先まで、再帰的に計算されます。
この演習では、1871 年から 1970 年までのナイル川の年間流量を記録した Nile データに AR モデルを適用し、シンプルな予測を作成します。
この演習はコースの一部です
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)