`panel11pt4.fn` <- function(ni=3100,nb=100,nthin=5,nc=3){ # R script to fit hierarchical formulation of the CJS model in WinBUGS. # The small yellow warbler data set (Table 11.3) is contained within # the R script. Results should coincide with Table 11.4. library("R2WinBUGS") data<-matrix(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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, 0, 0, 0, 0, 0, 0, 0),nrow=10,byrow=FALSE) nind<-nrow(data) nyear<-ncol(data) first<-rep(NA,nind) for(i in 1:nrow(data)){ first[i]<- (1:nyear)[!is.na(data[i,])][1] } Zst<-data Zst[Zst==0 & !is.na(Zst)]<-1 y<-data data <- list ("y","first","nind","nyear") inits <- function(){ list (phi=.2,p=.5,z=Zst) } parameters <- c("phi","p") cat(" model { phi~dunif(0,1) p~dunif(0,1) for(i in 1:nind){ z[i,first[i]]~dbern(1) for(j in (first[i]+1):nyear){ muy[i,j]<-p*z[i,j] y[i,j]~dbern(muy[i,j]) muz[i,j]<-phi*z[i,j-1] z[i,j]~dbern(muz[i,j]) } } } ",file="cjsmodel.txt") out <- bugs (data, inits, parameters, "cjsmodel.txt", n.thin=nthin,n.chains=nc, n.burnin=nb,n.iter=ni,debug=TRUE) out }