【问题标题】:R - mice - adding a column that sums columns with imputed valuesR - 老鼠 - 添加一列,将列与推算值相加
【发布时间】:2018-02-10 22:36:34
【问题描述】:

我有一个缺少数据的数据库。我需要估算数据(我正在使用鼠标),然后根据原始列创建新列(使用估算数据)。我需要使用这些新列进行统计分析。

具体来说,我的参与者使用 7 点李克特量表填写了几份问卷。有些人没有回答所有问题。我需要估算值,然后 1- 对列中的值求和,并可以访问此新值进行统计分析 2- 根据这个总和,将参与者分为“轻度、中度、高度”,并将其用于统计分析。

我已经根据这个 stackoverflow 的答案来做我想做的事情: Perform operation on each imputed dataset in R's MICE

这是我的代码(使用 R):

# Create a sample bdd
bdd=data.frame(
    gender=c("M","F","M", "M", "M", "F"),
    choice=c(1,2,NA,1,1,1),
    gardes=c(0,0,0,5,7,NA),
    EE1=c(3,4,1,NA,3,0),
    EE2=c(2,5,1,3,3,0),
    EE3=c(3,NA,1,5,3,0),
    EE4=c(3,6,1,2,3,0),
    EE5=c(1,4,1,2,3,5),
    EE6=c(3,1,1,3,3,4),
    EE7=c(5,0,1,5,3,5),
    EE8=c(2,6,1,1,3,3),
    EE9=c(3,4,1,6,3,4)
    )

# Create the additional variable - this will have missing values
bdd$EE <- bdd$EE1+bdd$EE2+bdd$EE3+bdd$EE4+bdd$EE5+bdd$EE6+bdd$EE7+bdd$EE8+bdd$EE9

# create ini to get access to meth and pred
ini <- mice(bdd, max = 0, print = FALSE)

# Change the method of imputation for EE, so that it always equals bdd$EE1+...+bdd$EE9
meth1 <- ini$meth
meth1["EE"] <- "~I(bdd$EE1+bdd$EE2+bdd$EE3+bdd$EE4+bdd$EE5+bdd$EE6+bdd$EE7+bdd$EE8+bdd$EE9)"

pred1 <- ini$pred  
# change the predictor matrix so only bdd$EE1-9 predicts EE (necessary?)
pred1[ "EE", ] <- 0 
pred1[ "EE", c("EE1", "EE2", "EE3", "EE4", "EE5", "EE6", "EE7", "EE8", "EE9")] <- 1
# change the predictor matrix so that EE isnt used to predict
pred1[ , "EE" ] <- 0  


# Imputations
imput <- mice(bdd, seed=1, pred = pred1, meth = meth1, m=1, print = FALSE)

请注意,这不起作用。还有其他方法可以优雅地做到这一点吗? TIA 提供任何和所有建议!!!

编辑添加:这是我尝试运行此代码时收到的错误消息:

Warning messages:
1: In `[<-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
    replacement element 1 has 456 rows to replace 2 rows
2: In `[<-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
    replacement element 1 has 456 rows to replace 2 rows
3: In `[<-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
    replacement element 1 has 456 rows to replace 2 rows
4: In `[<-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
    replacement element 1 has 456 rows to replace 2 rows
5: In `[<-.data.frame`(`*tmp*`, , i, value = list(`1` = c(20L, 14L,  :
    replacement element 1 has 456 rows to replace 2 rows

这是我为这个问题创建的 bdd:

      gender choice gardes EE1 EE2 E3 EE4 EE5 EE6 E7 EE8 EE9
1      M      1      0   3   2  3   3   1   3  5   2   3
2      F      2      0   4   5 NA   6   4   1  0   6   4
3      M     NA      0   1   1  1   1   1   1  1   1   1
4      M      1      5  NA   3  5   2   2   3  5   1   6
5      M      1      7   3   3  3   3   3   3  3   3   3
6      F      1     NA   0   0  0   0   5   4  5   3   4

【问题讨论】:

  • 您好,欢迎来到 SO,您能添加一些带有缺失值的示例数据吗?也许只有几行bdd?没有测试数据很难调试代码
  • 谢谢 Nate - 我马上就去 :)
  • 快速浏览一下,不要在插补边界调用中使用您的数据框,即将"~I(bdd$EE1+bdd$EE2 ... 更改为"~I(EE1+EE2...
  • 非常感谢 user20650!是的,那是我的错误!我花了很多时间试图弄清楚,但没有看到这一点。

标签: r dataframe statistics r-mice


【解决方案1】:

这是没有bug的代码,经过user20650指出的更正!

    # Create a sample bdd
bdd=data.frame(
    gender=c("M","F","M", "M", "M", "F"),
    choice=c(1,2,NA,1,1,1),
    gardes=c(0,0,0,5,7,NA),
    EE1=c(3,4,1,NA,3,0),
    EE2=c(2,5,1,3,3,0),
    EE3=c(3,NA,1,5,3,0),
    EE4=c(3,6,1,2,3,0),
    EE5=c(1,4,1,2,3,5),
    EE6=c(3,1,1,3,3,4),
    EE7=c(5,0,1,5,3,5),
    EE8=c(2,6,1,1,3,3),
    EE9=c(3,4,1,6,3,4)
    )

# Create the additional variable - this will have missing values
bdd$EE <- bdd$EE1+bdd$EE2+bdd$EE3+bdd$EE4+bdd$EE5+bdd$EE6+bdd$EE7+bdd$EE8+bdd$EE9

# create ini to get access to meth and pred
ini <- mice(bdd, max = 0, print = FALSE)

# Change the method of imputation for EE, so that it always equals bdd$EE1+...+bdd$EE9
meth1 <- ini$meth
meth1["EE"] <- "~I(EE1+EE2+EE3+EE4+EE5+EE6+EE7+EE8+EE9)"

pred1 <- ini$pred  
# change the predictor matrix so only bdd$EE1-9 predicts EE (necessary?)
pred1[ "EE", ] <- 0 
pred1[ "EE", c("EE1", "EE2", "EE3", "EE4", "EE5", "EE6", "EE7", "EE8", "EE9")] <- 1
# change the predictor matrix so that EE isnt used to predict
pred1[ , "EE" ] <- 0  


# Imputations
imput <- mice(bdd, seed=1, pred = pred1, meth = meth1, m=1, print = FALSE)

【讨论】:

  • 请注意,我不能 100% 确定更改预测矩阵的代码是否正确。如果您想使用此代码,请先仔细检查。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多