how to model all relationships between independent variables in R?

末鹿安然 提交于 2019-12-24 18:31:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!