Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R library for discrete Markov chain simulation

I am looking for something like the 'msm' package, but for discrete Markov chains. For example, if I had a transition matrix defined as such

Pi <- matrix(c(1/3,1/3,1/3,
0,2/3,1/6,
2/3,0,1/2))

for states A,B,C. How can I simulate a Markov chain according to that transition matrix?

like image 799
stevejb Avatar asked May 02 '10 18:05

stevejb


1 Answers

A while back I wrote a set of functions for simulation and estimation of Discrete Markov Chain probability matrices: http://www.feferraz.net/files/lista/DTMC.R.

Relevant code for what you're asking:

simula <- function(trans,N) {
        transita <- function(char,trans) {
                sample(colnames(trans),1,prob=trans[char,])
        }

 sim <- character(N)
 sim[1] <- sample(colnames(trans),1)
 for (i in 2:N) {
  sim[i] <- transita(sim[i-1],trans)
 }

 sim
}

#example
#Obs: works for N >= 2 only. For higher order matrices just define an
#appropriate mattrans
mattrans <- matrix(c(0.97,0.03,0.01,0.99),ncol=2,byrow=TRUE)
colnames(mattrans) <- c('0','1')
row.names(mattrans) <- c('0','1')
instancia <- simula(mattrans,255) # simulates 255 steps in the process
like image 163
Fernando H Rosa Avatar answered Sep 23 '22 07:09

Fernando H Rosa