问题
Working with a large number of variables and addressing them with constructed formula (via paste0()
) using variables passed to functions. I have stumbled across a problem/bug I cannot figure out. Easiest explained with a toy example:
library(mice)
imp2 = mice(nhanes)
# So both these models run fine:
mod1 <- glm(bmi ~ hyp + age, data=nhanes)
mod1.im <- with(imp2, glm(bmi ~ hyp + age))
# However if I try to pass a formula to glm() in the with() I get an error
formula = bmi ~ hyp + age
mod2 <- glm(formula, data=nhanes)
mod2 <- with(imp2, glm(formula))
#Running the above leads to the following error:
> mod2 <- with(imp2, glm(formula))
Error in eval(expr, envir, enclos) : object 'bmi' not found
How can I work around this? Why is it not finding BMI ?
来源:https://stackoverflow.com/questions/38358402/how-to-use-constructed-formula-with-glm-mids