使用 simputation 进行数据插补
R 中有许多用于插补的包。我们将重点使用 simputation 包,它提供了一个简单而强大的接口来执行插补。
构建一个好的插补模型至关重要,但这是一个复杂的话题——构建好插补模型与构建优秀统计模型一样需要投入。在本课程中,我们将重点学习如何评估插补结果。
首先,我们将使用 impute_lm() 函数,它会根据指定的线性模型进行插补。
在本练习中,我们将把之前学过的评估方法应用到使用 impute_lm() 生成的数据上,并在后续课程中在此方法基础上进一步扩展。
本练习是课程的一部分
在 R 中处理缺失数据
练习说明
使用 oceanbuoys 数据集:
- 使用
wind_ew和wind_ns对humidity进行插补,并用add_label_shadow()追踪缺失值。 - 绘制
air_temp_c与humidity的插补值,分别放在 x、y 轴上,并根据any_missing()着色。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Impute humidity and air temperature using wind_ew and wind_ns, and track missing values
ocean_imp_lm_wind <- ___ %>%
bind_shadow() %>%
impute_lm(air_temp_c ~ wind_ew + wind_ns) %>%
impute_lm(___ ~ ___ + ___) %>%
add_label_shadow()
# Plot the imputed values for air_temp_c and humidity, colored by missingness
ggplot(___,
aes(x = ___, y = ___, color = any_missing)) +
geom_point()