1

That is not a duplicate of this (Edit and reuse the formula part of the call for a model in R) but rather an extension.

So let's say I have several regression models to test. I also have a set of socio demographic variables that I want to add to each model to control for them.

m1 <- lm(a~b)
m1_full <- lm(a~b+age+gender+income)
m2 <- lm(c~d)
m2_full <- lm(c~d+age+gender+income)

Is there a way to store (age+gender+income) and add it to some models?

Something like that (in pseudocode):

ses <- age+gender+income
m1 <- lm(a~b)
m1_full <- lm(a~b+... ses)
m2 <- lm(c~d)
m2_full <- lm(c~d+...ses)
Philipp Chapkovski
  • 1,949
  • 3
  • 22
  • 43

1 Answers1

1

I guess you can just paste everything together into a formula. Here's a function that would do the whole process:

set.seed(0)
df <- data.frame(age = rpois(200,45),
                 gender = rbinom(200,1,0.5),
                 income = rnorm(200,30000,10000),
                 a = rnorm(200),
                 b = rnorm(200),
                 c = rnorm(200),
                 d = rnorm(200))
ses <- c("age", "gender", "income")
get_lm_model <- function(df, outcome_var, pred_var, ses) {
    fm <- as.formula(paste(outcome_var, "~", pred_var, "+", paste(ses, collapse = " + ")))
    lm(fm, data = df)
} 

get_lm_model(df, "a", "b", ses)
#> 
#> Call:
#> lm(formula = fm, data = df)
#> 
#> Coefficients:
#> (Intercept)            b          age       gender       income  
#>   3.345e-01   -9.516e-02   -3.748e-03   -7.033e-02   -6.718e-06
get_lm_model(df, "c", "d", ses)
#> 
#> Call:
#> lm(formula = fm, data = df)
#> 
#> Coefficients:
#> (Intercept)            d          age       gender       income  
#>   2.775e-01    5.570e-02   -4.564e-03   -5.359e-02   -5.898e-06

Created on 2021-10-26 by the reprex package (v2.0.1)

csgroen
  • 2,511
  • 11
  • 28