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

前端 未结 2 1538
甜味超标
甜味超标 2020-11-29 11:38

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 regresso

相关标签:
2条回答
  • 2020-11-29 12:09

    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)))
    
    0 讨论(0)
  • 2020-11-29 12:11

    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"))
    
    0 讨论(0)
提交回复
热议问题