#Build a food web from the niche model
#Import the niche model function
#You will need to modify the file path for the location of the file on your own computer
# source("~/Dropbox/jdyeakel.github.io/teaching/ecodyn/nichemodel_function.R")
nichemodel_function = function(S,C) {
#How many species?
S = S;
#Build a list of random niche values between zero and one
n = runif(S,0,1);
#Now just sort the species, so that species 1 has lowest n, and species S has highest n
n = sort(n)
#Define beta distribution parameters
a=1;
b=((1/(2*C)) - 1);
#What is the range of each species?
r = numeric(S);
for (i in 1:S) {
x = rbeta(1,a,b);
r[i] = n[i]*x;
}
#Plot the prey range vs. niche value of each species
# plot(n,r,xlab='Niche Value',ylab='Diet range')
#What is the center of the range for each consumer?
c = numeric(S);
for (i in 1:S) {
c[i] = runif(1,r[i]/2,n[i]);
}
#Now determine which prey each consumer eats
prey = list(S)
numprey = numeric(S)
for (i in 1:S) {
#Niche range for consumer i
nmin = c[i] - (r[i]/2);
nmax = c[i] + (r[i]/2);
#Find species greater than niche min
prey1 = which(n>nmin);
#Find species lower than niche max
prey2 = which(n