2014-06-07 98 views
3

所以,我有256個對象,並計算了它們之間的距離矩陣(成對距離)。我的距離矩陣的一個子集給出如下:給定距離矩陣的聚類256x256

> 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 

任何想法是什麼我的分析錯誤?

回答

5

dm需要成爲hclust中「dist」類的對象。

所以,你可以用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)