I'd have 10 groups and hundreds of observations per group. In this toy example I only have 3 groups with 20 observations each.

I am looking to see if groups are similar so I'm using kmeans to cluster them using 3 variables (x1, x2 and x3).

My questions is, when you have multiple observations per group how do determine the cluster a group is in given that not all 20 observations will be in the same cluster.

AS an example, the below example using all the 20 rows of data for each group and passes it to k means. As you can see not all of the 20 points end up in the same cluster for each group. Some of group A's points are in cluster 1 and some and in cluster 2. The goal it put group A into a single cluster. How can this be done using the individual row level data? Are there weighting schemes or furhter clustering techniques? i.e. if 51% of group A's points are in cluster 3 then call it cluster 3? Are there any paper on this problem?


 ###########  CLUSTERING AT THE ROW level...points for a group are NOT all in the same cluster. How to handle that?
    data = data.frame(group = rep(c("A","B","C"),each =20 ) ,x1 = rnorm(60),x2= rnorm(60),x3=rnorm(60) )
    k = kmeans( data %>% select(-group),centers = 2,  iter.max = 10, nstart = 1)
    data$cluster = k$cluster
    ggplot(data ,aes(x=x1 ,y = x2,color = factor(cluster) ,shape = group ))+geom_point()

Alternatively, you can aggregated the 20 points for each group to avoid confronting the problem above. In this case each group has gets average x1 ,x2, and x3 value and those values are passed to kmeans. This works but it loses a lot of the information of the individual level data.

# clustering at the aggregated level
data = data.frame(group = rep(c("A","B","C"),each =20 ) ,x1 = rnorm(60),x2= rnorm(60),x3=rnorm(60) )

data_aggregated = data %>% group_by(group) %>% summarise( avg_x1= mean(x1), avg_x2= mean(x2) , avg_x3= mean(x3)   )
k = kmeans( data_aggregated %>% select(-group),centers = 2,  iter.max = 10, nstart = 1)
data_aggregated$cluster = k$cluster
ggplot(data_aggregated ,aes(x=avg_x1 ,y = avg_x2,color = factor(cluster) ,shape = group ))+geom_point()

Any idea how to handle the first case or how the decision to use row level data or aggregated is handled?

Thank you.

  • $\begingroup$ Is it that you want to cluster points but add the restriction that points of the same group must fall in the same cluster? Or to put it equivalently, you want to cluster groups still you don't want for that to summarize points of a group into a single "average" point? $\endgroup$
    – ttnphns
    Mar 15 '18 at 18:57
  • $\begingroup$ I want to cluster groups. Each group should only get 1 cluster. that can easily be done by averaging the group data and then clustering like in my second example. But that excluded a lot of information at the lowest level and I was wondering how , instead of averaging, one can use all the points to put a group into a cluster. You'd have to deal with individual data points for a group being in different clusters like in my first example. $\endgroup$ Mar 15 '18 at 19:19
  • $\begingroup$ Of your comment, I didn't quite get 2nd sentence. If you "cluster groups" then groups (say) A, C will form cluster 1; groups B, D, F will form cluster 2, etc. My initial comment implies that. Did my comment described correctly what you want? $\endgroup$
    – ttnphns
    Mar 15 '18 at 19:27
  • $\begingroup$ Your comment correctly describes what I am looking for. Basically using averaging loses the information of all 20 data points for each group BUT using all 20 data points for each groups creates the problem that all 20 points will likely not be in the same cluster. $\endgroup$ Mar 15 '18 at 19:37
  • 1
    $\begingroup$ Hierarchical clustering can naturally do what you want. First, clustering is done within groups (= on subsamples). Then it is resumed to merge groups; when the distance matrix is computed between groups, all points are involved in that. The idea, as I've said, is straightforward. $\endgroup$
    – ttnphns
    Mar 15 '18 at 19:48

Kmeans is to limited for almost anything. I do not get why people use it all the time. The even older Hierarchical Clustering (HAC) is so much more powerful.

You'll need to define the similarity or distance of groups. This could be the distance of the means, but also the mean of the distances of any two samples, or the mean distance of each instance to the nearest insurance of the other group. This is your choice. Then compute a distance matrix and run HAC.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.