【问题标题】:Imputation with mice - exclude variable from being imputed but still use as predictor用老鼠进行插补 - 排除变量被插补但仍用作预测器
【发布时间】:2020-10-24 02:33:27
【问题描述】:

所以我尝试估算一些丢失的数据,但出现了问题。我希望将三个变量用作预测变量,但我希望它们被估算。即使我将预测矩阵指定如下:

# Initialize
m0 = mice(datimp, maxit = 0)

predmat = m0$predictorMatrix
m0$predictorMatrix
m0$method

meth = m0$method
meth["cm.dep.0"] = "2l.pan"
meth["cm.dep.1"] = "2l.pan"
meth["cm.dep.2"] = "2l.pan"


# Group is added as a fixed effect to all variables
predmat[,"trial"] = -2
predmat[,"group"] = 1
predmat["sess",]= 0
predmat["depmed",]= 0
predmat["prevpsychoth",]= 0
predmat

             trial group sex age sess degree rel child depmed prevpsychoth cm.dep.0 cm.dep.1
trial           -2     1   1   1    1      1   1     1      1            1        1        1
group           -2     1   1   1    1      1   1     1      1            1        1        1
sex             -2     1   0   1    1      1   1     1      1            1        1        1
age             -2     1   1   0    1      1   1     1      1            1        1        1
sess             0     0   0   0    0      0   0     0      0            0        0        0
degree          -2     1   1   1    1      0   1     1      1            1        1        1
rel             -2     1   1   1    1      1   0     1      1            1        1        1
child           -2     1   1   1    1      1   1     0      1            1        1        1
depmed           0     0   0   0    0      0   0     0      0            0        0        0
prevpsychoth     0     0   0   0    0      0   0     0      0            0        0        0
cm.dep.0        -2     1   1   1    1      1   1     1      1            1        0        1
cm.dep.1        -2     1   1   1    1      1   1     1      1            1        1        0
cm.dep.2        -2     1   1   1    1      1   1     1      1            1        1        1
             cm.dep.2
trial               1
group               1
sex                 1
age                 1
sess                0
degree              1
rel                 1
child               1
depmed              0
prevpsychoth        0
cm.dep.0            1
cm.dep.1            1
cm.dep.2            0

最终sessdepmedprevpsychoth 正在被估算。任何想法为什么会发生这种情况?

【问题讨论】:

    标签: r missing-data imputation multi-level r-mice


    【解决方案1】:

    将列而不是行设置为零以及清空不可估算变量的方法应该可以工作。 来自micenhanes 数据集示例

    library(mice)
    m0 <- mice(nhanes, maxit=0)
    
    meth <- m0$method
    meth[names(meth) %in% c("bmi")] <- ""
    pred <- m0$predictorMatrix
    pred[, colnames(pred) %in% c("bmi")] <- 0   
    
    imp <- mice(nhanes, predictorMatrix=pred, method=meth)
    imp$imp
    complete(imp, "long")[1:25, ]
    
    imp <- mice(nhanes, predictorMatrix=pred, method=m0$method)
    

    结果

    complete(imp, "long")[1:25, ]
    #    .imp .id age  bmi hyp chl
    # 1     1   1   1   NA   1 238
    # 2     1   2   2 22.7   1 187
    # 3     1   3   1   NA   1 187
    # 4     1   4   3   NA   1 206
    # 5     1   5   1 20.4   1 113
    # 6     1   6   3   NA   1 184
    # 7     1   7   1 22.5   1 118
    # 8     1   8   1 30.1   1 187
    # 9     1   9   2 22.0   1 238
    # 10    1  10   2   NA   1 186
    # 11    1  11   1   NA   1 238
    # 12    1  12   2   NA   1 186
    # 13    1  13   3 21.7   1 206
    # 14    1  14   2 28.7   2 204
    # 15    1  15   1 29.6   1 238
    # 16    1  16   1   NA   1 238
    # 17    1  17   3 27.2   2 284
    # 18    1  18   2 26.3   2 199
    # 19    1  19   1 35.3   1 218
    # 20    1  20   3 25.5   2 199
    # 21    1  21   1   NA   1 187
    # 22    1  22   1 33.2   1 229
    # 23    1  23   1 27.5   1 131
    # 24    1  24   3 24.9   1 186
    # 25    1  25   2 27.4   1 186
    

    【讨论】:

    • 你的回答被接受了,@jay.sf,但据我了解,BMI 没有被用作预测变量(@Lea 假装她提到的三个变量被用作预测变量)。我对吗?我想和 Lea 一样,但 this issue 暗示,事实上,这是不可能的。
    • @MDSF 您似乎是对的,感谢您指出这一点。至少没有估算 BMI。
    • @MDSF 也许我们可以将 cbind 未估算的 BMI 转换为 complete 结果:cbind(complete(imp, "long"), rep(nhanes$bmi, imp$m)),当然无需将 BMI 预测值列设置为零,因此使用 BMI 作为预测值。
    猜你喜欢
    • 2020-08-10
    • 1970-01-01
    • 2019-08-04
    • 2019-04-18
    • 2018-04-29
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多