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

Erich Studerus erich.studerus at bli.uzh.ch
Thu Sep 23 15:53:45 CEST 2010


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


#Create multiple imputations on the nhanes data contained in the mice 
imp <- mice(nahnes)

#Fitting a linear model with each imputed data set the regular way works 
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,

More information about the R-help mailing list