###############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