R 中的缺失值填补处理
Michal Oleszak
Machine Learning Engineer
Imputation(填补) = 对缺失值进行合理估计
本章关注基于供体的方法:

当时间序列围绕长期均值随机波动时,均值填补效果尚可。
对截面数据,均值填补往往很差:
任务:对 NHANES 的 Height 和 Weight 做均值填补。
nhanes <- nhanes %>%
mutate(Height_imp = ifelse(is.na(Height), TRUE, FALSE)) %>%
mutate(Weight_imp = ifelse(is.na(Weight), TRUE, FALSE))
Height 和 Weight 的缺失值。nhanes_imp <- nhanes %>%
mutate(Height = ifelse(is.na(Height), mean(Height, na.rm = TRUE), Height)) %>%
mutate(Weight = ifelse(is.na(Weight), mean(Weight, na.rm = TRUE), Weight))
nhanes_imp %>%
select(Weight, Height, Height_imp, Weight_imp) %>%
head()
Weight Height Height_imp Weight_imp
1 73.20000 166.2499 TRUE FALSE
2 72.30000 166.2499 TRUE FALSE
3 57.70000 158.9000 FALSE FALSE
4 88.90000 183.3000 FALSE FALSE
5 45.10000 157.6000 FALSE FALSE
6 66.77065 158.4000 FALSE TRUE
nhanes_imp %>% select(Weight, Height, Height_imp, Weight_imp) %>% marginplot(delimiter="imp")

破坏变量关系:
Height 和 Weight 做均值填补后,正相关会变弱。填补数据无变异:

R 中的缺失值填补处理