我正在使用序列分析方法来测量不同“空间使用序列”之间的相似性,表示为字符串。这是一个针对两个序列的三个类别(A:城市,B:农业,C:山)的理论示例:
t1,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 的未发布版本,它允许使用你自己的距离方法(在这种情况下:汉明)。有人知道如何解决这个问题吗?