问题
I have a small data set with 4 independent (call them a, b, c, d) and 1 dependent variables. Since there are few independent variables, I want to explore all combinations of these variables. There can be only 14 models (a, b, c, d, a+b, a+c, a+d, b+c, b+d, c+d, a+b+c, a+b+d, b+c+d, a+b+c+d). I build all models by hand and it is time-consuming. Therefore I want to automatize it. Is it possible in R?
glm(dep ~ a, family = "binomial", data = data) glm(dep ~ b + c, family = "binomial", data = data) etc
回答1:
Using the builtin anscombe
data frame, this will find the best combination (although stepwise regression is discouraged these days in favour of lasso, elasticnet and other similar approaches based on penalization).
fm <- glm(y1 ~ x1 + x2 + x3 + x4, data = anscombe)
step(fm)
Also see the leaps and glmnet packages.
来源:https://stackoverflow.com/questions/56598834/how-to-model-all-relationships-between-independent-variables-in-r