[英]How to run all possible combinations in multiple linear regression model in R
I have a data set of 7 variables and I want to run all possible combinations.我有一个包含 7 个变量的数据集,我想运行所有可能的组合。 What I exactly want is to run is different equations which choose different variables, for instance:
我真正想要的是运行的是选择不同变量的不同方程,例如:
Y = b_0 + b_1*X_1 + b_2*X_2
Y = b_0 + b_1*X_1 + b_2*X_3
Y = b_0 + b_1*X_1 + b_2*X_2 + b_3*X_3
Y = b_0 + b_1*X_1 + b_2*X_2 + b_3*X_3 + b_2*X_4
Y = b_0 + b_1*X_1 + b_2*X_2 + b_2*X_4
Y = b_0 + b_1*X_1 + b_2*X_4
All possible combination in this order.按此顺序所有可能的组合。 How should I setup my loop function?
我应该如何设置循环功能?
Generate example data:生成示例数据:
dat <- data.frame(
Y = rnorm(100),
X_1 = rnorm(100),
X_2 = rnorm(100),
X_3 = rnorm(100),
X_4 = rnorm(100),
X_5 = rnorm(100),
X_6 = rnorm(100),
X_7 = rnorm(100)
)
Find all 1 through 7 combinations of variables and paste them into a formula with Y as dependent variable:找出所有 1 到 7 个变量组合,并将它们粘贴到以 Y 作为因变量的公式中:
variables <- colnames(dat)[2:ncol(dat)]
formulas <- list()
for (i in seq_along(variables)) {
tmp <- combn(variables, i)
tmp <- apply(tmp, 2, paste, collapse="+")
tmp <- paste0("Y~", tmp)
formulas[[i]] <- tmp
}
formulas <- unlist(formulas)
formulas <- sapply(formulas, as.formula)
Estimate 127 regression models:估计 127 个回归模型:
models <- lapply(formulas, lm, data=dat)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.