我正在使用R中的markovchain
包和函数
mc<-markovchainFit(data)
我有一个可能性矩阵mc$estimate
,我想要围绕这些可能性。我怎么做?
另一个问题:如何将该矩阵写入文本文件或Excel?
我有这样的矩阵:
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
我会围绕那个2个原子,然后写入Excel或文本文件。怎么可能?
答案 0 :(得分:3)
mc$estimate
是以下类型的S4类,
# [1] "markovchain"
# attr(,"package")
# [1] "markovchain"
通过使用str
,您可以看到对象具有哪些可以打印/回合的插槽,
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"
由于您未提供数据,因此您的输出会略有不同。但您可能会看到插槽transitionMatrix
看起来像一个矩阵。我们来看看,
class(mc$estimate@transitionMatrix)
# [1] "matrix"
瞧!我们可以轻松地围绕矩阵,
print(round(mc$estimate@transitionMatrix, digits=2))
并存储它,
write.csv(mc$esimate@transitionMatrix, file = "transition_matrix.csv", row.names = FALSE)
希望这有帮助
答案 1 :(得分:0)
在write.csv
estimate
行mc$
中的拼写错误
write.csv(mc$estimate@transitionMatrix, file = "transition_matrix.csv", row.names = FALSE)
一旦修好就行了。