复现数据的可变性

R 中的缺失值填补处理

Michal Oleszak

Machine Learning Engineer

插补数据的变异性

一个边缘图:展示"Height"与"Weight"的散点图,任一变量被插补的点以不同颜色高亮。

  • 插补数据无变异性。
  • 期望插补能复现观测数据的变异性。
  • 基于模型的插补:相同自变量会给出相同的插补值。
  • 解决方案:从条件分布中抽样。
R 中的缺失值填补处理

什么是预测

大多数统计模型都会估计响应变量的条件分布:

p(y|X)

要做单次预测,会对条件分布做汇总:

  • 线性回归:取条件分布的期望值。
  • 逻辑回归:取概率最高的类别。

相反,我们可以从这些分布中抽样,以增加变异性。

R 中的缺失值填补处理

从条件分布中抽样

一张正态分布概率密度函数图。均值 25 被高亮显示。

R 中的缺失值填补处理

从条件分布中抽样

一张包含四列的表:逻辑回归的预测概率(所有行为 0.7)、该概率是否>0.5 的布尔值(全为 TRUE)、基于阈值的插补值(全为 1),以及从条件分布抽样得到的插补值(多数为 1,但有些为 0)。

R 中的缺失值填补处理

逻辑回归插补

任务:用逻辑回归填补 nhanes 中的 PhysActive

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
R 中的缺失值填补处理

逻辑回归插补

任务:用逻辑回归填补 nhanes 中的 PhysActive

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
R 中的缺失值填补处理

逻辑回归插补

任务:用逻辑回归填补 nhanes 中的 PhysActive

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
R 中的缺失值填补处理

逻辑回归插补

任务:用逻辑回归填补 nhanes 中的 PhysActive

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
preds <- ifelse(preds >= 0.5, 1, 0)
R 中的缺失值填补处理

逻辑回归插补

任务:用逻辑回归填补 nhanes 中的 PhysActive

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
preds <- ifelse(preds >= 0.5, 1, 0)
nhanes_imp[missing_physactive, "PhysActive"] <- preds[missing_physactive]
R 中的缺失值填补处理

逻辑回归插补

插补数据的变异性:

table(preds[missing_physactive])
 1 
26

观测到的 PhysActive 的变异性:

table(nhanes$PhysActive)
  0   1 
181 610
R 中的缺失值填补处理

按类别概率抽样

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
preds <- ifelse(preds >= 0.5, 1, 0)
nhanes_imp[missing_physactive, "PhysActive"] <- preds[missing_physactive]
R 中的缺失值填补处理

按类别概率抽样

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")

nhanes_imp[missing_physactive, "PhysActive"] <- preds[missing_physactive]
R 中的缺失值填补处理

按类别概率抽样

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
preds <- rbinom(length(preds), size = 1, prob = preds)
nhanes_imp[missing_physactive, "PhysActive"] <- preds[missing_physactive]
R 中的缺失值填补处理

按类别概率抽样

插补数据的变异性:

table(preds[missing_physactive])
0  1 
5 21

观测到的 PhysActive 的变异性:

table(nhanes$PhysActive)
  0   1 
181 610
R 中的缺失值填补处理

Vamos praticar!

R 中的缺失值填补处理

Preparing Video For Download...