Transform categorical variables for cluster analysis in R (mlr)?

Most clustering functions can't handle categorical variables. One way to deal with this is to transform them to binary integer variables (i.e. x in {0, 1}). This is straight forward for categorical variables with two categories (transformation to one binary integer variable with x in {0, 1}). Should I turn categorical variables with more than two categories into one binary integer variable per category, or should I also remove one category as reference category (which would mean that for some combinations of categories two binary variables have a different value and for others only one). Is there a sound theory behind that way of dealing with the issue of categorical variables when clustering (e.g. using kmeans) or is it only an emergency solution? Why does mlr not do this automatically (like for regression algorithms)?

• You can use the createDummyFeatures() function for this purpose. It doesn't happen automatically because you have a choice of method to use, or maybe you want to omit those features in your particular case. Not sure what you mean by "sound theory" -- cluster analysis is exploratory in nature, so there's no ground truth and no right or wrong answers. Sep 15 '17 at 16:04
• See stats.stackexchange.com/q/55798/3277. Usually there is no need to eliminate one dummy from the set in clustering task. Sep 17 '17 at 14:23

1 Answer

Dummy encoding categoricial variables is a popular hack, but in my experience it doesn't work too well.

Usually, it indicates that you are solving the wrong problem.

While e.g. k-means cannot work on categoricial variables, it doesn't work much better on binary variables either. The method assumes a continuous domain, where moving the mean by a small amount actually improves results. With binary variables, you get too many bad local "optima" that are all but optimal. But the real reason is that the data doesn't match the problem solved by the algorithm.

For clustering, ELKI is the best tool. MLR has very few algorithms, and most only delegate to the quite bad RWeka versions. ELKI is much faster and has many more algorithms. Although I don't remember anything for categoricial attributes if mixed data either. Maybe there just isn't anything that works reliably.