Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating transition-based sequences from state-based sequences

Tags:

r

I have a data frame with sequential 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  e  e  e  e  e
2  e  e  e  u  u
3  e  u  u  e  e

I would like to transform this data frame with state sequences into a data frame with transition 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 a starting state.

I would appreciate your help.

like image 689
POTENZA Avatar asked Feb 17 '23 22:02

POTENZA


2 Answers

Here's a way to create the data frame of transistions:

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
like image 172
Sven Hohenstein Avatar answered Apr 06 '23 01:04

Sven Hohenstein


Using the example data from TraMineR::seqetm

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

Your example appears to be the output of print.stslist which uses seqconc to create the sequences

so I will create this sequence manaully

actcal.seqconc <- seqconc(actcal.seq)

This is a matrix. So we can just apply this function to split on - then recombine with the transition states as you want. A function to do this is below:

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 the transition rates between states 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
like image 28
mnel Avatar answered Apr 05 '23 23:04

mnel