-2

I'm using the markovchain package in R and the function

mc<-markovchainFit(data)

I have a propablity matrix mc$estimate and I want to round the propabilities. How do I do that?

Another question: How I can write that matrix to text file or Excel?

I have matrix like this:

 mc$estimate
MLE Fit 
 A  22 - dimensional discrete Markov Chain with following states 
 A B C D E F G H I J K L M N O P Q R S T Y Z 
 The transition matrix   (by rows)  is defined as follows 
            A           B           C           D           E           F
A 0.468053492 0.008172363 0.028974740 0.014858841 0.023031204 0.063150074
B 0.003191489 0.590425532 0.020212766 0.019148936 0.011702128 0.102127660
C 0.004054198 0.001707031 0.817134322 0.015896725 0.004374267 0.017497066
D 0.004519774 0.006214689 0.052824859 0.505367232 0.024011299 0.035310734
E 0.005132930 0.001710977 0.005396157 0.010002632 0.698078442 0.068570676
F 0.001155435 0.001386522 0.002195326 0.001675381 0.007683642 0.903347873
G 0.004933473 0.002690985 0.014800419 0.012856929 0.020032890 0.073105098
H 0.005486028 0.004114521 0.016629522 0.022458426 0.035487742 0.053317332
I 0.007445734 0.002271580 0.020570419 0.021327612 0.031423523 0.028899546
J 0.011885111 0.003796633 0.024430505 0.021294156 0.015351601 0.056949488
K 0.008743754 0.001784440 0.022127052 0.026945039 0.021234832 0.070663812
L 0.003227759 0.003026024 0.012507565 0.014726649 0.016743998 0.052854549
M 0.007148954 0.002560819 0.013551003 0.014511310 0.015258216 0.067008109
N 0.010998878 0.002918070 0.018406285 0.025140292 0.029405163 0.073400673
O 0.003787879 0.001578283 0.003787879 0.008207071 0.006313131 0.067866162
P 0.000000000 0.000000000 0.000000000 0.007518797 0.000000000 0.007518797
Q 0.005144695 0.004501608 0.003215434 0.012861736 0.013504823 0.052733119
R 0.009460298 0.003566998 0.022797767 0.024193548 0.015973945 0.095068238

I would round that whit 2 desimals and then write to Excel or text file. How it is possible?

  • Check here for the rounding https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html . For the saving you'll need to save the model output as a data frame and then use the "write.csv" command. – AntoniosK Aug 21 '15 at 17:20
  • That don't work. It says that: non-numeric argument to mathematical function – Kristian Vepsäläinen Aug 21 '15 at 17:28
  • You have to make sure you apply it on the right thing. You have to provide some more info on what you are doing. – AntoniosK Aug 21 '15 at 17:31
  • Welcome to SO. First of all you should read [here](http://stackoverflow.com/help/how-to-ask) about how to ask a good question; a good question has better changes to be solved and you to receive help. On the other hand a read of [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is also good. It explains how to create a reproducible example in R. Help users to help you by providing a piece of your data a desired output and things you have tried so far. – SabDeM Aug 21 '15 at 17:33
  • I add more information. My orginal data is in text file and it is a list letters. – Kristian Vepsäläinen Aug 21 '15 at 18:34

2 Answers2

3

The mc$estimate is a S4 class of the following type,

# [1] "markovchain"
# attr(,"package")
# [1] "markovchain"

by using str you can see what slots that object has that you could print/round,

str(mc)
# Formal class 'markovchain' [package "markovchain"] with 4 slots
#   ..@ states          : chr -----
#   ..@ byrow           : logi TRUE
#   ..@ transitionMatrix: num -----
#   .. ..- attr(*, "dimnames")=List of 2
#   .. .. ..$ : chr [1:x] ----
#   .. .. ..$ : chr [1:x] ----
#   ..@ name            : chr "MLE Fit"

Your output will be a little different since you did not provide the data. But you might see that the slot transitionMatrix looks like a matrix. Let us check,

 class(mc$estimate@transitionMatrix)
 # [1] "matrix"

Voila! We can easily round a matrix,

print(round(mc$estimate@transitionMatrix, digits=2))

and to store it,

write.csv(mc$esimate@transitionMatrix, file = "transition_matrix.csv", row.names = FALSE)

Hope this helps

Stereo
  • 1,148
  • 13
  • 36
  • I am getting "Error in is.data.frame(x) : trying to get slot "transitionMatrix" from an object of a basic class ("NULL") with no slots" at write.csv() – Anish Oct 19 '15 at 05:13
  • What package version are you using? – Stereo Oct 21 '15 at 20:52
0

It's a typo in the write.csv line for estimate after mc$

write.csv(mc$estimate@transitionMatrix, file = "transition_matrix.csv", row.names = FALSE)

once fixed it works.

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46