2014-01-29 212 views
4

我知道還有另一篇文章與此類似,但它沒有幫助我的情況。我試圖從距離矩陣中繪製一個樹狀圖,我已經計算出沒有使用歐幾里得距離(使用地球移動者與emdist包的距離)。現在我想從這個矩陣畫樹狀圖:從預先計算的距離矩陣繪製樹狀圖

dim(x) 
[1] 8800 8800 

x <- x[1:10,1:10] 
x 
      1  2  3   4   5  6  7 
1 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 
2 0.67400563 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 
3 0.02577228 0.6526842 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 
4 0.37994900 0.7268372 0.1240314 0.0000000 0.0000000 0.0000000 0.0000000 
5 0.85156584 1.0248822 0.6165767 0.9077611 0.0000000 0.0000000 0.0000000 
6 0.51784015 0.5286874 0.5115762 0.6601093 1.1639417 0.0000000 0.0000000 
7 0.19290720 0.5906327 0.6576926 0.4350795 0.2986499 0.4130357 0.0000000 
8 1.57669127 1.3727582 1.4215065 1.9522834 1.0919793 0.9681544 1.0372481 
9 3.01650143 3.3004177 3.0651622 3.2502077 4.1505108 2.9940774 3.6078234 
10 0.48684093 0.6997258 0.3959822 0.3515030 0.8611233 0.5505790 0.3047047 
     8  9 10 
1 0.000000 0.000000 0 
2 0.000000 0.000000 0 
3 0.000000 0.000000 0 
4 0.000000 0.000000 0 
5 0.000000 0.000000 0 
6 0.000000 0.000000 0 
7 0.000000 0.000000 0 
8 0.000000 0.000000 0 
9 3.753577 0.000000 0 
10 1.500342 3.309016 0 

的問題是,當我運行

plot(hclust(x)) 

我得到這個錯誤:

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

而如果我運行DIST函數來計算我已經使用不同方法計算出的距離矩陣的歐式距離,它繪製該圖。

plot(hclust(dist(x))) 

enter image description here

然而,這是不現實的。我需要hclust從我已經使用不同方法計算出來的距離矩陣開始工作。有任何想法嗎?

回答

9

hclust需要類DIST的對象。 as.dist,而不是dist,應該讓你想要你正在尋找的東西。

plot(hclust(as.dist(x)))