[R] statistics help

Rohan Saldanha rohansaldanh at hotmail.com
Thu Nov 23 20:07:54 CET 2006


hi

im a bioinformatics student as i have never had any previous programming 
experience
i need help

this is the question i need to answer:

Random walk model

we want to model a random walk where you take a step to the left with 
probability p
and one to the right with probability 1-p. Now assume that there is a line 
of 11 squares.
once you are in square 0 or in square 10 the walk ends. The aim of this 
problem set is
to write a simulation for the random walk and analyse its dynamics

1. write a simulation for a random walk which allows you to calculate the 
probability of ending
     up in square 0 starting from any other square.

2. analyse the probability of ending up in square 0 starting from any other 
square. Also calculate the mean time until you have reached square 1 for p= 
0.1,0.2,0.3,0.4 and 0.5. What is the probability of reaching square 10 for 
these parameters
hint simulate each scenario 1000 times and plot on histogram


this is the code that i have come up with but its not working very well.

rw<-function(sw,p,nrep){
	Z=0
	T=0
	count=0
       for (i in 1:nrep)
   {
      n<-0
      s=sw

      while (s>0 & s<10)
     {
         x<-runif(1, min=0, max=1)
         if (x<p) {s<-s-1}
         else{s<-s+1}
         print (s)
         n<-n+1
     }
     count<-count+n
     print (count)

     if (s==0) {Z<-Z+1}
     else {T<-T+1}
   }
     PrZ=Z/nrep
     print(c("PrZ",PrZ),quote=FALSE)
}

if you could shed some light on this it woul be really helpful. please let 
me know how much
you would like as payment aswell.

thanks
rohan

_________________________________________________________________
Eat well and eat right. Get tips on nutrition from Naini Setalvad



More information about the R-help mailing list