简体   繁体   English

在来自小鼠(R)的部分估算数据上运行glm.mids

[英]Run glm.mids on a subset of imputed data from mice (R)

I get an error when I try to run glm.mids on a subset of a mids imputation object: 我得到一个错误,当我尝试运行glm.mids上的一个子集mids归集对象:

library(mice)
imp2 = mice(nhanes)
glm.mids( (hyp==2)~bmi+chl, data=imp2, subset=(age==1) )

gives the cryptic error message 给出错误的错误信息

"Error in eval(expr, envir, enclos) :
..1 used in an incorrect context, no ... to look in"

even though the syntax works with regular glm on the original dataset: 即使语法可以在原始数据集上使用常规glm进行操作:

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

The documentation ?glm.mids doesn't specifically address subset but says that you can pass additional parameters onto glm . 文档?glm.mids并未专门解决subset但说您可以将其他参数传递给glm If I can't use subset with glm.mids , is there a good way to subset the mids list object directly? 如果我不能在glm.mids使用subset ,是否有一个很好的方法直接将mids列表对象子集?

I have taken the liberty of rewriting glm.mids . 我已经拥有重写glm.mids的自由。 It is a bit kludgy. 这有点糊涂。 The issue seems to stem from the implicit nature by which attributes are passed into glm. 问题似乎源于将属性传递给glm的隐式本质。

also see these post: 另请参阅以下帖子:

https://stat.ethz.ch/pipermail/r-help/2003-November/041537.html https://stat.ethz.ch/pipermail/r-help/2003-November/041537.html

http://r.789695.n4.nabble.com/Question-on-passing-the-subset-argument-to-an-lm-wrapper-td3009725.html http://r.789695.n4.nabble.com/Question-on-passing-the-subset-argument-to-an-lm-wrapper-td3009725.html

library(mice)

glm.mids=function (formula, family = gaussian, data, ...) 
{
  call <- match.call()
  if (!is.mids(data)) 
    stop("The data must have class mids")
  analyses <- as.list(1:data$m)
  for (i in 1:data$m) {
    data.i <- complete(data, i)
    analyses[[i]] <- do.call("glm",list(formula=quote(formula),family=quote(family),data=quote(data.i),...))
  }
  object <- list(call = call, call1 = data$call, nmis = data$nmis, 
                 analyses = analyses)
  oldClass(object) <- c("mira", "glm", "lm")
 return(object)
}

imp2 = mice(nhanes)
glm.mids( (hyp==2)~bmi+chl, data=imp2 ,subset=quote(age==1))

The only part that I rewrote was the glm function call within glm.mids analyses[[i]] <- do.call("glm",list(formula=quote(formula),family=quote(family),data=quote(data.i),...)) 我重写的唯一部分是glm.mids analyses[[i]] <- do.call("glm",list(formula=quote(formula),family=quote(family),data=quote(data.i),...)) -do.call analyses[[i]] <- do.call("glm",list(formula=quote(formula),family=quote(family),data=quote(data.i),...))

In the old version it read analyses[[i]] <- glm(formula, family = family, data = data.i,...) 在旧版本中,它读取analyses[[i]] <- glm(formula, family = family, data = data.i,...)

Solution is to use 解决办法是用

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


(I think) the problem in your question is the use of ... within the glm.mids function. (我认为)您问题中的问题是在glm.mids函数中使用... They are used in the function argument to allow “Additional parameters passed to glm”. 它们在函数参数中使用,以允许“将其他参数传递给glm”。 However, when ... are passed to the glm call in the glm.mids function they are not processed this way. 但是,当在glm.mids函数glm.mids ...传递给glm调用时,不会以这种方式处理它们。 In ?glm the ... are “For glm: arguments to be used to form the default control argument if it is not supplied directly.”. ?glm...是“对于glm:如果未直接提供,则用于形成默认控制参数的参数”。 So the additional arguments will not work. 因此,其他参数将不起作用。

To see this, simplify the function 要看到这一点,简化功能

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

So the subset argument is not passed to the glm function call 因此子集参数不会传递给glm函数调用

Using the answer from R : Pass argument to glm inside an R function we can slightly alter the function 使用R的答案:在R函数内部将参数传递给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))

The use of substitute will substitute the arguments from the function environment (This needs more details - please feel free to update) 使用substitute将替换函数环境中的参数(这需要更多详细信息-请随时更新)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM