I am a new user of R and I have tried to write a script for similuting species invasion and community stability. I have almost finished it and I have only one tiny problem in a loop.
I have a pool of 40 species (1,2,...) and I create a community by successive invasions. Species in the community leave the invaders pool unless they go extinct(i put a density threshold value).
I want a lot of invasions (>4000) so I created a vector with 4000 number between 1 and 40 (random.order) but I have a problem because my matrix with the species density (init.x) has not the same number of elements as my vector.
time<- list(start=0,end=4000,steps=100)
# Initial conditions (set all species to zero in the beginning)
init.x <- runif(n)*0
# generate random order in which species are introduced
init.order<- sample(1:n)
order<-rep(order,100)
random.order<-sample(order,size=length(order))
outt <- init.x
**for (i in 1:4000){
# Introduce 1 new species (according to vector "random.order") with freq 1000*tol
# if the species is not yet in the init.x matrix
if (init.x[random.order[i]]<tol) {init.x[random.order[i]] <- 1000*tol}**
# integrate lvm model
out <-n.integrate(time=time,init.x=init.x,model=lvm)
# save out and attach it to outt
outt <- rbind(outt,out)
# generate new time window to continue integration
time <- list(start=ti开发者_开发问答me$end, end = time$end+time$end-time$start,
steps=100)
}
I know this is probably very simple but I can't find out a way to write my loop to have more invasions than the number of species (number of raws in my matrix).
Thanks a lot,
You probably want to change
# Initial conditions (set all species to zero in the beginning)
init.x <- runif(n)*0
# generate random order in which species are introduced
init.order<- sample(1:n)
order<-rep(order,100)
random.order<-sample(order,size=length(order))
Into
# Initial conditions (set all species to zero in the beginning)
init.x <- rep.int(0, n) #should be a lot faster
# generate random order in which species are introduced
random.order<-sample.int(n,size=4000, replace=TRUE)
...to solve your main problem (check ?sample). I have not checked the rest of you code, but there may be room for more optimization.
I'm not clear on what your problem is, and what it going into outt. You may want to initalise it with list().
As for choosing a random invader you could try:
init.x[sample(which(init.x<tol),1)] <- 1000*tol
This avoids the if statement and the need for the pre-computed random trials (which may fail to produce an invasion if a community species is selected).
time<- list(start=0,end=1000,steps=1000)
# Initial conditions (set all species to zero in the beginning)
init.x <- runif(n)*0
# generate random order in which species are introduced
order <- sample(1:n)
outt <- init.x
for (i in 1:n){
# Introduce 1 new species (according to vector "order") with freq 1000*tol
init.x[order[i]] <- 1000*tol
# integrate lvm model
out <-n.integrate(time=time,init.x=init.x,model=lvm)
# save out and attach it to outt
outt <- rbind(outt,out)
# generate new time window to continue integration
time <- list(start=time$end, end = time$end+time$end-time$start,
steps=1000)
}
加载中,请稍侯......
精彩评论