public
Created

Simulations of all predators and prey applying the p(eaten) formula for a probabilistic approach to food webs

  • Download Gist
allPredPrey.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
###############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")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.