在许多模型中使用其他自变量的所有可能组合获取特定变量的p值 [英] Get p values for a specific variable in many models with all possible combinations of other independent variables

查看:65
本文介绍了在许多模型中使用其他自变量的所有可能组合获取特定变量的p值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一组自变量的所有可能组合来运行许多回归模型.

I am trying to run many regression models with all possible combinations of a set of independent variables.

在此示例中,我对cyl的系数以及xlist中列出的其他变量的所有可能组合感兴趣.

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]))

获得cyl的所有系数都很好.但是,我不知道如何获取与每个系数相对应的p值.

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.

推荐答案

免责声明:: 此答案使用

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))))

如果您不想使用软件包:

Map(function(x) coef(summary(x))[,4]["cyl"],
  lapply(md_lst, function(x) do.call(lm, list(formula = x, data=mtcars))))

结果::(第一部分)

[[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 

这篇关于在许多模型中使用其他自变量的所有可能组合获取特定变量的p值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆