0

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!

Parfait
  • 104,375
  • 17
  • 94
  • 125
Joseph
  • 65
  • 1
  • 8
  • [Docs](https://www.rdocumentation.org/packages/markovchain/versions/0.6.9.12/topics/markovchainListFit) indicate the method returns a list of two slots: estimate and name. Please `print(myModel)`and/or run `str(myModel)`. – Parfait Sep 29 '18 at 21:48
  • Thansk @Parfait. I tried that but when you mentioned it again I finally figured it out: tranMatrix <- myModel$estimate@markovchains[[1]]@transitionMatrix. THANK YOU!! – Joseph Sep 29 '18 at 22:06

1 Answers1

0

To extract the transition matrix, I used:

myModel$estimate@markovchains[[1]]@transitionMatrix
Joseph
  • 65
  • 1
  • 8