################################################### ### chunk number 1: ################################################### source("chapskel.R") library(emdbook) library(plotrix) library(bbmle) ################################################### ### chunk number 2: ################################################### data(ReedfrogSizepred) attach(ReedfrogSizepred,warn=FALSE) logist2 <- function(x,sizep1,sizep2,sizep3) { exp(sizep1*(sizep3-x))/(1+exp(sizep2*sizep1*(sizep3-x))) } ricker <- function(x,a,b) { a*x*exp(-b*x) } powricker <- function(x,a,b,g) { b*(x/a*exp(1-x/a))^g } tricker <- function(x,a,b,t,min=0.0001) { ifelse(xs[1])),adj=0,yoff=0.21,cex=eqcex) box(col="gray") ## ##curve(ifelse(x<3,1,1+0.75*(x-3)),from=0,to=10,ylim=c(0,8),axes=FALSE,ann=FALSE) curve(ifelse(x<5,x,5),from=0,to=10,xlim=c(-1,12),ylim=c(0,8),axes=FALSE,ann=FALSE) corner.label("hockey stick:",adj=0) corner.label(expression(paste(f(x)==a*x," if ",xs[1])),adj=0,yoff=0.21,cex=eqcex) segments(5,0.5,5,5.5,col="gray",lty=2) segments(c(1,2),c(1,1),c(2,2),c(1,2),col="gray") text(2.5,1.5,"a") text(5,0,expression(s[1])) text(11,5,expression(a*s[1])) box(col="gray") ## a=2 s1=4 b=0.5 curve(ifelse(xs[1])), adj=0,yoff=0.21,cex=eqcex) segments(s1,0.5,s1,9,col="gray",lty=2) segments(c(1,2),c(a,a),c(2,2),c(a,2*a),col="gray") x1=10 x2=12 y1 = a*s1-b*(x1-s1) y2 = a*s1-b*(x2-s1) segments(c(x1,x1),c(y1,y2), c(x1,x2),c(y2,y2),col="gray") text(x1-0.2,(y1+y2)/2,"-b",adj=1) text(2.5,3,"a") text(4,0,expression(s[1])) box(col="gray") ## ## splines? x1 <- 1:6 y1 <- c(0,2,4,1,2,3) s1 <- splinefun(x1,y1) curve(s1,axes=FALSE,ann=FALSE,from=0.5,to=6.5,ylim=c(0,5)) points(x1,y1,pch=16) #y2 <- c(1,1.5,2,2.1,2.2,2.3) #points(x1,y2,pch=1) #s2 <- splinefun(x1,y2) #curve(s2,add=TRUE,lty=2) corner.label("splines:",adj=0) corner.label("f(x) is complicated",adj=0,yoff=0.13,cex=eqcex) box(col="gray") ################################################### ### chunk number 17: ################################################### par(mfrow=c(2,2),mar=c(0,0,0,0),lwd=2,cex=1.5,xaxs="i",yaxs="i") a=2 b=1 curve(a/(b+x),from=0,to=15,ylim=c(0,3),xlim=c(-2,15),axes=FALSE,ann=FALSE) corner.label("hyperbolic:",adj=0) corner.label(expression(f(x)==frac(a,b+x)),adj=0,yoff=0.17,cex=eqcex) text(-1.5,a/b,expression(frac(a,b)),adj=0) y1=a/(2*b) text(-1.5,y1,expression(frac(a,2*b)),adj=0) segments(0.2,y1,3,y1,lty=2,col="gray") x1=b segments(b,0.25,b,y1+0.25,lty=2,col="gray") text(x1,0.1,"b") box(col="gray") ## a=1 b=1 curve(a*x/(b+x),from=0,to=10,ylim=c(0,b*1.4), xlim=c(0,11),axes=FALSE,ann=FALSE) corner.label("Michaelis-Menten:",adj=0) corner.label(expression(f(x)==frac(a*x,b+x)),adj=0,yoff=0.17,cex=eqcex) text(10.9,a,"a",adj=1) segments(0,a,10,a,lty=2,col="gray") x1=0.02 x2=0.4 y1=a*x1/(b+x1) y2=a*x2/(b+x2) segments(c(x1,x2),c(y1,y1),c(x2,x2),c(y1,y2),col="gray") text(x2+0.04,(y1+y2)/2,expression(frac(a,b)),adj=0,cex=0.7) x1=b y1=a/2 segments(0,y1,4,y1,lty=2,col="gray") text(4.1,y1,adj=0,expression(frac(a,2))) segments(x1,0.15,x1,y1,lty=2,col="gray") text(x1+0.1,0.1,"b",adj=0) box(col="gray") ## a=1 b=1 curve(a*x^2/(b^2+x^2),from=0,to=5.5,ylim=c(0,b*1.4), xlim=c(0,6),axes=FALSE,ann=FALSE) corner.label("Holling type III:",adj=0) corner.label(expression(f(x)==frac(a*x^2,b^2+x^2)),adj=0,yoff=0.18,cex=eqcex) segments(0,a,5.5,a,lty=2,col="gray") text(5.9,a,"a",adj=1) text(b,0.05,"b") segments(b,0.1,b,a/2,lty=2,col="gray") segments(0,a/2,b+0.5,a/2,lty=2,col="gray") text(b+0.55,a/2,expression(frac(a,2)),adj=0) box(col="gray") ## a=1 b=1 c=-1.5 curve(a*x^2/(b+c*x+x^2),from=0,to=5.5,ylim=c(0,3.5), xlim=c(0,6),axes=FALSE,ann=FALSE) corner.label("Holling type IV (c<0):",adj=0) corner.label(expression(f(x)==frac(a*x^2,b+c*x+x^2)),adj=0,yoff=0.18,cex=eqcex) segments(0,a,5.5,a,lty=2,col="gray") text(5.9,a,"a",adj=1) segments(-2*b/c,0.9,-2*b/c,2.8,col="gray",lty=2) text(-2*b/c,0.35,expression(frac(-2*b,c))) ## text(b,0.05,"b") ## segments(b,0.1,b,a/2,lty=2,col="gray") ## segments(0,a/2,b+0.5,a/2,lty=2,col="gray") ## text(b+0.55,a/2,expression(frac(a,2)),adj=0) box(col="gray") ################################################### ### chunk number 18: ################################################### op <- par(mfrow=c(2,2),mar=c(0,0,0,0),lwd=2,cex=1.5,xaxs="i",yaxs="i") a=1 b=1 curve(a*exp(-b*x),from=0,to=3,ylim=c(0,1.3),axes=FALSE,ann=FALSE, xlim=c(-0.5,3)) corner.label("negative exponential:",adj=0) corner.label(expression(paste(f(x)==a*e^{-b*x})),adj=0,yoff=0.13,cex=eqcex) text(-0.3,1,adj=1,"a") segments(-0.25,a*exp(-1),1.4,a*exp(-1),col="gray",lty=2) text(-0.3,a*exp(-1),adj=1,expression(frac(a,e))) segments(1/b,0.26,1/b,a*exp(-1),col="gray",lty=2) text(1/b,0.15,expression(frac(1,b))) box(col="gray") ## a=1 b=1 xmax=7 curve(a*(1-exp(-b*x)),from=0,to=xmax,ylim=c(0,1.5),axes=FALSE,ann=FALSE) curve(a*x/(b+x),from=0,to=xmax,add=TRUE,col="gray") text(xmax*0.8,0.75,"M-M",col="gray") corner.label("monomolecular:",adj=0) corner.label(expression(f(x)==a*(1-e^{-b*x})), adj=0,yoff=0.13,cex=eqcex) segments(0.3,a,10,a,col="gray",lty=2) text(0,a,adj=0,"a") x1=0.05 x2=0.3 y1=a*(1-exp(-b*x1)) y2=a*(1-exp(-b*x2)) segments(c(x1,x2),c(y1,y1), c(x2,x2),c(y1,y2),col="gray") text(x2+0.1,(y1+y2)/2,adj=0,expression(a*b)) box(col="gray") ## a=1 b=1 curve(a*x*exp(-b*x),from=0,to=6,ylim=c(0,0.5),axes=FALSE,ann=FALSE) corner.label("Ricker:",adj=0) corner.label(expression(f(x)==a*x*e^{-b*x}), adj=0,yoff=0.13,cex=eqcex) x1=0.01 x2=0.2 y1=a*x1*exp(-b*x1) y2=a*x2*exp(-b*x2) segments(c(x1,x2),c(y1,y1), c(x2,x2),c(y1,y2),col="gray") text(x2+0.1,(y1+y2)/2,adj=0,expression(a)) y1 = a/b*exp(-1) segments(1/b,0.1,1/b,y1,col="gray",lty=2) text(1/b,0.05,expression(frac(1,b))) segments(0,y1,1/b+1,y1,col="gray",lty=2) text(1/b+1+0.1,y1,adj=0,expression(frac(a,b)*e^{-1})) box(col="gray") ## a=-5 b=1 curve(plogis(a+b*x),from=0,to=10,ylim=c(0,1.2),axes=FALSE,ann=FALSE) corner.label("logistic:",adj=0) corner.label(expression(f(x)==frac(e^{a+b*x},1+e^{a+b*x})), adj=0,yoff=0.17,cex=eqcex) x1=-a-0.5 x2=-a+0.5 y1=plogis(a+b*x1) y2=plogis(a+b*x2) segments(c(x1,x2),c(y1,y1), c(x2,x2),c(y1,y2),col="gray") text(x2+0.1,(y1+y2)/2,adj=0,expression(frac(b,4))) segments(-a/b,0.25,-a/b,0.5,col="gray",lty=2) text(-a/b-0.2,0.11,expression(-frac(a,b)),adj=0.5) segments(-a/b-1,0.5,-a/b,0.5,col="gray",lty=2) text(-a/b-1.2,0.5,adj=1,expression(frac(1,2))) segments(10,1,7,1,col="gray",lty=2) text(6.9,1,adj=1,1) box(col="gray") ################################################### ### chunk number 19: ################################################### op <- par(mfrow=c(2,2),mar=c(0,0,0,0),lwd=2,cex=1.5,xaxs="i",yaxs="i") a=1 b=2.5 curve(a*x^b,from=0,to=3,ylim=c(0,20),axes=FALSE,ann=FALSE) a2=10 b2=0.5 curve(a2*x^b2,from=0,to=3,add=TRUE,lty=2) a3=2 b3=-1 curve(a3*x^b3,from=0,to=3,add=TRUE,lty=3) corner.label("power laws:",adj=0) corner.label(expression(f(x)==a*x^b), adj=0,yoff=0.13,cex=eqcex) text(1.95,15,expression(paste(01),adj=0) text(2,2.4, expression(b<0)) box(col="gray") ## a <- 1 k <- 1 d <- 2/3 xmax=15 curve(a*(1 - exp(-k*(a-d)*x))^(1/(1-d)),from=0,to=xmax,ylim=c(0,1.3), ann=FALSE,axes=FALSE) corner.label("von Bertalanffy:",adj=0,yoff=0.06) corner.label(expression(f(x)==a*(1-e^{-k*(a-d)*x})^(1/(1-d))),adj=0,yoff=0.145,cex=eqcex) segments(xmax-5,a,xmax,a,lty=2,col="gray") text(xmax-5.5,a,adj=1,"a") box(col="gray") ## Shepherd, Hassell ## curve(x/(1+x)^1.8,from=0,to=3,ylim=c(0,0.45),axes=FALSE,ann=FALSE,lty=2) curve(0.6*x/(1+x^1.8),add=TRUE,lty=1) curve(0.8*x*exp(-x),add=TRUE,col="gray") text(2,0.13,"Ricker",col="gray",adj=0) corner.label("Shepherd, Hassell:",adj=0) corner.label(expression(list(f(x)==frac(a*x,b+x^c),f(x)==frac(a*x,(b+x)^c))),adj=0,yoff=0.17,cex=eqcex) text(2.8,0.3,"H") text(2.8,0.2,"S") box(col="gray") ## xi <- 0.9 alpha <- 1 pmax <- 20 curve(1/(2*xi)*(alpha*x+pmax-sqrt((alpha*x+pmax)^2-4*xi*alpha*x*pmax)),from=0,to=200,ylim=c(0,25), ann=FALSE,axes=FALSE) curve(20*x/(x+10),add=TRUE,col="gray") corner.label("non-rectangular\nhyperbola:",adj=0,yoff=0.09) text(154,17.7,"M-M",col="gray") box(col="gray") ################################################### ### chunk number 20: eval=FALSE ################################################### ## library(odesolve) ## tlfun = function(parms) { lsoda(y=0.1,times=seq(0,10,by=0.1),parms=parms, ## func=function(t,y,parms) { r=parms[1]; K=parms[2]; theta=parms[3]; ## list(r*y*(1-(y/K)^theta),NULL) }) } ## x1 = tlfun(c(r=1,K=1,theta=1)) ## x2 = tlfun(c(r=1,K=1,theta=2)) ## x3 = tlfun(c(r=1,K=1,theta=0.5)) ## matplot(x1[,1],cbind(x1[,2],x2[,2],x3[,2]),lty=1:3,col=1,type="l", ## xlab="Time",ylab="Pop. density") ## text(c(2.25,3.38,5.5),c(0.8,0.84,0.75), ## c(expression(theta==2),expression(theta==1),expression(theta==0.5))) ################################################### ### chunk number 21: ################################################### ## Richards curve: richards <- function(x,a,k,delta,gamma) { if (delta==1) { k*exp(-exp(-k*(x-gamma))) } else { a*(1+(delta-1)*exp(-k*(x-gamma)))^(1/(1-delta)) } } ################################################### ### chunk number 22: ################################################### curve(2*x/(1+x)) ################################################### ### chunk number 23: ################################################### micmen <- function(x,a=2,b=1) { a*x/(b+x) } ################################################### ### chunk number 24: ################################################### curve(micmen(x),from=0,to=8,ylim=c(0,10)) curve(micmen(x,b=3),add=TRUE,col=2) curve(micmen(x,a=8),add=TRUE,col=3) abline(h=8) ################################################### ### chunk number 25: ################################################### xvec <- seq(0,10,by=0.1) ################################################### ### chunk number 26: ################################################### curve(ifelse(x<5,1,2),from=0,to=10) ################################################### ### chunk number 27: ################################################### curve(ifelse(x<5,1+x,6-3*(x-5)),from=0,to=10) ################################################### ### chunk number 28: ################################################### curve(ifelse(x<5,1+x, ifelse(x<8, 6-3*(x-5), -3+2*(x-8))),from=0,to=10) ################################################### ### chunk number 29: ################################################### D(expression(log(x)),"x") D(expression(x^2),"x") logist <- expression(exp(x)/(1+exp(x))) dfun <- deriv(logist,"x",function.arg=TRUE) xvec <- seq(-4,4,length=40) y <- dfun(xvec) plot(xvec,y) lines(xvec,attr(y,"grad")) ################################################### ### chunk number 30: ################################################### d1 <- D(expression(a*x/(b+x)),"x") d1 eval(d1,list(a=2,b=1,x=3))