`panel11pt6.fn` <- function(){ # R script to fit CJS model to MAPS warbler data. This is a spatial # version of the model in which the survival parameter is spatially # indexed and the model contains a spatially correlated random effect. # This requires the data file mapsdataspatial.list which is a very large # file. Consult the WinBUGS manual for information about the data # required to fit the CAR model. if(!exists("mapsdataspatial.list")){ cat("data file: mapsdataspatial.list not found",fill=TRUE) return(NULL) } library("R2WinBUGS") y<-mapsdataspatial.list$y first<-mapsdataspatial.list$first last<-mapsdataspatial.list$last nind<-mapsdataspatial.list$nind nyear<-mapsdataspatial.list$nyear resident<-mapsdataspatial.list$resident gridid<-mapsdataspatial.list$gridid num<-mapsdataspatial.list$num adj<-mapsdataspatial.list$adj sumNumNeigh<-mapsdataspatial.list$sumNumNeigh ngrid<-mapsdataspatial.list$ngrid # gridid is the integer grid id that each MAPS station belongs to # num, adj, sumNumNeigh are data required by the CAR model specification data <- list ("y","first","nind","nyear","resident","gridid","num","adj","sumNumNeigh","ngrid") Zst<-y for(i in 1:nind){ Zst[i,first[i]:last[i]]<-1 if(first[i]>1) Zst[i,1:(first[i]-1)]<-NA } cat(" model { for(t in 1:nyear){ p[t]~dunif(0,1) } phi0~dunif(0,1) lphi0<-log(phi0/(1-phi0)) tau~dunif(0,1) for(j in 1:sumNumNeigh) { weights[j] <- 1} tau2~dgamma(.1,.1) alpha[1:ngrid] ~ car.normal(adj[], weights[], num[], tau2) for(i in 1:nind){ logit(phi[i])<- lphi0 + alpha[gridid[i]] xx[i]<- pow(1,resident[i])*pow(tau,1-resident[i]) R[i] ~ dbern(xx[i]) z[i,first[i]]~dbern(1) for(j in (first[i]+1):nyear){ mu1[i,j]<-p[j]*z[i,j] y[i,j]~dbern(mu1[i,j]) mu2[i,j]<-z[i,j-1]*phi[i]*R[i] z[i,j]~dbern(mu2[i,j]) } } } ",file="model.txt") inits <- function(){ list ( phi0=0.4,p=runif(nyear,.2,.6),z=Zst,alpha=rnorm(ngrid)) } parameters <- c("phi0","p","tau","tau2","alpha") out <- bugs (data, inits, parameters, "model.txt", n.thin=2,n.chains=2, n.burnin=1000,n.iter=2000,debug=TRUE) return(out) }