Skip to content

Instantly share code, notes, and snippets.

@jebyrnes
Created December 7, 2011 23:06
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 jebyrnes/1445197 to your computer and use it in GitHub Desktop.
Save jebyrnes/1445197 to your computer and use it in GitHub Desktop.
Simulations of all predators and prey applying the p(eaten) formula for a probabilistic approach to food webs
###############FUNCTIONS
#If you find the 0 predators remaining formulation more intuitive
pEaten2<-function(Sp.max, E, prey.vec) {
1-mean(dhyper(0,prey.vec, Sp.max-prey.vec, Sp.max-E))
}
#vectorize it
pEaten2<-Vectorize(pEaten2, vectorize.args="E")
#generate all permutations of prey being eaten
allPermute<-function(S){
expand.grid(data.frame(t(matrix(rep(1:S, S), ncol=S, byrow=T))))
}
##############SIMULATIONS
maxS<-5
preybase<-allPermute(maxS)
res<-apply(preybase, 1, function(aperm) data.frame(E=0:maxS, ld=rep(sum(aperm)/(maxS*2), maxS+1), pEat=pEaten2(maxS, 0:maxS, as.numeric(aperm))))
names(res)<-1:length(res) #keep the lines together
res<-ldply(res) #list to data frame
###########PLOT
library(ggplot2)
ggplot(data=res, aes(x=E, y=pEat, colour=ld, group=.id)) +
geom_line(size=1.2, alpha=0.7) +
theme_bw(base_size=20) +
xlab("\nE (no. of extinctions)") +
ylab("P(eaten | E)\n") +
scale_colour_gradient("Linkage Density")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment