2

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?

chrisjacques
  • 635
  • 1
  • 5
  • 17
  • That doesn't look like the parameter for the usual definition of a 2nd order MC, which should have a transition matrix that is 25 x 25 for your example. Lots of entries will be zero, but there should be 125 non-zero entries, and you don't have enough parameters for that. I think you'll need to read the reference from the `?fitMarkovChain` help page to figure out what model they are assuming. – user2554330 Dec 19 '18 at 13:23
  • @user2554330 that's the way the clickstream package fits kth order markov chain, it's a simplified way that only needs k + km^2 parameters and follows the generalisation of Raftery's model proposed by Ching, Huang, Ng and Siu., see https://www.jstatsoft.org/article/view/v074i04/v74i04.pdf – chrisjacques Dec 19 '18 at 15:50

0 Answers0