如何将回归线添加到 TraMineR 熵图中?

How to add a regression line to a TraMineR entropy plot?

我想知道是否以某种方式实现了向 TraMineR 熵图添加线性回归线。

示例:

library(TraMineR)
data(biofam)
set.seed(10)
biofam <- biofam[sample(nrow(biofam),300),]
biofam.lab <- c("Parent", "Left", "Married", "Left+Marr",
                "Child", "Left+Child", "Left+Marr+Child", "Divorced")
biofam.seq <- seqdef(biofam, 10:25, labels=biofam.lab)

seqplot(biofam.seq, type="Ht")

我想要这样的东西:

我知道我可以用seqient()

计算熵
biofam$ient <- seqient(biofam.seq, with.missing=TRUE)

但是由于数据是宽格式的,所以我不确定如何获取数据以便我可以采用 abline(lm(y ~ x)) 方法。

> head(biofam[c(1, 10:12, 28)])
     idhous ... a15 a16 a17   ... Entropy
1234     NA   ... 0   0   0 ... 0.3010904
1515  86261   ... 0   0   0 ... 0.3154649
276   17561   ... 0   0   0 ... 0.4012026
1212  69021   ... 0   0   0 ... 0.5517478
153   11391   ... 0   0   0 ... 0.2559298
1164  66641   ... 0   0   0 ... 0.4178207

另外 seqplot() 似乎没有像 plot():

那样提供数字 vector/matrix
p <- plot(sin, -pi, 2*pi, sub="ordinary plot")
s <- seqplot(biofam.seq, type="Ht")

> str(p)
List of 2
 $ x: num [1:101] -3.14 -3.05 -2.95 -2.86 -2.76 ...
 $ y: num [1:101] -1.22e-16 -9.41e-02 -1.87e-01 -2.79e-01 -3.68e-01 ...

> str(s)
 NULL

seqHtplotseqplot(..., type="Ht")渲染的横截面熵存储在seqstatd返回对象的$Entropy元素中。每个时间点的值就是该时间点截面状态分布的熵。所以每个时间点有一个值。

这与seqient返回的纵向熵不同。这里每个序列有一个值。该值为序列内状态分布的熵。

因此,要绘制回归线,您只需使用 seqstatd 提取横截面熵,定义时间变量,并在 abline(lm()):

中使用这些变量
stat.bf <- seqstatd(biofam.seq)
ent <- stat.bf$Entropy
time <- 1:length(ent)

seqplot(biofam.seq, type = "Ht")
abline(lm(ent ~ time), col = "red", lty = 2)