缺失数据:可能出的问题

R 中的缺失值填补处理

Michal Oleszak

Machine Learning Engineer

学习目标

完成本课程后,您将能够:

  • 理解为何缺失数据需要特殊处理。
  • 用统计检验和可视化检测缺失模式。
  • 使用统计与机器学习模型进行插补。
  • 将插补的不确定性纳入分析与预测,使其更稳健。
R 中的缺失值填补处理

先修要求

本课程假设您熟悉以下内容:

  • 使用 dplyr 与管道(%>%)进行基本数据操作。
  • 线性与逻辑回归模型(lm()glm())。
  • 基本概率概念:随机变量、分布。
R 中的缺失值填补处理

缺失数据入门

处理缺失数据的最佳方法,显然是不要产生它们。

但遗憾的是,缺失数据无处不在:

  • 调查中的不响应。
  • 采集设备的技术问题。
  • 融合多源数据。
  • ……

我们必须对缺失数据保持警惕

1 Orchard, T., and M. A. Woodbury. 1972. "A Missing Information Principle: Theory and Applications." In Proceedings of the Sixth Berkeley Symposium on Mathematical Statistics and Probability, 1:697–715.
R 中的缺失值填补处理

NHANES 数据

head(nhanes, 3)
  Age Gender Weight Height Diabetes TotChol Pulse PhysActive
1  16   male   73.2  172.0    FALSE    3.00    76       TRUE
2  17   male   72.3  176.0    FALSE    2.61    74       TRUE
3  12   male   57.7  158.9    FALSE    4.27    80       TRUE
nhanes %>% is.na() %>% colSums()
Age     Gender     Weight     Height   Diabetes    TotChol    Pulse   PhysActive 
0       0          9          8        1            85        32      26
R 中的缺失值填补处理

含缺失数据的线性回归

model_1 <- lm(Diabetes ~ Age + Weight, 
              data = nhanes)

summary(model_1) 的部分输出:

Residual standard error: 0.08571 on 804 
degrees of freedom (10 observations 
deleted due to missingness)

Adjusted R-squared:  0.005706 
F-statistic: 3.313 on 2 and 804 DF,  
p-value: 0.03691
model_2 <- lm(Diabetes ~ Age + Weight +
              TotChol, data = nhanes)

summary(model_2) 的部分输出:

Residual standard error: 0.08264 on 718 
degrees of freedom (95 observations
deleted due to missingness)

Adjusted R-squared:  0.008422 
F-statistic: 3.041 on 3 and 718 DF,
p-value: 0.02834
R 中的缺失值填补处理

要点回顾

  • 统计软件有时会静默忽略缺失数据。
  • 因此,可能无法比较不同模型。
  • 直接丢弃不完整观测会导致偏差。
  • 存在缺失数据时,必须妥善处理
R 中的缺失值填补处理

Vamos praticar!

R 中的缺失值填补处理

Preparing Video For Download...