简体   繁体   中英

drawing dendrogram from pre calculated distance matrix

I know there's another post similar to this one but it has not helped my situation. I am trying to draw a dendrogram from a distance matrix I've calculated not using euclidean distance (using an earth-mover's distance from the emdist package). I am now trying to draw a dendrogram from this matrix:

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

the problem is when I run

plot(hclust(x))

I get this error:

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

whereas if I run the dist function to calculate euclidean distances from the distance matrix that I've already calculated using a different approach, it draws the plot.

plot(hclust(dist(x)))

在此输入图像描述

However, this is not realistic. I need hclust to work from the distance matrix I've already calculated using a different approach. Any ideas?

hclust needs an object of class dist. as.dist, rather than dist, should give you want you are looking for.

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

You can try a neighbor joining method for creating trees based on distance metrics. The nj function in the Ape package can help.

http://r-eco-evo.blogspot.com/2007/09/neighbor-joining-tree-with-ape.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM