绘制和排序多通道序列对象

时间:2016-10-14 13:02:58

标签: r traminer

我想制作多通道序列对象的序列索引图,以用于第一个描述目的。但是,我仍然不确定如何正确地做到这一点。排序一个序列对象的常用方法不能很好地工作,因为没有嵌套排序函数可以跨多个通道进行排序。在我看来,最好的方法是在使用seqdistmc计算多通道序列距离后进行MDS并相应地对所有通道进行排序。这种方法需要有关距离测量等的多个决策,因此它几乎超出了我的第一个描述的意图。

  1. 以某种方式可以创建嵌套排序函数 对于多通道序列对象?也许通过排序第一个 从序列的开头通道然后对“关系”进行排序, 相等的序列,通过排序第二个通道等等? 更新:我使用seqHMM找到了问题的这一部分的答案,见下文。
  2. 你怎么想,最好的方法是绘制和排序a 用于描述的多通道序列对象?
  3. 这里有一些R语法可能有助于理解我的问题

    library(TraMineR)
    library(TraMineRextras)
    
    # Building sequence objects
    data(biofam)
    
    ## Building one channel per type of event left, children or married
    bf <- as.matrix(biofam[, 10:25])
    children <-  bf==4 | bf==5 | bf==6
    married <- bf == 2 | bf== 3 | bf==6
    left <- bf==1 | bf==3 | bf==5 | bf==6
    
    child.seq <- seqdef(children)
    marr.seq <- seqdef(married)
    left.seq <- seqdef(left)
    
    
    # Create unsorted Sequence Index Plots
    layout(matrix(c(1,2,3,4,4,4), 2, 3, byrow=TRUE), heights=c(3,1.5))
    seqIplot(child.seq, title="Children", withlegend=FALSE)
    seqIplot(marr.seq, title="Married", withlegend=FALSE)
    seqIplot(left.seq, title="Left parents", withlegend=FALSE)
    seqlegend(child.seq, horiz=TRUE, position="top", xpd=TRUE)
    
    
    # Create sequence Index Plots sorted by alignment of first channel from beginning
    mcsort.ch1 <- sortv(child.seq, start="beg")
    layout(matrix(c(1,2,3,4,4,4), 2, 3, byrow=TRUE), heights=c(3,1.5))
    seqIplot(child.seq, title="Children", sortv=mcsort.ch1, withlegend=FALSE)
    seqIplot(marr.seq, title="Married", sortv=mcsort.ch1, withlegend=FALSE)
    seqIplot(left.seq, title="Left parents", sortv=mcsort.ch1, withlegend=FALSE)
    seqlegend(child.seq, horiz=TRUE, position="top", xpd=TRUE)
    
    # Sequence Index Plots sorted by MDS scores of multi-channel distances
    
    ## Calculate multi-channel distances and MDS scores 
    mcdist <- seqdistmc(channels=list(child.seq, marr.seq, left.seq),
                        method="OM", sm =list("TRATE", "TRATE", "TRATE"))
    mcsort.mds <- cmdscale(mcdist, k=2, eig=TRUE)
    
    ## Create sequence Index Plots sorted by MDS scores of multi-channel distances
    layout(matrix(c(1,2,3,4,4,4), 2, 3, byrow=TRUE), heights=c(3,1.5))
    seqIplot(child.seq, title="Children", sortv=mcsort.mds$points[,1], withlegend=FALSE)
    seqIplot(marr.seq, title="Married", sortv=mcsort.mds$points[,1], withlegend=FALSE)
    seqIplot(left.seq, title="Left parents", sortv=mcsort.mds$points[,1], withlegend=FALSE)
    seqlegend(child.seq, horiz=TRUE, position="top", xpd=TRUE)
    

1 个答案:

答案 0 :(得分:1)

我偶然发现了seqHMM包,其名称暗示了其他用途,但能够对多通道序列对象进行排序。因此,seqHMM是我第一个问题的答案。 以下是使用seqHMM的示例代码:

library(TraMineR)
library(seqHMM)

# Building sequence objects
data(biofam)

## Building one channel per type of event left, children or married
bf <- as.matrix(biofam[, 10:25])
children <-  bf==4 | bf==5 | bf==6
married <- bf == 2 | bf== 3 | bf==6
left <- bf==1 | bf==3 | bf==5 | bf==6

child.seq <- seqdef(children)
marr.seq <- seqdef(married)
left.seq <- seqdef(left)

mcplot <- ssp(list(child.seq, marr.seq, left.seq), 
            type = "I",title = "Sequence index plots",
            sortv = "from.start", sort.channel = 1,
            withlegend = FALSE, ylab.pos = c(1, 1.5, 1),
            ylab = c("Parenthood", "Marriage", "Residence"))
plot(mcplot)

您可以在以下文章中找到有关该软件包功能的更多信息:
赫兹克,萨图; Helske,Jouni(2016):用于序列数据的混合隐马尔可夫模型:R.Jyväskylä中的seqHMM包。可在https://cran.r-project.org/web/packages/seqHMM/vignettes/seqHMM.pdf在线获取。