Erich Studerus
2010-Sep-23 13:53 UTC
[R] How to pass a model formula as argument to with.mids
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
Bert Gunter
2010-Sep-23 16:17 UTC
[R] How to pass a model formula as argument to with.mids
??? 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
Possibly Parallel Threads
- logistic regression with package 'mice'
- qr.solve and lm
- lattice::xyplot/ggplot2: plotting weighted data frames with lmline and smooth
- Help understanding why glm and lrm.fit runs with my data, but lrm does not
- Help understanding why glm and lrm.fit runs with my data, but lrm does not