对来自小鼠(R)的插补数据子集运行glm.mids

对来自小鼠(R)的插补数据子集运行glm.mids,r,subset,r-mice,R,Subset,R Mice,当我尝试在mids插补对象的子集上运行glm.mids时,我得到一个错误: library(mice) imp2 = mice(nhanes) glm.mids( (hyp==2)~bmi+chl, data=imp2, subset=(age==1) ) 给出神秘的错误消息 "Error in eval(expr, envir, enclos) : ..1 used in an incorrect context, no ... to look in" 即使该语法适用于原始数据集上的常规g

当我尝试在
mids
插补对象的子集上运行
glm.mids
时,我得到一个错误:

library(mice)
imp2 = mice(nhanes)
glm.mids( (hyp==2)~bmi+chl, data=imp2, subset=(age==1) )
给出神秘的错误消息

"Error in eval(expr, envir, enclos) :
..1 used in an incorrect context, no ... to look in"
即使该语法适用于原始数据集上的常规
glm

glm( (hyp==2)~bmi+chl, data=nhanes, subset=(age==1) )

文档
?glm.mids
并没有明确说明
子集
,但说明可以将附加参数传递到
glm
。如果无法将
子集
glm.mids
一起使用,是否有一种好方法可以直接将
mids
列表对象子集

我已擅自重写了
glm.mids
。这有点混乱。这个问题似乎源于属性传递到glm的隐式性质

另请参见以下帖子:

库(鼠标)
glm.mids=函数(公式,族=高斯,数据,…)
{
调用解决方案是使用

with(data=imp2, exp=glm((hyp==2)~bmi+chl, family=binomial , subset=(age==1) ))

(我认为)您问题中的问题是在
glm.mids
函数中使用
。它们在函数参数中用于允许“向glm传递其他参数”但是,当
..
传递到
glm.mids
函数中的
glm
调用时,它们不会以这种方式进行处理。在
?glm
中,
..
是“对于glm:如果不直接提供,则用于形成默认控制参数的参数”。因此,附加参数将不起作用

要看到这一点,请简化函数

f1 <- function (formula, family = binomial, data, ...) 
{
 glm(formula, family = family, data = data, ...)
  }

f1(formula=((hyp==2)~bmi+chl), data=nhanes, subset=(age==2)) 
#Error in eval(expr, envir, enclos) : 
#  ..1 used in an incorrect context, no ... to look in
f2 <- function (formula, family = binomial, data, ...) 
{
  eval(substitute(glm(formula, family = family, data = data, ...)))
}

# This now runs
f2(formula=((hyp==2)~bmi+chl), data=nhanes, subset=(age==2))

# check
glm((hyp==2)~bmi+chl, data=nhanes, family="binomial", subset=(age==2))

使用
substitute
将替换函数环境中的参数(这需要更多细节-请随时更新)

尝试
使用(data=imp2,exp=glm((hyp==2)~bmi+chl,family=二项式,subset=(age==1)))
。我不知道为什么子集在
glm.mids
中不起作用。谢谢!为我工作。欢迎光临。我不确定,但我认为
glm.mids
中使用
代码不是很正确(至少在当前版本的R中是这样)。如果您更改
glm
调用,它会像您预期的那样工作(大约在
glm.mids
的一半)使用
eval(替换(glm
。看一看这里就会看一看。所有这些都将是一个很好的答案,我会接受:)
f2 <- function (formula, family = binomial, data, ...) 
{
  eval(substitute(glm(formula, family = family, data = data, ...)))
}

# This now runs
f2(formula=((hyp==2)~bmi+chl), data=nhanes, subset=(age==2))

# check
glm((hyp==2)~bmi+chl, data=nhanes, family="binomial", subset=(age==2))