## set user-defined parameters # arena size, should be 8<=s<=60, best at about 50 s = 50 # birth probability, must be 00) if (min(runif(M[i,j]))0) if (min(runif(M[i,j]))=K)) T1 = T dP = P[2:(T+1)]-P[1:T] y1 = dP T = min(T,which(dP<0)) T2 = T x = c(x,P[1:T]) y = c(y,dP[1:T]) ## end the run loop } ## define functions for data analysis # Define the function f(p,x) for the model y=qf(p,x), where p is the nonlinear parameter. f <- function(p,x) x*(p-x) # Define the function F(p) that determines RSS, given optimal q. F <- function(p) -(sum(y*f(p,x)))^2/sum((f(p,x))^2) ## analyze the data numpoints = length(x) # find 1-parameter best fit X = f(K,x) r1 = sum(X*y)/sum(X^2) RSS1 = sum(y^2)+F(K) AIC1 = numpoints*log(RSS1/numpoints)+2*2 # find 2-parameter best fit result <- optimize(f=F, lower=0.5*K, upper=2*K, maximum=FALSE) K2 <- result$minimum X = f(K2,x) r2 <- sum(X*y)/sum(X^2) RSS2 <- sum(y^2)+result$objective AIC2 = numpoints*log(RSS2/numpoints)+2*3 # plot data and models par(mfrow=c(2,2)) #1 plot(0:T,P[1:(T+1)],xlab="time",ylab="population",type="l") #2 plot(1:T,dP[1:T],xlab="time",ylab="change in population") #3 plot(x,y,xlab="population",ylab="change in population") X = 0:K2 Y1 = r1*f(K,X) if (curvefit==1) lines(X,Y1,col="blue") Y2 = r2*f(K2,X) if (curvefit==2) lines(X,Y2,col="red") #4 i4 = which(x>6) plot(x,y/x,xlab="population",ylab="relative change (dP/P)",ylim=c(0,2*r2*K2)) X[1] = 1 Y1[1] = r1*K Y2[1] = r2*K2 if (curvefit==1) lines(X,Y1/X,col="blue") Y2 = r2*f(K2,X) if (curvefit==2) lines(X,Y2/X,col="red") # report results for chosen fitting protocol K r1 K2 r2