Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Last active December 10, 2015 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CnrLwlss/4430666 to your computer and use it in GitHub Desktop.
Save CnrLwlss/4430666 to your computer and use it in GitHub Desktop.
R function for carrying out discrete stochastic simulations of the logistic population model. http://cnr.lwlss.net/DiscreteStochasticLogistic/ #R #Rstats #discrete #stochastic #logistic
simDSLogistic=function(K,r,N0){
# Unusually, for this model, we know the number of events a priori
eventNo=K-N0
# So we can just generate all required random numbers (quickly) in one go
unifs=runif(eventNo)
# Every event produces one cell and consumes one unit of nutrients
clist=(N0+1):K
# Simulate time between events by generating
# exponential random numbers using the inversion method
dts=-log(1-unifs)/(r*clist*(1-clist/K))
return(data.frame(t=c(0,cumsum(dts)),c=c(N0,clist)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment