r语言 - 使用 glm 进行多重插补(小鼠):"argument to 'which' is not logical"



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

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

但是我收到以下错误:

错误其中(dat, glm(MI ~ 高血压 + 糖尿病, 家庭 = "二项式")):
对"哪个"的参数是不合逻辑的。

有人知道这是为什么吗?

我认为您收到错误是因为您使用的是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不是数据帧。

相反,小鼠具有使用多变量插补数据("mids")运行glm()的函数,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

相关内容

最新更新