【发布时间】:2014-01-24 21:22:34
【问题描述】:
使用 TraMineR 中的 biofam 数据集:
library(TraMineR)
data(biofam)
lab <- c("P","L","M","LM","C","LC","LMC","D")
biofam.seq <- seqdef(biofam[,10:25], states=lab)
head(biofam.seq)
Sequence
1167 P-P-P-P-P-P-P-P-P-LM-LMC-LMC-LMC-LMC-LMC-LMC
514 P-L-L-L-L-L-L-L-L-L-L-LM-LMC-LMC-LMC-LMC
1013 P-P-P-P-P-P-P-L-L-L-L-L-LM-LMC-LMC-LMC
275 P-P-P-P-P-L-L-L-L-L-L-L-L-L-L-L
2580 P-P-P-P-P-L-L-L-L-L-L-L-L-LMC-LMC-LMC
773 P-P-P-P-P-P-P-P-P-P-P-P-P-P-P-P
我可以进行聚类分析:
library(cluster)
couts <- seqsubm(biofam.seq, method = "TRATE")
biofam.om <- seqdist(biofam.seq, method = "OM", indel = 3, sm = couts)
clusterward <- agnes(biofam.om, diss = TRUE, method = "ward")
cluster3 <- cutree(clusterward, k = 3)
cluster3 <- factor(cluster3, labels = c("Type 1", "Type 2", "Type 3"))
但是,在这个过程中,来自 biofam.seq 的唯一 ID 已被替换为从 1 到 N 的数字列表:
head(cluster3, 10)
[1] Type 1 Type 2 Type 2 Type 2 Type 2 Type 3 Type 3 Type 2 Type 1
[10] Type 2
Levels: Type 1 Type 2 Type 3
现在,我想知道每个簇内有哪些序列,以便我可以应用其他函数来获得每个簇内的平均长度、熵、子序列、相异度等。我需要做的是:
- 将旧 ID 映射到新 ID
- 将每个簇中的序列插入到单独的序列对象中
- 对每个新序列对象运行我想要的统计数据
我怎样才能完成上面列表中的 2 和 3?
【问题讨论】:
-
我想帮忙,但我无法运行您的示例:
biofam.seq来自哪里? -
它加载了
TraMineR:mephisto.unige.ch/traminer/doc/biofam.html,如果它没有自动加载,你应该可以在运行biofam.seq <- seqdef(biofam)之后使用biofam.seq <- seqdef(biofam)来完成data(biofam) -
biofam数据还包含协变量。因此,在seqdef中,您应该指定我们找到序列数据的列,即seqdef(biofam[10:25,])。我已经相应地编辑了问题。
标签: r cluster-analysis data-manipulation traminer