5

我正在做一个名为pvclust的R包的层次聚类,它通过引入bootstrapping来计算获得的聚类的显着性水平而建立在hclust上。R中多尺度层次聚类的错误

考虑下面的数据与3个维度和10个观察组:

mat <- as.matrix(data.frame("A"=c(9000,2,238),"B"=c(10000,6,224),"C"=c(1001,3,259), 
         "D"=c(9580,94,51),"E"=c(9328,5,248),"F"=c(10000,100,50), 
         "G"=c(1020,2,240),"H"=c(1012,3,260),"I"=c(1012,3,260), 
         "J"=c(984,98,49))) 

当我独自使用hclust,聚类两个欧几里德措施和相关措施运行良好:

# euclidean-based distance 
dist1 <- dist(t(mat),method="euclidean") 
mat.cl1 <- hclust(dist1,method="average") 

# correlation-based distance 
dist2 <- as.dist(1 - cor(mat)) 
mat.cl2 <- hclust(dist2, method="average") 

然而,当使用pvclust的每个设置时,如下:

library(pvclust) 

# euclidean-based distance 
mat.pcl1 <- pvclust(mat, method.hclust="average", method.dist="euclidean", nboot=1000) 

# correlation-based distance 
mat.pcl2 <- pvclust(mat, method.hclust="average", method.dist="correlation", nboot=1000) 

...我收到以下错误:

  • 欧几里得:Error in hclust(distance, method = method.hclust) : must have n >= 2 objects to cluster
  • 相关:Error in cor(x, method = "pearson", use = use.cor) : supply both 'x' and 'y' or a matrix-like 'x'

请注意,距离的计算方法是pvclust,因此不需要预先计算距离。还请注意,hclust方法(平均值,中位数等)不会影响问题。

当我将数据集的维度增加到4时,pvclust现在可以正常运行。为什么我在3维和以下版本中获得pvclust的这些错误,但不是hclust?此外,当我使用4维以上的数据集时,为什么这些错误会消失?

回答

2

在功能pvclust年底,我们看到一行

mboot <- lapply(r, boot.hclust, data = data, object.hclust = data.hclust, 
    nboot = nboot, method.dist = method.dist, use.cor = use.cor, 
    method.hclust = method.hclust, store = store, weight = weight) 

然后更深的挖掘,我们发现

getAnywhere("boot.hclust") 
function (r, data, object.hclust, method.dist, use.cor, method.hclust, 
    nboot, store, weight = F) 
{ 
    n <- nrow(data) 
    size <- round(n * r, digits = 0) 
    .... 
      smpl <- sample(1:n, size, replace = TRUE) 
      suppressWarnings(distance <- dist.pvclust(data[smpl, 
       ], method = method.dist, use.cor = use.cor)) 
    .... 
} 

也注意到,该参数r对功能pvclust的默认值是r=seq(.5,1.4,by=.1)。嗯,其实我们可以看到这个值被改变的地方:

Bootstrap (r = 0.33)... 

所以我们得到的是size <- round(3 * 0.33, digits =0)1,终于data[smpl,]只有1个排,这是小于2的r修正它返回后一些可能是无害的错误和输出也给出:

mat.pcl1 <- pvclust(mat, method.hclust="average", method.dist="euclidean", 
        nboot=1000, r=seq(0.7,1.4,by=.1)) 
Bootstrap (r = 0.67)... Done. 
.... 
Bootstrap (r = 1.33)... Done. 
Warning message: 
In a$p[] <- c(1, bp[r == 1]) : 
    number of items to replace is not a multiple of replacement length 

让我知道如果结果是令人满意的。

+0

这很棒。改变'r'就能做到这一点。谢谢。 – oisyutat