小鼠的被动插补给出错误的总和

时间:2018-07-11 15:57:52

标签: r r-mice

我正在处理一个由374个变量组成的76人的大型数据集。我的主要结果变量是抑郁严重程度调查表(PH​​Q-9)上的抑郁总和。大约有4%的数据丢失,因此我想使用归因。我一直按照Buuren,S.van和Groothuis-Oudshoorn,K.(2011)中的说明使用鼠标包。小鼠:R。中链式方程的多元归因统计软件,45(3)。 https://doi.org/10.18637/jss.v045.i03。我试图复制他们的指令,说明如何使用无源插补来生成sumscores。但是,我得到了错误的结果。我不知道为什么-我认为我已经正确遵循了说明。

我无法发布数据,因为它很敏感,但是我可以使用以下代码复制错误,该代码实质上是复制原始代码:

library("mice")
library("lattice")
set.seed(1234)
m<-matrix(sample(c(NA, 1:10), 100, replace = T), 10)
df<-as.data.frame(m)

ini<-mice(cbind(df, sumScore=NA), max = 0, print=F)
meth<-ini$method
meth[1:4]<-""
meth[5:10]<-"pmm"
meth["sumScore"]<-"~I(rowSums(df[,5:10]))"
pred<-ini$predictorMatrix
pred[, 1:4]<-0
pred[5:10, "sumScore"]<-0
pred[1:4, "sumScore"]<-1

imp<-mice(cbind(df, sumScore=NA), predictorMatrix = pred, method =  meth)
com<-complete(imp, "long", indlude=T)

我得到以下输出:

    .imp .id V1 V2 V3 V4 V5 V6 V7 V8 V9 V10  sumScore
 1    1   1  1  7  3  5  6  1  9  1 10   1   0.9224428
 2    1   2  6  5  3  2  7  3  3  9  5   9   0.6210974
 3    1   3  6  3  1  3  3  7  3  5  1   1   0.3563335
 4    1   4  6 10 NA  5  6  5  5  8  5   1   0.0711464
 5    1   5  9  3  2  1  3  1  2  3  2   1   0.7318026
 6    1   6  7  9  8  8  5  5  7  5  9   5   0.6197897

1 个答案:

答案 0 :(得分:1)

您的预测矩阵混乱了(我不确定rowSums上的df是否可以使用-我不这么认为,因为df是指原始数据而非估算版本)。

预测矩阵应如下所示:对于每一行,使用哪些变量(列)来预测此变量。您的矩阵看起来像这样

> pred
         V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 sumScore
V1        0  0  0  0  1  1  1  1  1   0        1
V2        0  0  0  0  1  1  1  1  1   1        1
V3        0  0  0  0  1  1  1  1  1   1        1
V4        0  0  0  0  1  1  1  1  1   1        1
V5        0  0  0  0  0  1  1  1  1   1        0
V6        0  0  0  0  1  0  1  1  1   1        0
V7        0  0  0  0  1  1  0  1  1   1        0
V8        0  0  0  0  1  1  1  0  1   1        0
V9        0  0  0  0  1  1  1  1  0   1        0
V10       0  0  0  0  1  1  1  1  1   0        0
sumScore  0  0  0  0  0  0  0  0  0   0        0

当一行仅包含零时,则表示没有使用 any 的变量进行插补。这意味着实际上没有任何变量可用于sumScore的预测,并且最终会产生随机噪声。

尝试使用此代码

library("mice")
library("lattice")
set.seed(1234)
m <- matrix(sample(c(NA, 1:10), 100, replace = TRUE), 10)
df <- cbind(as.data.frame(m), sumScore=NA)

ini<-mice(df, max = 0, print=FALSE)
meth<-ini$method
meth[1:4] <- ""      # Never impute for these variables
meth[5:10]<-"pmm"    # Use pmm to impute for these
meth["sumScore"] <- "~I(V5+V6+V7+V8+V9+V10)"

pred <- ini$predictorMatrix
pred[, 1:4] <- 0    # Never use V1-V4 for imputation (since you had the same)
pred[1:4, "sumScore"] <- 1  # Use the sum to impute for first 4 (but no method so no point!)
pred[paste0("V", 5:10), "sumScore"] <- 0  # Make sure that we dont impute the "wrong way"
pred["sumScore", paste0("V", 5:10)] <- 1  # Make sure that V5 to V10 are available for sumScore

这应该给您您想要的