如何创建“宏" R中的回归变量? [英] How do I create a "macro" for regressors in R?

查看:115
本文介绍了如何创建“宏" R中的回归变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于长且重复的模型,我想创建一个宏"(在Stata中称呼,并用global var1 var2 ...完成),其中包含模型公式的 regressors .

For long and repeating models I want to create a "macro" (so called in Stata and there accomplished with global var1 var2 ...) which contains the regressors of the model formula.

例如来自

library(car)
lm(income ~ education + prestige, data = Duncan)

我想要类似的东西:

regressors <- c("education", "prestige")
lm(income ~ @regressors, data = Duncan)  

我可以找到这种方法.但是我在回归器上的应用程序无法正常工作

I could find is this approach. But my application on the regressors won't work:

reg = lm(income ~ bquote(y ~ .(regressors)), data = Duncan)

当它扔给我时:

Error in model.frame.default(formula = y ~ bquote(.y ~ (regressors)), data =
Duncan,  :  invalid type (language) for variable 'bquote(.y ~ (regressors))'

即使是相同问题的公认答案:

Even the accepted answer of same question:

lm(formula(paste('var ~ ', regressors)), data = Duncan)

罢工并向我展示:

Error in model.frame.default(formula = formula(paste("var ~ ", regressors)),
: object is not a matrix`. 

当然我尝试了as.matrix(regressors):)

那么,我还能做什么?

推荐答案

以下是一些替代方法.前3个未使用任何软件包.

Here are some alternatives. No packages are used in the first 3.

1)重新制定

fo <- reformulate(regressors, response = "income")
lm(fo, Duncan)

或者您可能希望这样写最后一行,以使输出中显示的公式看起来更好:

or you may wish to write the last line as this so that the formula that is shown in the output looks nicer:

do.call("lm", list(fo, quote(Duncan)))

在这种情况下,输出的Call:行将按预期显示,即:

in which case the Call: line of the output appears as expected, namely:

Call:
lm(formula = income ~ education + prestige, data = Duncan)

2)lm(数据框)

lm( Duncan[c("income", regressors)] )

输出的Call:行如下所示:

The Call: line of the output look like this:

Call:
lm(formula = Duncan[c("income", regressors)])

,但是我们可以使用以下代码使其与(1)中的do.call解决方案完全相同:

but we can make it look exactly as in the do.call solution in (1) with this code:

fo <- formula(model.frame(income ~., Duncan[c("income", regressors)]))
do.call("lm", list(fo, quote(Duncan)))

3)点

类似于@jenesaisquoi在评论中建议的替代方法是:

An alternative similar to that suggested by @jenesaisquoi in the comments is:

lm(income ~., Duncan[c("income", regressors)])

在(2)中讨论的Call:输出方法也可以在这里使用.

The approach discussed in (2) to the Call: output also works here.

4)fn $ 以fn $开头的函数会在其参数中启用字符串插值.此解决方案几乎与问题中显示的所需语法相同,使用$代替@来执行替换,并且灵活替换可以很容易地扩展到更复杂的场景.代码中的quote(Duncan)可以只写为Duncan,它仍然可以运行,但是如果使用quote(Duncan),则在lm输出中显示的Call:看起来会更好.

4) fn$ Prefacing a function with fn$ enables string interpolation in its arguments. This solution is nearly identical to the desired syntax shown in the question using $ in place of @ to perform substitution and the flexible substitution could readily extend to more complex scenarios. The quote(Duncan) in the code could be written as just Duncan and it will still run but the Call: shown in the lm output will look better if you use quote(Duncan).

library(gsubfn)

rhs <- paste(regressors, collapse = "+")
fn$lm("income ~ $rhs", quote(Duncan))

Call:行看起来与上面的do.call解决方案几乎相同-仅空格和引号不同:

The Call: line looks almost identical to the do.call solutions above -- only spacing and quotes differ:

Call:
lm(formula = "income ~ education+prestige", data = Duncan)

如果您希望它完全相同,则:

If you wanted it absolutely the same then:

fo <- fn$formula("income ~ $rhs")
do.call("lm", list(fo, quote(Duncan)))

这篇关于如何创建“宏" R中的回归变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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