rmarkovtraminerpst

PST: Error in names(StCol) <- A : attempt to set an attribute on NULL


Consider the following code:

library(PST)
library(TraMineR)
library(RCurl)

x <- getURL("https://gist.githubusercontent.com/aronlindberg/c79be941bc86274f4526705600962789/raw/6e3ee8d464c97f1c26631d604de41ca97dc22159/sequence_data.csv")
data <- read.csv(text = x)
data.seq <- seqdef(data[,2:ncol(data)], missing = "%")
pstree(data.seq)

This generates the following error message:

Error in names(StCol) <- A : attempt to set an attribute on NULL

I cannot see any reason why this should generate an error. The data works fine with multiple TraMineR functions, e.g.

seqient(data.seq)

What is the reason for this error message? How can I overcome it?


Solution

  • The pstree function of PST expects a state sequence object with a valid non-null cpal attribute. A default cpal color palette is automatically assigned by the TraMineR seqdef function only as long as the alphabet contains no more than 12 elements. In your example data, the alphabet is of size 29. Therefore, you need to explicitly define the color palette.

    You can check that the code below (where I use package viridis to define the color palette) runs without error.

    library(PST)
    library(TraMineR)
    library(RCurl)
    library(viridis)
    x <- getURL("https://gist.githubusercontent.com/aronlindberg/c79be941bc86274f4526705600962789/raw/6e3ee8d464c97f1c26631d604de41ca97dc22159/sequence_data.csv")
    data <- read.csv(text = x)
    data[data=="%"] <- NA
    ## n: size of alphabet 
    n <- length(seqstatl(data[,2:ncol(data)]))
    ## defining color palette
    cpal <- viridis_pal(option = "D")(n)
    data.seq <- seqdef(data[,2:ncol(data)], cpal=cpal)
    seqdplot(data.seq)
    pst.tree <- pstree(data.seq)