计算 TraMiner 中每个序列的转换率
Compute transition rates for each sequence in TraMiner
有没有办法为数据集中的所有序列(即id)生成转换矩阵?
在我的例子中,我的数据是 TSE 格式,所以我使用了 TraMineRextras
包的一些函数。
我的意图是遍历每个序列,但是当我想计算给定 id 的转换率时,在执行 TSE_to_STS()
函数后出现以下错误:
Error in 'rownames'<-('tmp', value = "1") :
attempt to set 'rownames' on an object with no dimensions
在 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)
TraMineRextras
中的函数 TSE_to_STS
和 TraMineR
中的 seqtrate
用于一组序列,不适用于单个序列。这是因为它们在内部使用了不适用于向量的表的函数。
解决方法是添加一个带有虚拟事件的虚拟序列,并从概率转换的结果矩阵中删除该虚拟事件。
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)]
希望对您有所帮助。
根据 Gilbert 的解释,这是我修改后的代码。它创建了一个具有不同 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)
有没有办法为数据集中的所有序列(即id)生成转换矩阵?
在我的例子中,我的数据是 TSE 格式,所以我使用了 TraMineRextras
包的一些函数。
我的意图是遍历每个序列,但是当我想计算给定 id 的转换率时,在执行 TSE_to_STS()
函数后出现以下错误:
Error in 'rownames'<-('tmp', value = "1") : attempt to set 'rownames' on an object with no dimensions
在 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)
TraMineRextras
中的函数 TSE_to_STS
和 TraMineR
中的 seqtrate
用于一组序列,不适用于单个序列。这是因为它们在内部使用了不适用于向量的表的函数。
解决方法是添加一个带有虚拟事件的虚拟序列,并从概率转换的结果矩阵中删除该虚拟事件。
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)]
希望对您有所帮助。
根据 Gilbert 的解释,这是我修改后的代码。它创建了一个具有不同 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)