最优集群公式:使用NbClust查找等价物

时间:2016-08-10 13:24:31

标签: r matrix cluster-analysis correlation hierarchical-clustering

我有两个我从Matrix B计算的变量:

1)相关矩阵cor(B)

2)相关矩阵的相异矩阵的层次聚类

然后我使用clustConfigurations函数计算“肘图”以确定最佳聚类数量。

参见下面的代码:

library(NetCluster)

B = matrix( 
      c(2, 0, 0, 1, 0, 0, 1,
        0, 1, 0, 0, 2, 1, 0,
        0, 0, 3, 1, 0, 0, 2,
        1, 0, 1, 4, 0, 0, 2,
        0, 0, 0, 0, 4, 0, 2,
        0, 1, 0, 0, 0, 2, 1,
        1, 0, 2, 2, 2, 1, 8), 
  nrow=7, 
  ncol=7) 
  colnames(B) = c("A", "B", "C", "D", "E", "F", "G")
  rownames(B) = c("A", "B", "C", "D", "E", "F", "G") 
B

  A B C D E F G
A 2 0 0 1 0 0 1
B 0 1 0 0 0 1 0
C 0 0 3 1 0 0 2
D 1 0 1 4 0 0 2
E 0 2 0 0 4 0 2
F 0 1 0 0 0 2 1
G 1 0 2 2 2 1 8

Correlation_Matrix <- cor(B)
dissimilarity <- 1 - Correlation_Matrix
Correlation_Matrix_dist <- as.dist(dissimilarity)
Correlation_Matrix_dist
HClust_Correlation_Matrix <- hclust(Correlation_Matrix_dist)
clustered_observed_cors = vector()
num_vertices <- ncol(B)
clustered_observed_cors1 <-clustConfigurations(num_vertices,HClust_Correlation_Matrix,Correlation_Matrix)

当我尝试使用更大的矩阵(特别是1213 x 1213)时,矩阵太大而无法运行此脚本,所以我决定使用另一个名为NbClust的包。

文档:

https://cran.r-project.org/web/packages/NbClust/NbClust.pdf

我的目标是使用这个新软件包重新创建上述过程,但我不确定以下代码是否与上述代码相同:

library(NbClust)

nbclustering<-NbClust(diss = Correlation_Matrix_dist, 
distance = NULL, 
min.nc=2, 
max.nc=20, 
method = "complete", 
index = "dunn")

This would give you the optimal amount of clusters:
nbclustering$Best.nc

以上代码是否等同于我的原始代码,如果没有,我需要做出哪些更改?

谢谢!

1 个答案:

答案 0 :(得分:1)

NbClust是一个比hclust更广泛的功能,更侧重于评估最终群集数量的指标。

hclust的默认方法是"complete"

与NbClust一起使用的方法与method = "complete"选项相同。

因此,使用Nbclust的结果来定义函数hclust获得的聚类的最终聚类数是正确的。

相关问题