3

我正在使用 R 来执行层次聚类。作为第一种方法,我使用hclust并执行了以下步骤:

  1. 我导入了距离矩阵
  2. 我使用该as.dist函数将其转换为dist对象
  3. hclustdist物体上奔跑

这是R代码:

distm <- read.csv("distMatrix.csv")
d <- as.dist(distm)
hclust(d, "ward")

在这一点上,我想对函数做一些类似的事情pvclust;但是,我不能,因为不可能传递预先计算的dist对象。考虑到我使用的距离在 R 函数提供的距离中不可用,我该如何继续dist

4

3 回答 3

3

我已经测试了Vincent的建议,您可以执行以下操作(我的数据集是一个相异矩阵):

# Import you data
distm <- read.csv("distMatrix.csv")
d <- as.dist(distm)

# Compute the eigenvalues
x <- cmdscale(d,1,eig=T)

# Plot the eigenvalues and choose the correct number of dimensions (eigenvalues close to 0)
plot(x$eig, 
   type="h", lwd=5, las=1, 
   xlab="Number of dimensions", 
   ylab="Eigenvalues")

# Recover the coordinates that give the same distance matrix with the correct number of dimensions    
x <- cmdscale(d,nb_dimensions)

# As mentioned by Stéphane, pvclust() clusters columns
pvclust(t(x))
于 2014-11-26T11:49:09.997 回答
2

如果数据集不是太大,您可以将 n 个点嵌入到维度为 n-1 的空间中,并使用相同的距离矩阵。

# Sample distance matrix
n <- 100
k <- 1000
d <- dist( matrix( rnorm(k*n), nc=k ), method="manhattan" )

# Recover some coordinates that give the same distance matrix
x <- cmdscale(d, n-1)
stopifnot( sum(abs(dist(x) - d)) < 1e-6 )

# You can then indifferently use x or d
r1 <- hclust(d)
r2 <- hclust(dist(x)) # identical to r1
library(pvclust)
r3 <- pvclust(x)

如果数据集很大,您可能必须检查如何pvclust实现。

于 2012-01-19T13:40:50.660 回答
1

我不清楚你是否只有一个距离矩阵,或者你事先计算了它。在前一种情况下,正如@Vincent 已经建议的那样,调整pvclust自身的 R 代码(使用fix()或其他;我在 CrossValidated 上的另一个问题上提供了一些提示)并不难。在后一种情况下,pvclust的作者提供了一个关于如何使用自定义距离函数的示例,尽管这意味着您必须安装他们的“非官方版本”

于 2012-01-19T14:48:22.663 回答