# Random Forest: Predictors have more than 53 categories? [duplicate]

What is the solution when we want to apply the Random Forest function in R to a predictor with more than 53 categories?

> RandomForestPrediction=function(alpha){
+   d = sort(sample(nrow(MPS), nrow(MPS)*alpha))
+   train<-MPS[d,]
+   test<-MPS[-d,]
+   myNtree=1000
+   myMtry=5
+   myImportance=TRUE
+   mod2 = randomForest(factor(m.Decision)~.,data=train,tree=myNtree,mtry=myMtry,importance=myImportance)
+   fitted=predict(mod2,test,type="response")
+   return(table(fitted,test\$m.Decision))
+ }
> RandomForestPrediction(0.7)
Error in randomForest.default(m, y, ...) :
Can not handle categorical predictors with more than 53 categories.

• Do you actually have a categorical variable with more than 53 categories/levels? Maybe a numerical variable was converted into a factor variable? Inspect the dataset using str(MPS) to identify the variable that causes the problem. If you really have a categorical variable with more than 53 levels, you just cant use the randomForest function, I'm afraid. Jun 17 '15 at 9:35
• It's exactly the categorical variable, because I'm applying the Random Forest to the data set from the customers. And one predictor is Occupation with 73 levels. I can not take this variable down because it's known as a valuable predictor! Jun 17 '15 at 9:38
• Another possibility is to recode the 73 levels into binary dummy variables. Or maybe group the 73 occupations into smaller categories. Other than that, it is simply a limitation of the code of randomForest. Maybe there are other implementations of random forests in R that don't have that limitation. Jun 17 '15 at 9:55

• Set 1 : levels for $$N_{obs}>100$$ or ( $$25 + predictive value)