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

Visualizing sequence of states according to an incidence date using TraMineR

我正在尝试生成一个与 ne2 状态序列相关的图,因为它与 ne3 中的发病日期相关(数据如下)。我有 2004-2015 年 11 年的数据。发病日期 (ne3$date_inc) 也在这 11 年期间内,但这些发病日期对于不同的 id 是不相等的。我想将发病日期作为参考,以便可以使用 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))

以下是如何使序列在其发生日期对齐。

我们首先将您的 SPELL 数据转换为 TraMineR 使用的 STS 格式。由于序列长于 100,我们必须指定将存储序列的 table 的最大列数 (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 天的很长的序列。

现在,我们需要移动序列以对齐它们的发生日期。这可以通过 TraMineRextras.

seqstart 函数来完成

偏移量是发生日期与其最小值之间的差异。所以我们将新的开始日期设置为

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)

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