# Single Species Single Season Occupancy models using JAGS packages

# Blue Gross Beaks.
#Downloaded from https://sites.google.com/site/asrworkshop/home/schedule/r-occupancy-1

#An occupancy study was made on Blue Grosbeaks (Guiraca caerulea) 
# on 41 old fields planted to longleaf pines (Pinus palustris) 
# in southern Georgia, USA. 

# Surveys were 500 m transects across each field 
# and were completed three times during the breeding season in 2001.

# Columns in the file are:
#    field - field number
#    v1, v2, v3 -  detection histories for each site on each of 3 visit during the 2001 breeding season.    
#    field.size - size of the files
#    bqi - unknown
#    crop.hist - crop history
#    crop1, crop2 - indicator variables for the crop history
#    count1, count2, count3 - are actual counts of birds detected in each visit
#
#    Bayesian model using JAGS with NO COVARIATES, either for psi or detection

library("R2jags")  # used for call to JAGS
## Loading required package: rjags
## Loading required package: coda
## Linked to JAGS 4.3.0
## Loaded modules: basemod,bugs
## 
## Attaching package: 'R2jags'
## The following object is masked from 'package:coda':
## 
##     traceplot
library(coda)
library(ggplot2)
library(reshape2)

# The BUGS model is specified as a text file.

# The model file.
# The cat() command is used to save the model to the working directory.
# Notice that you CANNOT have any " (double quotes) in the bugs code
# between the start and end of the cat("...",) command.

# Inputs to the model are 
#     Nsites  - number of sites
#     Nvisits - (max) number of visits over all sites.
#     Nsites.visits - number of sites x number of visits 
#          if there is missing data (no visits), simply drop the corresponding row
#     History - vector of 1 or 0 corresponding to Site-Visit pair
#     Site    - vector indicating which site the row corresponds to
#     Visit   - vector indicating which visit the row corresponds to
# 
#     psi     - occupancy parameter
#     p       - common detection probability

# 
cat(file="model.txt", "
############################################################

model {
   # set up the state model, i.e. is the site actually occupied or not
   for(i in 1:Nsites){
      z[i] ~  dbern(psi)
   }

   # the observation model.
   for(j in 1:Nsites.visits){
      p.z[j] <- z[Site[j]]*p
      History[j] ~ dbern(p.z[j])
   }

   # priors
   psi ~ dbeta(1,1)
   p   ~ dbeta(1,1)

   # derived variables
   # number of occupied sites
   occ.sites <- sum(z[1:Nsites])
 
   # belief that psi is above some value
   prob.psi.greater.50 <- ifelse( psi > 0.5, 1, 0)
}
") # End of the model



# Next create the data.txt file.
# Initialize the data values using standard R code by either reading
# in from an external file, or plain assignment.
 
input.data <- read.csv(file.path("..","blgr.csv"), 
                       header=TRUE, as.is=TRUE, strip.white=TRUE) 
head(input.data)
##   field v1 v2 v3 field.size bqi crop.hist crop1 crop2 count1 count2 count3
## 1     1  1  1  1       14.0   1      crop     1     0      1      2      2
## 2     2  1  1  0       12.7   1      crop     1     0      2      2      0
## 3     3  0  0  0       15.7   0     grass     0     1      0      0      0
## 4     4  0  1  0       19.5   0     grass     0     1      0      2      0
## 5     5  1  0  1       13.5   0      crop     1     0      1      0      1
## 6     6  0  0  1        9.6   0     mixed     0     1      0      0      2
##    X     logFS
## 1 NA 1.1461280
## 2 NA 1.1038037
## 3 NA 1.1958997
## 4 NA 1.2900346
## 5 NA 1.1303338
## 6 NA 0.9822712
# do some basic checks on your data 
# e.g. check number of sites; number of visits etc
nrow(input.data)
## [1] 41
range(input.data[, c("v1","v2","v3")], na.rm=TRUE)
## [1] 0 1
sum(is.na(input.data[, c("v1","v2","v3")]))
## [1] 0
input.history <- input.data[, c("v1","v2","v3")]
head(input.history)
##   v1 v2 v3
## 1  1  1  1
## 2  1  1  0
## 3  0  0  0
## 4  0  1  0
## 5  1  0  1
## 6  0  0  1
History <- as.vector(unlist(input.history))  # stacks the columns
Site    <- rep(1:nrow(input.history), ncol(input.history))
Visit   <- rep(1:ncol(input.history), each=nrow(input.history))
cbind(Site, Visit, History)[1:10,]
##       Site Visit History
##  [1,]    1     1       1
##  [2,]    2     1       1
##  [3,]    3     1       0
##  [4,]    4     1       0
##  [5,]    5     1       1
##  [6,]    6     1       0
##  [7,]    7     1       0
##  [8,]    8     1       1
##  [9,]    9     1       1
## [10,]   10     1       1
Nsites        <- nrow(input.history)
Nvisits       <- ncol(input.history)
Nsites.visits <- length(History)

# The datalist will be passed to JAGS with the names of the data
# values.
data.list <- list("Nsites","Nvisits","Nsites.visits",
                  "History", "Site", "Visit") # or

# check the list
data.list
## [[1]]
## [1] "Nsites"
## 
## [[2]]
## [1] "Nvisits"
## 
## [[3]]
## [1] "Nsites.visits"
## 
## [[4]]
## [1] "History"
## 
## [[5]]
## [1] "Site"
## 
## [[6]]
## [1] "Visit"
# Next create the initial values.
# If you are using more than one chain, you need to create a function
# that returns initial values for each chain.

# We define the initial value of z as 1 if any visit resulted in a detection, other wise 0
init.z <- apply(input.history, 1, max, na.rm=TRUE)

# we will use the naive estimate of occupancy.
init.psi <- sum(init.z)/Nsites

# initial p will be what fraction of 1's exist/occupancy
init.p <- mean(History)/init.psi

# we will start at the same initial starting point for each chain even though this
# is not recommended. 
init.list <- list(
      list(z=init.z, p=init.p, psi=init.psi ),
      list(z=init.z, p=init.p, psi=init.psi ),
      list(z=init.z, p=init.p, psi=init.psi )
      )  # end of list of lists of initial values

# Next create the list of parameters to monitor.
# The deviance is automatically monitored.
# 
monitor.list <- c("z","p", "psi", "occ.sites", "prob.psi.greater.50") # parameters to monitor
 
# Finally, the actual call to JAGS
set.seed(234234)  # intitalize seed for MCMC 

results <- R2jags::jags( 
      data      =data.list,   # list of data variables
      inits     =init.list,   # list/function for initial values
      parameters=monitor.list,# list of parameters to monitor
      model.file="model.txt",  # file with bugs model
      n.chains=3,
      n.iter  =5000,          # total iterations INCLUDING burn in
      n.burnin=2000,          # number of burning iterations
      n.thin=2,               # how much to thin
      DIC=TRUE,               # is DIC to be computed?
      working.dir=getwd()    # store results in current working directory
      )
## module glm loaded
## Warning in jags.model(model.file, data = data, inits = init.values,
## n.chains = n.chains, : Unused variable "Nvisits" in data
## Warning in jags.model(model.file, data = data, inits = init.values,
## n.chains = n.chains, : Unused variable "Visit" in data
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
## Graph information:
##    Observed stochastic nodes: 123
##    Unobserved stochastic nodes: 43
##    Total graph size: 339
## 
## Initializing model
#######################################
# extract some of the usual stuff and use R code directly
# use the standard print method

names(results)
## [1] "model"              "BUGSoutput"         "parameters.to.save"
## [4] "model.file"         "n.iter"             "DIC"
names(results$BUGSoutput)
##  [1] "n.chains"        "n.iter"          "n.burnin"       
##  [4] "n.thin"          "n.keep"          "n.sims"         
##  [7] "sims.array"      "sims.list"       "sims.matrix"    
## [10] "summary"         "mean"            "sd"             
## [13] "median"          "root.short"      "long.short"     
## [16] "dimension.short" "indexes.short"   "last.values"    
## [19] "program"         "model.file"      "isDIC"          
## [22] "DICbyR"          "pD"              "DIC"
# get the summary table
results$BUGSoutput$summary
##                            mean         sd        2.5%         25%
## deviance            151.0324389 9.95001926 132.9311625 143.6208610
## occ.sites            36.4402222 2.08066490  33.0000000  35.0000000
## p                     0.5503110 0.05534883   0.4435941   0.5119170
## prob.psi.greater.50   1.0000000 0.00000000   1.0000000   1.0000000
## psi                   0.8696077 0.06974181   0.7201308   0.8250719
## z[1]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[2]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[3]                  0.4304444 0.49519341   0.0000000   0.0000000
## z[4]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[5]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[6]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[7]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[8]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[9]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[10]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[11]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[12]                 0.4357778 0.49591346   0.0000000   0.0000000
## z[13]                 0.4191111 0.49346844   0.0000000   0.0000000
## z[14]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[15]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[16]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[17]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[18]                 0.4324444 0.49547028   0.0000000   0.0000000
## z[19]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[20]                 0.4271111 0.49471365   0.0000000   0.0000000
## z[21]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[22]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[23]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[24]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[25]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[26]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[27]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[28]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[29]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[30]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[31]                 0.4311111 0.49528662   0.0000000   0.0000000
## z[32]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[33]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[34]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[35]                 0.4304444 0.49519341   0.0000000   0.0000000
## z[36]                 0.4337778 0.49565029   0.0000000   0.0000000
## z[37]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[38]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[39]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[40]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[41]                 1.0000000 0.00000000   1.0000000   1.0000000
##                             50%         75%       97.5%     Rhat n.eff
## deviance            149.8072629 158.1584468 170.5667209 1.001913  1600
## occ.sites            36.0000000  38.0000000  41.0000000 1.002086  1400
## p                     0.5500037   0.5885745   0.6622420 1.001336  3000
## prob.psi.greater.50   1.0000000   1.0000000   1.0000000 1.000000     1
## psi                   0.8736507   0.9219870   0.9852478 1.003197   790
## z[1]                  1.0000000   1.0000000   1.0000000 1.000000     1
## z[2]                  1.0000000   1.0000000   1.0000000 1.000000     1
## z[3]                  0.0000000   1.0000000   1.0000000 1.001461  2500
## z[4]                  1.0000000   1.0000000   1.0000000 1.000000     1
## z[5]                  1.0000000   1.0000000   1.0000000 1.000000     1
## z[6]                  1.0000000   1.0000000   1.0000000 1.000000     1
## z[7]                  1.0000000   1.0000000   1.0000000 1.000000     1
## z[8]                  1.0000000   1.0000000   1.0000000 1.000000     1
## z[9]                  1.0000000   1.0000000   1.0000000 1.000000     1
## z[10]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[11]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[12]                 0.0000000   1.0000000   1.0000000 1.000704  4500
## z[13]                 0.0000000   1.0000000   1.0000000 1.002728   970
## z[14]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[15]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[16]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[17]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[18]                 0.0000000   1.0000000   1.0000000 1.002493  1100
## z[19]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[20]                 0.0000000   1.0000000   1.0000000 1.000992  4500
## z[21]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[22]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[23]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[24]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[25]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[26]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[27]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[28]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[29]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[30]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[31]                 0.0000000   1.0000000   1.0000000 1.001567  2200
## z[32]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[33]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[34]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[35]                 0.0000000   1.0000000   1.0000000 1.000787  4500
## z[36]                 0.0000000   1.0000000   1.0000000 1.000960  4500
## z[37]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[38]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[39]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[40]                 1.0000000   1.0000000   1.0000000 1.000000     1
## z[41]                 1.0000000   1.0000000   1.0000000 1.000000     1
results$BUGSoutput$summary[,c("mean", "sd", "2.5%","97.5%","Rhat", "n.eff")]
##                            mean         sd        2.5%       97.5%
## deviance            151.0324389 9.95001926 132.9311625 170.5667209
## occ.sites            36.4402222 2.08066490  33.0000000  41.0000000
## p                     0.5503110 0.05534883   0.4435941   0.6622420
## prob.psi.greater.50   1.0000000 0.00000000   1.0000000   1.0000000
## psi                   0.8696077 0.06974181   0.7201308   0.9852478
## z[1]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[2]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[3]                  0.4304444 0.49519341   0.0000000   1.0000000
## z[4]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[5]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[6]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[7]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[8]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[9]                  1.0000000 0.00000000   1.0000000   1.0000000
## z[10]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[11]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[12]                 0.4357778 0.49591346   0.0000000   1.0000000
## z[13]                 0.4191111 0.49346844   0.0000000   1.0000000
## z[14]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[15]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[16]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[17]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[18]                 0.4324444 0.49547028   0.0000000   1.0000000
## z[19]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[20]                 0.4271111 0.49471365   0.0000000   1.0000000
## z[21]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[22]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[23]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[24]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[25]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[26]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[27]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[28]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[29]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[30]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[31]                 0.4311111 0.49528662   0.0000000   1.0000000
## z[32]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[33]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[34]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[35]                 0.4304444 0.49519341   0.0000000   1.0000000
## z[36]                 0.4337778 0.49565029   0.0000000   1.0000000
## z[37]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[38]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[39]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[40]                 1.0000000 0.00000000   1.0000000   1.0000000
## z[41]                 1.0000000 0.00000000   1.0000000   1.0000000
##                         Rhat n.eff
## deviance            1.001913  1600
## occ.sites           1.002086  1400
## p                   1.001336  3000
## prob.psi.greater.50 1.000000     1
## psi                 1.003197   790
## z[1]                1.000000     1
## z[2]                1.000000     1
## z[3]                1.001461  2500
## z[4]                1.000000     1
## z[5]                1.000000     1
## z[6]                1.000000     1
## z[7]                1.000000     1
## z[8]                1.000000     1
## z[9]                1.000000     1
## z[10]               1.000000     1
## z[11]               1.000000     1
## z[12]               1.000704  4500
## z[13]               1.002728   970
## z[14]               1.000000     1
## z[15]               1.000000     1
## z[16]               1.000000     1
## z[17]               1.000000     1
## z[18]               1.002493  1100
## z[19]               1.000000     1
## z[20]               1.000992  4500
## z[21]               1.000000     1
## z[22]               1.000000     1
## z[23]               1.000000     1
## z[24]               1.000000     1
## z[25]               1.000000     1
## z[26]               1.000000     1
## z[27]               1.000000     1
## z[28]               1.000000     1
## z[29]               1.000000     1
## z[30]               1.000000     1
## z[31]               1.001567  2200
## z[32]               1.000000     1
## z[33]               1.000000     1
## z[34]               1.000000     1
## z[35]               1.000787  4500
## z[36]               1.000960  4500
## z[37]               1.000000     1
## z[38]               1.000000     1
## z[39]               1.000000     1
## z[40]               1.000000     1
## z[41]               1.000000     1
results$BUGSoutput$summary[,c("mean", "sd")]
##                            mean         sd
## deviance            151.0324389 9.95001926
## occ.sites            36.4402222 2.08066490
## p                     0.5503110 0.05534883
## prob.psi.greater.50   1.0000000 0.00000000
## psi                   0.8696077 0.06974181
## z[1]                  1.0000000 0.00000000
## z[2]                  1.0000000 0.00000000
## z[3]                  0.4304444 0.49519341
## z[4]                  1.0000000 0.00000000
## z[5]                  1.0000000 0.00000000
## z[6]                  1.0000000 0.00000000
## z[7]                  1.0000000 0.00000000
## z[8]                  1.0000000 0.00000000
## z[9]                  1.0000000 0.00000000
## z[10]                 1.0000000 0.00000000
## z[11]                 1.0000000 0.00000000
## z[12]                 0.4357778 0.49591346
## z[13]                 0.4191111 0.49346844
## z[14]                 1.0000000 0.00000000
## z[15]                 1.0000000 0.00000000
## z[16]                 1.0000000 0.00000000
## z[17]                 1.0000000 0.00000000
## z[18]                 0.4324444 0.49547028
## z[19]                 1.0000000 0.00000000
## z[20]                 0.4271111 0.49471365
## z[21]                 1.0000000 0.00000000
## z[22]                 1.0000000 0.00000000
## z[23]                 1.0000000 0.00000000
## z[24]                 1.0000000 0.00000000
## z[25]                 1.0000000 0.00000000
## z[26]                 1.0000000 0.00000000
## z[27]                 1.0000000 0.00000000
## z[28]                 1.0000000 0.00000000
## z[29]                 1.0000000 0.00000000
## z[30]                 1.0000000 0.00000000
## z[31]                 0.4311111 0.49528662
## z[32]                 1.0000000 0.00000000
## z[33]                 1.0000000 0.00000000
## z[34]                 1.0000000 0.00000000
## z[35]                 0.4304444 0.49519341
## z[36]                 0.4337778 0.49565029
## z[37]                 1.0000000 0.00000000
## z[38]                 1.0000000 0.00000000
## z[39]                 1.0000000 0.00000000
## z[40]                 1.0000000 0.00000000
## z[41]                 1.0000000 0.00000000
# get just the means
results$BUGSoutput$mean
## $deviance
## [1] 151.0324
## 
## $occ.sites
## [1] 36.44022
## 
## $p
## [1] 0.550311
## 
## $prob.psi.greater.50
## [1] 1
## 
## $psi
## [1] 0.8696077
## 
## $z
##  [1] 1.0000000 1.0000000 0.4304444 1.0000000 1.0000000 1.0000000 1.0000000
##  [8] 1.0000000 1.0000000 1.0000000 1.0000000 0.4357778 0.4191111 1.0000000
## [15] 1.0000000 1.0000000 1.0000000 0.4324444 1.0000000 0.4271111 1.0000000
## [22] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
## [29] 1.0000000 1.0000000 0.4311111 1.0000000 1.0000000 1.0000000 0.4304444
## [36] 0.4337778 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
results$BUGSoutput$mean$psi
## [1] 0.8696077
# the results$BUGSoutput$sims.array is a 3-d object [iterations, chains, variables]
dim(results$BUGSoutput$sims.array)
## [1] 1500    3   46
results$BUGSoutput$sims.array[1:5,,1:5]
## , , deviance
## 
##          [,1]     [,2]     [,3]
## [1,] 153.4604 171.4608 153.1523
## [2,] 162.9600 162.1560 157.7223
## [3,] 162.8787 163.6889 133.1059
## [4,] 167.2966 166.7140 152.6563
## [5,] 162.2269 170.6960 148.4164
## 
## , , occ.sites
## 
##      [,1] [,2] [,3]
## [1,]   37   41   37
## [2,]   39   39   38
## [3,]   39   39   33
## [4,]   40   40   36
## [5,]   39   41   35
## 
## , , p
## 
##           [,1]      [,2]      [,3]
## [1,] 0.5668788 0.4424528 0.5435832
## [2,] 0.5550481 0.5039863 0.5275552
## [3,] 0.5529624 0.4550106 0.5767290
## [4,] 0.5441958 0.5273146 0.6519774
## [5,] 0.4976729 0.5105672 0.6759793
## 
## , , prob.psi.greater.50
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    1    1
## [3,]    1    1    1
## [4,]    1    1    1
## [5,]    1    1    1
## 
## , , psi
## 
##           [,1]      [,2]      [,3]
## [1,] 0.8378131 0.9873153 0.9030364
## [2,] 0.9211029 0.9639474 0.8884114
## [3,] 0.9172542 0.9413326 0.6936131
## [4,] 0.9539518 0.9965744 0.9358349
## [5,] 0.9438991 0.9859744 0.8571180
results$BUGSoutput$sims.array[1:5,1,"psi"]
## [1] 0.8378131 0.9211029 0.9172542 0.9539518 0.9438991
# the results$BUGSoutput$sims.matrix is a 2-d object [iterations, variables] with chains stacked
# on top of each other
dim(results$BUGSoutput$sims.matrix)
## [1] 4500   46
results$BUGSoutput$sims.matrix[1:5,1:10]
##      deviance occ.sites         p prob.psi.greater.50       psi z[1] z[2]
## [1,] 170.7763        41 0.4617486                   1 0.9916120    1    1
## [2,] 167.4571        40 0.5477999                   1 0.8988662    1    1
## [3,] 133.2396        33 0.6398758                   1 0.7336665    1    1
## [4,] 139.9884        34 0.6519416                   1 0.7891217    1    1
## [5,] 153.5049        37 0.5686853                   1 0.9326727    1    1
##      z[3] z[4] z[5]
## [1,]    1    1    1
## [2,]    1    1    1
## [3,]    0    1    1
## [4,]    0    1    1
## [5,]    0    1    1
results$BUGSoutput$sims.matrix[1:5,"psi"]
## [1] 0.9916120 0.8988662 0.7336665 0.7891217 0.9326727
# make a posterior density plot
plotdata <- data.frame(parm=results$BUGSoutput$sims.matrix[,"psi"], stringsAsFactors=FALSE)
head(plotdata)
##        parm
## 1 0.9916120
## 2 0.8988662
## 3 0.7336665
## 4 0.7891217
## 5 0.9326727
## 6 0.8015004
postplot.parm <- ggplot2::ggplot( data=plotdata, aes(x=parm, y=..density..))+
  geom_histogram(alpha=0.3)+
  geom_density()+
  ggtitle("Posterior density plot for psi")
postplot.parm
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggsave(plot=postplot.parm, file='psi-posterior-p-dot-psi-dot.png', h=4, w=6, units="in", dpi=300)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# make a trace plot (notice we use the sims.array here)
plotdata <- data.frame(psi=results$BUGSoutput$sims.array[,,"psi"], stringsAsFactors=FALSE)
plotdata$iteration <- 1:nrow(plotdata)
head(plotdata)
##       psi.1     psi.2     psi.3 iteration
## 1 0.8378131 0.9873153 0.9030364         1
## 2 0.9211029 0.9639474 0.8884114         2
## 3 0.9172542 0.9413326 0.6936131         3
## 4 0.9539518 0.9965744 0.9358349         4
## 5 0.9438991 0.9859744 0.8571180         5
## 6 0.9172572 0.9537536 0.8981272         6
# convert from wide to long format
plotdata2 <- reshape2:::melt.data.frame(data=plotdata, 
                            id.vars="iteration",
                            measure.vars=paste("psi",1:results$BUGSoutput$n.chains,sep="."),
                            variable.name="chain",
                            value.name='psi')
head(plotdata2)
##   iteration chain       psi
## 1         1 psi.1 0.8378131
## 2         2 psi.1 0.9211029
## 3         3 psi.1 0.9172542
## 4         4 psi.1 0.9539518
## 5         5 psi.1 0.9438991
## 6         6 psi.1 0.9172572
traceplot.parm <- ggplot2::ggplot(data=plotdata2, aes(x=iteration, y=psi, color=chain))+
  ggtitle("Trace plot for psi")+
  geom_line(alpha=.2)
traceplot.parm

ggsave(plot=traceplot.parm, file='psi-trace-p-dot-psi-dot.png', h=4, w=6, units="in", dpi=300)


# autocorrelation plot
# First compute the autocorrelation plot
acf.parm <-acf( results$BUGSoutput$sims.matrix[,"psi"], plot=FALSE)
acf.parm
## 
## Autocorrelations of series 'results$BUGSoutput$sims.matrix[, "psi"]', by lag
## 
##      0      1      2      3      4      5      6      7      8      9 
##  1.000 -0.001  0.010 -0.011  0.040 -0.018  0.000  0.015 -0.011  0.013 
##     10     11     12     13     14     15     16     17     18     19 
## -0.019  0.000  0.005 -0.008 -0.013 -0.020 -0.013  0.000 -0.001  0.004 
##     20     21     22     23     24     25     26     27     28     29 
##  0.012 -0.002 -0.012  0.004  0.014  0.012 -0.002 -0.022 -0.018 -0.006 
##     30     31     32     33     34     35     36 
##  0.007 -0.005 -0.018 -0.008  0.019 -0.016  0.010
acfplot.parm <- ggplot(data=with(acf.parm, data.frame(lag, acf)), aes(x = lag, y = acf)) +
  ggtitle("Autocorrelation plot for psi")+
  geom_hline(aes(yintercept = 0)) +
  geom_segment(aes(xend = lag, yend = 0))
acfplot.parm

ggsave(plot=acfplot.parm, file="psi-acf-p-dot-psi-dot.png",h=4, w=6, units="in", dpi=300)
#
#    Bayesian model using JAGS with COVARIATES for detection
#    such a time effects.
#    You need to build the covariate matrix and send to JAGS

library("R2jags")  # used for call to JAGS
library(coda)
library(ggplot2)
library(reshape2)

# The BUGS model is specified as a text file.

# The model file.
# The cat() command is used to save the model to the working directory.
# Notice that you CANNOT have any " (double quotes) in the bugs code
# between the start and end of the cat("...",) command.

# Inputs to the model are 
#     Nsites  - number of sites
#     Nvisits - (max) number of visits over all sites.
#     Nsites.visits - number of sites x number of visits 
#          if there is missing data (no visits), simply drop the corresponding row
#     History - vector of 1 or 0 corresponding to Site-Visit pair
#     Site    - vector indicating which site the row corresponds to
#     Visit   - vector indicating which visit the row corresponds to
# 
#     psi     - occupancy parameter
#     p       - detection probability
#
#     Ncovar.p - number of covariates for logit(p)
#     covar.p - matrix of covariates (Nsites.visits x N.covar.p)

# 
cat(file="model.txt", "
############################################################

model {
   # set up the state model, i.e. is the site actually occupied or not
   for(i in 1:Nsites){
      z[i] ~  dbern(psi)
   }

   # the observation model.
   for(j in 1:Nsites.visits){
      logit(p.detect[j]) <- inprod(beta.p[1:Ncovar.p], covar.p[j, 1:Ncovar.p])
      p.z[j] <- z[Site[j]]*p.detect[j]
      History[j] ~ dbern(p.z[j])
   }

   # priors
   psi ~ dbeta(1,1)
   for(i in 1:Ncovar.p){
      beta.p[i]   ~ dnorm(0, .0001)
   }

   # derived variables
   # number of occupied sites
   occ.sites <- sum(z[1:Nsites])
 
   # belief that psi is above some value
   prob.psi.greater.50 <- ifelse( psi > 0.5, 1, 0)
}
") # End of the model



# Next create the data.txt file.
# Initialize the data values using standard R code by either reading
# in from an external file, or plain assignment.
 
input.data <- read.csv(file.path("..","blgr.csv"), 
                       header=TRUE, as.is=TRUE, strip.white=TRUE) 
head(input.data)
##   field v1 v2 v3 field.size bqi crop.hist crop1 crop2 count1 count2 count3
## 1     1  1  1  1       14.0   1      crop     1     0      1      2      2
## 2     2  1  1  0       12.7   1      crop     1     0      2      2      0
## 3     3  0  0  0       15.7   0     grass     0     1      0      0      0
## 4     4  0  1  0       19.5   0     grass     0     1      0      2      0
## 5     5  1  0  1       13.5   0      crop     1     0      1      0      1
## 6     6  0  0  1        9.6   0     mixed     0     1      0      0      2
##    X     logFS
## 1 NA 1.1461280
## 2 NA 1.1038037
## 3 NA 1.1958997
## 4 NA 1.2900346
## 5 NA 1.1303338
## 6 NA 0.9822712
# do some basic checks on your data 
# e.g. check number of sites; number of visits etc
nrow(input.data)
## [1] 41
range(input.data[, c("v1","v2","v3")], na.rm=TRUE)
## [1] 0 1
sum(is.na(input.data[, c("v1","v2","v3")]))
## [1] 0
input.history <- input.data[, c("v1","v2","v3")]
head(input.history)
##   v1 v2 v3
## 1  1  1  1
## 2  1  1  0
## 3  0  0  0
## 4  0  1  0
## 5  1  0  1
## 6  0  0  1
History <- as.vector(unlist(input.history))  # stacks the columns
Site    <- rep(1:nrow(input.history), ncol(input.history))
Visit   <- rep(1:ncol(input.history), each=nrow(input.history))

covar.p <- model.matrix( ~as.factor(Visit), data=as.data.frame(Visit))
covar.p[1:10,]
##    (Intercept) as.factor(Visit)2 as.factor(Visit)3
## 1            1                 0                 0
## 2            1                 0                 0
## 3            1                 0                 0
## 4            1                 0                 0
## 5            1                 0                 0
## 6            1                 0                 0
## 7            1                 0                 0
## 8            1                 0                 0
## 9            1                 0                 0
## 10           1                 0                 0
# create a covariate matrix for time effects.
cbind(Site, Visit, History, covar.p)[1:10,]
##    Site Visit History (Intercept) as.factor(Visit)2 as.factor(Visit)3
## 1     1     1       1           1                 0                 0
## 2     2     1       1           1                 0                 0
## 3     3     1       0           1                 0                 0
## 4     4     1       0           1                 0                 0
## 5     5     1       1           1                 0                 0
## 6     6     1       0           1                 0                 0
## 7     7     1       0           1                 0                 0
## 8     8     1       1           1                 0                 0
## 9     9     1       1           1                 0                 0
## 10   10     1       1           1                 0                 0
Nsites        <- nrow(input.history)
Nvisits       <- ncol(input.history)
Nsites.visits <- length(History)
Ncovar.p      <- ncol(covar.p)

# The datalist will be passed to JAGS with the names of the data
# values.
data.list <- list("Nsites","Nvisits","Nsites.visits",
                  "History", "Site", "Visit",
                  "Ncovar.p","covar.p") # or

# check the list
data.list
## [[1]]
## [1] "Nsites"
## 
## [[2]]
## [1] "Nvisits"
## 
## [[3]]
## [1] "Nsites.visits"
## 
## [[4]]
## [1] "History"
## 
## [[5]]
## [1] "Site"
## 
## [[6]]
## [1] "Visit"
## 
## [[7]]
## [1] "Ncovar.p"
## 
## [[8]]
## [1] "covar.p"
# Next create the initial values.
# If you are using more than one chain, you need to create a function
# that returns initial values for each chain.

# We define the initial value of z as 1 if any visit resulted in a detection, other wise 0
init.z <- apply(input.history, 1, max, na.rm=TRUE)

# we will use the naive estimate of occupancy.
init.psi <- sum(init.z)/Nsites

# initial p will be what fraction of 1's exist/occupancy
init.p <- mean(History)/init.psi

# we will start at the same initial starting point for each chain even though this
# is not recommended. 
init.list <- list(
      list(z=init.z, beta.p=rep(0,ncol(input.history)), psi=init.psi ),
      list(z=init.z, beta.p=rep(0,ncol(input.history)), psi=init.psi ),
      list(z=init.z, beta.p=rep(0,ncol(input.history)), psi=init.psi )
      )  # end of list of lists of initial values

# Next create the list of parameters to monitor.
# The deviance is automatically monitored.
# 
monitor.list <- c("z",
                  "p.detect", "beta.p",
                  "psi", "occ.sites", "prob.psi.greater.50") # parameters to monitor
 
# Finally, the actual call to JAGS
set.seed(234234)  # intitalize seed for MCMC 

results <- R2jags::jags( 
      data      =data.list,   # list of data variables
      inits     =init.list,   # list/function for initial values
      parameters=monitor.list,# list of parameters to monitor
      model.file="model.txt",  # file with bugs model
      n.chains=3,
      n.iter  =5000,          # total iterations INCLUDING burn in
      n.burnin=2000,          # number of burning iterations
      n.thin=2,               # how much to thin
      DIC=TRUE,               # is DIC to be computed?
      working.dir=getwd()    # store results in current working directory
      )
## Warning in jags.model(model.file, data = data, inits = init.values,
## n.chains = n.chains, : Unused variable "Nvisits" in data
## Warning in jags.model(model.file, data = data, inits = init.values,
## n.chains = n.chains, : Unused variable "Visit" in data
## Compiling model graph
##    Resolving undeclared variables
##    Allocating nodes
## Graph information:
##    Observed stochastic nodes: 123
##    Unobserved stochastic nodes: 45
##    Total graph size: 924
## 
## Initializing model
#######################################
# extract some of the usual stuff and use R code directly
# use the standard print method

names(results)
## [1] "model"              "BUGSoutput"         "parameters.to.save"
## [4] "model.file"         "n.iter"             "DIC"
names(results$BUGSoutput)
##  [1] "n.chains"        "n.iter"          "n.burnin"       
##  [4] "n.thin"          "n.keep"          "n.sims"         
##  [7] "sims.array"      "sims.list"       "sims.matrix"    
## [10] "summary"         "mean"            "sd"             
## [13] "median"          "root.short"      "long.short"     
## [16] "dimension.short" "indexes.short"   "last.values"    
## [19] "program"         "model.file"      "isDIC"          
## [22] "DICbyR"          "pD"              "DIC"
# get the summary table
results$BUGSoutput$summary
##                             mean          sd        2.5%          25%
## beta.p[1]           7.341136e-03  0.35027669  -0.6623809  -0.22930664
## beta.p[2]           4.570040e-01  0.48719020  -0.4838118   0.13396243
## beta.p[3]           2.295608e-01  0.47850860  -0.6929904  -0.08857968
## deviance            1.505223e+02 10.17831983 133.1999635 143.07861479
## occ.sites           3.610333e+01  2.06070088  33.0000000  35.00000000
## p.detect[1]         5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[2]         5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[3]         5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[4]         5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[5]         5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[6]         5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[7]         5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[8]         5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[9]         5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[10]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[11]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[12]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[13]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[14]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[15]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[16]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[17]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[18]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[19]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[20]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[21]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[22]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[23]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[24]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[25]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[26]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[27]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[28]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[29]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[30]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[31]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[32]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[33]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[34]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[35]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[36]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[37]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[38]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[39]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[40]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[41]        5.017003e-01  0.08505275   0.3402050   0.44292322
## p.detect[42]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[43]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[44]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[45]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[46]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[47]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[48]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[49]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[50]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[51]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[52]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[53]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[54]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[55]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[56]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[57]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[58]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[59]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[60]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[61]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[62]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[63]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[64]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[65]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[66]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[67]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[68]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[69]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[70]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[71]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[72]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[73]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[74]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[75]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[76]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[77]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[78]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[79]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[80]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[81]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[82]        6.102433e-01  0.08768922   0.4329100   0.55140452
## p.detect[83]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[84]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[85]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[86]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[87]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[88]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[89]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[90]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[91]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[92]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[93]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[94]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[95]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[96]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[97]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[98]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[99]        5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[100]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[101]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[102]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[103]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[104]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[105]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[106]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[107]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[108]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[109]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[110]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[111]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[112]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[113]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[114]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[115]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[116]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[117]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[118]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[119]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[120]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[121]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[122]       5.570885e-01  0.08618243   0.3906294   0.49531322
## p.detect[123]       5.570885e-01  0.08618243   0.3906294   0.49531322
## prob.psi.greater.50 1.000000e+00  0.00000000   1.0000000   1.00000000
## psi                 8.632998e-01  0.06923007   0.7167414   0.81710865
## z[1]                1.000000e+00  0.00000000   1.0000000   1.00000000
## z[2]                1.000000e+00  0.00000000   1.0000000   1.00000000
## z[3]                3.944444e-01  0.48878535   0.0000000   0.00000000
## z[4]                1.000000e+00  0.00000000   1.0000000   1.00000000
## z[5]                1.000000e+00  0.00000000   1.0000000   1.00000000
## z[6]                1.000000e+00  0.00000000   1.0000000   1.00000000
## z[7]                1.000000e+00  0.00000000   1.0000000   1.00000000
## z[8]                1.000000e+00  0.00000000   1.0000000   1.00000000
## z[9]                1.000000e+00  0.00000000   1.0000000   1.00000000
## z[10]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[11]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[12]               3.808889e-01  0.48565930   0.0000000   0.00000000
## z[13]               3.957778e-01  0.48907145   0.0000000   0.00000000
## z[14]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[15]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[16]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[17]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[18]               3.791111e-01  0.48521974   0.0000000   0.00000000
## z[19]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[20]               3.855556e-01  0.48678037   0.0000000   0.00000000
## z[21]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[22]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[23]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[24]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[25]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[26]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[27]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[28]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[29]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[30]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[31]               3.902222e-01  0.48785421   0.0000000   0.00000000
## z[32]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[33]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[34]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[35]               3.922222e-01  0.48830005   0.0000000   0.00000000
## z[36]               3.851111e-01  0.48667564   0.0000000   0.00000000
## z[37]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[38]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[39]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[40]               1.000000e+00  0.00000000   1.0000000   1.00000000
## z[41]               1.000000e+00  0.00000000   1.0000000   1.00000000
##                              50%         75%       97.5%     Rhat n.eff
## beta.p[1]           1.869553e-04   0.2417988   0.7054848 1.002438  1100
## beta.p[2]           4.498199e-01   0.7815119   1.4240114 1.001731  1900
## beta.p[3]           2.257393e-01   0.5499120   1.1876976 1.001227  3600
## deviance            1.494369e+02 157.7713365 171.1245369 1.001356  2900
## occ.sites           3.600000e+01  37.0000000  41.0000000 1.001418  2700
## p.detect[1]         5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[2]         5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[3]         5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[4]         5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[5]         5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[6]         5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[7]         5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[8]         5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[9]         5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[10]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[11]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[12]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[13]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[14]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[15]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[16]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[17]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[18]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[19]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[20]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[21]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[22]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[23]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[24]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[25]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[26]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[27]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[28]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[29]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[30]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[31]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[32]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[33]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[34]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[35]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[36]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[37]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[38]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[39]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[40]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[41]        5.000467e-01   0.5601569   0.6694026 1.002226  1300
## p.detect[42]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[43]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[44]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[45]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[46]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[47]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[48]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[49]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[50]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[51]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[52]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[53]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[54]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[55]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[56]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[57]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[58]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[59]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[60]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[61]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[62]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[63]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[64]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[65]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[66]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[67]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[68]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[69]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[70]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[71]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[72]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[73]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[74]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[75]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[76]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[77]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[78]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[79]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[80]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[81]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[82]        6.124293e-01   0.6719637   0.7758183 1.000667  4500
## p.detect[83]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[84]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[85]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[86]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[87]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[88]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[89]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[90]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[91]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[92]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[93]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[94]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[95]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[96]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[97]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[98]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[99]        5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[100]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[101]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[102]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[103]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[104]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[105]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[106]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[107]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[108]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[109]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[110]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[111]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[112]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[113]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[114]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[115]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[116]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[117]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[118]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[119]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[120]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[121]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[122]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## p.detect[123]       5.564936e-01   0.6191692   0.7178190 1.001166  4000
## prob.psi.greater.50 1.000000e+00   1.0000000   1.0000000 1.000000     1
## psi                 8.685032e-01   0.9140069   0.9836331 1.001179  3900
## z[1]                1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[2]                1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[3]                0.000000e+00   1.0000000   1.0000000 1.000695  4500
## z[4]                1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[5]                1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[6]                1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[7]                1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[8]                1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[9]                1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[10]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[11]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[12]               0.000000e+00   1.0000000   1.0000000 1.002014  1500
## z[13]               0.000000e+00   1.0000000   1.0000000 1.000735  4500
## z[14]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[15]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[16]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[17]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[18]               0.000000e+00   1.0000000   1.0000000 1.000693  4500
## z[19]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[20]               0.000000e+00   1.0000000   1.0000000 1.001224  3600
## z[21]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[22]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[23]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[24]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[25]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[26]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[27]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[28]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[29]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[30]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[31]               0.000000e+00   1.0000000   1.0000000 1.002157  1300
## z[32]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[33]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[34]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[35]               0.000000e+00   1.0000000   1.0000000 1.000766  4500
## z[36]               0.000000e+00   1.0000000   1.0000000 1.001324  3000
## z[37]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[38]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[39]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[40]               1.000000e+00   1.0000000   1.0000000 1.000000     1
## z[41]               1.000000e+00   1.0000000   1.0000000 1.000000     1
results$BUGSoutput$summary[,c("mean", "sd", "2.5%","97.5%","Rhat", "n.eff")]
##                             mean          sd        2.5%       97.5%
## beta.p[1]           7.341136e-03  0.35027669  -0.6623809   0.7054848
## beta.p[2]           4.570040e-01  0.48719020  -0.4838118   1.4240114
## beta.p[3]           2.295608e-01  0.47850860  -0.6929904   1.1876976
## deviance            1.505223e+02 10.17831983 133.1999635 171.1245369
## occ.sites           3.610333e+01  2.06070088  33.0000000  41.0000000
## p.detect[1]         5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[2]         5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[3]         5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[4]         5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[5]         5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[6]         5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[7]         5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[8]         5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[9]         5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[10]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[11]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[12]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[13]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[14]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[15]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[16]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[17]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[18]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[19]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[20]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[21]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[22]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[23]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[24]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[25]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[26]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[27]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[28]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[29]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[30]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[31]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[32]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[33]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[34]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[35]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[36]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[37]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[38]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[39]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[40]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[41]        5.017003e-01  0.08505275   0.3402050   0.6694026
## p.detect[42]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[43]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[44]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[45]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[46]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[47]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[48]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[49]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[50]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[51]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[52]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[53]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[54]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[55]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[56]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[57]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[58]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[59]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[60]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[61]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[62]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[63]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[64]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[65]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[66]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[67]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[68]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[69]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[70]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[71]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[72]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[73]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[74]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[75]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[76]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[77]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[78]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[79]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[80]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[81]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[82]        6.102433e-01  0.08768922   0.4329100   0.7758183
## p.detect[83]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[84]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[85]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[86]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[87]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[88]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[89]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[90]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[91]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[92]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[93]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[94]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[95]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[96]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[97]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[98]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[99]        5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[100]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[101]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[102]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[103]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[104]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[105]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[106]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[107]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[108]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[109]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[110]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[111]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[112]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[113]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[114]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[115]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[116]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[117]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[118]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[119]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[120]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[121]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[122]       5.570885e-01  0.08618243   0.3906294   0.7178190
## p.detect[123]       5.570885e-01  0.08618243   0.3906294   0.7178190
## prob.psi.greater.50 1.000000e+00  0.00000000   1.0000000   1.0000000
## psi                 8.632998e-01  0.06923007   0.7167414   0.9836331
## z[1]                1.000000e+00  0.00000000   1.0000000   1.0000000
## z[2]                1.000000e+00  0.00000000   1.0000000   1.0000000
## z[3]                3.944444e-01  0.48878535   0.0000000   1.0000000
## z[4]                1.000000e+00  0.00000000   1.0000000   1.0000000
## z[5]                1.000000e+00  0.00000000   1.0000000   1.0000000
## z[6]                1.000000e+00  0.00000000   1.0000000   1.0000000
## z[7]                1.000000e+00  0.00000000   1.0000000   1.0000000
## z[8]                1.000000e+00  0.00000000   1.0000000   1.0000000
## z[9]                1.000000e+00  0.00000000   1.0000000   1.0000000
## z[10]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[11]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[12]               3.808889e-01  0.48565930   0.0000000   1.0000000
## z[13]               3.957778e-01  0.48907145   0.0000000   1.0000000
## z[14]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[15]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[16]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[17]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[18]               3.791111e-01  0.48521974   0.0000000   1.0000000
## z[19]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[20]               3.855556e-01  0.48678037   0.0000000   1.0000000
## z[21]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[22]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[23]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[24]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[25]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[26]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[27]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[28]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[29]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[30]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[31]               3.902222e-01  0.48785421   0.0000000   1.0000000
## z[32]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[33]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[34]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[35]               3.922222e-01  0.48830005   0.0000000   1.0000000
## z[36]               3.851111e-01  0.48667564   0.0000000   1.0000000
## z[37]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[38]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[39]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[40]               1.000000e+00  0.00000000   1.0000000   1.0000000
## z[41]               1.000000e+00  0.00000000   1.0000000   1.0000000
##                         Rhat n.eff
## beta.p[1]           1.002438  1100
## beta.p[2]           1.001731  1900
## beta.p[3]           1.001227  3600
## deviance            1.001356  2900
## occ.sites           1.001418  2700
## p.detect[1]         1.002226  1300
## p.detect[2]         1.002226  1300
## p.detect[3]         1.002226  1300
## p.detect[4]         1.002226  1300
## p.detect[5]         1.002226  1300
## p.detect[6]         1.002226  1300
## p.detect[7]         1.002226  1300
## p.detect[8]         1.002226  1300
## p.detect[9]         1.002226  1300
## p.detect[10]        1.002226  1300
## p.detect[11]        1.002226  1300
## p.detect[12]        1.002226  1300
## p.detect[13]        1.002226  1300
## p.detect[14]        1.002226  1300
## p.detect[15]        1.002226  1300
## p.detect[16]        1.002226  1300
## p.detect[17]        1.002226  1300
## p.detect[18]        1.002226  1300
## p.detect[19]        1.002226  1300
## p.detect[20]        1.002226  1300
## p.detect[21]        1.002226  1300
## p.detect[22]        1.002226  1300
## p.detect[23]        1.002226  1300
## p.detect[24]        1.002226  1300
## p.detect[25]        1.002226  1300
## p.detect[26]        1.002226  1300
## p.detect[27]        1.002226  1300
## p.detect[28]        1.002226  1300
## p.detect[29]        1.002226  1300
## p.detect[30]        1.002226  1300
## p.detect[31]        1.002226  1300
## p.detect[32]        1.002226  1300
## p.detect[33]        1.002226  1300
## p.detect[34]        1.002226  1300
## p.detect[35]        1.002226  1300
## p.detect[36]        1.002226  1300
## p.detect[37]        1.002226  1300
## p.detect[38]        1.002226  1300
## p.detect[39]        1.002226  1300
## p.detect[40]        1.002226  1300
## p.detect[41]        1.002226  1300
## p.detect[42]        1.000667  4500
## p.detect[43]        1.000667  4500
## p.detect[44]        1.000667  4500
## p.detect[45]        1.000667  4500
## p.detect[46]        1.000667  4500
## p.detect[47]        1.000667  4500
## p.detect[48]        1.000667  4500
## p.detect[49]        1.000667  4500
## p.detect[50]        1.000667  4500
## p.detect[51]        1.000667  4500
## p.detect[52]        1.000667  4500
## p.detect[53]        1.000667  4500
## p.detect[54]        1.000667  4500
## p.detect[55]        1.000667  4500
## p.detect[56]        1.000667  4500
## p.detect[57]        1.000667  4500
## p.detect[58]        1.000667  4500
## p.detect[59]        1.000667  4500
## p.detect[60]        1.000667  4500
## p.detect[61]        1.000667  4500
## p.detect[62]        1.000667  4500
## p.detect[63]        1.000667  4500
## p.detect[64]        1.000667  4500
## p.detect[65]        1.000667  4500
## p.detect[66]        1.000667  4500
## p.detect[67]        1.000667  4500
## p.detect[68]        1.000667  4500
## p.detect[69]        1.000667  4500
## p.detect[70]        1.000667  4500
## p.detect[71]        1.000667  4500
## p.detect[72]        1.000667  4500
## p.detect[73]        1.000667  4500
## p.detect[74]        1.000667  4500
## p.detect[75]        1.000667  4500
## p.detect[76]        1.000667  4500
## p.detect[77]        1.000667  4500
## p.detect[78]        1.000667  4500
## p.detect[79]        1.000667  4500
## p.detect[80]        1.000667  4500
## p.detect[81]        1.000667  4500
## p.detect[82]        1.000667  4500
## p.detect[83]        1.001166  4000
## p.detect[84]        1.001166  4000
## p.detect[85]        1.001166  4000
## p.detect[86]        1.001166  4000
## p.detect[87]        1.001166  4000
## p.detect[88]        1.001166  4000
## p.detect[89]        1.001166  4000
## p.detect[90]        1.001166  4000
## p.detect[91]        1.001166  4000
## p.detect[92]        1.001166  4000
## p.detect[93]        1.001166  4000
## p.detect[94]        1.001166  4000
## p.detect[95]        1.001166  4000
## p.detect[96]        1.001166  4000
## p.detect[97]        1.001166  4000
## p.detect[98]        1.001166  4000
## p.detect[99]        1.001166  4000
## p.detect[100]       1.001166  4000
## p.detect[101]       1.001166  4000
## p.detect[102]       1.001166  4000
## p.detect[103]       1.001166  4000
## p.detect[104]       1.001166  4000
## p.detect[105]       1.001166  4000
## p.detect[106]       1.001166  4000
## p.detect[107]       1.001166  4000
## p.detect[108]       1.001166  4000
## p.detect[109]       1.001166  4000
## p.detect[110]       1.001166  4000
## p.detect[111]       1.001166  4000
## p.detect[112]       1.001166  4000
## p.detect[113]       1.001166  4000
## p.detect[114]       1.001166  4000
## p.detect[115]       1.001166  4000
## p.detect[116]       1.001166  4000
## p.detect[117]       1.001166  4000
## p.detect[118]       1.001166  4000
## p.detect[119]       1.001166  4000
## p.detect[120]       1.001166  4000
## p.detect[121]       1.001166  4000
## p.detect[122]       1.001166  4000
## p.detect[123]       1.001166  4000
## prob.psi.greater.50 1.000000     1
## psi                 1.001179  3900
## z[1]                1.000000     1
## z[2]                1.000000     1
## z[3]                1.000695  4500
## z[4]                1.000000     1
## z[5]                1.000000     1
## z[6]                1.000000     1
## z[7]                1.000000     1
## z[8]                1.000000     1
## z[9]                1.000000     1
## z[10]               1.000000     1
## z[11]               1.000000     1
## z[12]               1.002014  1500
## z[13]               1.000735  4500
## z[14]               1.000000     1
## z[15]               1.000000     1
## z[16]               1.000000     1
## z[17]               1.000000     1
## z[18]               1.000693  4500
## z[19]               1.000000     1
## z[20]               1.001224  3600
## z[21]               1.000000     1
## z[22]               1.000000     1
## z[23]               1.000000     1
## z[24]               1.000000     1
## z[25]               1.000000     1
## z[26]               1.000000     1
## z[27]               1.000000     1
## z[28]               1.000000     1
## z[29]               1.000000     1
## z[30]               1.000000     1
## z[31]               1.002157  1300
## z[32]               1.000000     1
## z[33]               1.000000     1
## z[34]               1.000000     1
## z[35]               1.000766  4500
## z[36]               1.001324  3000
## z[37]               1.000000     1
## z[38]               1.000000     1
## z[39]               1.000000     1
## z[40]               1.000000     1
## z[41]               1.000000     1
results$BUGSoutput$summary[,c("mean", "sd")]
##                             mean          sd
## beta.p[1]           7.341136e-03  0.35027669
## beta.p[2]           4.570040e-01  0.48719020
## beta.p[3]           2.295608e-01  0.47850860
## deviance            1.505223e+02 10.17831983
## occ.sites           3.610333e+01  2.06070088
## p.detect[1]         5.017003e-01  0.08505275
## p.detect[2]         5.017003e-01  0.08505275
## p.detect[3]         5.017003e-01  0.08505275
## p.detect[4]         5.017003e-01  0.08505275
## p.detect[5]         5.017003e-01  0.08505275
## p.detect[6]         5.017003e-01  0.08505275
## p.detect[7]         5.017003e-01  0.08505275
## p.detect[8]         5.017003e-01  0.08505275
## p.detect[9]         5.017003e-01  0.08505275
## p.detect[10]        5.017003e-01  0.08505275
## p.detect[11]        5.017003e-01  0.08505275
## p.detect[12]        5.017003e-01  0.08505275
## p.detect[13]        5.017003e-01  0.08505275
## p.detect[14]        5.017003e-01  0.08505275
## p.detect[15]        5.017003e-01  0.08505275
## p.detect[16]        5.017003e-01  0.08505275
## p.detect[17]        5.017003e-01  0.08505275
## p.detect[18]        5.017003e-01  0.08505275
## p.detect[19]        5.017003e-01  0.08505275
## p.detect[20]        5.017003e-01  0.08505275
## p.detect[21]        5.017003e-01  0.08505275
## p.detect[22]        5.017003e-01  0.08505275
## p.detect[23]        5.017003e-01  0.08505275
## p.detect[24]        5.017003e-01  0.08505275
## p.detect[25]        5.017003e-01  0.08505275
## p.detect[26]        5.017003e-01  0.08505275
## p.detect[27]        5.017003e-01  0.08505275
## p.detect[28]        5.017003e-01  0.08505275
## p.detect[29]        5.017003e-01  0.08505275
## p.detect[30]        5.017003e-01  0.08505275
## p.detect[31]        5.017003e-01  0.08505275
## p.detect[32]        5.017003e-01  0.08505275
## p.detect[33]        5.017003e-01  0.08505275
## p.detect[34]        5.017003e-01  0.08505275
## p.detect[35]        5.017003e-01  0.08505275
## p.detect[36]        5.017003e-01  0.08505275
## p.detect[37]        5.017003e-01  0.08505275
## p.detect[38]        5.017003e-01  0.08505275
## p.detect[39]        5.017003e-01  0.08505275
## p.detect[40]        5.017003e-01  0.08505275
## p.detect[41]        5.017003e-01  0.08505275
## p.detect[42]        6.102433e-01  0.08768922
## p.detect[43]        6.102433e-01  0.08768922
## p.detect[44]        6.102433e-01  0.08768922
## p.detect[45]        6.102433e-01  0.08768922
## p.detect[46]        6.102433e-01  0.08768922
## p.detect[47]        6.102433e-01  0.08768922
## p.detect[48]        6.102433e-01  0.08768922
## p.detect[49]        6.102433e-01  0.08768922
## p.detect[50]        6.102433e-01  0.08768922
## p.detect[51]        6.102433e-01  0.08768922
## p.detect[52]        6.102433e-01  0.08768922
## p.detect[53]        6.102433e-01  0.08768922
## p.detect[54]        6.102433e-01  0.08768922
## p.detect[55]        6.102433e-01  0.08768922
## p.detect[56]        6.102433e-01  0.08768922
## p.detect[57]        6.102433e-01  0.08768922
## p.detect[58]        6.102433e-01  0.08768922
## p.detect[59]        6.102433e-01  0.08768922
## p.detect[60]        6.102433e-01  0.08768922
## p.detect[61]        6.102433e-01  0.08768922
## p.detect[62]        6.102433e-01  0.08768922
## p.detect[63]        6.102433e-01  0.08768922
## p.detect[64]        6.102433e-01  0.08768922
## p.detect[65]        6.102433e-01  0.08768922
## p.detect[66]        6.102433e-01  0.08768922
## p.detect[67]        6.102433e-01  0.08768922
## p.detect[68]        6.102433e-01  0.08768922
## p.detect[69]        6.102433e-01  0.08768922
## p.detect[70]        6.102433e-01  0.08768922
## p.detect[71]        6.102433e-01  0.08768922
## p.detect[72]        6.102433e-01  0.08768922
## p.detect[73]        6.102433e-01  0.08768922
## p.detect[74]        6.102433e-01  0.08768922
## p.detect[75]        6.102433e-01  0.08768922
## p.detect[76]        6.102433e-01  0.08768922
## p.detect[77]        6.102433e-01  0.08768922
## p.detect[78]        6.102433e-01  0.08768922
## p.detect[79]        6.102433e-01  0.08768922
## p.detect[80]        6.102433e-01  0.08768922
## p.detect[81]        6.102433e-01  0.08768922
## p.detect[82]        6.102433e-01  0.08768922
## p.detect[83]        5.570885e-01  0.08618243
## p.detect[84]        5.570885e-01  0.08618243
## p.detect[85]        5.570885e-01  0.08618243
## p.detect[86]        5.570885e-01  0.08618243
## p.detect[87]        5.570885e-01  0.08618243
## p.detect[88]        5.570885e-01  0.08618243
## p.detect[89]        5.570885e-01  0.08618243
## p.detect[90]        5.570885e-01  0.08618243
## p.detect[91]        5.570885e-01  0.08618243
## p.detect[92]        5.570885e-01  0.08618243
## p.detect[93]        5.570885e-01  0.08618243
## p.detect[94]        5.570885e-01  0.08618243
## p.detect[95]        5.570885e-01  0.08618243
## p.detect[96]        5.570885e-01  0.08618243
## p.detect[97]        5.570885e-01  0.08618243
## p.detect[98]        5.570885e-01  0.08618243
## p.detect[99]        5.570885e-01  0.08618243
## p.detect[100]       5.570885e-01  0.08618243
## p.detect[101]       5.570885e-01  0.08618243
## p.detect[102]       5.570885e-01  0.08618243
## p.detect[103]       5.570885e-01  0.08618243
## p.detect[104]       5.570885e-01  0.08618243
## p.detect[105]       5.570885e-01  0.08618243
## p.detect[106]       5.570885e-01  0.08618243
## p.detect[107]       5.570885e-01  0.08618243
## p.detect[108]       5.570885e-01  0.08618243
## p.detect[109]       5.570885e-01  0.08618243
## p.detect[110]       5.570885e-01  0.08618243
## p.detect[111]       5.570885e-01  0.08618243
## p.detect[112]       5.570885e-01  0.08618243
## p.detect[113]       5.570885e-01  0.08618243
## p.detect[114]       5.570885e-01  0.08618243
## p.detect[115]       5.570885e-01  0.08618243
## p.detect[116]       5.570885e-01  0.08618243
## p.detect[117]       5.570885e-01  0.08618243
## p.detect[118]       5.570885e-01  0.08618243
## p.detect[119]       5.570885e-01  0.08618243
## p.detect[120]       5.570885e-01  0.08618243
## p.detect[121]       5.570885e-01  0.08618243
## p.detect[122]       5.570885e-01  0.08618243
## p.detect[123]       5.570885e-01  0.08618243
## prob.psi.greater.50 1.000000e+00  0.00000000
## psi                 8.632998e-01  0.06923007
## z[1]                1.000000e+00  0.00000000
## z[2]                1.000000e+00  0.00000000
## z[3]                3.944444e-01  0.48878535
## z[4]                1.000000e+00  0.00000000
## z[5]                1.000000e+00  0.00000000
## z[6]                1.000000e+00  0.00000000
## z[7]                1.000000e+00  0.00000000
## z[8]                1.000000e+00  0.00000000
## z[9]                1.000000e+00  0.00000000
## z[10]               1.000000e+00  0.00000000
## z[11]               1.000000e+00  0.00000000
## z[12]               3.808889e-01  0.48565930
## z[13]               3.957778e-01  0.48907145
## z[14]               1.000000e+00  0.00000000
## z[15]               1.000000e+00  0.00000000
## z[16]               1.000000e+00  0.00000000
## z[17]               1.000000e+00  0.00000000
## z[18]               3.791111e-01  0.48521974
## z[19]               1.000000e+00  0.00000000
## z[20]               3.855556e-01  0.48678037
## z[21]               1.000000e+00  0.00000000
## z[22]               1.000000e+00  0.00000000
## z[23]               1.000000e+00  0.00000000
## z[24]               1.000000e+00  0.00000000
## z[25]               1.000000e+00  0.00000000
## z[26]               1.000000e+00  0.00000000
## z[27]               1.000000e+00  0.00000000
## z[28]               1.000000e+00  0.00000000
## z[29]               1.000000e+00  0.00000000
## z[30]               1.000000e+00  0.00000000
## z[31]               3.902222e-01  0.48785421
## z[32]               1.000000e+00  0.00000000
## z[33]               1.000000e+00  0.00000000
## z[34]               1.000000e+00  0.00000000
## z[35]               3.922222e-01  0.48830005
## z[36]               3.851111e-01  0.48667564
## z[37]               1.000000e+00  0.00000000
## z[38]               1.000000e+00  0.00000000
## z[39]               1.000000e+00  0.00000000
## z[40]               1.000000e+00  0.00000000
## z[41]               1.000000e+00  0.00000000
# get just the means
results$BUGSoutput$mean
## $beta.p
## [1] 0.007341136 0.457004050 0.229560828
## 
## $deviance
## [1] 150.5223
## 
## $occ.sites
## [1] 36.10333
## 
## $p.detect
##   [1] 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003
##   [8] 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003
##  [15] 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003
##  [22] 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003
##  [29] 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003
##  [36] 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.5017003 0.6102433
##  [43] 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433
##  [50] 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433
##  [57] 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433
##  [64] 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433
##  [71] 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433
##  [78] 0.6102433 0.6102433 0.6102433 0.6102433 0.6102433 0.5570885 0.5570885
##  [85] 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885
##  [92] 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885
##  [99] 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885
## [106] 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885
## [113] 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885 0.5570885
## [120] 0.5570885 0.5570885 0.5570885 0.5570885
## 
## $prob.psi.greater.50
## [1] 1
## 
## $psi
## [1] 0.8632998
## 
## $z
##  [1] 1.0000000 1.0000000 0.3944444 1.0000000 1.0000000 1.0000000 1.0000000
##  [8] 1.0000000 1.0000000 1.0000000 1.0000000 0.3808889 0.3957778 1.0000000
## [15] 1.0000000 1.0000000 1.0000000 0.3791111 1.0000000 0.3855556 1.0000000
## [22] 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
## [29] 1.0000000 1.0000000 0.3902222 1.0000000 1.0000000 1.0000000 0.3922222
## [36] 0.3851111 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
results$BUGSoutput$mean$psi
## [1] 0.8632998
# the results$BUGSoutput$sims.array is a 3-d object [iterations, chains, variables]
dim(results$BUGSoutput$sims.array)
## [1] 1500    3  171
results$BUGSoutput$sims.array[1:5,,1:5]
## , , beta.p[1]
## 
##             [,1]        [,2]         [,3]
## [1,] -0.32531797 -0.31201262  0.176316153
## [2,] -0.47322175 -0.07523518 -0.074060904
## [3,] -0.41978523  0.13688101  0.170261487
## [4,]  0.04741348 -0.35102510 -0.006614073
## [5,]  0.28939496 -0.08783537 -0.264990061
## 
## , , beta.p[2]
## 
##             [,1]      [,2]       [,3]
## [1,] 0.940479557 1.1543225  0.9762515
## [2,] 0.746791686 0.5912226  0.7023764
## [3,] 0.460975243 0.1183342 -0.2587394
## [4,] 0.003454781 1.1264057  0.4182297
## [5,] 0.325821815 0.5147680  1.0589265
## 
## , , beta.p[3]
## 
##             [,1]       [,2]         [,3]
## [1,] -0.25771570  0.2467041 -0.024460492
## [2,]  0.36582309 -0.3488288  0.666756524
## [3,]  0.99711639 -0.1956646  0.227579034
## [4,]  0.05758707  0.4395928 -0.007287906
## [5,]  0.45371606  0.2213309  0.157285442
## 
## , , deviance
## 
##          [,1]     [,2]     [,3]
## [1,] 170.7253 169.6448 145.7575
## [2,] 162.5195 172.7529 148.9843
## [3,] 160.5977 171.2673 159.9565
## [4,] 149.0355 159.3192 152.5909
## [5,] 156.8460 152.3112 159.5719
## 
## , , occ.sites
## 
##      [,1] [,2] [,3]
## [1,]   40   40   35
## [2,]   39   41   36
## [3,]   38   41   38
## [4,]   36   38   37
## [5,]   37   37   38
results$BUGSoutput$sims.array[1:5,1,"psi"]
## [1] 0.9704670 0.9899337 0.8527430 0.8249385 0.9303492
# the results$BUGSoutput$sims.matrix is a 2-d object [iterations, variables] with chains stacked
# on top of each other
dim(results$BUGSoutput$sims.matrix)
## [1] 4500  171
results$BUGSoutput$sims.matrix[1:5,1:10]
##        beta.p[1] beta.p[2]   beta.p[3] deviance occ.sites p.detect[1]
## [1,] -0.08951469 0.3092667  0.48034466 138.8065        34   0.4776363
## [2,]  0.21363111 0.3073357 -0.03995562 163.4135        39   0.5532056
## [3,] -0.18455772 0.8006885  0.06779402 158.2085        38   0.4539911
## [4,] -0.03865623 0.2845696  0.18545638 143.3816        35   0.4903371
## [5,] -0.04672988 0.1926500  0.17666017 152.7937        37   0.4883197
##      p.detect[2] p.detect[3] p.detect[4] p.detect[5]
## [1,]   0.4776363   0.4776363   0.4776363   0.4776363
## [2,]   0.5532056   0.5532056   0.5532056   0.5532056
## [3,]   0.4539911   0.4539911   0.4539911   0.4539911
## [4,]   0.4903371   0.4903371   0.4903371   0.4903371
## [5,]   0.4883197   0.4883197   0.4883197   0.4883197
results$BUGSoutput$sims.matrix[1:5,"psi"]
## [1] 0.8305684 0.9282073 0.9657904 0.8206742 0.9455136
# make a posterior density plot
plotdata <- data.frame(parm=results$BUGSoutput$sims.matrix[,"psi"], stringsAsFactors=FALSE)
head(plotdata)
##        parm
## 1 0.8305684
## 2 0.9282073
## 3 0.9657904
## 4 0.8206742
## 5 0.9455136
## 6 0.8817393
postplot.parm <- ggplot2::ggplot( data=plotdata, aes(x=parm, y=..density..))+
  geom_histogram(alpha=0.3)+
  geom_density()+
  ggtitle("Posterior density plot for psi")
postplot.parm
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggsave(plot=postplot.parm, file='psi-posterior-p-t-psi-dot.png', h=4, w=6, units="in", dpi=300)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# make a trace plot (notice we use the sims.array here)
plotdata <- data.frame(psi=results$BUGSoutput$sims.array[,,"psi"], stringsAsFactors=FALSE)
plotdata$iteration <- 1:nrow(plotdata)
head(plotdata)
##       psi.1     psi.2     psi.3 iteration
## 1 0.9704670 0.9560543 0.8091537         1
## 2 0.9899337 0.9877420 0.8512016         2
## 3 0.8527430 0.9673665 0.8511991         3
## 4 0.8249385 0.9134451 0.9499588         4
## 5 0.9303492 0.8952087 0.8568037         5
## 6 0.7588782 0.8653957 0.7389135         6
# convert from wide to long format
plotdata2 <- reshape2:::melt.data.frame(data=plotdata, 
                            id.vars="iteration",
                            measure.vars=paste("psi",1:results$BUGSoutput$n.chains,sep="."),
                            variable.name="chain",
                            value.name='psi')
head(plotdata2)
##   iteration chain       psi
## 1         1 psi.1 0.9704670
## 2         2 psi.1 0.9899337
## 3         3 psi.1 0.8527430
## 4         4 psi.1 0.8249385
## 5         5 psi.1 0.9303492
## 6         6 psi.1 0.7588782
traceplot.parm <- ggplot2::ggplot(data=plotdata2, aes(x=iteration, y=psi, color=chain))+
  ggtitle("Trace plot for psi")+
  geom_line(alpha=.2)
traceplot.parm

ggsave(plot=traceplot.parm, file='psi-trace-p-t-psi-dot.png', h=4, w=6, units="in", dpi=300)


# autocorrelation plot
# First compute the autocorrelation plot
acf.parm <-acf( results$BUGSoutput$sims.matrix[,"psi"], plot=FALSE)
acf.parm
## 
## Autocorrelations of series 'results$BUGSoutput$sims.matrix[, "psi"]', by lag
## 
##      0      1      2      3      4      5      6      7      8      9 
##  1.000  0.014 -0.021 -0.002  0.001  0.015 -0.008 -0.006 -0.046  0.001 
##     10     11     12     13     14     15     16     17     18     19 
## -0.009 -0.008  0.001  0.002  0.029  0.002 -0.002  0.006  0.024  0.022 
##     20     21     22     23     24     25     26     27     28     29 
## -0.017  0.012  0.005 -0.020 -0.001 -0.011 -0.045 -0.001  0.000  0.032 
##     30     31     32     33     34     35     36 
##  0.009  0.015  0.018  0.027 -0.026  0.006 -0.019
acfplot.parm <- ggplot(data=with(acf.parm, data.frame(lag, acf)), aes(x = lag, y = acf)) +
  ggtitle("Autocorrelation plot for psi")+
  geom_hline(aes(yintercept = 0)) +
  geom_segment(aes(xend = lag, yend = 0))
acfplot.parm

ggsave(plot=acfplot.parm, file="psi-acf-p-t-psi-dot.png",h=4, w=6, units="in", dpi=300)