Joseph
Joseph

Reputation: 75

Exporting the Transition Matrix from a markovchain object

I had a sequence of states organized as a data frame that looks like this:

Year1 Year2 Year3 ...
1       2     5   ...
3       9     4   ...

I used markovchain's markovchainListfit function because I'd like to construct the transition matrix for this sequence of events, so I ran:

myModel <- markovchainListFit(data = myData) 

where myData is the data shown above.

The code seems to work, but I get back a markovchain object and I'm having trouble exporting only the transition matrix to a data frame or a matrix so I can analyze it. I know this is super simple but for some reason I'm not seeing it.

I have tried the following:

  1. Exporting the whole list to a txt file which didn't work:

    capture.output(summary(myModel), file = "myModel.txt")
    
  2. Accessing the elements of the list like this but I got an empty list:

    tranMatrix <- myModel$estimate@markovchains 
    
  3. I also tried coercing into a different object that I know how to work with, like a data frame using:

    as(myModel, from = "markovchain", to = "data.frame") 
    

    But I got an error

    (Error in as(myModel, from = "markovchain", to = "data.frame"): unused arguments (from = "markovchain", to = "data.frame")

Thanks!

Upvotes: 1

Views: 457

Answers (1)

Joseph
Joseph

Reputation: 75

To extract the transition matrix, I used:

myModel$estimate@markovchains[[1]]@transitionMatrix

Upvotes: 0

Related Questions