Professional Documents
Culture Documents
#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');
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)
yinit=c(3,4); yout=lsoda(yinit,x,model,parms);lines(yout[,2],yout[,3],lty=2);