我正在尝试使用一组自变量的所有可能组合来运行许多回归模型。
在此示例中,我对以下系数感兴趣cyl
与列出的其他变量的所有可能组合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 值。
pvalues <- lapply(md, function(x) lm(as.formula(x),data=df)$?[2]))
任何建议,将不胜感激。