R - 鼠标 - 添加一列,将列与推算值相加

时间:2017-09-01 20:14:15

标签: r dataframe statistics sum imputation

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

具体来说,我的参与者使用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

1 个答案:

答案 0 :(得分:1)

在用户20650指出的更正后,这里是没有错误的代码!

    # 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)