[R] simulate binary markov chain
Chris Oldmeadow
c.oldmeadow at student.qut.edu.au
Wed Dec 17 00:23:20 CET 2008
Hi all, I was hoping somebody may know of a function for simulating a
large binary sequence (length >10 million) using a (1st order) markov
model with known (2x2) transition matrix. It needs to be reasonably
fast. I have tried the following;
mc<-function(sq,P){
s<-c()
x<-row.names(P)
n<-length(sq)
p1<-sum(sq)/n
s[1] <- rbinom(1,1,p1);
for ( i in 2:n){
s[i] <- rbinom( 1, 1, P[s[i-1]+1] )
}
return(s)
}
P<-c(0.63,0.27)
x<-rbinom(500,1,0.5)
new<-mc(x,P)
thanks in advance!
Chris
More information about the R-help
mailing list