I want to use k-means to cluster my data. I have broken one column into 4 dummy variables and I have normalized all of the data to mean=0 and sd=1. Will k-means work with these dummy variables?

I have run the k-means in R and the results look pretty good, but are much more dependent on the value of these dummy variables than the rest of the data. My 'between_SS / total_ss' = 58%

Data Sample:

num_months, sales, dummy_a, dummy_b, dummy_c, dummy_d
10, 102.33, 1, 0, 0, 0
5.7, 57.5, 0, 0, 0, 1
21.3, 152.88, 0, 1, 0, 0



mydata <- read.csv("data.csv", stringsAsFactors = FALSE) 
data <- scale(data)

km <- kmeans(data, 4)     #Break into 4 clusters

##...combine the dummy variables into 1 field so I can use it as the 3rd dimension to graph it

results$color[results$cluster1==1] <- "red"
results$color[results$cluster2==1] <- "blue"
results$color[results$cluster3==1] <- "green"
results$color[results$cluster4==1] <- "orange"
with(results, {
    s3d <- scatterplot3d(num_months, sales, dummy_combined,       
                  color=color, pch=19)       
     s3d.coords <- s3d$xyz.convert(num_months, sales, dummy_combined)

edit: Here is some code for my comment below. It uses kmeans to cluster 3-dimensional data, 2 of which are binary data. It looks like it does a fine job clustering.

v1 <- c(runif(500, min = -10, -5), runif(500, min = 5, 10))
v2 <- round(runif(1000, min=0, max=1))
v3 <- round(runif(1000, min=0, max=1))
v1 <- scale(v1)
v2 <- scale(v2)
v3 <- scale(v3)

mat <- matrix(c(v1,v2),nrow=length(v1))

k <- kmeans(mat,4)

plot3d(v1, v2, v3,  size=7, col = k$cluster)        

enter image description here

  • 5
    $\begingroup$ It looks inappropiate, almost absurd to apply k-means clustering directly to the qualitative data - be they original nominal or dummy-recoded categories. Normalizing the dummies doesn't help it. Even with non-dummy binary variables k-means will be very much questionnable, because it is an issue what meaning the mean of a binary variable might have (see e.g. 1, 2). $\endgroup$
    – ttnphns
    Sep 28, 2015 at 16:46
  • 2
    $\begingroup$ K-means assumes continuous, numeric variables. Only this scale can have a real mean, a mean as a substantive value on the scale. Binary variables do not have such substantive mean, their "mean" has the meaning of proportion of cases falling into this or that category. Although, with a frawn or fear of critics, one might venture to do k-means on purely binary data, he would not do it on a mixture of continuous and binary data - because the two above meanings of the "mean" are incompatible. $\endgroup$
    – ttnphns
    Sep 29, 2015 at 14:51
  • 1
    $\begingroup$ In regards to your code. I'd be better if you show the results (pictures). Not everybody is R user here. $\endgroup$
    – ttnphns
    Sep 29, 2015 at 14:53
  • 1
    $\begingroup$ What about first using some form of multidimensional scaling (maybe multiple cotrrespondence analysis) and only then using $k$-means in the reconstructed representation space? I will try to add an example of this when I have time! $\endgroup$ Sep 29, 2015 at 18:09
  • 1
    $\begingroup$ @kjetil, sure, this is one of fine ways (but prone with losing much information, especially given that MDS, generally, better reconstructs large, between-cluster distances than small, within-cluster ones). But clustering can be effectively done on nominal/dummy data, using specialized similarities such as Dice and then hierarchical or DB methods; and it is unclear to me why the OP seems to stick to k-means. $\endgroup$
    – ttnphns
    Sep 29, 2015 at 18:50


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.