I'm trying to model a population where each year the population is either growing quickly or slowly, to a max size of 5000. I then am running this while loop 5000 times to look at variation in time to 5000.
However, the loop just keeps running, and I have to stop it manually.
One odd thing is that popVector
, which is a vector that records the size of the population each round, grows to a tremendous size, often into the tens of thousands, which I would not predict from the numbers I'm using.
Any help to resolve this would be greatly appreciated!
MAXPOP <- 5000 #Set max population
trials <- 5000
genVector <- numeric(trials)
set.seed(1)
for(j in 1:trials) {
curr_pop <- 20 #Set initial population
genTime <- 1
popVector <- curr_pop;
while(curr_pop <= MAXPOP) {
if(genTime%%2 == 0) {
p <- 0.25
}
if(genTime%%2 != 0) {
p <- 0.5
}
curr_pop <- sum(rgeom(curr_pop, p)) #Set current population
popVector <- c(popVector, curr_pop)
genTime <- genTime + 1
}
genVector[j] <- genTime
}