我的目标是使用另一个列表中的所有自变量对列表中的每个因变量进行多重回归。然后我想通过 AIC 为每个因变量存储最佳模型。
我在这篇文章的指导下编写了以下函数。但是,我不想单独使用每个自变量,而是将模型作为多元回归针对整个列表运行。
有关如何构建此功能的任何提示?
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