我的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!')
我的输入是距离矩阵(当然是对称的)。当我执行上面的程序时,距离矩阵大于大约数千条记录(几百条没有发生),它给了我错误信息:
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
我的机器有大约16GB的RAM和4CPU,因此不会出现资源问题。
任何人都可以让我知道这是什么问题?谢谢!
答案 0 :(得分:6)
答案 1 :(得分:1)
您可以尝试为组数添加k scaler,这将覆盖height参数。如果不是,你可以查看hc $ height是什么,因为如果它不是数字,复数,字符或逻辑向量,is.unsorted将返回true并给你这个错误。
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)
}