将pvclust R函数应用于预先计算的dist对象

时间:2012-01-19 10:28:31

标签: r pvclust

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

  1. 我导入了距离矩阵
  2. 我使用as.dist函数在dist对象
  3. 中对其进行转换
  4. 我在hclust对象
  5. 上运行dist

    这是R代码:

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

    此时我想用函数pvclust做类似的事情;但是,我不能,因为传递预先计算的dist对象是不可能的。考虑到我使用R的dist函数提供的距离不可用,我该怎么办?

3 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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的实施方式。

答案 2 :(得分:1)

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