[R] Generation of a Markov Chain
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Tue Sep 17 23:58:13 CEST 2013
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Lorenzo Isella
> Sent: Tuesday, September 17, 2013 2:26 PM
> To: r-help at stat.math.ethz.ch
> 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
More information about the R-help
mailing list