繁体   English   中英

如何使用预定义的集群/类执行层次聚类?

[英]How to perform hierarchical clustering with predifined clusters/classes?

我有一个数据库,我做了分层聚类(使用agnes() )并且它运行良好(我像这里描述的那样做了: https : //uc-r.github.io/hc_clustering 。现在我想比较人造集群或类在数据库中使用层次聚类找到的那些。我想我可以用tanglegram()做到这一点。当我已经有组时,我不知道如何生成树状图/进行层次聚类。我怎么能告诉 R 有关这些组的信息? 如果你能有条不紊地回答这个问题,那就太好了。`

set.seed(73)
great <- data.frame(c0=c("r1","r2","r3","r4","r5","r6"),c1=c("0.89","46","0","0.56","12","0"),c2=c("0","0.45","45","79","0.45","4.4"))

#euclidean distance

great_dist <- dist(great)

#agglomerative with agnes()
#wards minimizes total within cluster variance
#minimum between-cluster-distance is merged

hc1_wards <- agnes(great,method ="ward")
 #agglomerative coefficient
hc1_wards$ac

hc1_wards_plot <- pltree(hc1_wards, cex = 0.6, hang = -1, main = "Dendrogram\nagglomerative clustering",labels=F) 

#cutting into a specific amount of clusters

#average silhouette method

fviz_nbclust(great, FUN = hcut, method = "silhouette")

# Cut tree into 2 groups

great_grp <-
agnes(great, method = "ward")
great_grp_cut <- cutree(as.hclust(great), k = 2)

#using the cutree output to add the cluster each observation belongs to sub

great_cluster <- mutate(great,cluster = great_grp_cut)  


#evaluating goodness of cluster with dunn()
#with count() how many obs. in one cluster

count(great_cluster,cluster)

dunn <- clValid::dunn(distance = great_dist,clusters = great_grp_cut)

`

第 1、2、4 和 3、5、6 行是人造的巨大星团。

cl1 <- great[c(1,2,4), ]
cl2 <- great[c(3,5,6, ]

我想比较层次聚类和人工聚类。 我如何使用人造聚类执行树状图,以便将它们与tenglegram()进行比较。 有没有其他方法可以比较它们?

要直观地比较集群,您可以使用WGCNA包中的plotDendroAndColors()函数。 该函数仅显示树状图下每个对象的自定义颜色信息。

我无法重现您的示例(未指定您在代码中使用的包),因此我使用iris数据集的简单聚类来演示这一点:

library(WGCNA)

fit     <- hclust(dist(iris[,-5]), method="ward")
groups  <- cutree(fit, 3)
manmade <- as.numeric(iris$Species)

plotDendroAndColors(fit, cbind(clusters=labels2colors(groups), manmade=labels2colors(manmade)))

集群

由于您使用某种第三方软件包进行聚类,您可能必须先将它们的对象转换为树状图,才能使该绘图功能正常工作。 也许通过:

fit <- as.dendrogram(hc1_wards)
plotDendroAndColors(fit, cbind(clusters=labels2colors(groups), manmade=labels2colors(manmade)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM