最後觀測值向前填補(LOCF)
另一個重要的插補方法是「最後觀測值向前填補」(Last observation carried forward,簡稱 LOCF)。這個方法會用先前最近的非 NA 值來填入遺漏值。
在這個練習中,你要對一位學生每月考試分數的時間序列 monthly_test_scores 進行插補。這個資料集以及 ggplot2 與 zoo 套件都已提供給你使用。
本練習屬於課程
在 R 中操作時間序列資料
練習說明
使用「最後觀測值向前填補」對
monthly_test_scores的遺漏值進行插補,將結果存為monthly_scores_locf。完成第一個
autoplot(),繪製原始monthly_test_scores時間序列圖。以 'minimal' 佈景主題繪製
monthly_scores_locf,並設定 y 軸標籤為"Examination Score (LOCF)"。透過在圖之間切換,比較
monthly_scores_locf與monthly_test_scores的圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Impute using last observation carried forward
___ <- ___
# Create a plot of the original time series
autoplot(___) +
theme_minimal() +
labs(y = "Examination Score")
# Autoplot with theme and axis title and compare to the above plot
autoplot(___) +
___