这是我第一次执行多重插补,我选择R包MICE是因为它具有处理不同类型数据的灵活性。我有一个非常大的数据集,其中包含100多个变量,因此我需要将模型限制为最重要的预测变量。
我发现了以下关于如何为插补模型选择预测变量的建议(经过稍微修改以适合下面的示例数据集,这是原始来源:https://stefvanbuuren.name/fimd/sec-knowledge.html):
”假定数据由结果变量out,两个背景变量bck1和bck2,具有3个项a1-a3的标度a,具有4个项b1-b4的标度b以及所有变量包含缺失值组成。填写初始估算后,估算模型将采取以下步骤:
我根据上述说明创建了一个示例数据集:
> out<-c(45,433,234,435, 234,234,243,243,243,24,244,242, 453,234,689)
> a1<-c(1,4,2,1,4,4,2,5,7,3,2,5,2,2,2)
> a2<-c(4,3,4,3,2,1,3,4,2,3,4,5,6,6,7)
> a3<-c(2,2,1,3,4,2,1,3,4,2,3,4,4,2,1)
> b1<-c(5,5,2,3,6,7,3,2,4,5,6,7,4,5,6)
> b2<-c(2,3,1,2,3,2,1,2,2,3,4,3,1,2,4)
> b3<-c(4,3,4,5,2,1,2,3,4,2,1,2,3,4,2)
> b4<-c(1,1,1,2,4,2,2,4,2,1,1,3,4,2,1)
> bck1<-factor(c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2), level=c(1,2), label = c('x','y'))
> bck2<-c(44,32,34,23,45,23,67,43,23,18,45,33,23,45,74)
> test<-data.frame(out,a1,a2,a3,b1,b2,b3,b4,bck1,bck2)
> #Produce NAs
> library(missForest)
> test.na <- prodNA(test, noNA = 0.1)
> head(test.na)
out a1 a2 a3 b1 b2 b3 b4 bck1 bck2
1 45 1 4 2 5 2 4 1 x 44
2 433 4 3 NA 5 3 3 1 <NA> 32
3 234 NA NA 1 2 1 4 1 x 34
4 435 1 NA 3 3 2 NA 2 x 23
5 234 4 2 4 6 3 2 4 x 45
6 234 4 1 2 7 2 NA 2 x 23
然后,我尝试按照推荐的方式对MI模型进行编码。如果我做对了,我会很不安全。使用此代码,R会在插补过程中更新标度总和吗?哪个变量首先出现在预测变量矩阵中是否重要?
> ini <- mice(cbind(test.na,a=NA, b =NA),max=0,print=FALSE)
Warning message:
Number of logged events: 2
> meth <- ini$meth
> meth["a"] <- "~I(a1 + a2 + a3)"
> meth["b"] <- "~I(b1 + b2 + b3 + b4)"
>
> pred<-ini$pred
> pred[c("out"),c("a1","a2", "a3","b1","b2","b3","b4")] <- 0
> pred[c("a1","a2","a3"),"a"] <- 0
> pred[c("out"),c("a","b")] <- 1
> pred[c("b1","b2","b3", "b4"),"b"] <- 0
> pred[c("a1","a2","a3"),c("b1","b2","b3", "b4")] <- 0
> pred[c("a1","a2","a3"),c("b")] <- 1
> pred[c("b1","b2","b3","b4"),c("a")] <- 1
> pred[c("bck1","bck2"),c("a1", "a2","a3","b1","b2","b3", "b4")] <- 0
> pred[c("bck1","bck2"),c("a", "b")] <- 1
> pred
out a1 a2 a3 b1 b2 b3 b4 bck1 bck2 a b
out 0 0 0 0 0 0 0 0 1 1 1 1
a1 1 0 1 1 0 0 0 0 1 1 0 1
a2 1 1 0 1 0 0 0 0 1 1 0 1
a3 1 1 1 0 0 0 0 0 1 1 0 1
b1 1 1 1 1 0 1 1 1 1 1 1 0
b2 1 1 1 1 1 0 1 1 1 1 1 0
b3 1 1 1 1 1 1 0 1 1 1 1 0
b4 1 1 1 1 1 1 1 0 1 1 1 0
bck1 1 0 0 0 0 0 0 0 0 1 1 1
bck2 1 0 0 0 0 0 0 0 1 0 1 1
a 0 0 0 0 0 0 0 0 0 0 0 0
b 0 0 0 0 0 0 0 0 0 0 0 0
>
> impdat<-mice(cbind(test.na, a = NA, b = NA), pred=pred, meth = meth)
iter imp variable
1 1 out a1 a2 a3 b1 b3 bck1 a b
1 2 out a1 a2 a3 b1 b3 bck1 a b
1 3 out a1 a2 a3 b1 b3 bck1 a b
1 4 out a1 a2 a3 b1 b3 bck1 a b
1 5 out a1 a2 a3 b1 b3 bck1 a b
2 1 out a1 a2 a3 b1 b3 bck1 a b
2 2 out a1 a2 a3 b1 b3 bck1 a b
2 3 out a1 a2 a3 b1 b3 bck1 a b
2 4 out a1 a2 a3 b1 b3 bck1 a b
2 5 out a1 a2 a3 b1 b3 bck1 a b
3 1 out a1 a2 a3 b1 b3 bck1 a b
3 2 out a1 a2 a3 b1 b3 bck1 a b
3 3 out a1 a2 a3 b1 b3 bck1 a b
3 4 out a1 a2 a3 b1 b3 bck1 a b
3 5 out a1 a2 a3 b1 b3 bck1 a b
4 1 out a1 a2 a3 b1 b3 bck1 a b
4 2 out a1 a2 a3 b1 b3 bck1 a b
4 3 out a1 a2 a3 b1 b3 bck1 a b
4 4 out a1 a2 a3 b1 b3 bck1 a b
4 5 out a1 a2 a3 b1 b3 bck1 a b
5 1 out a1 a2 a3 b1 b3 bck1 a b
5 2 out a1 a2 a3 b1 b3 bck1 a b
5 3 out a1 a2 a3 b1 b3 bck1 a b
5 4 out a1 a2 a3 b1 b3 bck1 a b
5 5 out a1 a2 a3 b1 b3 bck1 a b
There were 11 warnings (use warnings() to see them)
> impdat
Class: mids
Number of multiple imputations: 5
Imputation methods:
out a1 a2
"pmm" "pmm" "pmm"
a3 b1 b2
"pmm" "pmm" ""
b3 b4 bck1
"pmm" "" "logreg"
bck2 a b
"" "~I(a1 + a2 + a3)" "~I(b1 + b2 + b3 + b4)"
PredictorMatrix:
out a1 a2 a3 b1 b2 b3 b4 bck1 bck2 a b
out 0 0 0 0 0 0 0 0 1 1 1 1
a1 1 0 1 1 0 0 0 0 1 1 0 1
a2 1 1 0 1 0 0 0 0 1 1 0 1
a3 1 1 1 0 0 0 0 0 1 1 0 1
b1 1 1 1 1 0 1 1 1 1 1 1 0
b2 1 1 1 1 1 0 1 1 1 1 1 0