用公式作为变量运行鼠标:即时评估而不是后期评估? [英] Running mice with a formula as a variable: instant evaluation instead of later evaluation?

查看:113
本文介绍了用公式作为变量运行鼠标:即时评估而不是后期评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R软件包mice附带以下示例:

The R package micecomes with following example:

library("mice")
imp <- mice(nhanes)
fit <- with(data=imp,exp=lm(bmi~hyp+chl))

我想要一个灵活的with()呼叫,例如:

I want a flexible call of with() like:

model_formula <- bmi~hyp+chl
fit <- with(data=imp,exp=lm(model_formula))

但是这会抛出Error in eval(predvars, data, env) : object 'bmi' not found.我搜索了类似的问题.我发现的壁橱问题是帮助了解错误在我在R 中定义的函数中. 我的印象是,编写exp=lm(model_formula)表达式lm(model_formula)会立即进行求值,但是编写exp = lm(bmi~hyp+chl)时不会立即对其求值-而是会在函数with.mice()中进行赋值?如果是这样,如何防止即时评估?

But this throws Error in eval(predvars, data, env) : object 'bmi' not found. I searched for similar problems. The closet problem I found was Help understand the error in a function I defined in R. My impression is, that writing exp=lm(model_formula) the expression lm(model_formula) is evaluted instantly, but when writing exp = lm(bmi~hyp+chl) it is not evaluated straight away - instead the eavluation will take place in the function with.mice()? And if so, how can I prevent instant evaluation?

推荐答案

@ user20650的评论是解决方案的线索.需要首先将公式更改为字符,这将通过format实现,然后再次使其成为公式:

The comment by @user20650 was the clue to the solution. It is needed to change the formula first to a character, which will be achieved by format, and made it then a formula again:

model_formula <- bmi~hyp+chl
fit <- with(data=imp,exp=lm(formula(format(model_formula))))

这篇关于用公式作为变量运行鼠标:即时评估而不是后期评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆