# This script presents an example of calculating life history variation in relation to standard # deviation of season length and within-season frost risk (defined by parameter "tau"). # In this example, pupal diapause strategy (r=1) is investigated when the mean season # length is 34 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", the analysed values of # standard deviation of season length in "SDseq", and the analysed values of tau in "tauseq". # The result of the analysis is a list that contains matrices of trait values. Rows in the matrices # correspond to the analysed values of standard deviation in season length and columns the values of tau. # 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]=(prod(fitness[i,j,]))^(1/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)) gt2=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 gt2[,,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=gt2, 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") }} } ############################################################################################### LHcombine=function(x,SDlen){ {if (is.matrix(x[[1]]$RE1)){ Row=numeric(length(x)) Col=numeric(length(x)) for(i in 1:length(x)){ Row[i]=dim(x[[i]]$RE1)[1] Col[i]=dim(x[[i]]$RE1)[2] } geom.mean.fitness=matrix(nrow=sum(Row),ncol=Col) RE1=matrix(nrow=sum(Row),ncol=Col) RE2=matrix(nrow=sum(Row),ncol=Col) growth.time1=matrix(nrow=sum(Row),ncol=Col) growth.time2=matrix(nrow=sum(Row),ncol=Col) body.mass1=matrix(nrow=sum(Row),ncol=Col) body.mass2=matrix(nrow=sum(Row),ncol=Col) lifespan1=matrix(nrow=sum(Row),ncol=Col) lifespan2=matrix(nrow=sum(Row),ncol=Col) fecundity1=matrix(nrow=sum(Row),ncol=Col) fecundity2=matrix(nrow=sum(Row),ncol=Col) growth.rate1=matrix(nrow=sum(Row),ncol=Col) growth.rate2=matrix(nrow=sum(Row),ncol=Col) direct.contrib=matrix(nrow=sum(Row),ncol=Col) crit.date=matrix(nrow=sum(Row),ncol=Col) proportion.direct.coh=matrix(nrow=sum(Row),ncol=Col) SDseq=numeric(sum(Row)) tauseq=x[[1]]$tau for(j in 1:length(x)){ {if (j==1){ geom.mean.fitness[(1:Row[j]),]=x[[j]]$geom.mean.fitness RE1[(1:Row[j]),]=x[[j]]$RE1 RE2[(1:Row[j]),]=x[[j]]$RE2 growth.time1[(1:Row[j]),]=x[[j]]$growth.time1 growth.time2[(1:Row[j]),]=x[[j]]$growth.time2 body.mass1[(1:Row[j]),]=x[[j]]$body.mass1 body.mass2[(1:Row[j]),]=x[[j]]$body.mass2 lifespan1[(1:Row[j]),]=x[[j]]$lifespan1 lifespan2[(1:Row[j]),]=x[[j]]$lifespan2 fecundity1[(1:Row[j]),]=x[[j]]$fecundity1 fecundity2[(1:Row[j]),]=x[[j]]$fecundity2 growth.rate1[(1:Row[j]),]=x[[j]]$growth.rate1 growth.rate2[(1:Row[j]),]=x[[j]]$growth.rate2 direct.contrib[(1:Row[j]),]=x[[j]]$direct.contrib crit.date[(1:Row[j]),]=x[[j]]$crit.date proportion.direct.coh[(1:Row[j]),]=x[[j]]$proportion.direct.coh SDseq[(1:Row[j])]=x[[j]]$SD } else{ geom.mean.fitness[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$geom.mean.fitness RE1[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$RE1 RE2[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$RE2 growth.time1[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$growth.time1 growth.time2[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$growth.time2 body.mass1[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$body.mass1 body.mass2[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$body.mass2 lifespan1[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$lifespan1 lifespan2[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$lifespan2 fecundity1[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$fecundity1 fecundity2[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$fecundity2 growth.rate1[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$growth.rate1 growth.rate2[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$growth.rate2 direct.contrib[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$direct.contrib crit.date[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$crit.date proportion.direct.coh[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j]),]=x[[j]]$proportion.direct.coh SDseq[(sum(Row[1:(j-1)])+1):((sum(Row[1:(j-1)]))+Row[j])]=x[[j]]$SD }} } } else { Col=length(x[[1]]$tau) Row=rep(SDlen,SDlen) geom.mean.fitness=matrix(nrow=SDlen,ncol=Col) RE1=matrix(nrow=SDlen,ncol=Col) RE2=matrix(nrow=SDlen,ncol=Col) growth.time1=matrix(nrow=SDlen,ncol=Col) growth.time2=matrix(nrow=SDlen,ncol=Col) body.mass1=matrix(nrow=SDlen,ncol=Col) body.mass2=matrix(nrow=SDlen,ncol=Col) lifespan1=matrix(nrow=SDlen,ncol=Col) lifespan2=matrix(nrow=SDlen,ncol=Col) fecundity1=matrix(nrow=SDlen,ncol=Col) fecundity2=matrix(nrow=SDlen,ncol=Col) growth.rate1=matrix(nrow=SDlen,ncol=Col) growth.rate2=matrix(nrow=SDlen,ncol=Col) direct.contrib=matrix(nrow=SDlen,ncol=Col) crit.date=matrix(nrow=SDlen,ncol=Col) proportion.direct.coh=matrix(nrow=SDlen,ncol=Col) SDseq=numeric(SDlen) tauseq=x[[1]]$tau for(j in 1:length(x)){ geom.mean.fitness[j,]=x[[j]]$geom.mean.fitness RE1[j,]=x[[j]]$RE1 RE2[j,]=x[[j]]$RE2 growth.time1[j,]=x[[j]]$growth.time1 growth.time2[j,]=x[[j]]$growth.time2 body.mass1[j,]=x[[j]]$body.mass1 body.mass2[j,]=x[[j]]$body.mass2 lifespan1[j,]=x[[j]]$lifespan1 lifespan2[j,]=x[[j]]$lifespan2 fecundity1[j,]=x[[j]]$fecundity1 fecundity2[j,]=x[[j]]$fecundity2 growth.rate1[j,]=x[[j]]$growth.rate1 growth.rate2[j,]=x[[j]]$growth.rate2 direct.contrib[j,]=x[[j]]$direct.contrib crit.date[j,]=x[[j]]$crit.date proportion.direct.coh[j,]=x[[j]]$proportion.direct.coh SDseq[j]=x[[j]]$SD[1] } }} result=list(geom.mean.fitness,RE1,RE2,growth.time1,growth.time2,body.mass1,body.mass2, lifespan1,lifespan2,fecundity1,fecundity2,growth.rate1,growth.rate2, direct.contrib,crit.date,proportion.direct.coh,SDseq,tauseq) names(result)=c("geom.mean.fitness","RE1","RE2","growth.time1","growth.time2","body.mass1","body.mass2", "lifespan1","lifespan2","fecundity1","fecundity2","growth.rate1","growth.rate2", "direct.contrib","crit.date","proportion.direct.coh","SD","tau") result } ############################################################################################### lhvec=function(x){ geom.mean.fitness=numeric(length(x)) RE1=numeric(length(x)) RE2=numeric(length(x)) growth.time1=numeric(length(x)) growth.time2=numeric(length(x)) body.mass1=numeric(length(x)) body.mass2=numeric(length(x)) lifespan1=numeric(length(x)) lifespan2=numeric(length(x)) fecundity1=numeric(length(x)) fecundity2=numeric(length(x)) growth.rate1=numeric(length(x)) growth.rate2=numeric(length(x)) direct.contrib=numeric(length(x)) crit.date=numeric(length(x)) proportion.direct.coh=numeric(length(x)) SD=numeric(length(x)) tau=numeric(length(x)) for(i in 1:length(x)){ geom.mean.fitness[i]=x[[i]]$geom.mean.fitness RE1[i]=x[[i]]$RE1 RE2[i]=x[[i]]$RE2 growth.time1[i]=x[[i]]$growth.time1 growth.time2[i]=x[[i]]$growth.time2 body.mass1[i]=x[[i]]$body.mass1 body.mass2[i]=x[[i]]$body.mass2 lifespan1[i]=x[[i]]$lifespan1 lifespan2[i]=x[[i]]$lifespan2 fecundity1[i]=x[[i]]$fecundity1 fecundity2[i]=x[[i]]$fecundity2 growth.rate1[i]=x[[i]]$growth.rate1 growth.rate2[i]=x[[i]]$growth.rate2 direct.contrib[i]=x[[i]]$direct.contrib crit.date[i]=x[[i]]$crit.date proportion.direct.coh[i]=x[[i]]$proportion.direct.coh SD[i]=x[[i]]$SD tau[i]=x[[i]]$tau } output=list(geom.mean.fitness,RE1,RE2,growth.time1,growth.time2, body.mass1,body.mass2,lifespan1,lifespan2, fecundity1,fecundity2,growth.rate1,growth.rate2, direct.contrib,crit.date,proportion.direct.coh,SD,tau) names(output)=c("geom.mean.fitness","RE1","RE2","growth.time1","growth.time2", "body.mass1","body.mass2","lifespan1","lifespan2", "fecundity1","fecundity2","growth.rate1","growth.rate2", "direct.contrib","crit.date","proportion.direct.coh","SD","tau") output } ############################################################################################### SDseq=seq(0,7,len=8) set.seed(12341) mu34.sd0=round(rnorm(100,mean=34,sd=0)) set.seed(12341) mu34.sd1=round(rnorm(100,mean=34,sd=1)) set.seed(12341) mu34.sd2=round(rnorm(100,mean=34,sd=2)) set.seed(12341) mu34.sd3=round(rnorm(100,mean=34,sd=3)) set.seed(12341) mu34.sd4=round(rnorm(100,mean=34,sd=4)) set.seed(12341) mu34.sd5=round(rnorm(100,mean=34,sd=5)) set.seed(12341) mu34.sd6=round(rnorm(100,mean=34,sd=6)) set.seed(12341) mu34.sd7=round(rnorm(100,mean=34,sd=7)) set.seed(12341) sb=round(rnorm(100,mean=163,sd=4.3)) parms=list(c1=2.5, c2=2, tpupa=1, tpupa.2=1, season.lengths=list(mu34.sd0,mu34.sd1,mu34.sd2,mu34.sd3,mu34.sd4,mu34.sd5,mu34.sd6,mu34.sd7), season.beginnings=sb, 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) ############################################################################################### LH<-function(tau){ model=Opt.D.Fr(c1=parms$c1, c2=parms$c2, tpupa=parms$tpupa, tpupa.2=parms$tpupa.2, season.lengths=parms$season.lengths[[v]], season.beginnings=parms$season.beginnings, 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) fitness=model$geom.mean.fitness RE1=model$RE1 RE2=model$RE2 g.time1=model$growth.time1 g.time2=model$growth.time2 mass1=model$body.mass1 mass2=model$body.mass2 lifespan1=model$lifespan1 lifespan2=model$lifespan2 fecundity1=model$fecundity1 fecundity2=model$fecundity2 gr1=model$growth.rate1 gr2=model$growth.rate2 critD=model$crit.date direct.contrib=model$direct.contrib prop.direct.coh=model$proportion.direct.coh parameters=model$parameters matrices=model$matrices vectors=model$vectors trait.values=model$trait.values out=list(fitness,RE1,RE2,g.time1,g.time2,mass1,mass2,lifespan1,lifespan2, fecundity1,fecundity2,gr1,gr2, direct.contrib,critD,prop.direct.coh,trait.values,vectors,matrices,parameters, model$parameters$tau,SDseq[v]) names(out)=c("geom.mean.fitness","RE1","RE2","growth.time1","growth.time2", "body.mass1","body.mass2","lifespan1","lifespan2", "fecundity1","fecundity2","growth.rate1","growth.rate2", "direct.contrib","crit.date","proportion.direct.coh", "trait.values","vectors","matrices","parameters","tau","SD") out } ########################################### tauseq=seq(9.28,13.92,len=9) ########################################### # The following analyses conducted with different values of v can be # computed by different computers etc. to finish the whole analysis faster. v=1 lhr1.34.1=lapply(tauseq,LH) LHr1.34.1=lhvec(lhr1.34.1) save(LHr1.34.1,file="LHr1.34.1") save(lhr1.34.1,file="lhr1.34.1") ########################################### v=2 lhr1.34.2=lapply(tauseq,LH) LHr1.34.2=lhvec(lhr1.34.2) save(LHr1.34.2,file="LHr1.34.2") save(lhr1.34.2,file="lhr1.34.2") ########################################### v=3 lhr1.34.3=lapply(tauseq,LH) LHr1.34.3=lhvec(lhr1.34.3) save(LHr1.34.3,file="LHr1.34.3") save(lhr1.34.3,file="lhr1.34.3") ########################################### v=4 lhr1.34.4=lapply(tauseq,LH) LHr1.34.4=lhvec(lhr1.34.4) save(LHr1.34.4,file="LHr1.34.4") save(lhr1.34.4,file="lhr1.34.4") ########################################### v=5 lhr1.34.5=lapply(tauseq,LH) LHr1.34.5=lhvec(lhr1.34.5) save(LHr1.34.5,file="LHr1.34.5") save(lhr1.34.5,file="lhr1.34.5") ########################################### v=6 lhr1.34.6=lapply(tauseq,LH) LHr1.34.6=lhvec(lhr1.34.6) save(LHr1.34.6,file="LHr1.34.6") save(lhr1.34.6,file="lhr1.34.6") ########################################### v=7 lhr1.34.7=lapply(tauseq,LH) LHr1.34.7=lhvec(lhr1.34.7) save(LHr1.34.7,file="LHr1.34.7") save(lhr1.34.7,file="lhr1.34.7") ########################################### v=8 lhr1.34.8=lapply(tauseq,LH) LHr1.34.8=lhvec(lhr1.34.8) save(LHr1.34.8,file="LHr1.34.8") save(lhr1.34.8,file="lhr1.34.8") ########################################### LHr1.34=LHcombine(list(LHr1.34.1,LHr1.34.2,LHr1.34.3,LHr1.34.4,LHr1.34.5, LHr1.34.6,LHr1.34.7,LHr1.34.8),SDlen=length(SDseq)) save(LHr1.34,file="LHr1.34")