# Nest survival model using JAGS 
# Here we want nest survival to be a function of early vs late periods
# Notice the only change is in the definition of the fe.design matrix

#
# 2019-06-28 CHJS First Edition
#

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)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
library(reshape2)

options(width=200)

source(file.path("..","..","jags-nest-survival-fixed-effects.r"))


# The input dataframe must contain the following fields with the following names
#
#    NestID: id code of the nest (alpha numeric)
#    FirstFound: day the nest was first found
#    LastPresent: last day that a chick was present in the nest
#    LastChecked: last day the nest was checked
#    Fate: fate of the nest; 0 = success; 1=fail
#    AgheDay1 = age of the nest on day 1 (if you are fitting age of nest models) 
#
# You could also have a nest level covariates, survey level covariates, and
# next x survey time covariates as well

nestdata <- readxl::read_excel(file.path("..","Killdeer.xlsx"), 
                               sheet="killdeer-age")
nestdata <- plyr::rename(nestdata, c("id"="NestId"))
head(nestdata)
## # A tibble: 6 x 7
##   NestId FirstFound LastPresent LastChecked  Fate  Freq AgeDay1
##   <chr>       <dbl>       <dbl>       <dbl> <dbl> <dbl>   <dbl>
## 1 /*A*/           1           9           9     0     1       0
## 2 /*B*/           5           5           9     1     1      -2
## 3 /*C*/           5          40          40     0     1      -3
## 4 /*D*/           9          32          32     0     1      -4
## 5 /*E*/           7           8           8     0     1      -4
## 6 /*F*/           3          15          15     0     1       1
# Unfortunately, JAGS cannot deal with alpha numeric code and 
# so we need to convert the alphanumberic NestID to numeric codes
# by declaring NestId as a factor and extracting the level values
nestdata$NestId.num <- as.numeric(factor(nestdata$NestId))

# We must create a file with every combination of next x day nest was "active"
# being every day from FirstCound to LastChecked-1

nesttime <- plyr::adply(nestdata, 1, function(x){
     nesttime <- expand.grid(NestId.num=x$NestId.num, 
                             Day=x$FirstFound:(x$LastChecked-1),
                             stringsAsFactors=FALSE)
     nesttime
})


# Extract the nest level covariates (including AgeNest1)
# The next level covariates should be indexed using NestId
# If AgeNest1 variable is present then the age of the nest is computed
#
nest.covariates <- NULL

if( !is.null(nest.covariates)){
   nesttime <- merge(nesttime, nest.covariates, by="NestId")
}


# Extract any survey time covariates such as time, time^2, early/late
# weather covariates ect.
# All of these covariates will affect all nests simultaneouls
nesttime $Day2 <- (nesttime$Day-20)^2  # day^2 for quadratic trends
nesttime $Period <- car::recode(nesttime$Day,
                    paste("lo:20='Early';",
                          "else='Late'"))
xtabs(~Period+Day, data=nesttime, exclude=NULL, na.action=na.pass)
##        Day
## Period   1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
##   Early  1  1  2  2  4  4  5  6  5  5  5  5  6  6  6  8  7  7  7  7  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
##   Late   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  8  8  9  9  9  9 10 10  9  9  9  6  4  3  3  3  2  2  2
# if there is a AgeDay1 variable, we compute the nest age for each time for each nest
if( !is.null(nesttime$AgeDay1)){
   nesttime$NestAge <- nesttime$AgeDay1 + nesttime$Day -1
}
head(nesttime)
##   NestId FirstFound LastPresent LastChecked Fate Freq AgeDay1 NestId.num Day Day2 Period NestAge
## 1  /*A*/          1           9           9    0    1       0          1   1  361  Early       0
## 2  /*A*/          1           9           9    0    1       0          1   2  324  Early       1
## 3  /*A*/          1           9           9    0    1       0          1   3  289  Early       2
## 4  /*A*/          1           9           9    0    1       0          1   4  256  Early       3
## 5  /*A*/          1           9           9    0    1       0          1   5  225  Early       4
## 6  /*A*/          1           9           9    0    1       0          1   6  196  Early       5
# Add any next x day survey covariates to the nesttime data
#

# there is nothing here for this example


# Set up the design matrix for the fixed effects
fe.design <- model.matrix( ~ Period, data=nesttime)

head(fe.design)
##   (Intercept) PeriodLate
## 1           1          0
## 2           1          0
## 3           1          0
## 4           1          0
## 5           1          0
## 6           1          0
# Finally, the actual call to JAGS
fitted.model <- jags.nest.survival.fixed.effects(
         nestdata=nestdata,    # nest data
         nesttime=nesttime,    # daily nest values with nest, time, nest x time covariates
         fe.design=fe.design,  # fixed effects design matrix
         init.seed=12321312)   # initial seed)  
## module glm loaded
## Compiling data graph
##    Declaring variables
##    Resolving undeclared variables
##    Allocating nodes
##    Initializing
##    Reading data back into data table
## Compiling model graph
##    Declaring variables
##    Resolving undeclared variables
##    Allocating nodes
## Graph information:
##    Observed stochastic nodes: 22
##    Unobserved stochastic nodes: 2
##    Total graph size: 1254
## 
## Initializing model
# the nesttime dataframe has the estimated DSR for every combination of NestId.num and Day

# the results list has lots of other stuff
results <- fitted.model$results

# the nesttime dataframe has the estimated DSR for every combination of NestId.num and Day
head(fitted.model$nesttime)
##   NestId.num Day NestId FirstFound LastPresent LastChecked Fate Freq AgeDay1 Day2 Period NestAge      mean         sd     X2.5.      X25.      X50.      X75.    X97.5.     Rhat n.eff
## 1          1   1  /*A*/          1           9           9    0    1       0  361  Early       0 0.9794804 0.01410726 0.9448086 0.9723939 0.9826046 0.9896273 0.9979615 1.002161  1300
## 2          1   2  /*A*/          1           9           9    0    1       0  324  Early       1 0.9794804 0.01410726 0.9448086 0.9723939 0.9826046 0.9896273 0.9979615 1.002161  1300
## 3          1   3  /*A*/          1           9           9    0    1       0  289  Early       2 0.9794804 0.01410726 0.9448086 0.9723939 0.9826046 0.9896273 0.9979615 1.002161  1300
## 4          1   4  /*A*/          1           9           9    0    1       0  256  Early       3 0.9794804 0.01410726 0.9448086 0.9723939 0.9826046 0.9896273 0.9979615 1.002161  1300
## 5          1   5  /*A*/          1           9           9    0    1       0  225  Early       4 0.9794804 0.01410726 0.9448086 0.9723939 0.9826046 0.9896273 0.9979615 1.002161  1300
## 6          1   6  /*A*/          1           9           9    0    1       0  196  Early       5 0.9794804 0.01410726 0.9448086 0.9723939 0.9826046 0.9896273 0.9979615 1.002161  1300
# in this case, we fit a S~Period model, which is the same for all nests, so just extract
# days and plot
plotdata <- plyr::ddply(fitted.model$nesttime, "Day", function(x){ x[1,]})
ggplot(data=plotdata, aes(x=Day, y=mean))+
  ggtitle("Estimated DSR in Early vs Late model")+
  geom_line(group=1)+
  geom_ribbon(aes(ymin=X2.5., ymax=X97.5.), alpha=0.2)+
  ylim(0,1)

# the results list has lots of other stuff
names(results)
## [1] "model"              "BUGSoutput"         "parameters.to.save" "model.file"         "n.iter"             "DIC"
names(results$BUGSoutput)
##  [1] "n.chains"        "n.iter"          "n.burnin"        "n.thin"          "n.keep"          "n.sims"          "sims.array"      "sims.list"       "sims.matrix"     "summary"        
## [11] "mean"            "sd"              "median"          "root.short"      "long.short"      "dimension.short" "indexes.short"   "last.values"     "program"         "model.file"     
## [21] "isDIC"           "DICbyR"          "pD"              "DIC"
# we can also look at the beta estimates
# in this case this is the logit DSR which is the same for all nest x days
results$BUGSoutput$summary[ grepl("beta", row.names(results$BUGSoutput$summary)),,drop=FALSE]
##               mean        sd      2.5%       25%        50%        75%    97.5%     Rhat n.eff
## beta[1]  4.1369318 0.8434642  2.840176  3.561724  4.0340018 4.55815097 6.193585 1.013118   340
## beta[2] -0.6435272 1.0046469 -2.922889 -1.225793 -0.5636499 0.02239442 1.101678 1.018464   280
#######################################

# get the full summary table
results$BUGSoutput$summary
##                mean         sd       2.5%        25%        50%         75%      97.5%     Rhat n.eff
## S[1,1]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[1,2]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[1,3]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,3]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[1,4]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,4]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[1,5]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[2,5]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,5]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,5]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[1,6]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[2,6]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,6]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,6]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[1,7]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[2,7]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,7]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[5,7]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,7]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[1,8]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[2,8]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,8]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,8]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,8]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[9,8]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,9]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,9]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,9]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,9]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[9,9]    0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,10]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,10]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,10]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,10]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[9,10]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,11]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,11]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,11]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,11]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[9,11]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,12]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,12]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,12]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,12]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[9,12]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,13]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,13]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,13]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,13]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[9,13]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[10,13]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,14]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,14]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[6,14]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,14]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[8,14]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[11,14]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,15]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,15]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,15]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[8,15]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[11,15]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[12,15]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,16]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,16]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,16]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[11,16]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[12,16]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[13,16]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[14,16]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[15,16]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,17]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,17]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,17]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[11,17]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[12,17]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[13,17]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[14,17]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,18]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,18]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,18]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[11,18]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[12,18]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[13,18]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[14,18]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,19]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,19]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,19]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[11,19]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[12,19]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[13,19]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[14,19]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,20]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[4,20]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[7,20]   0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[11,20]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[12,20]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[13,20]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[14,20]  0.9794804 0.01410726  0.9448086  0.9723939  0.9826046  0.98962730  0.9979615 1.002161  1300
## S[3,21]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,21]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,21]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,21]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,21]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,21]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,21]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,21]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,22]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,22]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,22]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,22]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,22]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,22]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,22]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,22]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,23]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,23]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,23]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,23]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,23]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,23]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,23]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,23]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,23]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,24]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,24]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,24]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,24]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,24]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,24]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,24]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,24]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,24]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,25]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,25]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,25]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,25]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,25]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,25]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,25]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,25]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,25]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,26]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,26]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,26]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,26]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,26]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,26]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,26]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,26]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,26]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,27]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,27]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,27]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,27]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,27]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,27]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,27]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,27]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,27]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[18,27]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,28]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,28]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,28]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,28]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,28]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,28]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,28]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,28]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,28]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[18,28]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,29]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,29]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,29]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,29]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,29]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,29]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,29]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,29]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,29]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,30]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,30]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,30]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,30]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,30]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,30]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,30]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,30]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,30]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,31]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[4,31]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[7,31]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,31]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,31]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,31]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[14,31]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,31]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,31]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,32]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[11,32]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,32]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,32]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[16,32]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,32]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,33]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,33]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,33]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[17,33]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,34]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,34]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,34]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,35]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,35]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,35]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,36]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[12,36]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,36]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,37]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,37]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,38]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,38]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[3,39]   0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## S[13,39]  0.9666730 0.01636281  0.9279712  0.9575134  0.9694467  0.97841333  0.9911513 1.000871  4500
## beta[1]   4.1369318 0.84346424  2.8401763  3.5617243  4.0340018  4.55815097  6.1935846 1.013118   340
## beta[2]  -0.6435272 1.00464695 -2.9228886 -1.2257925 -0.5636499  0.02239442  1.1016778 1.018464   280
## deviance 44.3188919 2.27140073 42.2280511 42.7582402 43.6127872 45.06506746 50.6602985 1.018773   300
results$BUGSoutput$summary[grepl("beta",rownames(results$BUGSoutput$summary)),
                           c("mean", "sd", "2.5%","97.5%","Rhat", "n.eff")]
##               mean        sd      2.5%    97.5%     Rhat n.eff
## beta[1]  4.1369318 0.8434642  2.840176 6.193585 1.013118   340
## beta[2] -0.6435272 1.0046469 -2.922889 1.101678 1.018464   280