如何使用预定义的 clusters/classes 执行层次聚类?
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)))
我有一个数据库,我对其进行了层次聚类(使用 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)))