開始使用免費開始

估計隨機漫步模型

給定時間序列 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_difforder 參數設定為 c(0, 0, 0)。把模型儲存到 model_wn
  • model_wnintercept 值儲存到 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

編輯並執行程式碼