[我有一个数据库,我使用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()
进行比较。还有另一种比较方式吗?
要从视觉上比较群集,可以使用plotDendroAndColors()
程序包中的WGCNA
功能。该功能仅显示树状图下每个对象的自定义颜色信息。
我无法复制您的示例(未指定您在代码中使用的软件包),因此,我使用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)))