Hi! I am trying to reorder a dendrogram via reorder.dendrogram. However, I observed some problems with this, and I will illustrate them with an example. Take the following clustering problem: datamatrix <- matrix(c(2,2,2.5,2,1.5,2,2,1.5,2,2.5, 6,2,6.5,2,5.5,2,6,1.5,6,2.5, 4,4,4.5,4,3.5,4,4,3.5,4,4.5), ncol=2, byrow=TRUE) distmatrix <- dist(datamatrix, method="manhattan") hc <- hclust(distmatrix, method="single") dendro <- as.dendrogram(hc) The datamatrix contains three equidistant (for manhattan distance) clusters, each of which contains 5 points. Now, I want to impose an order: weights <- c(2.0, 2.0, 2.0, 2.0, 2.0, 6.0, 6.0, 6.0, 6.0, 6.0, 4.0, 4.0, 4.0, 4.0, 4.0) ddd <- reorder(dendro, weights, agglo.FUN=mean) but if you compare the order of ddd with dendro, you see no change: unlist(ddd) [1] 15 14 13 11 12 5 4 3 1 2 10 9 8 6 7 unlist(dendro) [1] 15 14 13 11 12 5 4 3 1 2 10 9 8 6 7 I would have expected something like: 5 4 3 1 2 15 14 13 11 12 10 9 8 6 7 or something of the sort. (I still do not know, if the order should be ascending or descending, but in the obtained result, it is neither nor). I do not see, where my mistake is ... Thanks for your advice! Thomas.
Oups ... sorry, actually I found the problem. The problem lies not with the reordering but with the construction of the dendrogram: if a hierarchical method is used, there are never more than 2 branches for a node. Therefore, the reordering does not give the expected result. I will have to try something different ... Thanks anyway. Thomas. Thomas Walter wrote:>Hi! > >I am trying to reorder a dendrogram via reorder.dendrogram. However, I >observed some problems with this, and I will illustrate them with an >example. > >Take the following clustering problem: > >datamatrix <- matrix(c(2,2,2.5,2,1.5,2,2,1.5,2,2.5, >6,2,6.5,2,5.5,2,6,1.5,6,2.5, 4,4,4.5,4,3.5,4,4,3.5,4,4.5), ncol=2, >byrow=TRUE) >distmatrix <- dist(datamatrix, method="manhattan") >hc <- hclust(distmatrix, method="single") >dendro <- as.dendrogram(hc) > >The datamatrix contains three equidistant (for manhattan distance) >clusters, each of which contains 5 points. >Now, I want to impose an order: > >weights <- c(2.0, 2.0, 2.0, 2.0, 2.0, 6.0, 6.0, 6.0, 6.0, 6.0, 4.0, 4.0, >4.0, 4.0, 4.0) >ddd <- reorder(dendro, weights, agglo.FUN=mean) > >but if you compare the order of ddd with dendro, you see no change: > >unlist(ddd) > [1] 15 14 13 11 12 5 4 3 1 2 10 9 8 6 7 > >unlist(dendro) > [1] 15 14 13 11 12 5 4 3 1 2 10 9 8 6 7 > >I would have expected something like: > 5 4 3 1 2 15 14 13 11 12 10 9 8 6 7 > >or something of the sort. (I still do not know, if the order should be >ascending or descending, but in the obtained result, it is neither nor). >I do not see, where my mistake is ... > >Thanks for your advice! > >Thomas. > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. > > >-- ------------------------- Thomas Walter EMBL Heidelberg Meyerhofstrasse 1 69117 Heidelberg Germany Tel: +49 (0)6221 387-8857
Hello, Perhaps, you should chose another toy example closer to the reality. Cases with exactly same distance rarely occur "in the field". You should, at least, add some random error: datamatrix <- matrix(c(2,2,2.5,2,1.5,2,2,1.5,2,2.5, 6,2,6.5,2,5.5,2,6,1.5,6,2.5, 4,4,4.5,4,3.5,4,4,3.5,4,4.5) + rnorm(30, sd = 0.1), ncol = 2, byrow = TRUE) distmatrix <- dist(datamatrix, method = "manhattan") hc <- hclust(distmatrix, method = "single") dendro <- as.dendrogram(hc) plot(dendro) #Now, I want to impose an order: weights <- c(2.0, 2.0, 2.0, 2.0, 2.0, 6.0, 6.0, 6.0, 6.0, 6.0, 4.0, 4.0, 4.0, 4.0, 4.0) ddd <- reorder(dendro, weights, agglo.FUN=mean) plot(ddd) unlist(ddd) # [1] 4 2 3 1 5 13 14 15 11 12 10 7 9 6 8 unlist(dendro) # [1] 10 7 9 6 8 13 14 15 11 12 4 2 3 1 5 Best, Philippe Grosjean ..............................................<?}))><........ ) ) ) ) ) ( ( ( ( ( Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( ( Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Belgium ( ( ( ( ( .............................................................. Thomas Walter wrote:> Hi! > > I am trying to reorder a dendrogram via reorder.dendrogram. However, I > observed some problems with this, and I will illustrate them with an > example. > > Take the following clustering problem: > > datamatrix <- matrix(c(2,2,2.5,2,1.5,2,2,1.5,2,2.5, > 6,2,6.5,2,5.5,2,6,1.5,6,2.5, 4,4,4.5,4,3.5,4,4,3.5,4,4.5), ncol=2, > byrow=TRUE) > distmatrix <- dist(datamatrix, method="manhattan") > hc <- hclust(distmatrix, method="single") > dendro <- as.dendrogram(hc) > > The datamatrix contains three equidistant (for manhattan distance) > clusters, each of which contains 5 points. > Now, I want to impose an order: > > weights <- c(2.0, 2.0, 2.0, 2.0, 2.0, 6.0, 6.0, 6.0, 6.0, 6.0, 4.0, 4.0, > 4.0, 4.0, 4.0) > ddd <- reorder(dendro, weights, agglo.FUN=mean) > > but if you compare the order of ddd with dendro, you see no change: > > unlist(ddd) > [1] 15 14 13 11 12 5 4 3 1 2 10 9 8 6 7 > > unlist(dendro) > [1] 15 14 13 11 12 5 4 3 1 2 10 9 8 6 7 > > I would have expected something like: > 5 4 3 1 2 15 14 13 11 12 10 9 8 6 7 > > or something of the sort. (I still do not know, if the order should be > ascending or descending, but in the obtained result, it is neither nor). > I do not see, where my mistake is ... > > Thanks for your advice! > > Thomas. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >