開始使用免費開始

由估計出的 MA 模型取得簡單預測

你已用 Nile 資料估計了一個 MA 模型,下一步是用這個模型做一些簡單的預測。和其他模型一樣,你可以使用 predict() 函式,從你估計的 MA 模型產生簡單的預測。請記得,$pred 是預測值,而 $se 是該預測的標準誤,兩者都依據配適好的 MA 模型計算。

同樣地,若要對最後一筆觀測之後的多個期間進行預測,你可以在呼叫 predict() 時使用 n.ahead = h 參數。這些預測會自觀測期末端起,遞迴地產生從 1 到 h 步的超前預測。不過要注意,除了 1 步預測之外,MA 模型產生的所有超前預測都會等於估計的平均數(intercept)。

在這個練習中,你會使用從 Nile 資料得到的 MA 模型,對尼羅河未來流量做簡單預測。上一個練習得到的 MA 模型已可在你的環境中使用。

本練習屬於課程

R 的時間序列分析

檢視課程

練習說明

  • 使用 predict() 產生 1971 年尼羅河流量的預測。將結果存為 predict_MA
  • 使用 predict_MA$pred[1] 取得 1 步預測。
  • 再次呼叫 predict(),對 1971 年到 1980 年進行預測。請將 n.ahead 參數設為 10
  • 執行已寫好的程式碼,繪製 Nile 時間序列以及預測與 95% 預測區間。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Make a 1-step forecast based on MA
predict_MA <-

# Obtain the 1-step forecast using $pred[1]


# Make a 1-step through 10-step forecast based on MA


# Plot the Nile series plus the forecast and 95% prediction intervals
ts.plot(Nile, xlim = c(1871, 1980))
MA_forecasts <- predict(MA, n.ahead = 10)$pred
MA_forecast_se <- predict(MA, n.ahead = 10)$se
points(MA_forecasts, type = "l", col = 2)
points(MA_forecasts - 2*MA_forecast_se, type = "l", col = 2, lty = 2)
points(MA_forecasts + 2*MA_forecast_se, type = "l", col = 2, lty = 2)
編輯並執行程式碼