gpt4 book ai didi

R - 鼠标 - 添加一个列,该列对具有估算值的列求和

转载 作者:行者123 更新时间:2023-12-02 03:01:19 28 4
gpt4 key购买 nike

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

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

最佳答案

这是在 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)

关于R - 鼠标 - 添加一个列,该列对具有估算值的列求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46007409/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com