R 中用于多元回归的映射函数
Map function in R for multiple regression
我的目标是 运行 使用另一个列表中的所有自变量对列表中的每个因变量进行多元回归。然后我想通过 AIC 为每个因变量存储最佳模型。
我在 this post 的指导下编写了以下函数。但是,我不想单独使用每个自变量,而是希望 运行 模型针对整个列表作为多元回归。
关于如何构建此功能的任何提示?
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
y
需要用 +
折叠,然后粘贴到 x
并且 y
需要作为向量传递给 [= 的每个值13=]
models <- lapply(dep, function(x, y)
step(lm(as.formula(paste(x, paste(y, collapse="+"))), data=mtcars),
direction="backward"), y = indep)
我的目标是 运行 使用另一个列表中的所有自变量对列表中的每个因变量进行多元回归。然后我想通过 AIC 为每个因变量存储最佳模型。
我在 this post 的指导下编写了以下函数。但是,我不想单独使用每个自变量,而是希望 运行 模型针对整个列表作为多元回归。
关于如何构建此功能的任何提示?
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
y
需要用 +
折叠,然后粘贴到 x
并且 y
需要作为向量传递给 [= 的每个值13=]
models <- lapply(dep, function(x, y)
step(lm(as.formula(paste(x, paste(y, collapse="+"))), data=mtcars),
direction="backward"), y = indep)