簡體   English   中英

R中層次聚類的奇怪錯誤

[英]Strange error of Hierarchical Clustering in R

我的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,所以它不會是資源問題。

誰能告訴我這是什么問題? 謝謝!!

我不是一個R精靈 - 但我遇到了這個問題。

這里描述了一個潛在的答案:

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

在這里查看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 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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM