前值填充(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(___) +
___