# How to handle categorical predictors with too many levels? [duplicate]

I think it may be a problem if we directly use dummy variable for a categorical predictor having hundreds of levels.

I have found one solution from the book 'Elements of Statistical Learning' (p.329). The solution is mentioned in classification tree sections. Specifically, the solution orders the levels of the categorical predictor by the number of occurrence of each level in one class, and then treats the predictor as an ordered predictors.

I wonder for models other than classification tree, such as linear regression, what would be proper ways of handling categorical predictors with too many levels.

I found an old post asking similar questions, but no answers have been posted:

• 1) Can you provide us with the categorical predictor of interest (or a similar example) so that we know what these "hundreds of levels" mean? 2) What is the distribution of the outcome throughout these levels? Thanks! Aug 21, 2013 at 5:13
• Suppose we have a data-set collecting the answer (Yes or No) to a question from participants of different regions, represented by their county name. Is it possible to add the factor county name as a predictor to predict the probability of people answering 'Yes'? I just make up this example and I hope it makes sense. Aug 21, 2013 at 5:50
• Your country-scenario seems perfectly suited for a multilevel aka hierarchical aka mixed model, where participants would be located on level 1 and countries on level 2 treating country as a random factor. If the example you gave matches your actual research question, go for multilevel; if not, maybe you can provide an example closer to your question? Aug 21, 2013 at 6:23
• @hajöpe, I could understand your reasoning of using mixed model, because I tend to believe the collected response will have correlations within each county. And using the county as random effect term could model such correlation. But if I use it as random effect I would be estimating the variance of the random effect (suppose I add the effect as the intercept, which is assumed normal distribution), so I could not give prediction on specific county. Also, can you explain a bit more why using random effect model could address the questions of predictors with too many levels? Aug 21, 2013 at 7:35
• You can add country-level predictors to your model (e.g., size, wealth, continent), but these can - without another superordinate level - only be estimated as fixed effects, i.e., the slope is modeled for ALL countries. Having hypotheses and models for every single country probably makes no sense. Either focus on a few, or build one multilevel model including all countries with broader hypotheses (e.g., country wealth increases the prob of people answering 'yes'). Aug 21, 2013 at 8:09

Shrinkage is necessary to deal with this problem, either by using penalized maximum likelihood estimation (e.g., R rms package's ols and lrm functions for quadratic (ridge) L2 penalty) or using random effects. You can get predictions for individual levels easily using penalized maximum likelihood estimation, or by using BLUPS in the mixed effects modeling context.