【问题标题】:bootstrapping own built function pvclust does not work引导自己构建的函数 pvclust 不起作用
【发布时间】:2015-03-26 16:57:43
【问题描述】:

我正在使用序列分析方法来测量不同“空间使用序列”之间的相似性,以字符串表示。这是一个针对两个序列的三个类别(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**

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

现在我还想包含一个很好的集群稳健性度量,以便识别相关集群。为此,我尝试使用 pvclust,它包含多种计算引导值的方法,但仅限于一些距离度量。在未发布的 pvclust 版本中,我尝试实现正确的距离度量(即汉明距离),并尝试创建一个引导树。 脚本正在运行,但结果不正确。使用 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 包 pvclust 的未发布版本,它允许使用你自己的距离方法(在这种情况下:汉明)。有人知道如何解决这个问题吗?

【问题讨论】:

    标签: sequence statistics-bootstrap traminer pvclust


    【解决方案1】:

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

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

    要测试案例集群的稳定性,您可以使用包fpc 中的clusterboot。在这里查看我的答案:Measuring reliability of tree/dendrogram (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 个集群(通过构造)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2012-08-20
      • 1970-01-01
      • 2018-10-19
      • 2014-08-20
      • 1970-01-01
      • 2019-06-19
      相关资源
      最近更新 更多