1
votes

Is there a way to generate the transition matrix for all the sequences (ie the id) in a dataset ?

In my case, my data is in TSE format, so I use some functions of the TraMineRextras package.

My intention is to loop over each sequence, but when I want to compute transition rates for a given id, I get the following error after executing TSE_to_STS() function :

Error in 'rownames'<-('tmp', value = "1") : attempt to set 'rownames' on an object with no dimensions

It looks like a minimum of two sequences is expected in argument of 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

2 Answers

1
votes

Based on Gilbert explanations, here is my modified code. It create a identical sequence with a different id (=99). With the transitions rate of the two sequences identicals, the transiton matrix is the same as computed with one sequence. It works without create a dummy event.

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)
0
votes

The functions TSE_to_STS from TraMineRextras and seqtratefrom TraMineR are intended for a set of sequences and do not work with a single sequence. This is because internally they use functions intended for tables that do not work with vectors.

A workaround is to add a dummy sequence with a dummy event and to delete the dummy event from the resulting matrix of probability transitions.

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)]

Hope this helps.