I have a linear regression model:

$Y~X_1+X_2+X_3+X_4+X_5+X_6+X_7+X_8+X_9$ and I need to create a function that find all possible models (e.g.

  • $Y = X_1+X_2+X_3+X_4$

  • $Y = X_2+X_3+X_4$

  • $Y = X_1+X_3+X_4+X_5+X_6$ etc) and then calculates each model's DIC values.

Can someone help me since I'm not experienced at programming?

  • 1
    $\begingroup$ Just out of curiousity, why do you want this technique? Is it to find the most overfit model? $\endgroup$
    – IWS
    Apr 4, 2017 at 7:29
  • $\begingroup$ It's in the context of my internship project where I have to use different model selection criteria for a given dataset $\endgroup$
    – j.erm
    Apr 4, 2017 at 9:35
  • $\begingroup$ See the dredge function in package MuMIn. $\endgroup$
    – Roland
    Apr 4, 2017 at 12:48
  • 1
    $\begingroup$ R has a package for all possible subsets: leaps (on CRAN) $\endgroup$ Sep 21, 2018 at 15:36

1 Answer 1


Just use a backward or forward method to do so.

Forward: Just add and remove all your independent variables and see how each variable affect the model. Then keep adding and subtracting variables until you got a good model.

Backward: Add all variables and then remove them one by one and readd them to evaluate your model.

  • 2
    $\begingroup$ On could argue that "just use" should rather be replaced with "don't use": stats.stackexchange.com/questions/20836/… $\endgroup$
    – Tim
    Apr 4, 2017 at 8:17
  • $\begingroup$ Fitting every model and using a criterion (AIC, BIC, BICc) is a valid way to do model selection for a small number of predictors. Backwards and forward variable selection have fallen out of favor because they produce highly variable results. $\endgroup$
    – Eli
    Sep 21, 2018 at 15:41
  • $\begingroup$ This algorithm does not explore the "all possible subsets" of models requested in the question. $\endgroup$
    – whuber
    Sep 21, 2018 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.