Converting from Spell Format to STS when each individual has multiple, separate spells

85 views Asked by At

I am trying to convert data of this form to STS format in order to perform sequence analysis:

|Person ID |Spell |Start Month |End Month |Status (Economic Activity) |
| -------- |----- |------------|----------|---------------------------|            
|1|1|300|320|4|
|1|2|320|360|4|
|2|1|330|360|4|
|3|1|270|360|7|
|4|1|280|312|4|
|4|2|312|325|4|
|4|3|325|360|6|

Does anyone know how I can deal with the issue of multiple spells per person and somehow combine each spell for a given individual?

1

There are 1 answers

2
maraab On

You should have a look at TraMiner's excellent documentation. Particularly, the user guide is very helpful. There you would find a section on the seqformat function, which is exactly what you are looking for

library(TraMineR)

## Create spell data
data <-
  as.data.frame(
    matrix(
      c(1, 1, 300, 320, 4,   
        1, 2, 320, 360, 4, 
        2, 1, 330, 360, 4, 
        3, 1, 270, 360, 7, 
        4, 1, 280, 312, 4, 
        4, 2, 312, 325, 4, 
        4, 3, 325, 360, 6),
      ncol = 5, byrow = T)
  )

names(data) <- c("id", "spell", "start", "end", "status")


## Converting from SPELL to STS format with TraMineR::seqformat
data.sts <- 
  seqformat(data, from = "SPELL", to = "STS",
            id = "id", begin = "start", end = "end", status = "status",
                      process = FALSE)