# This script presents an example of calculating clinal variation in the investigated life history traits. # In this example, pupal diapause strategy (r=1) is investigated when the standard deviation of season # length is 4 days. # The beginning of the script includes functions that are used in derivation of life history predictions. # These functions are modified versions of those used in an earlier analysis focusing on deterministic # environments (see Kivelä, S.M., Välimäki, P. & Gotthard, K. (2013) Seasonality maintains alternative # life-history phenotypes. Evolution 67: 3145–3160). # The parameters of the analysis (see Table 1) are set in the list "parms" and the standard deviation # of season length is determined by "SD". The analysed gradient of mean season lengths is set by "museq1". # The result of the analysis is a list that contains vectors of trait values at the mean season lengths # included in the analysis. Diapause generation trait values are indicated by "1", and direct generation # trait values by "2". ############################################################################################### Diap.gen.Fr<-function(c1,c2,a,b,B,E,tlarva,tpupa,T,season.beginning,N,Mad,Mjuv0,d,k,z,mmin,Cd,critDate,r,tau){ frost.probability=(1:T)^tau/((T+1)^tau) frost.free.prob=cumprod(1-frost.probability) pre.diap.tlarva=round(r*tlarva) post.diap.tlarva=tlarva-pre.diap.tlarva tlarva.induction=round(r*4/5*tlarva) TII=critDate-season.beginning sE=(-E^c2+1) Cmin=mmin/(tlarva^B) {if ((-(log(z)+Mjuv0*tlarva)*Cd^k/(d*tlarva))<0) Cmax=0 else Cmax=(-(log(z)+Mjuv0*tlarva)*Cd^k/(d*tlarva))^(1/k)} {if (Cmin>Cmax){ output=list(0,0,0,0,0,0,0,0,0,0,0) names(output)=c("diap.offspring","direct.offspring","first.offspring", "direct.cohorts","cohort.sizes","body.mass","lifespan","fecundity", "growth.rate","prop.direct","Mjuv.diap") output} else { c=seq(Cmin,Cmax,len=40) MJuv=Mjuv0+(d/(Cd^k))*c^k i=1:T mass=c*tlarva^B fmax=a*(1-exp(-b*mass)) F=exp(-i*Mad)*sE^(i-1)%*%t((exp(-tlarva*MJuv)*fmax*(1-exp(-c1*E)))) F[F<0.1]=0 Fsums=colSums(F) Cmaxindex=match(max(Fsums),Fsums) C=c[Cmaxindex] fec=F[,Cmaxindex] omega=sum(as.numeric(fec>0)) {if (omega==0) { output=list(0,0,0,0,0,0,0,0,0,0,0) names(output)=c("diap.offspring","direct.offspring","first.offspring", "direct.cohorts","cohort.sizes","body.mass","lifespan","fecundity", "growth.rate","prop.direct","Mjuv.diap") output} else { fecundity=Fsums[Cmaxindex] Mjuv=MJuv[Cmaxindex] larval.cohorts1=numeric(T) {if (post.diap.tlarva+tpupa+1+omega0])<=T) pupal.cohorts1[(tpupa+tlarva+1):min((tpupa+tlarva+omega),((tpupa+tlarva)+length(larval.cohorts1[larval.cohorts1>0])))]=exp(-pre.diap.tlarva*Mjuv)*larval.cohorts1[larval.cohorts1>0] else {if (tpupa+tlarva+1>T) pupal.cohorts1=numeric(T) else pupal.cohorts1[(tpupa+tlarva+1):T]=exp(-pre.diap.tlarva*Mjuv)*larval.cohorts1[larval.cohorts1>0][1:length((tpupa+tlarva+1):T)]}} pupal.cohorts1=frost.free.prob*pupal.cohorts1 {if (TII<=tlarva.induction+post.diap.tlarva+tpupa+1) L=1 else if (TII>tlarva.induction+post.diap.tlarva+tpupa+1 & TII<=tlarva.induction+post.diap.tlarva+tpupa+omega) L=length((tpupa+post.diap.tlarva+1):(TII-tlarva.induction)) else L=omega} first.offspring=1+tpupa+post.diap.tlarva direct.offspring=numeric(T) cohort.sizes=numeric(L) {if (TII<=tlarva.induction+post.diap.tlarva+tpupa+1) {direct.offspring=0 cohort.sizes=0} else {direct.offspring[(post.diap.tlarva+tpupa+1):(post.diap.tlarva+tpupa+L)]=larval.cohorts1[(post.diap.tlarva+tpupa+1):(post.diap.tlarva+tpupa+L)] cohort.sizes=larval.cohorts1[(post.diap.tlarva+tpupa+1):(post.diap.tlarva+tpupa+L)]}} {if (cohort.sizes[1]==0) direct.coh=0 else direct.coh=L} {if (T<=tlarva+tpupa) OW1=0 else if (TII<=tlarva.induction+post.diap.tlarva+tpupa+1) OW1=pupal.cohorts1 else if (length(cohort.sizes)>=omega) OW1=0 else if (TII>tlarva.induction+post.diap.tlarva+tpupa+1 & TII<=tlarva.induction+post.diap.tlarva+tpupa+omega) OW1=pupal.cohorts1[(TII-tlarva.induction+pre.diap.tlarva+1):min(T,(tlarva+tpupa+omega))] else OW1=0} diap.offspring=sum(OW1,na.rm=T) prop.direct=sum(cohort.sizes)/sum(larval.cohorts1) output=list(diap.offspring,direct.offspring,first.offspring,direct.coh,cohort.sizes, mass[Cmaxindex],omega,fecundity,C,prop.direct,Mjuv) names(output)=c("diap.offspring","direct.offspring","first.offspring", "direct.cohorts","cohort.sizes","body.mass","lifespan","fecundity", "growth.rate","prop.direct","Mjuv.diap") output}}}} } # This function (Diap.gen.Fr2) is used only in calculating the fitness of a known # life history strategy (i.e., all the trait values are known) at a known season length. Diap.gen.Fr2<-function(c1,c2,a,b,B,E,tlarva,tlarva.2,tpupa,T,season.beginning,N,Mad,Mjuv0,d,k,z,mmin,Cd,critDate,r,tau){ frost.probability=(1:T)^tau/((T+1)^tau) frost.free.prob=cumprod(1-frost.probability) pre.diap.tlarva=round(r*tlarva) post.diap.tlarva=tlarva-pre.diap.tlarva {if (tlarva.2>0) tlarva.induction=round(r*4/5*tlarva.2) else tlarva.induction=round(r*4/5*tlarva)} TII=critDate-season.beginning sE=(-E^c2+1) Cmin=mmin/(tlarva^B) {if ((-(log(z)+Mjuv0*tlarva)*Cd^k/(d*tlarva))<0) Cmax=0 else Cmax=(-(log(z)+Mjuv0*tlarva)*Cd^k/(d*tlarva))^(1/k)} {if (Cmin>Cmax){ output=list(0,0,0,0,0,0,0,0,0,0,0) names(output)=c("diap.offspring","direct.offspring","first.offspring", "direct.cohorts","cohort.sizes","body.mass","lifespan","fecundity", "growth.rate","prop.direct","Mjuv.diap") output} else { c=seq(Cmin,Cmax,len=40) MJuv=Mjuv0+(d/(Cd^k))*c^k i=1:T mass=c*tlarva^B fmax=a*(1-exp(-b*mass)) F=exp(-i*Mad)*sE^(i-1)%*%t((exp(-tlarva*MJuv)*fmax*(1-exp(-c1*E)))) F[F<0.1]=0 Fsums=colSums(F) Cmaxindex=match(max(Fsums),Fsums) C=c[Cmaxindex] fec=F[,Cmaxindex] omega=sum(as.numeric(fec>0)) {if (omega==0) { output=list(0,0,0,0,0,0,0,0,0,0,0) names(output)=c("diap.offspring","direct.offspring","first.offspring", "direct.cohorts","cohort.sizes","body.mass","lifespan","fecundity", "growth.rate","prop.direct","Mjuv.diap") output} else { fecundity=Fsums[Cmaxindex] Mjuv=MJuv[Cmaxindex] larval.cohorts1=numeric(T) {if (post.diap.tlarva+tpupa+1+omega0])<=T) pupal.cohorts1[(tpupa+tlarva+1):min((tpupa+tlarva+omega),((tpupa+tlarva)+length(larval.cohorts1[larval.cohorts1>0])))]=exp(-pre.diap.tlarva*Mjuv)*larval.cohorts1[larval.cohorts1>0] else {if (tpupa+tlarva+1>T) pupal.cohorts1=numeric(T) else pupal.cohorts1[(tpupa+tlarva+1):T]=exp(-pre.diap.tlarva*Mjuv)*larval.cohorts1[larval.cohorts1>0][1:length((tpupa+tlarva+1):T)]}} pupal.cohorts1=frost.free.prob*pupal.cohorts1 {if (TII<=tlarva.induction+post.diap.tlarva+tpupa+1) L=1 else if (TII>tlarva.induction+post.diap.tlarva+tpupa+1 & TII<=tlarva.induction+post.diap.tlarva+tpupa+omega) L=length((tpupa+post.diap.tlarva+1):(TII-tlarva.induction)) else L=omega} first.offspring=1+tpupa+post.diap.tlarva direct.offspring=numeric(T) cohort.sizes=numeric(L) {if (TII<=tlarva.induction+post.diap.tlarva+tpupa+1) {direct.offspring=0 cohort.sizes=0} else {direct.offspring[(post.diap.tlarva+tpupa+1):(post.diap.tlarva+tpupa+L)]=larval.cohorts1[(post.diap.tlarva+tpupa+1):(post.diap.tlarva+tpupa+L)] cohort.sizes=larval.cohorts1[(post.diap.tlarva+tpupa+1):(post.diap.tlarva+tpupa+L)]}} {if (cohort.sizes[1]==0) direct.coh=0 else direct.coh=L} {if (T<=tlarva+tpupa) OW1=0 else if (TII<=tlarva.induction+post.diap.tlarva+tpupa+1) OW1=pupal.cohorts1 else if (length(cohort.sizes)>=omega) OW1=0 else if (TII>tlarva.induction+post.diap.tlarva+tpupa+1 & TII<=tlarva.induction+post.diap.tlarva+tpupa+omega) OW1=pupal.cohorts1[(TII-tlarva.induction+pre.diap.tlarva+1):min(T,(tlarva+tpupa+omega))] else OW1=0} diap.offspring=sum(OW1,na.rm=T) prop.direct=sum(cohort.sizes)/sum(larval.cohorts1) output=list(diap.offspring,direct.offspring,first.offspring,direct.coh,cohort.sizes, mass[Cmaxindex],omega,fecundity,C,prop.direct,Mjuv) names(output)=c("diap.offspring","direct.offspring","first.offspring", "direct.cohorts","cohort.sizes","body.mass","lifespan","fecundity", "growth.rate","prop.direct","Mjuv.diap") output}}}} } Direct.gen.Fr<-function(c1,c2,a,b,B,E2,tlarva.2,tpupa.2,tlarva,tpupa,T,N,Mad,Mjuv0,k,d,z,mmin,Cd, direct.cohorts,cohort.sizes,Mjuv.diap,r,tau){ frost.probability=(1:T)^tau/((T+1)^tau) frost.free.prob=cumprod(1-frost.probability) pre.diap.tlarva=round(r*tlarva) post.diap.tlarva=tlarva-pre.diap.tlarva {if (cohort.sizes[1]==0 | tlarva.2==0){ output=list(0,0,0,0,0) names(output)=c("diap.offspring","body.mass","lifespan","fecundity","growth.rate") output} else { sE2=(-E2^c2+1) Cmin2=mmin/(tlarva.2^B) {if ((-(log(z)+Mjuv0*tlarva.2)*Cd^k/(d*tlarva.2))<0) Cmax2=0 else Cmax2=max((-(log(z)+Mjuv0*tlarva.2)*Cd^k/(d*tlarva.2))^(1/k),0)} {if (Cmin2>Cmax2){ output=list(0,0,0,0,0) names(output)=c("diap.offspring","body.mass","lifespan","fecundity","growth.rate") output} else { c.2=seq(Cmin2,Cmax2,len=40) MJuv2=Mjuv0+(d/(Cd^k))*c.2^k i=1:T mass2=c.2*tlarva.2^B fmax2=a*(1-exp(-b*mass2)) F2=exp(-i*Mad)*sE2^(i-1)%*%t((exp(-tlarva.2*MJuv2)*fmax2*(1-exp(-c1*E2)))) F2[F2<0.1]=0 F2sums=colSums(F2) Cmaxindex2=match(max(F2sums),F2sums) C2=c.2[Cmaxindex2] fec2=F2[,Cmaxindex2] omega2=sum(as.numeric(fec2>0)) m2=mass2[Cmaxindex2] {if (omega2==0){ output=list(0,0,0,0,0) names(output)=c("diap.offspring","body.mass","lifespan","fecundity","growth.rate") output} else { fecundity2=F2sums[Cmaxindex2] ef=fec2[1:omega2] eII=matrix(nrow=direct.cohorts,ncol=direct.cohorts+omega2-1) for (i in 1:direct.cohorts){ eII[i,i:(i+omega2-1)]=cohort.sizes[i]*ef } eII[is.na(eII)]=0 densityII=colSums(eII) L2=sum(as.numeric(densityII>0)) larval.cohorts2=numeric(T) {if (post.diap.tlarva+1+tpupa+tlarva.2+tpupa.2>T) larval.cohorts2=0 else larval.cohorts2[(post.diap.tlarva+1+tpupa+tlarva.2+tpupa.2):(min((post.diap.tlarva+tpupa+tlarva.2+tpupa.2+L2),T))]= densityII[1:(length((post.diap.tlarva+1+tpupa+tlarva.2+tpupa.2):(min((post.diap.tlarva+tpupa+tlarva.2+tpupa.2+L2),T))))]} pupal.cohorts2=numeric(T) {if (1+tpupa+tlarva.2+tpupa.2+tlarva>T) pupal.cohorts2=0 else pupal.cohorts2[(1+tpupa+tlarva.2+tpupa.2+tlarva):(min((tlarva+tpupa+tlarva.2+tpupa.2+L2),T))]=exp(-pre.diap.tlarva*Mjuv.diap)* larval.cohorts2[(post.diap.tlarva+1+tpupa+tlarva.2+tpupa.2):((post.diap.tlarva+1+tpupa+tlarva.2+tpupa.2)-1+length((1+tpupa+tlarva.2+tpupa.2+tlarva):(min((tlarva+tpupa+tlarva.2+tpupa.2+L2),T))))]} pupal.cohorts2=frost.free.prob*pupal.cohorts2 OW2=pupal.cohorts2 OW=sum(OW2,na.rm=T) output=list(OW,m2,omega2,fecundity2,C2) names(output)=c("diap.offspring","body.mass","lifespan","fecundity", "growth.rate") output}}}}}} } #################################################################################################### Both.gen.Fr<-function(c1, c2, tpupa, tpupa.2, season.lengths, season.beginnings, N, B, a, b, Mad, Mjuv0, k, d, z, Cd, mmin, ERes, critDate, r, tau){ T=round(mean(season.lengths)) sb=round(mean(season.beginnings)) t=seq(1,T,by=0.01) mc=round(((-(log(z)+Mjuv0*t)*Cd^k/(d*t))^(1/k))*t^B) MC=mc[mc!="NaN"] {if (max(MC)>=mmin){ {if (min(MC)>mmin) minindex=1 else minindex=match(1,as.numeric(MC==mmin))} mintlarva=round(t[minindex]) TimeRes=length(mintlarva:(T-tpupa-1)) g.time=mintlarva:(T-tpupa-1) Eseq=seq(0.01,1,len=ERes) GenI=matrix(nrow=ERes,ncol=TimeRes) MassI=matrix(nrow=ERes,ncol=TimeRes) OmegaI=matrix(nrow=ERes,ncol=TimeRes) FecI=matrix(nrow=ERes,ncol=TimeRes) CI=matrix(nrow=ERes,ncol=TimeRes) Total.offspring=matrix(nrow=ERes,ncol=TimeRes) EII=matrix(nrow=ERes,ncol=TimeRes) g.timeII=matrix(nrow=ERes,ncol=TimeRes) OmegaII=matrix(nrow=ERes,ncol=TimeRes) MassII=matrix(nrow=ERes,ncol=TimeRes) FecII=matrix(nrow=ERes,ncol=TimeRes) CII=matrix(nrow=ERes,ncol=TimeRes) DirectCoh=matrix(nrow=ERes,ncol=TimeRes) NpropDCoh=matrix(nrow=ERes,ncol=TimeRes) Offspring.ratio=matrix(nrow=ERes,ncol=TimeRes) for(i in 1:ERes){ for(j in 1:TimeRes){ ## Beginning of the DiapF-function ## DiapF<-function(x){ frost.probability=(1:T)^tau/((T+1)^tau) frost.free.prob=cumprod(1-frost.probability) pre.diap.tlarva=round(r*g.time[j]) post.diap.tlarva=g.time[j]-pre.diap.tlarva tlarva.induction=round(r*4/5*x) TII=critDate-sb sE=(-Eseq[i]^c2+1) Cmin=mmin/(g.time[j]^B) {if ((-(log(z)+Mjuv0*g.time[j])*Cd^k/(d*g.time[j]))<0) Cmax=0 else Cmax=(-(log(z)+Mjuv0*g.time[j])*Cd^k/(d*g.time[j]))^(1/k)} {if (Cmin>Cmax){ output=list(0,0,0,0,0,0,0,0,0,0,0) names(output)=c("diap.offspring","direct.offspring","first.offspring", "direct.cohorts","cohort.sizes","body.mass","lifespan","fecundity", "growth.rate","prop.direct","Mjuv.diap") output} else { c=seq(Cmin,Cmax,len=40) MJuv=Mjuv0+(d/(Cd^k))*c^k y=1:T mass=c*g.time[j]^B fmax=a*(1-exp(-b*mass)) F=exp(-y*Mad)*sE^(y-1)%*%t((exp(-g.time[j]*MJuv)*fmax*(1-exp(-c1*Eseq[i])))) F[F<0.1]=0 Fsums=colSums(F) Cmaxindex=match(max(Fsums),Fsums) C=c[Cmaxindex] fec=F[,Cmaxindex] omega=sum(as.numeric(fec>0)) {if (omega==0) { output=list(0,0,0,0,0,0,0,0,0,0,0) names(output)=c("diap.offspring","direct.offspring","first.offspring", "direct.cohorts","cohort.sizes","body.mass","lifespan","fecundity", "growth.rate","prop.direct","Mjuv.diap") output} else { fecundity=Fsums[Cmaxindex] Mjuv=MJuv[Cmaxindex] larval.cohorts1=numeric(T) {if (post.diap.tlarva+tpupa+1+omega0])<=T) pupal.cohorts1[(tpupa+g.time[j]+1):min((tpupa+g.time[j]+omega),((tpupa+g.time[j])+length(larval.cohorts1[larval.cohorts1>0])))]=exp(-pre.diap.tlarva*Mjuv)*larval.cohorts1[larval.cohorts1>0] else {if (tpupa+g.time[j]+1>T) pupal.cohorts1=numeric(T) else pupal.cohorts1[(tpupa+g.time[j]+1):T]=exp(-pre.diap.tlarva*Mjuv)*larval.cohorts1[larval.cohorts1>0][1:length((tpupa+g.time[j]+1):T)]}} pupal.cohorts1=frost.free.prob*pupal.cohorts1 {if (TII<=tlarva.induction+post.diap.tlarva+tpupa+1) L=1 else if (TII>tlarva.induction+post.diap.tlarva+tpupa+1 & TII<=tlarva.induction+post.diap.tlarva+tpupa+omega) L=length((tpupa+post.diap.tlarva+1):(TII-tlarva.induction)) else L=omega} first.offspring=1+tpupa+post.diap.tlarva direct.offspring=numeric(T) cohort.sizes=numeric(L) {if (TII<=tlarva.induction+post.diap.tlarva+tpupa+1) {direct.offspring=0 cohort.sizes=0} else {direct.offspring[(post.diap.tlarva+tpupa+1):(post.diap.tlarva+tpupa+L)]=larval.cohorts1[(post.diap.tlarva+tpupa+1):(post.diap.tlarva+tpupa+L)] cohort.sizes=larval.cohorts1[(post.diap.tlarva+tpupa+1):(post.diap.tlarva+tpupa+L)]}} {if (cohort.sizes[1]==0) direct.coh=0 else direct.coh=L} {if (T<=g.time[j]+tpupa) OW1=0 else if (TII<=tlarva.induction+post.diap.tlarva+tpupa+1) OW1=pupal.cohorts1 else if (length(cohort.sizes)>=omega) OW1=0 else if (TII>tlarva.induction+post.diap.tlarva+tpupa+1 & TII<=tlarva.induction+post.diap.tlarva+tpupa+omega) OW1=pupal.cohorts1[(TII-tlarva.induction+pre.diap.tlarva+1):min(T,(g.time[j]+tpupa+omega))] else OW1=0} diap.offspring=sum(OW1,na.rm=T) prop.direct=sum(cohort.sizes)/sum(larval.cohorts1) output=list(diap.offspring,direct.offspring,first.offspring,direct.coh,cohort.sizes, mass[Cmaxindex],omega,fecundity,C,prop.direct,Mjuv) names(output)=c("diap.offspring","direct.offspring","first.offspring", "direct.cohorts","cohort.sizes","body.mass","lifespan","fecundity", "growth.rate","prop.direct","Mjuv.diap") output}}}} } ## End of the DiapF-function ## cohseq=lapply(g.time,DiapF) cohvec=numeric(length(cohseq)) for(q in 1:length(cohseq)){ cohvec[q]=cohseq[[q]]$direct.cohorts} maxcoh=max(cohvec) cohindex=match(1,as.numeric(cohvec==maxcoh))+sum(as.numeric(cohvec==maxcoh))-1 {if (maxcoh==0) timeindex2=1 else timeindex2=1:length(g.time[cohvec>0])} for(v in timeindex2){ model=cohseq[[v]] GenI[i,j]=model$diap.offspring MassI[i,j]=model$body.mass OmegaI[i,j]=model$lifespan FecI[i,j]=model$fecundity CI[i,j]=model$growth.rate DirectCoh[i,j]=model$direct.cohorts NpropDCoh[i,j]=model$prop.direct GenII=matrix(nrow=ERes,ncol=TimeRes) massII=matrix(nrow=ERes,ncol=TimeRes) omegaII=matrix(nrow=ERes,ncol=TimeRes) fecII=matrix(nrow=ERes,ncol=TimeRes) cII=matrix(nrow=ERes,ncol=TimeRes) {if (model$cohort.sizes[1]>0) {for(m in 1:ERes){ for(l in timeindex2){ model2=Direct.gen.Fr(c1=c1,c2=c2,a=a,b=b,B=B,E2=Eseq[m],tlarva.2=g.time[cohvec>0][l],tpupa.2=tpupa.2, tlarva=g.time[j],tpupa=tpupa,T=T,N=N,Mad=Mad,Mjuv0=Mjuv0,k=k,d=d,z=z,mmin=mmin,Cd=Cd, direct.cohorts=model$direct.cohorts, cohort.sizes=model$cohort.sizes, Mjuv.diap=model$Mjuv.diap,r=r,tau=tau) GenII[m,l]=model2$diap.offspring massII[m,l]=model2$body.mass omegaII[m,l]=model2$lifespan fecII[m,l]=model2$fecundity cII[m,l]=model2$growth.rate } } GenII[is.na(GenII)]=0 {if (max(GenII)==0) {GenII=0 EII[i,j]=0 g.timeII[i,j]=0 MassII[i,j]=0 OmegaII[i,j]=0 FecII[i,j]=0 CII[i,j]=0 model=Diap.gen.Fr(c1=c1,c2=c2,a=a,b=b,B=B,E=Eseq[i],tlarva=g.time[j],tpupa=tpupa, T=T,season.beginning=sb,N=N,Mad=Mad,Mjuv0=Mjuv0,d=d,k=k,z=z,mmin=mmin,Cd=Cd, critDate=critDate,r=r,tau=tau) GenI[i,j]=model$diap.offspring MassI[i,j]=model$body.mass OmegaI[i,j]=model$lifespan FecI[i,j]=model$fecundity CI[i,j]=model$growth.rate DirectCoh[i,j]=model$direct.cohorts NpropDCoh[i,j]=model$prop.direct {break}} else{ rowII=match(max(rowSums(GenII==max(GenII))),rowSums(GenII==max(GenII))) colII=match(max(colSums(GenII==max(GenII))),colSums(GenII==max(GenII))) EII[i,j]=Eseq[rowII] g.timeII[i,j]=g.time[colII] MassII[i,j]=massII[rowII,colII] OmegaII[i,j]=omegaII[rowII,colII] FecII[i,j]=fecII[rowII,colII] CII[i,j]=cII[rowII,colII] {if (g.time[v]==g.time[colII]) {break}}}} } else {GenII=0 EII[i,j]=0 g.timeII[i,j]=0 MassII[i,j]=0 OmegaII[i,j]=0 FecII[i,j]=0 CII[i,j]=0}} } Total.offspring[i,j]=GenI[i,j]+max(GenII) Offspring.ratio[i,j]=max(GenII)/(GenI[i,j]+max(GenII)) } } ### fitness=array(numeric(ERes*TimeRes*length(season.lengths)),dim=c(ERes,TimeRes,length(season.lengths))) direct.contrib.S=array(numeric(ERes*TimeRes*length(season.lengths)),dim=c(ERes,TimeRes,length(season.lengths))) proportion.direct.coh.S=array(numeric(ERes*TimeRes*length(season.lengths)),dim=c(ERes,TimeRes,length(season.lengths))) for(effort in 1:ERes){ for(time in 1:TimeRes){ for(i in 1:length(season.lengths)){ diap.gen=Diap.gen.Fr2(c1=c1,c2=c2,a=a,b=b,B=B,E=Eseq[effort],tlarva=g.time[time], tlarva.2=g.timeII[effort,time],tpupa=tpupa, T=season.lengths[i],season.beginning=season.beginnings[i],N=N,Mad=Mad,Mjuv0=Mjuv0,d=d,k=k,z=z,mmin=mmin,Cd=Cd, critDate=critDate,r=r,tau=tau) direct.gen=Direct.gen.Fr(c1=c1,c2=c2,a=a,b=b,B=B,E2=EII[effort,time],tlarva.2=g.timeII[effort,time],tpupa.2=tpupa.2, tlarva=g.time[time],tpupa=tpupa,T=season.lengths[i],N=N,Mad=Mad,Mjuv0=Mjuv0,k=k,d=d,z=z, mmin=mmin,Cd=Cd, direct.cohorts=diap.gen$direct.cohorts, cohort.sizes=diap.gen$cohort.sizes, Mjuv.diap=diap.gen$Mjuv.diap,r=r,tau=tau) OW1=diap.gen$diap.offspring OW2=direct.gen$diap.offspring fitness[effort,time,i]=OW1+OW2 direct.contrib.S[effort,time,i]=OW2/(OW1+OW2) proportion.direct.coh.S[effort,time,i]=diap.gen$direct.cohorts/diap.gen$lifespan } } } geom.mean=matrix(nrow=ERes,ncol=TimeRes) fitness.var=matrix(nrow=ERes,ncol=TimeRes) for(i in 1:ERes){ for(j in 1:TimeRes){ geom.mean[i,j]=exp(sum(log(fitness[i,j,]))/length(season.lengths)) fitness.var[i,j]=var(fitness[i,j,]) } } bestrow=match(max(rowSums(geom.mean==max(geom.mean))), rowSums(geom.mean==max(geom.mean))) bestcol=match(max(colSums(geom.mean==max(geom.mean))), colSums(geom.mean==max(geom.mean))) best.growth.time1=g.time[bestcol] best.growth.rate1=CI[bestrow,bestcol] best.body.mass1=MassI[bestrow,bestcol] best.RE1=Eseq[bestrow] best.lifespan1=OmegaI[bestrow,bestcol] best.fecundity1=FecI[bestrow,bestcol] best.growth.time2=g.timeII[bestrow,bestcol] best.growth.rate2=CII[bestrow,bestcol] best.body.mass2=MassII[bestrow,bestcol] best.RE2=EII[bestrow,bestcol] best.lifespan2=OmegaII[bestrow,bestcol] best.fecundity2=FecII[bestrow,bestcol] best.direct.contrib.S=direct.contrib.S[bestrow,bestcol,] best.proportion.direct.coh.S=proportion.direct.coh.S[bestrow,bestcol,] ### growth.time.ratio=g.timeII/matrix(rep(g.time,ERes),ERes,TimeRes,byrow=TRUE) growth.rate.ratio=CII/CI body.mass.ratio=MassII/MassI RE.ratio=EII/matrix(rep(Eseq,TimeRes),ERes,TimeRes,byrow=FALSE) lifespan.ratio=OmegaII/OmegaI fecundity.ratio=FecII/FecI ### trait.values=list(growth.time1=matrix(rep(g.time,ERes),ERes,TimeRes,byrow=TRUE),growth.time2=g.timeII, growth.rate1=CI,growth.rate2=CII, body.mass1=MassI,body.mass2=MassII, RE1=matrix(rep(Eseq,TimeRes),ERes,TimeRes,byrow=FALSE),RE2=EII, lifespan1=OmegaI,lifespan2=OmegaII, fecundity1=FecI,fecundity2=FecII) parms=list(c1=c1,c2=c2,tpupa=tpupa,tpupa.2=tpupa.2,season.lengths=season.lengths, season.beginnings=season.beginnings,N=N, B=B,a=a,b=b,Mad=Mad,Mjuv0=Mjuv0,k=k,d=d,z=z,Cd=Cd,mmin=mmin,ERes=ERes, critDate=critDate,r=r,tau=tau) output=list(geom.mean[bestrow,bestcol],best.RE1,best.RE2,best.growth.time1,best.growth.time2, best.body.mass1,best.body.mass2,best.lifespan1,best.lifespan2,best.fecundity1,best.fecundity2, best.growth.rate1,best.growth.rate2,best.proportion.direct.coh.S,best.direct.contrib.S, geom.mean,fitness.var,fitness.var[bestrow,bestcol],g.time,Eseq, growth.time.ratio,growth.rate.ratio,body.mass.ratio,RE.ratio, lifespan.ratio,fecundity.ratio,trait.values,parms) names(output)=c("geom.mean.fitness","RE1","RE2","growth.time1","growth.time2", "body.mass1","body.mass2","lifespan1","lifespan2","fecundity1","fecundity2", "growth.rate1","growth.rate2","proportion.direct.coh", "direct.contrib", "geom.mean.matrix","fitness.var.matrix","fitness.var","tlarvaseq","Eseq", "growth.time.ratio","growth.rate.ratio","body.mass.ratio","RE.ratio", "lifespan.ratio","fecundity.ratio", "trait.values","parameters") output} else { output=list(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) names(output)=c("geom.mean.fitness","RE1","RE2","growth.time1","growth.time2", "body.mass1","body.mass2","lifespan1","lifespan2","fecundity1","fecundity2", "growth.rate1","growth.rate2","proportion.direct.coh", "direct.contrib", "geom.mean.matrix","fitness.var.matrix","fitness.var","tlarvaseq","Eseq", "growth.time.ratio","growth.rate.ratio","body.mass.ratio","RE.ratio", "lifespan.ratio","fecundity.ratio", "trait.values","parameters") output }} } ####################################################### Opt.D.Fr<-function(c1, c2, tpupa, tpupa.2, season.lengths, season.beginnings, N, B, a, b, Mad, Mjuv0, k, d, z, Cd, mmin, ERes, DRes, r, tau, coh.threshold){ T=round(mean(season.lengths)) t=seq(1,max(season.lengths),by=0.01) mc=round(((-(log(z)+Mjuv0*t)*Cd^k/(d*t))^(1/k))*t^B) MC=mc[mc!="NaN"] {if (max(MC)>=mmin){ {if (min(MC)>mmin) minindex=1 else minindex=match(1,as.numeric(MC==mmin))} mintlarva=round(t[minindex]) TimeRes=length(mintlarva:(T-tpupa-1)) Dateseq=seq(tpupa+round((1-r)*t[minindex])+round(r*4/5*t[minindex])+1,T,by=DRes)+min(season.beginnings) Dlen=length(Dateseq) fitness=numeric(Dlen) EI=numeric(Dlen) EII=numeric(Dlen) MassI=numeric(Dlen) MassII=numeric(Dlen) OmegaI=numeric(Dlen) OmegaII=numeric(Dlen) FecI=numeric(Dlen) FecII=numeric(Dlen) GrI=numeric(Dlen) GrII=numeric(Dlen) g.timeI=numeric(Dlen) g.timeII=numeric(Dlen) DirectCoh=matrix(nrow=length(season.lengths),ncol=Dlen) DirectContrib=matrix(nrow=length(season.lengths),ncol=Dlen) geom.mean=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) fitness.var=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) g.time.r=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) Gr.r=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) Mass.r=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) E.r=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) Omega.r=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) Fec.r=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) gt1=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) gtpupa=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) gr1=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) gr2=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) bm1=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) bm2=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) re1=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) re2=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) ls1=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) ls2=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) fe1=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) fe2=array(numeric(ERes*TimeRes*Dlen),dim=c(ERes,TimeRes,Dlen)) best.fitness.var=numeric(Dlen) for(i in 1:Dlen){ model=Both.gen.Fr(c1=c1, c2=c2, tpupa=tpupa, tpupa.2=tpupa.2, season.lengths=season.lengths, season.beginnings=season.beginnings, N=N, B=B, a=a, b=b, Mad=Mad, Mjuv0=Mjuv0, k=k, d=d, z=z, Cd=Cd, mmin=mmin, ERes=ERes, critDate=Dateseq[i], r=r, tau=tau) fitness[i]=model$geom.mean.fitness EI[i]=model$RE1 EII[i]=model$RE2 MassI[i]=model$body.mass1 MassII[i]=model$body.mass2 OmegaI[i]=model$lifespan1 OmegaII[i]=model$lifespan2 FecI[i]=model$fecundity1 FecII[i]=model$fecundity2 GrI[i]=model$growth.rate1 GrII[i]=model$growth.rate2 g.timeI[i]=model$growth.time1 g.timeII[i]=model$growth.time2 DirectCoh[,i]=model$proportion.direct.coh DirectContrib[,i]=model$direct.contrib geom.mean[,,i]=model$geom.mean.matrix fitness.var[,,i]=model$fitness.var.matrix g.time.r[,,i]=model$growth.time.ratio Gr.r[,,i]=model$growth.rate.ratio Mass.r[,,i]=model$body.mass.ratio E.r[,,i]=model$RE.ratio Omega.r[,,i]=model$lifespan.ratio Fec.r[,,i]=model$fecundity.ratio gt1[,,i]=model$trait.values$growth.time1 gtpupa[,,i]=model$trait.values$growth.time2 gr1[,,i]=model$trait.values$growth.rate1 gr2[,,i]=model$trait.values$growth.rate2 bm1[,,i]=model$trait.values$body.mass1 bm2[,,i]=model$trait.values$body.mass2 re1[,,i]=model$trait.values$RE1 re2[,,i]=model$trait.values$RE2 ls1[,,i]=model$trait.values$lifespan1 ls2[,,i]=model$trait.values$lifespan2 fe1[,,i]=model$trait.values$fecundity1 fe2[,,i]=model$trait.values$fecundity2 best.fitness.var[i]=model$fitness.var tlarvaseq=model$tlarvaseq Eseq=model$Eseq {if (i>2 & fitness[i]==0) {break}} {if (colMeans(DirectCoh)[i]>=coh.threshold) {break}} } index=match(max(fitness),fitness) vectors=list(fitness,EI,EII,MassI,MassII,OmegaI,OmegaII,FecI,FecII, GrI,GrII,g.timeI,g.timeII,Dateseq,best.fitness.var) names(vectors)=c("geom.mean.fitness","RE1","RE2","body.mass1","body.mass2", "lifespan1","lifespan2","fecundity1","fecundity2", "growth.rate1","growth.rate2","growth.time1", "growth.time2","crit.date","fitness.var") matrices=list(DirectCoh,DirectContrib,geom.mean,fitness.var,tlarvaseq,Eseq, g.time.r,Gr.r,Mass.r,E.r,Omega.r,Fec.r) names(matrices)=c("proportion.direct.coh","direct.contrib", "geom.mean.matrix","fitness.var.matrix","tlarvaseq","Eseq", "growth.time.ratio","growth.rate.ratio","body.mass.ratio","RE.ratio", "lifespan.ratio","fecundity.ratio") trait.values=list(growth.time1=gt1,growth.time2=gtpupa, growth.rate1=gr1,growth.rate2=gr2, body.mass1=bm1,body.mass2=bm2, RE1=re1,RE2=re2, lifespan1=ls1,lifespan2=ls2, fecundity1=fe1,fecundity2=fe2) parms=list(c1=c1,c2=c2,tpupa=tpupa,tpupa.2=tpupa.2,season.lengths=season.lengths,N=N, B=B,a=a,b=b,Mad=Mad,Mjuv0=Mjuv0,k=k,d=d,z=z,Cd=Cd,mmin=mmin,ERes=ERes,DRes=DRes, r=r,tau=tau,coh.threshold=coh.threshold) output=list(fitness[index],EI[index],EII[index],g.timeI[index],g.timeII[index], MassI[index],MassII[index],OmegaI[index],OmegaII[index],FecI[index],FecII[index], GrI[index],GrII[index],best.fitness.var[index],mean(DirectCoh[,index]), Dateseq[index],mean(DirectContrib[,index]),vectors,matrices,trait.values,parms) names(output)=c("geom.mean.fitness","RE1","RE2","growth.time1","growth.time2", "body.mass1","body.mass2","lifespan1","lifespan2","fecundity1","fecundity2", "growth.rate1","growth.rate2","fitness.var","proportion.direct.coh", "crit.date","direct.contrib","vectors","matrices","trait.values","parameters") output} else { vectors=list(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) names(vectors)=c("geom.mean.fitness","RE1","RE2","body.mass1","body.mass2", "lifespan1","lifespan2","fecundity1","fecundity2", "growth.rate1","growth.rate2","growth.time1", "growth.time2","crit.date","fitness.var") matrices=list(0,0,0,0,0,0) names(matrices)=c("proportion.direct.coh","direct.contrib", "geom.mean.matrix","fitness.var.matrix","tlarvaseq","Eseq", "growth.time.ratio","growth.rate.ratio","body.mass.ratio","RE.ratio", "lifespan.ratio","fecundity.ratio") parms=list(c1=c1,c2=c2,tpupa=tpupa,tpupa.2=tpupa.2,season.lengths=season.lengths,N=N, B=B,a=a,b=b,Mad=Mad,Mjuv0=Mjuv0,k=k,d=d,z=z,Cd=Cd,mmin=mmin,ERes=ERes,DRes=DRes, r=r,tau=tau,coh.threshold=coh.threshold) output=list(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) names(output)=c("geom.mean.fitness","RE1","RE2","growth.time1","growth.time2", "body.mass1","body.mass2","lifespan1","lifespan2","fecundity1","fecundity2", "growth.rate1","growth.rate2","fitness.var","proportion.direct.coh", "crit.date","direct.contrib","vectors","matrices","trait.values","parameters") }} } ############################################################################################### parms=list(c1=2.5, c2=2, tpupa=1, tpupa.2=1, N=1, B=3, a=50, b=0.01, mmin=80, k=2, Cd=0.05, z=0.001, d=0.15, Mad=0.15, Mjuv0=0.01, ERes=67, DRes=1, r=1, coh.threshold=1) SD=4 ################################################################################################# Cline<-function(Tdist,sbdist,tau){ TRes=1 diap.t1=numeric(TRes) diap.RE=numeric(TRes) diap.lifespan=numeric(TRes) diap.mass=numeric(TRes) diap.fec=numeric(TRes) diap.gr=numeric(TRes) direct.t1=numeric(TRes) direct.RE=numeric(TRes) direct.lifespan=numeric(TRes) direct.mass=numeric(TRes) direct.fec=numeric(TRes) direct.gr=numeric(TRes) prop.direct=numeric(TRes) geom.fit=numeric(TRes) direct.contrib=numeric(TRes) crit.D=numeric(TRes) for(i in 1:TRes){ model=Opt.D.Fr(c1=parms$c1, c2=parms$c2, tpupa=parms$tpupa, tpupa.2=parms$tpupa.2, season.lengths=Tdist, season.beginnings=sbdist, N=parms$N, B=parms$B, a=parms$a, b=parms$b, d=parms$d, z=parms$z, Mjuv0=parms$Mjuv0, Mad=parms$Mad, mmin=parms$mmin, k=parms$k, Cd=parms$Cd, ERes=parms$ERes, DRes=parms$DRes, r=parms$r, tau=tau, coh.threshold=parms$coh.threshold) diap.t1[i]=model$growth.time1 diap.RE[i]=model$RE1 diap.lifespan[i]=model$lifespan1 diap.mass[i]=model$body.mass1 diap.fec[i]=model$fecundity1 diap.gr[i]=model$growth.rate1 direct.t1[i]=model$growth.time2 direct.RE[i]=model$RE2 direct.lifespan[i]=model$lifespan2 direct.mass[i]=model$body.mass2 direct.fec[i]=model$fecundity2 direct.gr[i]=model$growth.rate2 prop.direct[i]=model$proportion.direct.coh geom.fit[i]=model$geom.mean.fitness direct.contrib[i]=model$direct.contrib crit.D[i]=model$crit.date } output=list(diap.t1,diap.gr,diap.RE,diap.lifespan,diap.mass,diap.fec, direct.t1,direct.gr,direct.RE,direct.lifespan,direct.mass,direct.fec, prop.direct,geom.fit,direct.contrib,crit.D) names(output)=c("growth.time1","growth.rate1","RE1","lifespan1","body.mass1","fecundity1", "growth.time2","growth.rate2","RE2","lifespan2","body.mass2","fecundity2", "proportion.direct.coh","geom.mean.fitness","direct.contrib","crit.date") output } ################################################################################################# museq1=22:45 tauseq1=-0.19+0.35*museq1 diap.t1=numeric(length(museq1)) diap.RE=numeric(length(museq1)) diap.lifespan=numeric(length(museq1)) diap.mass=numeric(length(museq1)) diap.fec=numeric(length(museq1)) diap.gr=numeric(length(museq1)) direct.t1=numeric(length(museq1)) direct.RE=numeric(length(museq1)) direct.lifespan=numeric(length(museq1)) direct.mass=numeric(length(museq1)) direct.fec=numeric(length(museq1)) direct.gr=numeric(length(museq1)) prop.direct=numeric(length(museq1)) geom.fit=numeric(length(museq1)) direct.contrib=numeric(length(museq1)) crit.D=numeric(length(museq1)) for(i in 1:length(museq1)){ print(paste("Season length = ",museq1[i],sep="")) set.seed(12361) sl=round(rnorm(200,mean=museq1[i],sd=SD)) set.seed(12361) sb=round(rnorm(200,mean=round(180-0.5*museq1[i]),sd=6.5-0.065*museq1[i])) model=Cline(Tdist=sl,sbdist=sb,tau=tauseq1[i]) diap.t1[i]=model$growth.time1 diap.RE[i]=model$RE1 diap.lifespan[i]=model$lifespan1 diap.mass[i]=model$body.mass1 diap.fec[i]=model$fecundity1 diap.gr[i]=model$growth.rate1 direct.t1[i]=model$growth.time2 direct.RE[i]=model$RE2 direct.lifespan[i]=model$lifespan2 direct.mass[i]=model$body.mass2 direct.fec[i]=model$fecundity2 direct.gr[i]=model$growth.rate2 prop.direct[i]=model$proportion.direct.coh geom.fit[i]=model$geom.mean.fitness direct.contrib[i]=model$direct.contrib crit.D[i]=model$crit.date result=list(geom.mean.fitness=geom.fit,growth.time1=diap.t1, growth.time2=direct.t1,growth.rate1=diap.gr,growth.rate2=direct.gr, body.mass1=diap.mass,body.mass2=direct.mass, RE1=diap.RE,RE2=direct.RE,lifespan1=diap.lifespan,lifespan2=direct.lifespan, fecundity1=diap.fec,fecundity2=direct.fec, proportion.direct.coh=prop.direct,direct.contrib=direct.contrib, crit.date=crit.D) CLr1.sd4=result save(CLr1.sd4,file="CLr1.sd4") } ####