### Appendix S1 R functions to fit the Stochastic Ricker Model ### Time Series ### Armigeres subalbatus abundance at t+1 (biweeks 2-12) nt1<-c(4, 7, 5, 34, 1, 17, 5, 25, 9, 1, 4) ### Armigeres subalbatus abundance at t (biweeks 1-11) nt<- c(2, 4, 7, 5, 34, 1, 17, 5, 25, 9, 1) ### Cumulative bi-weekly rainfall before t+1 RR<- c(34.5, 29.0, 197.5, 376.5, 34.0, 301.0, 73.0, 188.0, 43.5, 68.5, 53.5) ### Calling a library with the Negative Binomial Generalized linear model fitting function library(MASS) ### Fitting the environmental stochastic Ricker model as a Neg-Bin GLM (Autonomous) rglmnba<-glm.nb(nt1~offset(log(nt))+nt) ### Calling the model summary (to see parameter estimates) summary(rglmnba) ### Fitting the environmental stochastic Ricker model as a Neg-Bin GLM (Forced by Rainfall) rglmnbF<-glm.nb(nt1~offset(log(nt))+nt+RR) ### Calling the model summary summary(rglmnbF) ### Likelihood functions ### Calling a library with convenient functions to fit Maximum likelihood models library(bbmle) ###Function to fit the environmental stochastic Ricker model (Autonomous) renva <- function (lambda0, b, kappa){ nt1<-nt1 nt<-nt -sum(dnbinom(nt1, mu=(lambda0*nt*exp(-1*b*nt)),size=kappa,log=TRUE)) } ### Fitting the model fitrenva=mle2(renva,start=list(lambda0=7.37, b=0.15, kappa=1.43)) ### Calling the model summary summary(fitrenva) ###Function to fit the environmental stochastic Ricker model (Forced by Rainfall) renvF <- function (lambda0, b, kappa,g){ nt1<-nt1 nt<-nt RR<-RR -sum(dnbinom(nt1, mu=(lambda0*nt*exp(-1*b*nt+g*RR)),size=kappa,log=TRUE)) } ### Fitting the model fitrenvF=mle2(renvF,start=list(lambda0=2.1714, b=0.115, kappa=2.678,g=0.0054)) ### Calling the model summary summary(fitrenvF) ###Function to fit the demographic stochastic Ricker model (Autonomous) rdema <- function (lambda0, b, kappa){ nt1<-nt1 nt<-nt -sum(dnbinom(nt1, mu=(lambda0*nt*exp(-1*b*nt)),size=kappa*nt,log=TRUE)) } ### Fitting the model fitrdema=mle2(rdema,start=list(lambda0=5.4005, b=0.1314215, kappa=0.2724)) ### Calling the model summary summary(fitrdema) ###Function to fit the demographic stochastic Ricker model (Forced by Rainfall) rdemF <- function (lambda0, b, kappa,g){ nt1<-nt1 nt<-nt RR<-RR -sum(dnbinom(nt1, mu=(lambda0*nt*exp(-1*b*nt+g*RR)),size=kappa*nt,log=TRUE)) } ### Fitting the model fitrdemF=mle2(rdemF,start=list(lambda0=1.5716, b=0.0979, kappa=0.5574,g=0.00536)) ### Calling the model summary summary(fitrdemF)