使用传递给函数的变量处理大量变量并使用构造公式(通过paste0()
)对它们进行寻址。我偶然发现了一个我无法弄清楚的问题/错误。最容易用玩具示例解释:
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
我该如何解决这个问题?为什么找不到BMI?