【发布时间】:2016-12-16 22:00:53
【问题描述】:
我有两个从矩阵 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
上面的代码是否等同于我的原始代码,如果不是,我需要进行哪些更改?
谢谢!
【问题讨论】:
-
你到底想达到什么目的?它是否对您的变量进行聚类?您的问题可能不是您想要的答案。
-
我想找到最佳的聚类数量,然后返回到分层聚类树状图并根据我的发现进行截断。我不会使用树状图,我只会使用结果。 @YCR
-
我使用斯坦福 R 实验室作为参考,他们提到使用相关矩阵。这最终用于社交网络分析。链接在这里:sna.stanford.edu/lab.php?l=6 代码从第 124 行开始。
-
当我尝试测试他们的代码时,我的矩阵太大了,所以我正在尝试为第 196 行寻找替代方案
-
另外,拿回来,你也可以通过nbclustering$Best.partition找到哪些变量属于哪些集群。所以我基本上只需要知道如何使nbclust公式等效于原始代码@YCR
标签: r matrix cluster-analysis correlation hierarchical-clustering