hello R-masters. i have an R-issue here that i don't know if you'd wish to help me about it:
briefly i'd like to generate many (say hundred) realizations of a random walk, execute a few operations on each of them (mean time of return), and graph each realization on the same plot. IN OTHER WORDS I'D LIKE TO IMPOSE A LOOPING CYCLE TO THE COMMAND NOT THE ARGUMENT OF THE COMMAND. for some of these questions i have already a partial answer: my main problem here is automatizing the process for 100 times. my random walk generating function is: rwalk <- function(n,p, x0=0) { x <- rbinom(n,1,p) x[which(x==0)] <- -1 y<-c(x0,x) y <- cumsum(y) } THIS IS THE CODE THAT I'D LIKE TO REITERATE a<- rwalk(n,p,x0=0) #whish to generate 100 of those #and for each calculate the following o<-which(a==0) N<-length(o) # number of times the walk returns to 0 Nn<-(o[-1]-N) # number of steps to get to 0 V<-mean(Nn) # mean time of return to 0 par(mfrow=c(1,1)) plot(0:n, a, type= "l", main="...", xlab="tempo", ylab="stato", ylim=c(...), lwd=3, col="red", lty=1) par(new=T) plot(0:n, b, type= "l", main="", xlab="", ylab="", ylim=c(), lwd=3, col="green", lty=2) #technically i've tryed to fuse all those parts in a function reiterating k=100 times with a for cycle, but with no succes, and i dare there must be something really disturbing in the code below .... function(k){ for (i in 1:k){ a[i]<-rwalk(1e+04,0.5,0) o[i]<-which(a[i]==0) N[i]<-length(o[i]) Nn[i]<-(o[i][-1]-N[i]) V[i]<-mean(Nn[i]) w<-c(V[1]:V[k]) M<-mean(w) #mean over all the realizations par(mfrow=c(1,1)) plot(0:n, a[1], type= "l", main="...", xlab="tempo", ylab="stato", ylim=c(...), lwd=3, col="red", lty=1) par(new=T) plot(0:n, a[i], type= "l", main="", xlab="", ylab="", ylim=c(...), lwd=3, col=c(1:i), lty=2) } } #NOTE: I'VE ALSO TRIED TO REITERATE THE WALK WITH THE rep() # t<-rep(rwalk(1e+04,0.5,0),100) # then of course i have the problem of splicing this ridicolously #one milion long vector in 100 bits of tenthousand, and still goig through all the calculations and plotting i meant before.... i wonder what...... THANK YOU FOR YOUR PRECIUOS ATTENTION!!! ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.