I'm having a problem using the group function in TraMineR. I have a data set that contains SPELL data, so multiple rows per case. I also have demographic data per case, at one row per case. I merge these together and end up with data that has a demographic covariate per row, so multiple rows per case. An example of this data would be:

id startmin stopmin activity educ4
4      1      20       work    HS
4      20     40       play    HS
8      1      15       sleep   College
8      15     40       work    College

I can make sequence data from this, but when I try to run a plot using the group command

      seqiplot(atus.seq, group = atus.seqdata$educ4, border=NA, 
               withlegend="right",  cex.plot=.55)

it tells me:

    >"Error: group must contain one value for each row in the sequence object".

I have gotten this to work with the example of the mvad data in the training manual, but I can't seem to get it to work with the groups, whether I link to the original demographic data, the merged data, or try to pass the covariates by seqformat and seqdef. Ideas?


1 Answer 1


The problem comes from your atus.seqdata$educ4 variable which is of length 4 while you have only two sequences. This is the normal result if you merge a table with one row per case with a table with multiple rows per case such as spell data.

When there are covariates in the SPELL file, those covariates should be aggregated into a table with one value per id. The following example with SPELL data including a covariate sex, demonstrates how this can be done.


## Small example of SPELL data
id    <- c(1,1,2,2,3,3,4,4)
start <- c(20,23,20,25,20,21,20,22)
end   <- c(23,26,25,26,21,26,22,26)
state <- c("A","B","A","B","A","B","A","B")
sex   <- c("M","M","F","F","F","F","M","M")

da.spell <- data.frame(id,start,end,state,sex)

## Transform sequences into STS 

da.sts <- seqformat(da.spell, id="id", begin="start", end="end", 
       status="state", from="SPELL", to="STS", process=FALSE)

## Aggregate sex covariate

da.v <- da.spell[,c("id","sex")]
## transform sex factor into a numeric variable for aggregation
sex.lev <- levels(da.v$sex) ## first save sex levels
    da.v$sex <- as.numeric(da.v$sex)
    ## now aggregate table of covariates
    da.av <- aggregate(da.v, by = list(da.v$id), FUN= min)[,-1]
## set aggregated sex as factor with original levels
da.av$sex <- factor(da.av$sex)
levels(da.av$sex) <- sex.lev

## Now we can use TraMineR functions

da.seq <- seqdef(da.sts, cnames=20:26)
seqiplot(da.seq, group=da.av$sex)

You can indeed put STS data and covariates in a same data.frame if you prefer

da.stsv <- cbind(da.sts, da.av)
da.stsv <- da.stsv[,-which(names(da.stsv)=="id")] ## deleting "id" column
  • $\begingroup$ This worked great, thanks! Merging the covariates into the sts data worked correctly. $\endgroup$
    – mCorey
    Sep 19, 2012 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.