问题
Let's do a quick 3-clusters classification on the iris dataset with the FactoMineR package:
library(FactoMineR)
model <- HCPC(iris[,1:4], nb.clust = 3)
summary(model$data.clust$clust)
1 2 3
50 62 38
We see that 50 observations are in cluster 1, 62 in cluster 2 and 38 in cluster 3.
Now, we want to visualize these 3 clusters in a dendrogram, with the package dendextend which enables to make pretty ones:
library(dendextend)
library(dplyr)
model$call$t$tree %>%
as.dendrogram() %>%
color_branches(k = 3, groupLabels = unique(model$data.clust$clust)) %>%
plot()
The problem is that the labels on the dendrogram don't meet the true labels of the classification. The cluster 2 should be the biggest one (62 observations according to the data), but on the dendrogram, we clearly see it is the smallest one.
I tried different thinks but nothing work for now, so if you have any idea of which input give to groupLabels =
in order to match the real labels, that would be great.
回答1:
Looking inside dendextend::color_branches
, we can see that group labels are assigned using the command g <- dendextend::cutree(dend, k = k, h = h, order_clusters_as_data = FALSE)
.
This fact can be used for building a map between the cluster labels assigned by HCPC
and group labels assigned by dendextend::color_branches
.
library(FactoMineR)
library(dendextend)
library(dplyr)
model <- HCPC(iris[,1:4], nb.clust = 3)
clust.hcpc <- as.numeric(model$data.clust$clust)
clust.cutree <- dendextend:::cutree(model$call$t$tree, k=3, order_clusters_as_data = FALSE)
idx <- order(as.numeric(names(clust.cutree)))
clust.cutree <- clust.cutree[idx]
( tbl <- table(clust.hcpc, clust.cutree) )
###########
clust.cutree
clust.hcpc 1 2 3
1 50 0 0
2 0 0 62
3 0 36 2
This table shows that cluster labels 2 and 3 are matched with group labels 3 and 2, respectively. (Surprisingly, for two sample units this rule is not true.)
The groups levels that need to be passed to dendextend::color_branches
can be found as follows:
( lbls <- apply(tbl,2,which.max) )
##############
1 2 3
1 3 2
Here is the dendrogram:
model$call$t$tree %>%
color_branches(k=3, groupLabels =lbls) %>%
set("labels_cex", .5) %>%
plot(horiz=T)
来源:https://stackoverflow.com/questions/44188612/r-dendrogram-grouplabels-not-match-real-labels-package-dendextend