首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中的层次聚类的奇怪错误

R中的层次聚类的奇怪错误
EN

Stack Overflow用户
提问于 2012-02-27 05:45:54
回答 2查看 2.4K关注 0票数 6

我的R程序如下:

代码语言:javascript
复制
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!')

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

代码语言:javascript
复制
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

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

有谁能告诉我出了什么问题吗?谢谢!!

EN

回答 2

Stack Overflow用户

发布于 2012-04-04 18:57:16

我不是一个好的R向导--但是我遇到了这个问题。

这里描述了一个可能的答案:

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

票数 6
EN

Stack Overflow用户

发布于 2012-12-18 04:42:08

看一下这里的cutree函数http://code.ohloh.net/file?fid=QM4q0tWQlv2VywAoSr2MfgcNjnA&cid=ki3UJjFJ8jA&s=cutree%20component%20of%20is%20not%20sorted&mp=1&ml=1&me=1&md=1&browser=Default#L1

您可以尝试为组的数量添加k定标器,这将覆盖高度参数。如果不是,你可以看看hc$height是什么,因为如果它不是数值、复数、字符或逻辑向量,is.unsorted将返回true并给你这个错误。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9457473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档