[R] Generation of a Markov Chain

Lorenzo Isella lorenzo.isella at gmail.com
Tue Sep 17 23:25:33 CEST 2013


Dear All,
While looking for a way to generate a Markov chain given a transition  
matrix, I found this

http://bit.ly/1a1CFl8

but the example provided does not work on my machine

> y<-numeric(100)
> x=matrix(runif(16),4,4) for(i in 2:100) {
+  y[i]=which(rmultinom(1, size = 1, prob = x[y[i-1], ])==1) }
Error in rmultinom(1, size = 1, prob = x[y[i - 1], ]) :
   too few positive probabilities

Any idea about how to fix this?
Cheers

Lorenzo



More information about the R-help mailing list