# function for all possible regression models in model selection

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?

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

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.

• On could argue that "just use" should rather be replaced with "don't use": stats.stackexchange.com/questions/20836/…
– Tim
Apr 4 '17 at 8:17
• 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.
– Eli
Sep 21 '18 at 15:41
• This algorithm does not explore the "all possible subsets" of models requested in the question.
– whuber
Sep 21 '18 at 15:42