> -----Original Message-----
> From: [email protected] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Lorenzo Isella
> Sent: Tuesday, September 17, 2013 2:26 PM
> To: [email protected]
> Subject: [R] Generation of a Markov Chain
> 
> 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
> 

Seems to me that the code you point to requires you to set the initial state. 
So do something like this

y=numeric(100)
y[1]=1  # set initial state (to 1, 2, 3 or 4)

x=matrix(runif(16),4,4) 
 for(i in 2:100) { 
 y[i]=which(rmultinom(1, size = 1, prob = x[y[i-1], ])==1) 
 } 


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to