Reputation: 41
my transition probability matrix is like this
BP IP SP
BPBP 0.4586757 0.3772354 0.1640889
IPBP 0.3489484 0.4746654 0.1763862
SPBP 0.3756522 0.4162319 0.2081159
BPIP 0.3646061 0.4640000 0.1713939
IPIP 0.2666122 0.5654956 0.1678922
SPIP 0.3054187 0.4860427 0.2085386
BPSP 0.4125561 0.3974215 0.1900224
IPSP 0.2974337 0.5069415 0.1956247
SPSP 0.3576642 0.4333942 0.2089416
and the code to simulate a first order MC from it is
function(trans,initprob,N)
{
BrokerPosition <- c("BP", "IP", "SP")
mysequence<-character()
firstposition <- sample(BrokerPosition, 1, rep=TRUE, prob=initprob)
mysequence[1] <- firstposition
for (i in 2:N){
prevposition <- mysequence[i-1]
probabilities <- trans[,prevposition]
BPosition<- sample(BrokerPosition, 1, rep=TRUE, prob=probabilities)
mysequence[i] <- BPosition
}
return(mysequence)
}
but since this is a non square matrix I am getting an error of mismatch of probabilities ,any idea how to solve this
Upvotes: 2
Views: 661
Reputation: 32351
You are using your transition matrix in the wrong direction: try with
trans[ paste( mysequence[c(i-2,i-1)], collapse="" ), ]
An alternative would be to to convert your second order Markov chain into a first order one: for instance, the states after IPBP would be BPIP, BPBP, BPSP (and the other ones, IP* and SP*, would have zero probabilities). The transition matrix is then a 9*9 matrix with a lot of zeroes.
Upvotes: 2