Get the cut heights using identify.hclust()

458 views Asked by At

I am manually cutting a dendrogram created from hclust in R using identify.hclust. The default return of the function is the IDs of observations in each group. I need this information, but I also need to know the height of this group. Is there any way of doing it? Thanks alot!

Reproducible data:

set.seed(1)
dat = rnorm(100,0,1)
hca = hclust(dist(dat))
plot(hca, hang=-1, sub="", xlab="", labels=F)
heightsAndIDs = identify(hca) #Gives only IDs

As example, I cut the dendrogram at the following heights by using identify and want to get the height of the merge for the branch:

segments(3,2,8, col="red")
segments(15,1,18, col="green")
segments(20,1,24,col="blue")
segments(38,1.5,45,col="purple")
segments(75, 1.5, 82,col="cyan")
1

There are 1 answers

0
Tal Galili On

I suspect you could get your answer from the two functions heights_per_k.dendrogram and get_branches_heights from the R package dendextend.

Here is a small example:

set.seed(1)
dat = rnorm(100,0,1)
hca = hclust(dist(dat))

library(dendextend)

For example:

> sort(heights_per_k.dendrogram(dend))[1:7]
          100            99            98 
0.00002485728 0.00010400211 0.00020365009 
           97            96            95 
0.00118445439 0.00180321776 0.00215161572 
           94 
0.00230368982 
> sort(heights_per_k.dendrogram(dend), T)[1:7]
        1         2         3         4         5 
4.6163377 4.6162976 3.1585161 1.8779138 1.3384979 
        6         7 
1.1705453 0.9620798 

Does this give you the tools for getting your answer?