[R] stochastic process

Bernardo Rangel Tura tura at centroin.com.br
Tue Oct 13 12:20:53 CEST 2009


On Tue, 2009-10-13 at 14:43 +0800, 刘哲 wrote:
> Hi,
>  
> I'm a student in China, and I never used R before.
>  
> I'm now wondering how to simulate a sample of Markov chain of ,say 500 entries with a certain transition matrix. 
>  
> Thanks a lot.

Hi

Well the naive script for you problem is something like this

#
# initial condition
#
initial<-matrix(c(300,200,0),nrow=1)
initial
     [,1] [,2] [,3]
[1,]  300  200    0

#
# transition matrix (third state is absortive)
#
transition<-matrix(c(
.5,.3,.2,
.3,.4,.3,
0,0,1
),nrow=3,byrow=T)
transition
     [,1] [,2] [,3]
[1,]  0.5  0.3  0.2
[2,]  0.3  0.4  0.3
[3,]  0.0  0.0  1.0

#
# first step
#
S1<-initial%*%transition
S1
     [,1] [,2] [,3]
[1,]  210  170  120

#
# Second step
#
S2<-S1%*%transition
S2
     [,1] [,2] [,3]
[1,]  156  131  213


If you use a large number of step is more pratical use command mtx.exp
of Biodem package, something like this


require(Biodem)
#
# 10th step direct
#
initial%*%mtx.exp(transition,10)


-- 
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil




More information about the R-help mailing list