【问题标题】:How to perform hierarchical clustering with predifined clusters/classes?如何使用预定义的集群/类执行层次聚类?
【发布时间】:2020-03-30 11:51:14
【问题描述】:

我有一个数据库,我在上面进行了层次聚类(使用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() 进行比较。还有其他方法可以比较它们吗?

【问题讨论】:

  • 你想如何比较它们?视觉上,还是使用一些相似度指标?
  • 我考虑过视觉比较以获得更好的直观理解
  • 我使用的包库(tidyverse)#数据操作库(cluster)#聚类算法库(factoextra)#聚类可视化库(dendextend)#用于比较两个树状图库(plotrix)库(clValid)#邓恩指数
  • @takelTeasy - 你可能应该在问题中添加这些,也许在代码中,使用调用库的行。

标签: r compare hierarchical-clustering


【解决方案1】:

要直观地比较集群,您可以使用 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)))

【讨论】:

    猜你喜欢
    • 2013-11-22
    • 2016-02-17
    • 2021-02-11
    • 2013-07-11
    • 2020-06-10
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多