R中层次聚类的奇怪错误

Kev*_*vin 6 r cluster-analysis hierarchical-clustering

我的R程序如下:

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!')
Run Code Online (Sandbox Code Playgroud)

我的输入是距离矩阵(当然是对称的).当我执行上面的程序时,距离矩阵大于大约数千条记录(几百条没有发生),它给了我错误信息:

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
Run Code Online (Sandbox Code Playgroud)

我的机器有大约16GB的RAM和4CPU,所以它不会是资源问题.

谁能告诉我这是什么问题?谢谢!!

plo*_*lof 6

我不是一个R精灵 - 但我遇到了这个问题.

这里描述了一个潜在的答案:

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