開始使用免費開始

將序列對齊到每月的第一天與最後一天

有時你可能無法使用像 yearmon 這類的便利類別來表示時間戳。這個練習會帶你學會如何手動將合併後的資料,對齊到你偏好的時間戳表示方式。

先把低頻資料與彙總後的資料合併,接著用 na.locf()NA 以前向方式填補(或使用 fromLast = TRUE 進行後向填補)。之後,你可以用偏好表示方式的物件之索引來對結果做子集化。

你的工作環境已包含 FEDFUNDSmonthly_fedfundsapply.monthly(DFF, mean) 的結果),以及 merged_fedfundsmerge(FEDFUNDS, monthly_fedfunds) 的結果,其中 monthly_fedfunds 的索引為 Date)。請留意 merged_fedfunds 中的 NA 值。

本練習屬於課程

在 R 匯入與管理金融資料

檢視課程

練習說明

  • 使用 na.locf() 填補 merged_fedfunds 中的 NA 值。將結果指定給 merged_fedfunds_locf
  • index(monthly_fedfunds)merged_fedfunds_locf 做子集化,建立時間戳位於月末的 xts 物件。將結果命名為 aligned_last_day
  • na.locf() 使用 fromLast 參數,以「下一筆」觀測值來填補 NA。將結果指定給 merged_fedfunds_locb
  • index(FEDFUNDS)merged_fedfunds_locb 做子集化,建立時間戳位於每月 1 日的 xts 物件。將結果命名為 aligned_first_day

動手互動練習

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

# Fill NA forward
merged_fedfunds_locf <- ___(___)

# Extract index values containing last day of month
aligned_last_day <- merged_fedfunds_locf[___]

# Fill NA backward
merged_fedfunds_locb <- na.locf(___, fromLast = ___)

# Extract index values containing first day of month
aligned_first_day <- merged_fedfunds_locb[___]
編輯並執行程式碼