[R] How to pass a model formula as argument to with.mids

Bert Gunter gunter.berton at gene.com
Thu Sep 23 18:17:54 CEST 2010


??? Use lm's data argument to do this, not the with() construction:

> x <- 1:10
> y <- 2*x+5 +rnorm(10)
> form <- formula(y~x)
> class(form)
[1] "formula"
> lm(form)

Call:
lm(formula = form)

Coefficients:
(Intercept)            x
      5.747        1.921

> dat <- data.frame(x=x,y=y)
> rm(x,y)
> lm(form,data=dat)

Call:
lm(formula = form, data = dat)

Coefficients:
(Intercept)            x
      5.747        1.921



On Thu, Sep 23, 2010 at 6:53 AM, Erich Studerus
<erich.studerus at bli.uzh.ch> wrote:
>  Hello
>
> I would like to pass a model formula as an argument to the with.mids
> function from the mice package. The with.mids functon fits models to
> multiply imputed data sets.
>
> Here's a simple example
>
> library(mice)
>
> #Create multiple imputations on the nhanes data contained in the mice
> package.
> imp <- mice(nahnes)
>
> #Fitting a linear model with each imputed data set the regular way works
> fine
> with(imp, lm(bmi~hyp+chl))
>
> #Creating a formula object and than passing it as argument does not work:
> form.obj <- formula(bmi~hyp+chl)
> with(imp, lm(form.obj))
>
> #The following doesn't work either
> expr <-"lm(bmi~hyp+chl)"
> with(imp, expr)
>
> Looking at the definition of with.mids reveals that the second argument is
> first substituted and than evaluated within each data.frame of the multiply
> imputed data sets. Is there a way to pass "lm(bmi~hyp+chl)" without having
> to change the definition of the with.mids function?
>
> Thanks in advance,
> Erich
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Bert Gunter
Genentech Nonclinical Biostatistics



More information about the R-help mailing list