R: Model selection with categorical variables using leaps and glmnet I have a linear model containing a few continuous variables and four categorical variables, each represented by 12, 3, 4, and 5 dummy variables respectively. When using model selection criteria such as PRESS, Mallows's Cp, and BIC using the leaps package, the best model returned for each criterion contains only some of the dummy variables for each categorical variable. It is my understanding that this is not good practice, and either all or none of the dummies must be included in the model. Is there a way to have leaps treat the dummy variables for each categorical variable as one variable?
Also, could this method be extended to use with the glmnet package? I'm having the same issue with lasso and ridge regression.
EDIT: Is there a way to specify an lm object with a subset of independent variables to be treated as one?
 A: regsubsets (a function in the leaps package that also performs exhaustive model searches) can accept categorical variables that are not split out into dummy variables and, thus, treats them as groups of variables that are either all part of a model or not.  
For example, if Year has levels 2013, 2014  and Treatment has levels C,N,O I can run the following statement: 
> search_output<-regsubsets(y~Year+Treatment,data=stats_df, method="exhaustive")

Output:
Subset selection object
Call: regsubsets.formula(mu_ln ~ Year + Treatment, data = SS_stats_df, 
    nbest = 1, method = "exhaustive")
3 Variables  (and intercept)
           Forced in Forced out
Year2014       FALSE      FALSE
TreatmentN     FALSE      FALSE
TreatmentO     FALSE      FALSE
1 subsets of each size up to 3
Selection Algorithm: exhaustive

> summary(search_output)$which
  (Intercept) Year2014 TreatmentN TreatmentO
1        TRUE    FALSE       TRUE      FALSE
2        TRUE    FALSE       TRUE       TRUE
3        TRUE     TRUE       TRUE       TRUE

When faced with this same problem I found this post very helpful (my answer here is essentially an abbreviated version of the pertinent portion):
http://rstudio-pubs-static.s3.amazonaws.com/2897_9220b21cfc0c43a396ff9abf122bb351.html
And for recoding or converting to factors or renaming factors these posts are helpful:
https://stackoverflow.com/questions/5372896/recoding-variables-with-r
http://www.cookbook-r.com/Manipulating_data/Recoding_data/
