Skip to content

Instantly share code, notes, and snippets.

@aammd
Created March 24, 2012 19:31
Show Gist options
  • Save aammd/2187007 to your computer and use it in GitHub Desktop.
Save aammd/2187007 to your computer and use it in GitHub Desktop.
A simulation of size-dependent diversity effects in experiments that test for diversity
## simulation of experiment
## to test patterns of randomization
## first set up the relationships
## volume is a uniform variable
n.mathoms <- 30
n.trts <- 3
vols.mathoms <- runif(n.mathoms,90,210)
## seven treatments (and seven replicates of each)
trts <- as.factor(rep(letters[1:n.trts],rep(n.mathoms/n.trts,n.trts)))
#potential abundance is a function of volume
## number of species is a function of volume *and* treatment:
trt.means <- seq(from=10,to=10+5*n.trts,by=5)
## with poisson errors
n.spp.mathom <- mapply(function(vol,n.spp) {
rpois(1,lambda=round(0.1*vol+n.spp))},
vols.mathoms,
trt.means[trts])
plot(n.spp.mathom~vols.mathoms)
plot(n.spp.mathom~trts)
# now with the number of species, get the distribution of abundances
# among species by multiplying the
brokestick <- function(n){
x <- runif(n)
x <- sort(x)
c(x,1)-c(0,x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment