在 R 中以插補處理遺漏值
Michal Oleszak
Machine Learning Engineer

優點:
可對 MNAR 進行敏感度分析。
缺點:
對 nhanes 進行 20 次插補:
library(mice)
nhanes_multiimp <- mice(nhanes, m = 20)
對每個插補資料集擬合線性迴歸模型:
lm_multiimp <- with(nhanes_multiimp, lm(Weight ~ Height + TotChol + PhysActive))
匯整迴歸結果:
lm_pooled <- pool(lm_multiimp)
summary(lm_pooled, conf.int = TRUE, conf.level = 0.95)
estimate std.error statistic df p.value 2.5 % 97.5 %
(Intercept) -122.964 10.933 -11.247 735.389 0.000 -144.428 -101.500
Height 1.086 0.060 18.158 796.106 0.000 0.968 1.203
TotChol 2.653 0.884 3.003 305.460 0.003 0.915 4.392
PhysActive -1.746 1.422 -1.228 733.957 0.220 -4.536 1.045

mice() 的參數 defaultMethod 是一個含 4 個字串的向量,用來指定下列方法:
nhanes_multiimp <- mice(nhanes, m = 20,
defaultMethod = c("pmm", "logreg", "polyreg", "polr"))
predictorMatrix 決定用哪些變數來插補其他變數。
nhanes_multiimp <- mice(nhanes, m = 20)
nhanes_multiimp$predictorMatrix
Age Gender Weight Height Diabetes TotChol Pulse PhysActive
Age 0 1 1 1 1 1 1 1
Gender 1 0 1 1 1 1 1 1
Weight 1 1 0 1 1 1 1 1
Height 1 1 1 0 1 1 1 1
Diabetes 1 1 1 1 0 1 1 1
TotChol 1 1 1 1 1 0 1 1
Pulse 1 1 1 1 1 1 0 1
PhysActive 1 1 1 1 1 1 1 0
pred_mat <- quickpred(nhanes, mincor = 0.25)
nhanes_multiimp <- mice(nhanes, m = 20, predictorMatrix = pred_mat)
print(pred_mat)
Age Gender Weight Height Diabetes TotChol Pulse PhysActive
Age 0 0 0 0 0 0 0 0
Gender 0 0 0 0 0 0 0 0
Weight 1 1 0 0 0 0 1 0
...
在 R 中以插補處理遺漏值