Creating sequences based on transitions from state sequences

I have a data frame with serial data:
df <- data.frame(
t1=c("e","e","e"),t2=c("e","e","u"),t3=c("e","e","u"),t4=c("e","u","e"),t5=c("e","u","e"))

which looks like

 > df t1 t2 t3 t4 t5 1 eeeee 2 eeeuu 3 euuee 

I would like to convert this data frame with state sequences into a data frame with transient sequences as follows:

 > dfNew t1 t2 t3 t4 t5 1 se ee ee ee ee 2 se ee ee eu uu 3 se eu uu ue ee 

where "s" indicates the initial state.

I would be grateful for your help.

+4
source share
2 answers

Here you can create a data frame of transistors:

 setNames(as.data.frame(t(apply(df, 1, function(x) paste(c("s", head(x, -1)), x, sep = "")))), names(df)) t1 t2 t3 t4 t5 1 se ee ee ee ee 2 se ee ee eu uu 3 se eu uu ue ee 
+5
source

Using the sample data from TraMineR::seqetm

 data(actcal) actcal.seq <- seqdef(actcal,13:24, labels=c("FullTime", "PartTime", "LowPartTime", "NoWork")) 

Your example looks like the output of print.stslist , which uses seqconc to create sequences

so i will create this sequence manaully

 actcal.seqconc <- seqconc(actcal.seq) 

This is the matrix. Therefore, we can simply apply this function to divide by - and then recombine with the transition states as you want. Below is the function:

 transitions <- function(x, start = 'S') { x <- unlist(strsplit(x, '-') paste0(c(start, head(x, -1)), x, collapse = '-') } actcal.tseq <- as.matrix(apply( actcal.seqconc, 1, transitions)) 

If you want state transition rates to use seqtrate

 seqtrate(actcal.seq) [>] computing transition rates for states A/B/C/D ... [-> A] [-> B] [-> C] [-> D] [A ->] 0.986991870 0.005203252 0.001084011 0.006720867 [B ->] 0.009700665 0.970343681 0.007760532 0.012195122 [C ->] 0.005555556 0.014814815 0.934259259 0.045370370 [D ->] 0.008705580 0.006279435 0.014985015 0.970029970 
+3
source

All Articles