使用TraMineR中的组从seqrep中提取单个代表性序列图

时间:2015-04-21 18:55:11

标签: r traminer

我正在尝试使用seqrep中的group=选项重现TraMineR制作的图表,因为我想要一个图像文件,其中每个组的代表性序列都用于演示文稿。似乎我在语法中缺少一个重要部分,因为序列对象的子集会产生与group中的板载TraMineR选项截然不同的结果。正如您在下面的图片中看到的,seqrep标识的代表性序列强烈不同。 你知道我的语法中哪些部分缺失或错误了吗?

library(TraMineR)
library(WeightedCluster)

data(mvad)
mvad.alphabet <- c("employment", "FE", "HE", "joblessness", "school",
                   "training")
mvad.labels <- c("Employment", "Further Education", "Higher Education",
                 "Joblessness", "School", "Training")
mvad.scodes <- c("EM", "FE", "HE", "JL", "SC", "TR")

## Define sequence objects
mvad.seq <- seqdef(mvad[, 17:86], alphabet = mvad.alphabet, 
                   states = mvad.scodes, labels = mvad.labels, 
                   weights = mvad$weight, xtstep = 6)

## Defining cost matrices for OM - custom and transition based
subm.trate <- seqsubm(mvad.seq, method="TRATE")

## Computing dissimilarity matrices
mvad.ham <- seqdist(mvad.seq, method="HAM", sm=subm.trate)

## Clustering OM dissimilarities
wardCluster <- hclust(as.dist(mvad.ham), method = "ward",
                      members = mvad$weight)
clust4 <- cutree(wardCluster, k = 4)

## Plotting representative sequences
seqrplot(mvad.seq, criterion="dist", group=clust4, dist.matrix=mvad.ham, 
         withlegend=FALSE, border=NA, weighted=TRUE, stats=FALSE)
seqrplot(mvad.seq[clust4==2,], criterion="dist", dist.matrix=mvad.ham, 
         withlegend=FALSE, border=NA, weighted=TRUE, stats=FALSE)

四组代表性序列:见第二组 Four groups of representative sequences 第二组代表性序列 Second group of representative sequences

1 个答案:

答案 0 :(得分:2)

在对状态序列对象进行子集化时,应相应地对不相似矩阵进行子集

seqrplot(mvad.seq[clust4==2,], criterion="dist", 
         dist.matrix=mvad.ham[clust4==2, clust4==2], 
         withlegend=FALSE, border=NA, weighted=TRUE, stats=FALSE)

如果这样做,seqrep将使用错误的距离来确定代表性序列。