R generate random sample using higher order markov chain

353 Views Asked by At

is there a way to generate a random sample from a higher order markov chain? I used the package clickstream to estimate a 2nd order markov chain and i'm now trying to generate a sample from it. I understand how to do this from a transition matrix with the randomClickstreams function but that would only work for a 1st order markov chain.

Here's a reproducible example where we generate a sample from a transition matrix and then fit a 2nd order markov chain on the sample:

trans_mat <- matrix(c(0, 0.2, 0.7, 0, 0.1,
                  0.2, 0, 0.5, 0, 0.3,
                  0.1, 0.1, 0.1, 0.7, 0,
                  0, 0.4, 0.2, 0.1, 0.3,
                  0, 0 , 0 , 0, 1), nrow = 5)


cls <- randomClickstreams(states = c("P1", "P2", "P3", "P4", "end"),
                          startProbabilities = c(0.5, 0.5, 0, 0, 0),
                          transitionMatrix = trans_mat,
                          meanLength = 20, n = 1000)

# fit 2nd order markov chain:
mc <- fitMarkovChain(clickstreamList = cls, order = 2,
                     control = list(optimizer = "quadratic"))

This is made of 2 transition matrices and 2 lambda parameters:

2nd order markov chain parameters

How can i then use these elements to create a random sample of say 10000 journeys?

0

There are 0 best solutions below