[英]applying the pvclust R function to a precomputed dist object
我正在使用R来执行分层聚类。 作为第一种方法,我使用了hclust
并执行了以下步骤:
as.dist
函数在dist
对象中转换它 dist
对象上运行了hclust
这是R代码:
distm <- read.csv("distMatrix.csv")
d <- as.dist(distm)
hclust(d, "ward")
此时我想用函数pvclust
做类似的pvclust
; 但是,我不能,因为它不可能传递预先计算的dist
对象。 考虑到我使用R的dist
函数提供的距离不可用,我该怎么办?
我已经测试了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))
如果数据集不是太大,则可以将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
的实现方式。
我不清楚你是否只有一个距离矩阵,或者你事先计算过它。 在前一种情况下,正如@Vincent所建议的那样,调整pvclust
本身的R代码(使用fix()
或其他任何东西都不会太难;我在CrossValidated的另一个问题上提供了一些提示)。 在后一种情况下, pvclust的作者提供了一个如何使用自定义距离函数的示例 ,尽管这意味着您必须安装他们的“非官方版本”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.