2

My goal is to run a multiple regression on each dependent variable in a list, using all of the independent variables in another list. I then would like to store the best model for each dependent variable by AIC.

I have written the below function guided by this post. However, instead of employing each independent variable individually, I'd like to run the model against the entire list as a multiple regression.

Any tips on how to build this function?

dep<-list("mpg~","cyl~","disp~") # list of unique dependent variables with ~ 
indep<-list("hp","drat","wt")  # list of first unique independent variables 
models<- Map(function(x,y) step(lm(as.formula(paste(x,paste(y),collapse="+")),data=mtcars),direction="backward"),dep,indep)

Start:  AIC=88.43
mpg ~ hp

        Df     Sum of Sq     RSS     AIC
<none>                      447.67  88.427
- hp     1      678.37     1126.05 115.943
Start:  AIC=18.56
cyl ~ drat

        Df     Sum of Sq     RSS    AIC
<none>                      50.435 18.558
- drat   1      48.44       98.875 38.100
Start:  AIC=261.74
disp ~ wt

        Df     Sum of Sq     RSS    AIC
<none>                      100709 261.74
- wt     1      375476      476185 309.45
[[1]]

Call:
lm(formula = mpg ~ hp, data = mtcars)

Coefficients:
(Intercept)           hp  
30.09886          -0.06823  


[[2]]

Call:
lm(formula = cyl ~ drat, data = mtcars)

Coefficients:
(Intercept)         drat  
14.596            -2.338  


[[3]]

Call:
lm(formula = disp ~ wt, data = mtcars)

Coefficients:
(Intercept)           wt  
 -131.1             112.5  
Community
  • 1
  • 1
Trevor McCormick
  • 366
  • 1
  • 3
  • 12

1 Answers1

2

The y needs to be collapsed with + and then pasted to the x and y needs to be passed as a vector to each value of x

models <- lapply(dep, function(x, y)
    step(lm(as.formula(paste(x, paste(y, collapse="+"))), data=mtcars), 
         direction="backward"), y = indep)
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • Thanks! This seems to work. Just for my understanding, `lapply` takes each element in `dep` and applies a function to it, and returns a list the length of `dep`. – Trevor McCormick Jun 10 '15 at 16:49
  • 1
    Yea, so the `x` in the anonymous function correspond to elements of `dep`. `lapply` also also you to pass further arguments to the function by tacking them on after the function definition, which is where `y = indep` appears above. – Rorschach Jun 10 '15 at 16:54