[R] generating markov chain

Ingmar Visser I.Visser at uva.nl
Mon Feb 6 10:52:35 CET 2006


You can use the following, with x your transition matrix
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)
}
hth, ingmar

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Norman Goodacre
Sent: maandag 6 februari 2006 5:26
To: r-help at stat.math.ethz.ch
Subject: [R] generating markov chain


Dear help group,
  
    Just fyi a markov chain is a sequence of transitions between  states (say A,T,G,C - on a gene) with a given probability for each  transition. In this case there'd be 16 different kinds, each with a  different weight. 
    Given a transition matrix (4x4) filled with all transition  probabilities of course, how can I generate a random sequence of a  given length, say 200?
  
   -Norman Goodacre
  
		
---------------------------------

	[[alternative HTML version deleted]]

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list