How to use constructed formula with glm.mids

故事扮演 提交于 2019-12-24 17:15:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!