【问题标题】:Multiple imputation (mice) with glm: "argument to 'which' is not logical"使用 glm 进行多重插补(小鼠):“对 'which' 的论证不合逻辑”
【发布时间】:2020-11-14 19:23:14
【问题描述】:

我应该成功地使用mice 对数据框进行多重插补。我现在想在该数据集上运行 glm。我的结果变量是“MI”,我的自变量是“高血压”和“糖尿病”。 我试过了:

dat <- mice(regression)
model <- which(dat, glm(MI ~ Hypertension + Diabetes, family = "binomial"))

但我收到以下错误:

其中的错误(dat,glm(MI ~ 高血压 + 糖尿病,家庭 = “二项式”)):
'which' 的论点不合逻辑。

有人知道这是为什么吗?

【问题讨论】:

  • 对不起,我没有提到这一点......如果我运行你的代码,我会得到:'Error cannot coerce class'"mids"' to a data.frame'。
  • which() 在这里用错了。 which() 用于输出所有“TRUE”条件的索引号,例如询问“从 11 到 15 的哪些数字小于 13?” (即which(11:15 &lt; 13)),你会得到 1 和 2,因为 11:15 中的第一个和第二个数字小于 13,即 TRUE 和其余 FALSE)。您需要在 which() 中提供一个逻辑对象——即 TRUE 或 FALSE。
  • 我认为您想使用with() 而不是which()

标签: r glm imputation r-mice


【解决方案1】:

我认为您遇到错误是因为您使用的是which() 而不是with()which() 是一个函数,它会询问(用外行的话来说)“哪些这些值是真的?”你必须指定一些可以是真或假的东西。

with() 是一个类似于“使用这个数据集,评估其中的某些东西”的函数。您必须提供某种数据环境(例如,列表、数据框),并使用内部的向量,而无需再次命名该数据环境。

with() 可以像这样与mice 包一起使用:

# example data frame
set.seed(123)
df <- data.frame(
    MI = factor(rep(c(0,1),5)), 
    Hypertension = c(rnorm(9), NA), 
    Diabetes = c(NA, rnorm(9)))

# imputation
library(mice)

dat <- mice(df)

with(dat, glm(MI ~ Hypertension + Diabetes, family = "binomial"))

with(dat, glm(MI ~ Hypertension + Diabetes, family = "binomial")) 向您显示dat 中每个插补的 glm() 输出。 mice() 默认进行五次插补。

glm.mids() 的替代方案

为什么glm(MI ~ Hypertension + Diabetes, family = "binomial", data = dat) 不起作用?它给出了一个错误,“错误不能将类‘mids’强制转换为 data.frame”,因为估算的 dat 不是数据帧。

相反,mice 具有运行 glm() 与多变量插补数据(“mids”)的功能,glm.mids()

#glm(MI ~ Hypertension + Diabetes, family = "binomial", data = dat) # it does not work

glm.mids(MI ~ Hypertension + Diabetes, family = "binomial", data = dat) # it works

with(dat, glm(MI ~ Hypertension + Diabetes, family = "binomial")) # does the same thing

编辑备注

当您在使用mice 包时使用with(),我认为它实际上是从mice 包的“with.mids”中调用with(),这允许您将with() 与mice 包的“mids”数据类一起使用.它取代了glm.mids()。详情见这里:https://rdrr.io/cran/mice/man/with.mids.html

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 1970-01-01
    • 2018-08-11
    • 2019-08-04
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    相关资源
    最近更新 更多