`panel11pt7.fn` <- function(ni=3000,nb=2000,nthin=1,nchain=3){ # R script containing WinBUGS specification of CJS model with individual # heterogeneity in detection and survival probabilities. Model is fitted # to the Dipper data. Results should be consistent with Table 11.6 in the # text. The MCMC algorithm should be run for 100k iterations or more due # to slow mixing. Mixing can potentially be improved by a number of reformulations # of the model, but the one provided here is the most direct. # From Royle (2008; Biometrics 64:364-370). if(!exists("dipper.data")){ cat("data file: dipper.data not found",fill=TRUE) return(NULL) } library("R2WinBUGS") logit<-function(x){ log(x/(1-x)) } y<-dipper.data$y first<-dipper.data$first nind<-dipper.data$nind nyear<-dipper.data$nyear Zst<-dipper.data$Zst data <- list ("y","first","nind","nyear") cat(" model { for(j in 1:(nyear-2)){ yeffp[j]~dunif(0,1) yeffp2[j]<-log(yeffp[j]/(1-yeffp[j])) yeffphi[j]~dunif(0,1) yeffphi2[j]<-log(yeffphi[j]/(1-yeffphi[j])) } yeffp[nyear-1]~dunif(0,1) yeffp2[nyear-1]<-log(yeffp[nyear-1]/(1-yeffp[nyear-1])) yeffphi2[nyear-1]<-0 sigma.phi~dunif(0,10) sigma.p~dunif(0,10) tauphi<-1/(sigma.phi*sigma.phi) taup<-1/(sigma.p*sigma.p) for(i in 1:nind){ eta[i]~dnorm(0,taup) delta[i]~dnorm(0,tauphi) for(t in 1:(nyear-1)){ logit(p[i,t])<- yeffp2[t] + eta[i] logit(phi[i,t])<- yeffphi2[t] + delta[i] } } for(i in 1:nind){ z[i,first[i]]~dbern(1) for(j in (first[i]+1):nyear){ muz[i,j]<-phi[i,j-1]*z[i,j-1] z[i,j]~dbern(muz[i,j]) muy[i,j]<-p[i,j-1]*z[i,j] y[i,j]~dbern(muy[i,j]) } } } ",file="cjsmodel.txt") pst<-logit(c(.67,.87,.88,.88,.990,.73)) phist<-logit(c(.73,.45,.48,.62,.6,.73)) inits <- function(){ list ( yeffp=runif(6,.5,.7), yeffphi=runif(5,.5,.7), sigma.p=runif(1,0,5), sigma.phi=runif(1,0,5), z=Zst ) } parameters <- c("sigma.p","sigma.phi","yeffphi","yeffp") out <- bugs (data, inits, parameters, "cjsmodel.txt", n.thin=nthin,n.chains=nchain, n.burnin=nb,n.iter=ni,debug=TRUE) return(out) }