2016-09-08 5 views
0

私はmarkovchainListFitから行列を抽出しようとしていますが、できません。markovchainListFitのリストから行列を抽出する

library(markovchain) 
mat <- data.frame(A = c(rep(0, 10)),             
        B = c(40 ,37, 35 ,30, 27, 21, 15, 16, 21, 19),  
        C = c(10, 15, 20, 23, 44, 34, 47, 22, 37, 29), 
        D = c(1, 2, 3, 5, 9, 21, 8, 12, 17, 12))  

mat$A <- apply(mat, 1, function(x) 100 - sum(x))          

# Build sequence from mat 
tseq <- apply(t(mat), 2, function(x) rep(row.names(t(mat)), x)) 

# Fit Markov Matrices to sequences 
mcListFit <- markovchainListFit(data = tseq) 

私が試したもの:

> mcListFit$estimate[[1]] 

Unnamed Markov chain 
A 4 - dimensional discrete Markov Chain defined by the following states: 
A, B, C, D 
The transition matrix (by rows) is defined as follows: 
      A   B C D 
A 0.9387755 0.06122449 0.00 0.0 
B 0.0000000 0.85000000 0.15 0.0 
C 0.0000000 0.00000000 0.90 0.1 
D 0.0000000 0.00000000 0.00 1.0 

> as.matrix(mcListFit$estimate[[1]]) 
Error in as.vector(data) : 
    no method for coercing this S4 class to a vector 

> as.matrix(unlist(mcListFit$estimate[[1]])) 
Error in as.vector(data) : 
    no method for coercing this S4 class to a vector 

しかし、私はまだ行列のいずれかを抽出することができませんよ。これをどうやってやりますか?

+1

'mcListFit $推定[[1]] []'、 'mcListFit $推定、それをやった[[2]] []' ... – HubertL

+0

@HubertLうんを。回答と私はそれをチェックします。ありがとう!! – Vedda

答えて

0

このコードは助けることができる:

#allocate a generic list 
matrixList<-list() 
#sequentially fill the list with the matrices 
#using dim method to get the length of the estimates 
for (i in 1:dim(mcListFit$estimate)) { 
    myMatr<- mcListFit$estimate[[i]]@transitionMatrix 
    matrixList[[i]]<-myMatr 
} 
matrixList 
関連する問題