How do I create a “macro” for regressors in R?

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

1) reformulate

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)))

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

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

2) lm(dataframe)

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

The Call: line of the output look like this:

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

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) dot

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

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

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

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))

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)))

Leave a Comment