Wow, that was really quick, thanks a lot. Will try this and get back to you.
Cheers, Umesh On Sun, Mar 22, 2009 at 3:52 PM, Jim Lemon <j...@bitwrit.com.au> wrote: > Umesh Srinivasan wrote: > >> Hi, >> >> I am trying to simulate animal movement in a gridded landscape made up of >> cells. At each time step (iteration), the animal moves from one cell to >> another in a random fashion. >> ... >> The problem is >> 1. I want to limit animals to a pre-defined circular home range >> 2. I want to limit animals to the borders of the landscape itself >> >> >> I tried to limit animals to their home range using: >> >> if (sqrt((x - a)^2 + (y - b)^2) > radius.range) { >> a <- a[i-1] >> b <- b[i-1] >> } >> >> Where x and y are co-ordinates for the centre of the home range >> >> But this is not working - giving NA values for x and y co-ordinates. Does >> anyone know what to do? >> >> > Hi Umesh, > The reason the above is generating NAs may be that you have defined x and y > outside the function you are using to simulate movement. In general, an > animal would slow down near the edges of a range, so something like this > might give you a more realistic simulated exploration: > > explore.circle<-function(x,y,radius,nsteps) { > > plot(0,xlim=c(x-radius-1,x+radius+1),ylim=c(y-radius-1,y+radius+1),type="n") > newx<-newy<-rep(NA,nsteps+1) > xyprob<-matrix(NA,ncol=6,nrow=nsteps+1) > newx[1]<-x > newy[1]<-y > for(astep in 1:nsteps) { > xprob<-c((newx[astep]-(x-radius))/radius,1,(x+radius-newx[astep])/radius) > newx[astep+1]<-newx[astep]+sample(-1:1,1,prob=xprob) > yprob<-c((newy[astep]-(y-radius))/radius,1,(y+radius-newy[astep])/radius) > newy[astep+1]<-newy[astep]+sample(-1:1,1,prob=yprob) > text(newx[astep],newy[astep],astep) > xyprob[astep,]<-c(xprob,yprob) > } > text(newx[nsteps+1],newy[nsteps+1],nsteps+1) > return(cbind(newx,newy,xyprob)) > } > > This is not guaranteed to stay inside the circle, so you might have to add > more constraints. > > Jim > > [[alternative HTML version deleted]] ______________________________________________ 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.