首页 文章

计算TraMiner中每个序列的转换率

提问于
浏览
1

有没有办法为数据集中的所有序列(即id)生成转换矩阵?

在我的情况下,我的数据是TSE格式,所以我使用了 TraMineRextras 包的一些功能 .

我的意图是遍历每个序列,但是当我想计算给定id的转换速率时,执行 TSE_to_STS() 函数后会出现以下错误:

'rownames'中的错误< - ('tmp',value =“1”):尝试在没有维度的对象上设置'rownames'

看起来在 TSE_to_STS() 的参数中预计至少有两个序列 .

test.events <- c("A","B","C")
test.stm <- seqe2stm(test.events, dropList=list("A"=test.events[-1], B=test.events[-2], C=test.events[-3]))
test.tse <- data.frame(id = c(1,1,1), time = c(1,2,3), event = c("A","B","C"))
test.sts <- TSE_to_STS(test.tse, id = "id", timestamp = "time", event = "event", stm=test.stm, tmin=1, tmax=4, firstState="None")
test.seqdef <- seqdef(test.sts,informat = "STS")
seqtrate(test.seqdef)

2 回答

  • 0

    来自 TraMineRextrasTSE_to_STS 和来自 TraMineRseqtrate 的函数用于一组序列,不适用于单个序列 . 这是因为在内部它们使用的函数用于不适用于向量的表 .

    解决方法是添加具有虚拟事件的虚拟序列,并从结果的概率转换矩阵中删除虚拟事件 .

    test.events <- c("A","B","C","X")
    test.stm <- seqe2stm(test.events, dropList=list("A"=test.events[-1],
         B=test.events[-2], C=test.events[-3], X=test.events[-4]))
    test.tse <- data.frame(id = c(99,1,1,1), time = c(0,1,2,3), 
         event = c("X","A","B","C"))
    test.sts <- TSE_to_STS(test.tse, id = "id", timestamp = "time", 
         event = "event", stm=test.stm, tmin=1, tmax=4, firstState="None")
    test.seqdef <- seqdef(test.sts,informat = "STS")
    test.trate <- seqtrate(test.seqdef)
    test.trate[-nrow(test.trate),-ncol(test.trate)]
    

    希望这可以帮助 .

  • 1

    基于吉尔伯特的解释,这是我修改过的代码 . 它创建一个具有不同id(= 99)的相同序列 . 利用两个序列的转换率相同,转换矩阵与用一个序列计算的相同 . 它无需创建虚拟事件即可工作 .

    test.events <- c("A","B","C")
    test.stm <- seqe2stm(test.events, dropList=list("A"=test.events[-1], B=test.events[-2], C=test.events[-3]))
    test.tse <- data.frame(id = c(1,1,1), time = c(1,2,3), event = c("A","B","C"))
    test.tse.bis <- test.tse
    test.tse.bis[,1] <- 99
    test.tse <- rbind(test.tse,test.tse.bis)
    test.sts <- TSE_to_STS(test.tse, id = "id", timestamp = "time", event = "event", stm=test.stm, tmin=1, tmax=4, firstState="None")
    test.seqdef <- seqdef(test.sts,informat = "STS")
    seqtrate(test.seqdef)
    

相关问题