给定距离矩阵256x256的聚类

时间:2014-06-07 01:40:20

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

所以,我有256个物体,并计算了它们之间的距离矩阵(成对距离)。我的距离矩阵的一个子集如下:

> dm[1:10, 1:10]
      V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
 [1,]  0  1  1  1  1  2  2  2  1   2
 [2,]  1  0  1  1  2  1  2  2  2   1
 [3,]  1  1  0  1  2  2  1  2  2   2
 [4,]  1  1  1  0  2  2  2  1  2   2
 [5,]  1  2  2  2  0  1  1  1  1   2
 [6,]  2  1  2  2  1  0  1  1  2   1
 [7,]  2  2  1  2  1  1  0  1  2   2
 [8,]  2  2  2  1  1  1  1  0  2   2
 [9,]  1  2  2  2  1  2  2  2  0   1
[10,]  2  1  2  2  2  1  2  2  1   0

> str(dm)
 int [1:256, 1:256] 0 1 1 1 1 2 2 2 1 2 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:256] "V1" "V2" "V3" "V4" ...

现在,我想使用此距离矩阵来相应地聚类这256个对象。因此,我使用了hclust,但收到了错误:

> hclust(dm, method="single")
Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") : 
  missing value where TRUE/FALSE needed


> hclust(dm, method="complete")
Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") : 
  missing value where TRUE/FALSE needed

所以,即使我使用矩阵的较小子集,我仍会得到相同的错误:

> hclust(dm[1:10,1:10], method="complete")
Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") : 
  missing value where TRUE/FALSE needed

知道我的分析有什么问题吗?

1 个答案:

答案 0 :(得分:5)

dm需要成为班级的对象" dist"在hclust

因此,您可以使用dist函数计算不相似度矩阵,然后使用hclust内的对象。

m_trix = matrix(data=1:2,nrow=10,ncol=10)

dm = dist(m_trix,method="euclidean")
cluster = hclust(dm, method="single")
plot(cluster)

或者您可以直接在dist(m_trix)内使用hclust

cluster = hclust(dist(m_trix), method="single")
plot(cluster)
相关问题