You are on page 1of 5

Lecture 1

#geometric and exponential growth


x=seq(0,20,by=1); par(mfcol=c(2,1));
plot(x,10*exp(0.1*x),type= "l",xlab= "Days",ylab= "Abundance",main= "Exponential Growth",ylim=c(0,80));
lines(x,10*exp(-0.1*x))
plot(x,10*(1.1^x),type= "s",xlab= "Days",ylab= "Abundance",main= "Geometric Growth",ylim=c(0,80));
lines(x, 10*(0.9^x),type= "s")
#Human population growth in Billions
data.x=c(-10000,-8000,-7000,-6000,-5000,-4000,-3000,-2000,-1000,-750,-500,-400,-200,0,200,400,500,6
00,700,800,900,1000,1100,1200,1250,1300,1340,1400,1500,1600,1650,1700,1750,1800,1815,1850,190
0,1910,1920,1927,1930,1940,1950,1960,1970,1974,1980,1987,1990,1999,2000,2005,2007);
data.y=c(4,5,5,5,5,7,14,27,50,60,100,160,150,170,190,190,190,200,210,220,226,310,301,360,400,360,4
43,350,425,545,470,600,790,980,1000,1260,1650,1750,1860,2000,2070,2300,2400,3020,3700,4000,443
0,5000,5260,6000,6070,6500,6576);
x=data.x[37:length(data.x)]; y=data.y[37:length(data.y)];par(mfcol=c(1,1));
fit=glm(log(y)~x);
plot(x,y,pch=19,xlab="Year",ylab="Human population (Billions)");
lines(x,exp(fit$coefficient[[1]])*exp(fit$coefficient[[2]]*x))
#This R -code simulates logistic growth for a population undergoing continuously or discrete growth.
#continuous growth
par(mfrow=c(1,1)); library(odesolve);
model=function(t,y,parms){r=parms[1];K=parms[2];N=y;dN=r*N*(1-N/K);list(c(dN));}
x=seq(0,30,by=0.1); y0=0.1; r=0.5; K=100; out=lsoda(y0,x,model,c(r,K));
plot(out[,1],out[,2],type='l',xlab= 'Days',ylab= 'Population Abundance',main= 'Continuous
Growth',ylim=c(0,120));
y0=120; r=0.5; K=100; out=lsoda(y0,x,model,c(r,K));
lines(out[,1],out[,2])

#discrete growth
par(mfrow=c(2,1))
x=0:20; y=10;r=0.5;K=100; for(i in 2:length(x)){y[i]=y[i-1]+r*y[i-1]*(1-y[i-1]/K)}
plot(x,y,type='b',xlab= 'Days',ylab= 'Population Abundance',main= 'Steady-state Dynamics',ylim=c(0,120));
x=0:20; y=120;r=0.5;K=100; for(i in 2:length(x)){y[i]=y[i-1]+r*y[i-1]*(1-y[i-1]/K)};lines(x,y, type='b')
x=0:20; y=10;r=1.8;K=100; for(i in 2:length(x)){y[i]=y[i-1]+r*y[i-1]*(1-y[i-1]/K)}
plot(x,y,type='b',xlab= 'Days',ylab= 'Population Abundance',main= 'Damped Oscillations');
par(mfrow=c(3,1))
x=0:50; y=10;r=2.3;K=100; for(i in 2:length(x)){y[i]=y[i-1]+r*y[i-1]*(1-y[i-1]/K)}
plot(x,y,type='b',xlab= 'Days',ylab= 'Population Abundance',main= 'Single-point Cycles');
x=0:50; y=10;r=2.55;K=100; for(i in 2:length(x)){y[i]=y[i-1]+r*y[i-1]*(1-y[i-1]/K)}
plot(x,y,type='b',xlab= 'Days',ylab= 'Population Abundance',main= 'Double-point Cycles');
x=0:50; y=10;r=3;K=100; for(i in 2:length(x)){y[i]=y[i-1]+r*y[i-1]*(1-y[i-1]/K)}
plot(x,y,type='b',xlab= 'Days',ylab= 'Population Abundance',main= 'Chaotic Dynamics');
#Illustration of divergence of similar points under chaotic dynamics
par(mfrow=c(2,1))
x=0:20; y=10;r=2.3;K=100; for(i in 2:length(x)){y[i]= y[i-1]+r*y[i-1]*(1-y[i-1]/K)}
plot(x,y,type='b',xlab= 'Days',ylab= 'Population Abundance',main= 'Single-point Cycles, No=10,10.1');
x=0:20; y=10.1;r=2.3;K=100; for(i in 2:length(x)){y[i]=y[i-1]+r*y[i-1]*(1-y[i-1]/K)}
points(x,y,type='b',col= 'blue');

x=0:20; y=10;r=3;K=100; for(i in 2:length(x)){y[i]=y[i-1]+r*y[i-1]*(1-y[i-1]/K)}


plot(x,y,type='b',xlab= 'Days',ylab= 'Population Abundance',main= 'Chaotic Dynamics, No=10,10.1');
x=0:20; y=10.1;r=3;K=100; for(i in 2:length(x)){y[i]=y[i-1]+r*y[i-1]*(1-y[i-1]/K)}
points(x,y,type='b',col= 'blue');
#Time-delayed logistic
library(ddesolve)
model=function(t,y,parms) {
if (t < parms$tau) lag=parms$initial
else lag=pastvalue(t - parms$tau)

dy=parms$r*y[1]*(1-(lag[1]/parms$K))

return(dy)
}
# solve the dde system
par(mfrow=c(3,1));
yinit=c(10); parms=list(tau=2, r=0.1, K=100, initial=yinit); x=seq(0,100,0.1);
yout=dde(y=yinit,times=x,func=model,parms=parms)
plot(x,yout[,2], type='l',ylab= 'Population Abundance', xlab= 'Days', main= 'Time-delayed Logistic: steadystate dynamics')
yinit=c(10); parms=list(tau=2, r=0.5, K=100, initial=yinit); x=seq(0,100,0.1);
yout=dde(y=yinit,times=x,func=model,parms=parms)
plot(x,yout[,2], type='l',ylab= 'Population Abundance', xlab= 'Days', main= 'Time-delayed Logistic: damped
oscillations')
yinit=c(10); parms=list(tau=2, r=1, K=100, initial=yinit); x=seq(0,100,0.1);
yout=dde(y=yinit,times=x,func=model,parms=parms)
plot(x,yout[,2], type='l',ylab= 'Population Abundance', xlab= 'Days', main= 'Time-delayed Logistic: limit
cycle')
# Allee effect examples
library(odesolve)
model=function(t,y,parms) {
dy=parms$b*y*(y-parms$a)*(parms$K-y)/(parms$K^2)

return(list(dy))
}
a=25; b=6; K=100;
par(mfrow=c(2,1));
x=seq(0,K*1.1,length=100);
plot(x,b*x*(x-a)*(K-x)/(K*K),type= 'l',ylab='Total Growth Rate', xlab='Density',ylim=c(-50,150));
lines(x,b*x*(1-x/K),lty=3);lines(c(0,max(x)*2),c(0,0));
# simulate model
for(i in 1:50){
yinit=22+0.1*i; parms=list(a=a, b=b, K=K); x=seq(0,10,0.1);
yout=lsoda(yinit,x,model,parms)
if(i==1){plot(x,yout[,2],type='l',ylab='Population Abundance', xlab= 'Days', main= 'Dynamics with an Allee
effect',ylim=c(0,K*1.1))}
if(i>1){lines(x,yout[,2])}
}

# fit to human population dynamics

data.x=c(-10000,-8000,-7000,-6000,-5000,-4000,-3000,-2000,-1000,-750,-500,-400,-200,0,200,400,500,6
00,700,800,900,1000,1100,1200,1250,1300,1340,1400,1500,1600,1650,1700,1750,1800,1815,1850,190
0,1910,1920,1927,1930,1940,1950,1960,1970,1974,1980,1987,1990,1999,2000,2005,2007);
data.y=c(4,5,5,5,5,7,14,27,50,60,100,160,150,170,190,190,190,200,210,220,226,310,301,360,400,360,4
43,350,425,545,470,600,790,980,1000,1260,1650,1750,1860,2000,2070,2300,2400,3020,3700,4000,443
0,5000,5260,6000,6070,6500,6576);
x=data.x[14:length(data.x)]; y=data.y[14:length(data.y)];
fit=glm(log(y)~x);
par(mfrow=c(1,1)); plot(x,y,pch=19,xlab="Year",ylab="Human population (Billions)");
lines(x,exp(fit$coefficient[[1]])*exp(fit$coefficient[[2]]*x),lty=3,lwd=2);
a=0; b=4.9; K=1000000; yinit=100; parms=list(a=a, b=b, K=K); yout=lsoda(yinit,x,model,parms);
lines(x,yout[,2],lwd=2)
Lecture 2
#This R-code simulates a time-series, and computes the ACF
x=0:6000;


#a vector of time units from 0->1000 days
sd=1;

#standard deviation for the noise
s=0;


#linear trend in time-series
y=5+sin(x/10)+rnorm(length(x),0,sd)+s*x; #simulated data from a sin function with gaussian noise
par(mfcol=c(3,1))

#sets the graphic object to plot 2 graphs
plot(x,y,type='l',ylim=c(0,max(y)),xlab="Days",ylab="Population Density") #plots the time-series
lines(x,5+sin(x/10)+s*x, col="red");
#overlays the base sin function
y.bar=mean(y);
#average of y
ac0f1=0;n=length(y);
#initiate vector that will hold autocovariance values
for(i in 0:400){ac0f1[i+1]=sum((y[(1+i):n]-y.bar)*(y[1:(n-i)]-y.bar))/n}; #calculates autocovariance fcn
acf1=ac0f1/ac0f1[1];
#calculates ACF
plot(0:400,acf1,type='h',ylim=c(-1,1),xlab="Lag (days)",ylab="ACF") #plots the ACF
acf2=acf(y,lag.max=400)
#canned function in R
#This R-code explores a worked example of ACF & PACF for the Larch budmoth (Berryman & Turchin
2001). Uses the Yule-Walker method to estimate PACF & PRCF
x=1952:1979;

#years
y=c(4110, 70819, 346442, 136601, 22432, 2377, 89, 80, 390, 1808, 25028, 249449, 189715, 3487, 21, 2,
64, 214, 1167, 12284, 200392, 278313, 200392, 5404, 15, 9, 64, 239); #larch budmoth density
par(mfcol=c(3,1)); plot(x,y,type='b',log='y',pch=19,xlab="Year",ylab="log Density");
#plots timeseries
#calculates PACF
Y=log(y); pacf1=array(0,c(14,14)); n=length(Y);acf1=acf(Y,plot=F);acf1=acf1[[1]];acf1=acf1[2:length(acf1)]
for(i in 1:14){ if(i==1){pacf1[i,i]=acf1[i];}
if(i>=2){
den=0;num=0; for(j in 1:(i-1)){den=den+pacf1[i-1,j]*acf1[j];num=num+pacf1[i-1,j]*acf1[i-1-j+1];};
pacf1[i,i]=(acf1[i]-num)/(1-den);
for(j in 1:(i-1)){pacf1[i,j]=pacf1[i-1,j]-pacf1[i,i]*pacf1[i-1,i-1-j+1]}
}}
plot(acf1,xlim=c(1,14),type= 'h');lines(c(0,15),c(0,0));
lines(c(0,15),c(-2/sqrt(n), -2/sqrt(n)),lty=3,col= 'blue'); lines(c(0,15),c(2/sqrt(n), 2/sqrt(n)),lty=3,col= 'blue');
plot(diag(pacf1),type= 'h',xlab= 'Lag',ylab= 'PACF');lines(c(0,15),c(0,0));
lines(c(0,15),c(-2/sqrt(n), -2/sqrt(n)),lty=3,col= 'blue'); lines(c(0,15),c(2/sqrt(n), 2/sqrt(n)),lty=3,col= 'blue');
Lecture 3
#simulates matrix model and asymptotic growth
x=matrix(nrow=4,ncol=1,c(0.1,0,0,0));
A=matrix(nrow=4,ncol=4,c(0,0,10,5,0.4,0,0,0,0,0.5,0,0,0,0,0.8,0),byrow=T)

time=1:20; #number of years to simulate


n=array(-1,c(4,length(time))) #storage array for plotting
for(i in time){n[,i]=x;x=A%*%x;}
#plot of stage dynamics
matplot(as.matrix(time),t(n),type='l')
#plot of calculated lambda
time=1:50; #number of years to simulate
n=array(-1,c(4,length(time))) #storage array for plotting
for(i in time){n[,i]=x;x=A%*%x;}
N=colSums(n); plot(time[1:(length(time)-1)],N[2:length(N)]/N[1:(length(N)-1)],type='b',ylab=
'Growth',xlab='Generations'); lambda=eigen(A)$values[1];
lines(c(min(time),max(time)),c(lambda,lambda),col= ' red ')
#plot of transient versus asymptotic dynamics
time=1:20; #number of years to simulate
n=array(-1,c(4,length(time))) #storage array for plotting
lambda=eigen(A)$values[1]
plot(time,10*(lambda^time),lwd=2,type='l',col= ' red ')
for(j in 1:20){
no= runif(4,0,1);
x=matrix(nrow=4,ncol=1,no*10/sum(no));
for(i in time){n[,i]=x;x=A%*%x;}
lines(time,colSums(n))}
Lecture 4
#Lotka-Voltera competition model
library(odesolve)
model=function(t,y,parms) {
r1=parms$r1; r2=parms$r2; K1=parms$K1; K2=parms$K2;a1=parms$a1; a2=parms$a2;
dy1=r1*y[1]*(K1-y[1]-a1*y[2])/K1;

dy2=r2*y[2]*(K2-y[2]-a2*y[1])/K2;

return(list(c(dy1,dy2)))
}
r1=1;r2=1;K1=1;K2=2;a1=0.2;a2=0.2;
x=seq(0,100,0.1);yinit=c(0.1,0.1); parms=list(r1=r1,r2=r2,K1=K1,K2=K2,a1=a1,a2=a2);
yout=lsoda(yinit,x,model,parms)
par(mfcol=c(2,1));
plot(x,yout[,2],type='l',ylab='Population Abundance', xlab= 'Days',ylim=c(0,max(yout[,2:3])));
lines(x,yout[,3],col= 'blue')
#phase plot
plot(yout[,2],yout[,3],type='l',ylab='N2', xlab= 'N1',ylim=c(0,4),xlim=c(0,4))
#adds isoclines & equilibrium
lines(seq(0,4,0.1),(K1-seq(0,4,0.1))/a1,col= 'blue',lwd=2)
lines(seq(0,4,0.1),K2-a2*seq(0,4,0.1),col= 'red',lwd=2)
yinit=c(0.1,4); yout=lsoda(yinit,x,model,parms);lines(yout[,2],yout[,3],lty=2);
yinit=c(4,0.1); yout=lsoda(yinit,x,model,parms);lines(yout[,2],yout[,3],lty=2);
yinit=c(4,4); yout=lsoda(yinit,x,model,parms);lines(yout[,2],yout[,3],lty=2);
yinit=c(1,4); yout=lsoda(yinit,x,model,parms);lines(yout[,2],yout[,3],lty=2);
yinit=c(2,4); yout=lsoda(yinit,x,model,parms);lines(yout[,2],yout[,3],lty=2);

yinit=c(3,4); yout=lsoda(yinit,x,model,parms);lines(yout[,2],yout[,3],lty=2);

You might also like