估計隨機漫步模型
給定時間序列 y,要配適帶有漂移(drift)的隨機漫步模型,可以先對資料做一次差分,接著使用 arima() 並將參數 order = c(0, 0, 0)),對差分後的資料配適白雜訊(WN)模型。
arima() 指令會顯示已配適模型的相關資訊或輸出。在 Coefficients: 標題下,會列出估計出的漂移變數,名稱為 intercept。其近似標準誤(s.e.)會顯示在其正下方。模型中 WN 部分的變異數也會在 sigma^2 標籤下提供估計值。
本練習屬於課程
R 的時間序列分析
練習說明
- 時間序列
random_walk已載入,並顯示在旁邊的圖中。使用diff()產生資料的一次差分。將結果儲存為rw_diff。 - 使用
ts.plot()繪製差分後的資料。 - 使用
arima()對差分後的資料配適 WN 模型。將x參數設定為rw_diff,order參數設定為c(0, 0, 0)。把模型儲存到model_wn。 - 將
model_wn的intercept值儲存到int_wn。你可以用model_wn$coef取得這個值。 - 使用
ts.plot()重新繪製原始的random_walk圖。 - 使用
abline()將估計出的時間趨勢加到旁邊的圖上。你可以把int_wn作為第二個引數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Difference your random_walk data
rw_diff <-
# Plot rw_diff
# Now fit the WN model to the differenced data
model_wn <-
# Store the value of the estimated time trend (intercept)
int_wn <-
# Plot the original random_walk data
# Use abline(0, ...) to add time trend to the figure