Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a "macro" for regressors in R?

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.

For example from

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

I want something like:

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)

as it throws me:

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)

strikes and shows me:

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

And of course I tried as.matrix(regressors) :)

So, what else can I do?

like image 679
jay.sf Avatar asked Oct 07 '17 00:10

jay.sf


Video Answer


2 Answers

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)))
like image 110
G. Grothendieck Avatar answered Oct 13 '22 08:10

G. Grothendieck


For the scenario you described, where regressors is in the global environment, you could use:

lm(as.formula(paste("income~", paste(regressors, collapse="+"))), data = 
Duncan)

Alternatively, you could use a function:

modincome <- function(regressors){
    lm(as.formula(paste("income~", paste(regressors, collapse="+"))), data = 
Duncan)  
}

modincome(c("education", "prestige"))
like image 8
Julia Wilkerson Avatar answered Oct 13 '22 06:10

Julia Wilkerson