问题
I am trying to run many regression models with all possible combinations of a set of independent variables.
In this example, I am interested in the coefficients of cyl
with all possible combinations of other variables listed in xlist
.
df <- mtcars
md <- "mpg ~ cyl"
xlist <- c("disp", "hp", "am")
n <- length(xlist)
# get a list of all possible combinations of xlist
comb_lst <- unlist(lapply(1:n, function(x) combn(xlist, x, simplify=F)), recursive = F)
# get a list of all models
md_lst <- lapply(comb_lst, function(x) paste(md, "+", paste(x, collapse = "+")))
# run those models and obtain coefficients for cyl
coefs <- unlist(lapply(md_lst, function(x) lm(as.formula(x),data=df)$coe[2]))
It works fine to get all coefficients for cyl
. However, I don't know how to get the p value corresponding to each of those coefficients.
pvalues <- lapply(md, function(x) lm(as.formula(x),data=df)$?[2]))
Any suggestions would be appreciated.
回答1:
Disclaimer:: This answer uses the developer version of manymodelr that I also happen to have written. You can then go on to choose only those variables you are interested in.
Map(function(x) manymodelr::extract_model_info(x,"p_value"),
lapply(md_lst, function(x) do.call(lm, list(formula = x, data=mtcars))))
# Just cyl
Map(function(x) manymodelr::extract_model_info(x,"p_value")["cyl"],
lapply(md_lst, function(x) do.call(lm, list(formula = x, data=mtcars))))
If you don't want to use packages:
Map(function(x) coef(summary(x))[,4]["cyl"],
lapply(md_lst, function(x) do.call(lm, list(formula = x, data=mtcars))))
Results::(first part)
[[1]]
(Intercept) cyl disp
4.022869e-14 3.366495e-02 5.418572e-02
[[2]]
(Intercept) cyl hp
1.620660e-16 4.803752e-04 2.125285e-01
[[3]]
(Intercept) cyl am
7.694408e-14 1.284560e-07 5.635445e-02
[[4]]
(Intercept) cyl disp hp
1.537198e-13 1.349044e-01 8.092901e-02 3.249519e-01
[[5]]
(Intercept) cyl disp am
2.026114e-12 2.823412e-02 1.544849e-01 1.610559e-01
[[6]]
(Intercept) cyl hp am
9.270924e-12 8.635578e-02 1.692706e-02 5.464020e-03
[[7]]
(Intercept) cyl disp hp am
3.724625e-11 2.800850e-01 4.760672e-01 4.416647e-02 2.520516e-02
回答2:
A simple approach for each coefficient of cyl
with no package:
pvalues <- lapply(md_lst, function(x) summary(lm(as.formula(x),data=df))$coefficients[,4])
[[1]]
(Intercept) cyl disp
4.022869e-14 3.366495e-02 5.418572e-02
[[2]]
(Intercept) cyl hp
1.620660e-16 4.803752e-04 2.125285e-01
[[3]]
(Intercept) cyl am
7.694408e-14 1.284560e-07 5.635445e-02
[[4]]
(Intercept) cyl disp hp
1.537198e-13 1.349044e-01 8.092901e-02 3.249519e-01
[[5]]
(Intercept) cyl disp am
2.026114e-12 2.823412e-02 1.544849e-01 1.610559e-01
[[6]]
(Intercept) cyl hp am
9.270924e-12 8.635578e-02 1.692706e-02 5.464020e-03
[[7]]
(Intercept) cyl disp hp am
3.724625e-11 2.800850e-01 4.760672e-01 4.416647e-02 2.520516e-02
Using broom::glance
for all:
pvalues <- lapply(md_lst, function(x) glance(summary(lm(as.formula(x),data=df)))$p.value)
[[1]]
[1] 1.057904e-09
[[2]]
[1] 3.161781e-09
[[3]]
[1] 1.093687e-09
[[4]]
[1] 5.053802e-09
[[5]]
[1] 3.060153e-09
[[6]]
[1] 4.790959e-10
[[7]]
[1] 2.540038e-09
来源:https://stackoverflow.com/questions/58213038/get-p-values-for-a-specific-variable-in-many-models-with-all-possible-combinatio