Strange hierarchical clustering error in R

My R program looks like this:

hcluster <- function(dmatrix) { imatrix <- NULL hc <- hclust(dist(dmatrix), method="average") for(h in sort(unique(hc$height))) { hc.index <- c(h,as.vector(cutree(hc,h=h))) imatrix <- cbind(imatrix, hc.index) } return(imatrix) } dmatrix_file = commandArgs(trailingOnly = TRUE)[1] print(paste('Reading distance matrix from', dmatrix_file)) dmatrix <- as.matrix(read.csv(dmatrix_file,header=FALSE)) imatrix <- hcluster(dmatrix) imatrix_file = paste("results",dmatrix_file,sep="-") print(paste('Wrinting results to', imatrix_file)) write.table(imatrix, file=imatrix_file, sep=",", quote=FALSE, row.names=FALSE, col.names=FALSE) print('done!') 

My input is a distance matrix (of course, symmetric). When I execute on a program with a distance matrix more than about a thousand records (nothing happens in a few hundred), this gave me an error message:

 Error in cutree(hc, h = h) : the 'height' component of 'tree' is not sorted (increasingly); consider applying as.hclust() first Calls: hcluster -> as.vector -> cutree Execution halted 

My machine has about 16 GB of RAM and 4CPU, so this is not a resource problem.

Can someone please let me know what the problem is? Thanks!!

+6
source share
2 answers

I do not really like master R, but I ran into this problem.

A potential response is described here:

https://stat.ethz.ch/pipermail/r-help/2008-May/163409.html

+6
source

Looking at the cutree function here http://code.ohloh.net/file?fid=QM4q0tWQlv2VywAoSr2MfgcNjnA&cid=ki3UJjFJ8jA&s=cutree%20component%20of%20is%20not%20sorted&mp=1&ml=1&me=f&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&==&&

You can try adding k scaler for the number of groups, this will override the height argument. If not, you can see what the height of hc $ is, because if it is not a numeric, complex, symbolic or logical vector, is.unsorted will return true and give you this error.

 if(is.null(k)) { if(is.unsorted(tree$height)) stop("the 'height' component of 'tree' is not sorted (increasingly)") ## h |--> k ## S+6 help(cutree) says k(h) = k(h+), but does k(h-) [continuity] ## h < min() should give k = n; k <- n+1L - apply(outer(c(tree$height,Inf), h, ">"), 2, which.max) if(getOption("verbose")) message("cutree(): k(h) = ", k, domain = NA) } 
+1
source

Source: https://habr.com/ru/post/909412/


All Articles