引导自己构建的函数pvcluster不起作用



我使用序列分析方法来测量不同"空间使用序列"之间的相似性,用字符串表示。以下是一个理论示例,其中包含两个序列的三个类(a:城市,B:农业,C:山区):

                t­1,t2,........tx
  Individual 1: A A A B B B C C
  Individual 2: A B B B A A C C
                0 1 1 0 1 1 0 0 = **4**

我们用来测量序列之间相似性的距离度量是hamming距离(即,测量序列中的一个字符需要被替换的频率以使序列相等,在上面的例子中,4字符需要被取代以使序列等效)。基于计算汉明距离后获得的距离矩阵(给出每对可能序列的距离或相异性),使用Ward(Ward.D2)的聚类方法创建了树状图。

现在,我还想包括一个很好的集群健壮性度量,以便识别相关的集群。为此,我尝试使用pvcluster,它包含了几种计算引导值的方法,但仅限于一些距离度量。在pvcluster的未发布版本中,我尝试实现正确的距离度量(即hamming距离),并尝试创建一个自举树。脚本正在运行,但结果不正确。应用于我的数据集,使用1000的nboot,"bp"值接近0,所有其他值"au"、"se.au"、"se.bp"、"v"、"c"、"pchi"均为0,这表明聚类是人为的。

这里我提供了一个示例脚本:

数据涉及非常同质的模拟序列(例如,连续使用1个特定状态),因此每个集群肯定是重要的。为了限制计算时间,我将引导的数量限制为只有10个。

####################################################################
####Create the sequences#### 
dfr = data.frame()
a = list(dfr)
b = list(dfr)
c = list(dfr)
d = list(dfr)
data = list(dfr)
for (i in c(1:10)){
set.seed(i)
a[[i]] <- sample(c(rep('A',10),rep('B', 90)))
b[[i]] <- sample(c(rep('B',10),rep('A', 90)))
c[[i]] <- sample(c(rep('C',10),rep('D', 90)))
d[[i]] <- sample(c(rep('D',10),rep('C', 90)))
}
a = as.data.frame(a, header = FALSE)
b = as.data.frame(b, header = FALSE)
c = as.data.frame(c, header = FALSE)
d = as.data.frame(d, header = FALSE)
colnames(a) <- paste(rep('seq_urban'),rep(1:10), sep ='')
colnames(b) <- paste(rep('seq_agric'),rep(1:10), sep ='')
colnames(c) <- paste(rep('seq_mount'),rep(1:10), sep ='')
colnames(d) <- paste(rep('seq_sea'),rep(1:10), sep ='')
data = rbind(t(a),t(b),t(c),t(d))
#####################################################################
####Analysis####
## install packages if necessary
#install.packages(c("TraMineR", "devtools")) 
library(TraMineR)
library(devtools)
source_url("https://www.dropbox.com/s/9znkgks1nuttlxy/pvclust.R?dl=0") # url    to my dropbox for unreleased pvclust package
source_url("https://www.dropbox.com/s/8p6n5dlzjxmd6jj/pvclust-internal.R?dl=0") # url to my dropbox for unreleased pvclust package
dev.new()
par( mfrow = c(1,2))
## Color definitions and alphabet/labels/scodes for sequence definition
palet <- c(rgb(230, 26, 26, max = 255), rgb(230, 178, 77, max = 255),     "blue", "deepskyblue2") # color palet used for the states
s.alphabet <- c("A", "B", "C", "D") # the alphabet of the sequence object
s.labels <- c("country-side", "urban", "sea", "mountains") # the labels of    the sequence object
s.scodes <- c( "A", "U", "S", "M") # the states of the sequence object
## Sequence definition
seq_ <- seqdef(data, # data  
                  1:100, # columns corresponding to the sequence data  
                  id = rownames(data), # id of the sequences
                  alphabet = s.alphabet, states = s.scodes, labels = s.labels, 
                  xtstep = 6, 
                  cpal = palet) # color palet 
##Substitution matrix used to calculate the hamming distance
Autocor <- seqsubm(seq_, method = "TRATE", with.missing = FALSE) 
# Function with the hamming distance (i.e. counts how often a character  needs to be substituted to equate two sequences to each other. Result is a  distance matrix giving the distances for each pair of sequences)
hamming <- function(x,...) {
res <- seqdist(x, method = "HAM",sm = Autocor)
res <- as.dist(res)
attr(res, "method") <- "hamming"
return(res)
}
## Perform the bootstrapping using the distance method "hamming"
result <- pvclust(seq_, method.dist = hamming, nboot = 10, method.hclust =  "ward")
result$hclust$labels <- rownames(test[,1])
plot(result)

为了进行此分析,我使用了R包pvcluster的未发布版本,它允许使用您自己的距离方法(在本例中为:hamming)。有人知道如何解决这个问题吗?

pvclust的目的是集群变量(或属性)而不是案例。这就是为什么你的结果没有意义。你可以试试

data(iris)
res <- pvclust(iris[, 1:4])
plot(res)

要测试案例集群的稳定性,可以使用包fpc中的clusterboot。在这里查看我的答案:测量树/树状图(Traminer)的可靠性

在您的示例中,您可以使用:

library(fpc)
ham <- seqdist(seq_, method="HAM",sm = Autocor)
cf2 <- clusterboot(as.dist(ham), clustermethod=disthclustCBI, k=4, cut="number", method="ward.D")

例如,使用k=10会得到糟糕的结果,因为您的数据实际上有4个集群(通过构造)。

最新更新