首页 文章

使用TraMineR根据发生日期可视化状态序列

提问于
浏览
1

我正在尝试生成一个与 ne2 状态序列相关的图,因为它与 ne3 中的发生日期有关(下面的数据) . 我的数据跨越了2004年至2015年的11年 . 发病日期( ne3$date_inc )也在这11年内,但这些发病日期对于不同的身份不同 . 我希望将发病日期作为参考,以便可以使用 seqdplot 显示每个id在此发生日期之前和之后的状态分布,其中x轴随后根据发病日期(即之前的几个月)具有相互参考并在发病日期之后) . 但是,根据发生日期将状态日期引用为零会导致在发生之前发生的状态的负值 . 不知道是否可以使用 TraMineR 完成此操作?还是其他建议?

library(TraMineR)
ne2 <- structure(list(id = c(4885109L, 4885109L, 4885109L, 7673891L, 
    11453161L, 13785017L, 13785017L, 16400365L), status = structure(c(4L, 
    2L, 3L, 4L, 4L, 1L, 5L, 4L), .Label = c("A", "B", "C", "D", "E"
    ), class = "factor"), date_start = structure(c(12432, 15262, 
    15385, 12432, 12432, 12432, 14318, 12432), class = "Date"), date_end = structure(c(15262, 
    15385, 16450, 16450, 16450, 14318, 16450, 16450), class = "Date")), class = "data.frame", .Names = c("id", 
    "status", "date_start", "date_end"), row.names = c(NA, -8L))

ne3 <- structure(list(id = c(4885109L, 7673891L, 11453161L, 13785017L, 
        16400365L), date_inc = structure(c(15170, 13406, 13528, 13559, 
        15598), class = "Date")), .Names = c("id", "date_inc"), class = "data.frame", row.names = c(NA, 
        -5L))

1 回答

  • 1

    以下是如何使序列在其发生日期上对齐的方法 .

    我们首先将您的SPELL数据转换为 TraMineR 使用的STS格式 . 由于序列长于100,我们必须指定将存储序列的表的最大列数( limit ) . 所以我们首先计算序列的最大长度

    limit <- max(ne2$date_end) - min(ne2$date_start)
    

    现在我们将SPELL数据转换为STS形式

    ne2.sts <- seqformat(ne2, id='id', begin='date_start', end='date_end', status='status',
                         from='SPELL', to='STS', limit=as.numeric(limit), process=FALSE)
    
    dim(ne2.sts)
    ## [1]    5 4019
    

    请注意,由于以数据格式提供开始日期和结束日期,因此使用每日时间粒度 . 因此,我们得到了4019天的非常长的序列 .

    现在,我们需要移动序列以对齐它们的发生日期 . 这可以通过 TraMineRextrasseqstart 函数完成 .

    这种转变是发病日期和最小发生日期之间的差异 . 所以我们将新的开始日期设置为

    ne3$bd <- ne3$date_inc - min(ne3$date_inc) + min(ne2$date_start)
    

    我们加载 TraMineRextras 以获取对 seqstart 的访问权限

    library(TraMineRextras)
    

    我们移动序列,创建状态序列对象并用 seqdplot 绘制它 . 我们还在发病日期的天数内定义了x标签 .

    ne2.sts.a <- seqstart(ne2.sts, data.start=min(ne2$date_start), new.start=ne3$bd)
    inc.pos <- as.numeric(ne3$date_inc[1] - ne3$bd[1])
    xtlab <- 1:ncol(ne2.sts.a) - inc.pos + 1
    ne2.a.seq <- seqdef(ne2.sts.a, xtstep=365, cnames=xtlab)
    seqdplot(ne2.a.seq, border=NA)
    

    chronogram of shifted sequences

    请注意,由于序列的长度,生成绘图需要几分钟 . 我建议使用月度数据而不是每日数据 .

相关问题