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