2

处理大量变量并paste0()使用传递给函数的变量通过构造公式 (via) 处理它们。我偶然发现了一个我无法弄清楚的问题/错误。用一个玩具例子最容易解释:

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?

4

0 回答 0