將序列對齊到每月的第一天與最後一天
有時你可能無法使用像 yearmon 這類的便利類別來表示時間戳。這個練習會帶你學會如何手動將合併後的資料,對齊到你偏好的時間戳表示方式。
先把低頻資料與彙總後的資料合併,接著用 na.locf() 將 NA 以前向方式填補(或使用 fromLast = TRUE 進行後向填補)。之後,你可以用偏好表示方式的物件之索引來對結果做子集化。
你的工作環境已包含 FEDFUNDS、monthly_fedfunds(apply.monthly(DFF, mean) 的結果),以及 merged_fedfunds(merge(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[___]