[R] creating dendrogram from cluster hierarchy

Timo Becker timo.becker at oeaw.ac.at
Tue Feb 28 17:26:11 CET 2006


Timo Becker schrieb:

> Martin Maechler schrieb:
>
>>>>>>> "Timo" == Timo Becker <timo.becker at oeaw.ac.at>
>>>>>>>    on Tue, 28 Feb 2006 11:01:26 +0100 writes:
>>>>>>>           
>>>>>>
>>
>>    Timo> Dear R users, I have created data for hierarchical
>>    Timo> agglomerative cluster analysis which consist of the
>>    Timo> merging pairs and the agglomeration heights, e.g.
>>    Timo> something like
>>
>>    Timo> my.merge <- matrix(c(-1,-2,-3,1), ncol=2, byrow=TRUE)
>>    Timo> my.height <- c(0.5, 1)
>>
>>    Timo> I'd like to plot a corresponding dendrogram but I
>>    Timo> don't know how to convert my data to achieve this.  Is
>>    Timo> it possible to create a dendrogram object from a
>>    Timo> cluster hierarchy?
>>
>> Yes, it is possible.   R does it already with the
>> as.dendrogram() method for objects of class "hclust".
>>  
>>
> Unfortunately the hierarchy is created by another program than R. This 
> is the reason why the only available data for the hclust or dendrogram 
> object creation are the merge-matrix and the agglomeration heights. So 
> as.dendrogram() does not work here.
>
>> But I assume you'd also like to know *how* you can do it... ;-)
>>
>> I'd strongly recommend to take the example of hclust() and have
>> your function return an object ``like'' the one hclust()
>> returns.  Then, as.dendrogram( <your object> ) will work.
>>
>> You have to decide for yourself if your function should return
>> an object of class "hclust" (which is partly described by
>> ?hclust ), and you use as.dendrogram[.hclust]() directly, or rather 
>> your function returns a class "hclustTimo" and you
>> write your own  as.dendrogram.hclustTimo() method.
>>
>> I'd recommend looking at and using the R's source code, e.g., from 
>> https://svn.R-project.org/R/trunk/src/library/stats/R/hclust.R and
>> https://svn.R-project.org/R/trunk/src/library/stats/R/dendrogram.R
>>
>> Regards,
>> Martin Maechler, ETH Zurich
>>
>>  
>>
> I adapted the source code of hclust.R and a quick (and VERY dirty) 
> solution is as follows:
>
> hierarchy2dendrogram <- function(hierarchy) {
> tree <- list(merge = hierarchy[,1:2],
>             height= hierarchy[,3],
>             order = seq(1:(dim(hierarchy)[1]+1)),
>         method=NULL,
>         call = match.call(),
>         dist.method = "whatever")
>  class(tree) <- "hclust"
>  return(tree)
> }
> my.merge <- matrix(c(-1,-2,-3,1), ncol=2, byrow=TRUE)
> my.height <- c(0.5, 1)
> my.hierarchy <- cbind(my.merge, my.height)
> my.hclust.object <- hierarchy2dendrogram(my.hierarchy)
> plot(my.hclust.object)
>
> Perhaps there exists a "cleaner" solution which also returns the 
> optimal order (if I am right the ordering is accomplished by the 
> Fortran function "hcass2") but the above works fine for me.
>
> Thanks a lot and best regards,
> Timo
>
The above is not recommended, since

hierarchy <- matrix(c(-1,2,-2,-3,0.5,1), ncol=3)
hc <- hierarchy2dendrogram(hierarchy)
plot(hc)

causes R 2.2.1 (WinXP) to crash.
I am aware that I am using objects in an unusual way they were not meant to.
I still do not understand what went wrong though (I suppose the merge 
matrix has to follow several restrictions).

Regards,
Timo

-- 
Timo Becker
Phonetics
Austrian Academy of Sciences
Acoustics Research Institute




More information about the R-help mailing list