# Nest survival model using JAGS 
# Here we want nest survival to be a linear function of DAY only
# 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),
                             Survive=1-x$Fate,
                             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:", (max(nesttime$Day)+min(nesttime$Day))/2, "='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 Survive Day2 Period NestAge
## 1  /*A*/          1           9           9    0    1       0          1   1       1  361  Early       0
## 2  /*A*/          1           9           9    0    1       0          1   2       1  324  Early       1
## 3  /*A*/          1           9           9    0    1       0          1   3       1  289  Early       2
## 4  /*A*/          1           9           9    0    1       0          1   4       1  256  Early       3
## 5  /*A*/          1           9           9    0    1       0          1   5       1  225  Early       4
## 6  /*A*/          1           9           9    0    1       0          1   6       1  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( Survive ~ Day, data=nesttime)

head(fe.design)
##   (Intercept) Day
## 1           1   1
## 2           1   2
## 3           1   3
## 4           1   4
## 5           1   5
## 6           1   6
# 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: 1330
## 
## 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 Survive 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       1  361  Early       0 0.9894300 0.01389614 0.9515142 0.9863922 0.9944973 0.9978913 0.9997014 1.002285  1500
## 2          1   2  /*A*/          1           9           9    0    1       0       1  324  Early       1 0.9892269 0.01348458 0.9515298 0.9858699 0.9940870 0.9976522 0.9996477 1.002029  1500
## 3          1   3  /*A*/          1           9           9    0    1       0       1  289  Early       2 0.9889999 0.01310471 0.9522519 0.9853476 0.9936807 0.9973970 0.9995780 1.002044  1500
## 4          1   4  /*A*/          1           9           9    0    1       0       1  256  Early       3 0.9887473 0.01275452 0.9531398 0.9848603 0.9932625 0.9971180 0.9995008 1.002049  1400
## 5          1   5  /*A*/          1           9           9    0    1       0       1  225  Early       4 0.9884672 0.01243203 0.9539056 0.9844020 0.9927780 0.9968174 0.9994079 1.002043  1500
## 6          1   6  /*A*/          1           9           9    0    1       0       1  196  Early       5 0.9881577 0.01213533 0.9546079 0.9839654 0.9922716 0.9964791 0.9993044 1.002026  1500
# in this case, we fit a S~Day 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 linear trend")+
  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]  5.35727512 1.43898795  2.9519872  4.3166322  5.25858215  6.26469962 8.28716447 1.002601  1600
## beta[2] -0.06985192 0.05365924 -0.1757258 -0.1049163 -0.06850355 -0.03237192 0.02650618 1.004691  1600
#######################################

# get the full summary table
results$BUGSoutput$summary
##                 mean         sd       2.5%        25%         50%         75%       97.5%     Rhat n.eff
## S[1,1]    0.98943003 0.01389614  0.9515142  0.9863922  0.99449731  0.99789125  0.99970141 1.002285  1500
## S[1,2]    0.98922694 0.01348458  0.9515298  0.9858699  0.99408704  0.99765222  0.99964774 1.002029  1500
## S[1,3]    0.98899991 0.01310471  0.9522519  0.9853476  0.99368066  0.99739701  0.99957797 1.002044  1500
## S[6,3]    0.98899991 0.01310471  0.9522519  0.9853476  0.99368066  0.99739701  0.99957797 1.002044  1500
## S[1,4]    0.98874728 0.01275452  0.9531398  0.9848603  0.99326247  0.99711796  0.99950079 1.002049  1400
## S[6,4]    0.98874728 0.01275452  0.9531398  0.9848603  0.99326247  0.99711796  0.99950079 1.002049  1400
## S[1,5]    0.98846723 0.01243203  0.9539056  0.9844020  0.99277796  0.99681742  0.99940790 1.002043  1500
## S[2,5]    0.98846723 0.01243203  0.9539056  0.9844020  0.99277796  0.99681742  0.99940790 1.002043  1500
## S[3,5]    0.98846723 0.01243203  0.9539056  0.9844020  0.99277796  0.99681742  0.99940790 1.002043  1500
## S[6,5]    0.98846723 0.01243203  0.9539056  0.9844020  0.99277796  0.99681742  0.99940790 1.002043  1500
## S[1,6]    0.98815775 0.01213533  0.9546079  0.9839654  0.99227157  0.99647914  0.99930440 1.002026  1500
## S[2,6]    0.98815775 0.01213533  0.9546079  0.9839654  0.99227157  0.99647914  0.99930440 1.002026  1500
## S[3,6]    0.98815775 0.01213533  0.9546079  0.9839654  0.99227157  0.99647914  0.99930440 1.002026  1500
## S[6,6]    0.98815775 0.01213533  0.9546079  0.9839654  0.99227157  0.99647914  0.99930440 1.002026  1500
## S[1,7]    0.98781656 0.01186266  0.9557825  0.9834316  0.99169615  0.99609594  0.99917475 1.001996  1500
## S[2,7]    0.98781656 0.01186266  0.9557825  0.9834316  0.99169615  0.99609594  0.99917475 1.001996  1500
## S[3,7]    0.98781656 0.01186266  0.9557825  0.9834316  0.99169615  0.99609594  0.99917475 1.001996  1500
## S[5,7]    0.98781656 0.01186266  0.9557825  0.9834316  0.99169615  0.99609594  0.99917475 1.001996  1500
## S[6,7]    0.98781656 0.01186266  0.9557825  0.9834316  0.99169615  0.99609594  0.99917475 1.001996  1500
## S[1,8]    0.98744115 0.01161237  0.9562738  0.9827776  0.99114387  0.99569663  0.99902227 1.001955  1600
## S[2,8]    0.98744115 0.01161237  0.9562738  0.9827776  0.99114387  0.99569663  0.99902227 1.001955  1600
## S[3,8]    0.98744115 0.01161237  0.9562738  0.9827776  0.99114387  0.99569663  0.99902227 1.001955  1600
## S[6,8]    0.98744115 0.01161237  0.9562738  0.9827776  0.99114387  0.99569663  0.99902227 1.001955  1600
## S[7,8]    0.98744115 0.01161237  0.9562738  0.9827776  0.99114387  0.99569663  0.99902227 1.001955  1600
## S[9,8]    0.98744115 0.01161237  0.9562738  0.9827776  0.99114387  0.99569663  0.99902227 1.001955  1600
## S[3,9]    0.98702871 0.01138306  0.9565738  0.9822150  0.99049809  0.99523174  0.99885228 1.001902  1600
## S[4,9]    0.98702871 0.01138306  0.9565738  0.9822150  0.99049809  0.99523174  0.99885228 1.001902  1600
## S[6,9]    0.98702871 0.01138306  0.9565738  0.9822150  0.99049809  0.99523174  0.99885228 1.001902  1600
## S[7,9]    0.98702871 0.01138306  0.9565738  0.9822150  0.99049809  0.99523174  0.99885228 1.001902  1600
## S[9,9]    0.98702871 0.01138306  0.9565738  0.9822150  0.99049809  0.99523174  0.99885228 1.001902  1600
## S[3,10]   0.98657611 0.01117354  0.9570114  0.9815632  0.98980810  0.99472127  0.99866820 1.001837  1700
## S[4,10]   0.98657611 0.01117354  0.9570114  0.9815632  0.98980810  0.99472127  0.99866820 1.001837  1700
## S[6,10]   0.98657611 0.01117354  0.9570114  0.9815632  0.98980810  0.99472127  0.99866820 1.001837  1700
## S[7,10]   0.98657611 0.01117354  0.9570114  0.9815632  0.98980810  0.99472127  0.99866820 1.001837  1700
## S[9,10]   0.98657611 0.01117354  0.9570114  0.9815632  0.98980810  0.99472127  0.99866820 1.001837  1700
## S[3,11]   0.98607984 0.01098296  0.9576623  0.9809393  0.98909166  0.99414498  0.99845904 1.001761  1800
## S[4,11]   0.98607984 0.01098296  0.9576623  0.9809393  0.98909166  0.99414498  0.99845904 1.001761  1800
## S[6,11]   0.98607984 0.01098296  0.9576623  0.9809393  0.98909166  0.99414498  0.99845904 1.001761  1800
## S[7,11]   0.98607984 0.01098296  0.9576623  0.9809393  0.98909166  0.99414498  0.99845904 1.001761  1800
## S[9,11]   0.98607984 0.01098296  0.9576623  0.9809393  0.98909166  0.99414498  0.99845904 1.001761  1800
## S[3,12]   0.98553601 0.01081085  0.9581506  0.9802938  0.98835431  0.99353205  0.99819480 1.001675  2000
## S[4,12]   0.98553601 0.01081085  0.9581506  0.9802938  0.98835431  0.99353205  0.99819480 1.001675  2000
## S[6,12]   0.98553601 0.01081085  0.9581506  0.9802938  0.98835431  0.99353205  0.99819480 1.001675  2000
## S[7,12]   0.98553601 0.01081085  0.9581506  0.9802938  0.98835431  0.99353205  0.99819480 1.001675  2000
## S[9,12]   0.98553601 0.01081085  0.9581506  0.9802938  0.98835431  0.99353205  0.99819480 1.001675  2000
## S[3,13]   0.98494024 0.01065726  0.9582002  0.9796105  0.98751062  0.99287956  0.99786573 1.001583  2200
## S[4,13]   0.98494024 0.01065726  0.9582002  0.9796105  0.98751062  0.99287956  0.99786573 1.001583  2200
## S[6,13]   0.98494024 0.01065726  0.9582002  0.9796105  0.98751062  0.99287956  0.99786573 1.001583  2200
## S[7,13]   0.98494024 0.01065726  0.9582002  0.9796105  0.98751062  0.99287956  0.99786573 1.001583  2200
## S[9,13]   0.98494024 0.01065726  0.9582002  0.9796105  0.98751062  0.99287956  0.99786573 1.001583  2200
## S[10,13]  0.98494024 0.01065726  0.9582002  0.9796105  0.98751062  0.99287956  0.99786573 1.001583  2200
## S[3,14]   0.98428766 0.01052292  0.9581602  0.9787763  0.98667804  0.99214214  0.99750804 1.001486  2400
## S[4,14]   0.98428766 0.01052292  0.9581602  0.9787763  0.98667804  0.99214214  0.99750804 1.001486  2400
## S[6,14]   0.98428766 0.01052292  0.9581602  0.9787763  0.98667804  0.99214214  0.99750804 1.001486  2400
## S[7,14]   0.98428766 0.01052292  0.9581602  0.9787763  0.98667804  0.99214214  0.99750804 1.001486  2400
## S[8,14]   0.98428766 0.01052292  0.9581602  0.9787763  0.98667804  0.99214214  0.99750804 1.001486  2400
## S[11,14]  0.98428766 0.01052292  0.9581602  0.9787763  0.98667804  0.99214214  0.99750804 1.001486  2400
## S[3,15]   0.98357281 0.01040938  0.9580448  0.9781822  0.98574564  0.99131187  0.99708037 1.001389  2800
## S[4,15]   0.98357281 0.01040938  0.9580448  0.9781822  0.98574564  0.99131187  0.99708037 1.001389  2800
## S[7,15]   0.98357281 0.01040938  0.9580448  0.9781822  0.98574564  0.99131187  0.99708037 1.001389  2800
## S[8,15]   0.98357281 0.01040938  0.9580448  0.9781822  0.98574564  0.99131187  0.99708037 1.001389  2800
## S[11,15]  0.98357281 0.01040938  0.9580448  0.9781822  0.98574564  0.99131187  0.99708037 1.001389  2800
## S[12,15]  0.98357281 0.01040938  0.9580448  0.9781822  0.98574564  0.99131187  0.99708037 1.001389  2800
## S[3,16]   0.98278959 0.01031935  0.9576475  0.9772889  0.98481784  0.99048662  0.99663724 1.001296  3200
## S[4,16]   0.98278959 0.01031935  0.9576475  0.9772889  0.98481784  0.99048662  0.99663724 1.001296  3200
## S[7,16]   0.98278959 0.01031935  0.9576475  0.9772889  0.98481784  0.99048662  0.99663724 1.001296  3200
## S[11,16]  0.98278959 0.01031935  0.9576475  0.9772889  0.98481784  0.99048662  0.99663724 1.001296  3200
## S[12,16]  0.98278959 0.01031935  0.9576475  0.9772889  0.98481784  0.99048662  0.99663724 1.001296  3200
## S[13,16]  0.98278959 0.01031935  0.9576475  0.9772889  0.98481784  0.99048662  0.99663724 1.001296  3200
## S[14,16]  0.98278959 0.01031935  0.9576475  0.9772889  0.98481784  0.99048662  0.99663724 1.001296  3200
## S[15,16]  0.98278959 0.01031935  0.9576475  0.9772889  0.98481784  0.99048662  0.99663724 1.001296  3200
## S[3,17]   0.98193116 0.01025697  0.9573000  0.9764662  0.98389002  0.98953459  0.99613042 1.001213  3700
## S[4,17]   0.98193116 0.01025697  0.9573000  0.9764662  0.98389002  0.98953459  0.99613042 1.001213  3700
## S[7,17]   0.98193116 0.01025697  0.9573000  0.9764662  0.98389002  0.98953459  0.99613042 1.001213  3700
## S[11,17]  0.98193116 0.01025697  0.9573000  0.9764662  0.98389002  0.98953459  0.99613042 1.001213  3700
## S[12,17]  0.98193116 0.01025697  0.9573000  0.9764662  0.98389002  0.98953459  0.99613042 1.001213  3700
## S[13,17]  0.98193116 0.01025697  0.9573000  0.9764662  0.98389002  0.98953459  0.99613042 1.001213  3700
## S[14,17]  0.98193116 0.01025697  0.9573000  0.9764662  0.98389002  0.98953459  0.99613042 1.001213  3700
## S[3,18]   0.98098988 0.01022825  0.9568592  0.9753126  0.98275016  0.98853771  0.99551484 1.001148  4100
## S[4,18]   0.98098988 0.01022825  0.9568592  0.9753126  0.98275016  0.98853771  0.99551484 1.001148  4100
## S[7,18]   0.98098988 0.01022825  0.9568592  0.9753126  0.98275016  0.98853771  0.99551484 1.001148  4100
## S[11,18]  0.98098988 0.01022825  0.9568592  0.9753126  0.98275016  0.98853771  0.99551484 1.001148  4100
## S[12,18]  0.98098988 0.01022825  0.9568592  0.9753126  0.98275016  0.98853771  0.99551484 1.001148  4100
## S[13,18]  0.98098988 0.01022825  0.9568592  0.9753126  0.98275016  0.98853771  0.99551484 1.001148  4100
## S[14,18]  0.98098988 0.01022825  0.9568592  0.9753126  0.98275016  0.98853771  0.99551484 1.001148  4100
## S[3,19]   0.97995717 0.01024159  0.9560166  0.9741995  0.98161518  0.98747264  0.99487642 1.001107  4500
## S[4,19]   0.97995717 0.01024159  0.9560166  0.9741995  0.98161518  0.98747264  0.99487642 1.001107  4500
## S[7,19]   0.97995717 0.01024159  0.9560166  0.9741995  0.98161518  0.98747264  0.99487642 1.001107  4500
## S[11,19]  0.97995717 0.01024159  0.9560166  0.9741995  0.98161518  0.98747264  0.99487642 1.001107  4500
## S[12,19]  0.97995717 0.01024159  0.9560166  0.9741995  0.98161518  0.98747264  0.99487642 1.001107  4500
## S[13,19]  0.97995717 0.01024159  0.9560166  0.9741995  0.98161518  0.98747264  0.99487642 1.001107  4500
## S[14,19]  0.97995717 0.01024159  0.9560166  0.9741995  0.98161518  0.98747264  0.99487642 1.001107  4500
## S[3,20]   0.97882345 0.01030832  0.9546889  0.9730033  0.98038474  0.98632609  0.99414583 1.001100  4500
## S[4,20]   0.97882345 0.01030832  0.9546889  0.9730033  0.98038474  0.98632609  0.99414583 1.001100  4500
## S[7,20]   0.97882345 0.01030832  0.9546889  0.9730033  0.98038474  0.98632609  0.99414583 1.001100  4500
## S[11,20]  0.97882345 0.01030832  0.9546889  0.9730033  0.98038474  0.98632609  0.99414583 1.001100  4500
## S[12,20]  0.97882345 0.01030832  0.9546889  0.9730033  0.98038474  0.98632609  0.99414583 1.001100  4500
## S[13,20]  0.97882345 0.01030832  0.9546889  0.9730033  0.98038474  0.98632609  0.99414583 1.001100  4500
## S[14,20]  0.97882345 0.01030832  0.9546889  0.9730033  0.98038474  0.98632609  0.99414583 1.001100  4500
## S[3,21]   0.97757797 0.01044331  0.9531361  0.9717658  0.97915414  0.98511173  0.99329897 1.001131  4300
## S[4,21]   0.97757797 0.01044331  0.9531361  0.9717658  0.97915414  0.98511173  0.99329897 1.001131  4300
## S[7,21]   0.97757797 0.01044331  0.9531361  0.9717658  0.97915414  0.98511173  0.99329897 1.001131  4300
## S[11,21]  0.97757797 0.01044331  0.9531361  0.9717658  0.97915414  0.98511173  0.99329897 1.001131  4300
## S[12,21]  0.97757797 0.01044331  0.9531361  0.9717658  0.97915414  0.98511173  0.99329897 1.001131  4300
## S[13,21]  0.97757797 0.01044331  0.9531361  0.9717658  0.97915414  0.98511173  0.99329897 1.001131  4300
## S[14,21]  0.97757797 0.01044331  0.9531361  0.9717658  0.97915414  0.98511173  0.99329897 1.001131  4300
## S[16,21]  0.97757797 0.01044331  0.9531361  0.9717658  0.97915414  0.98511173  0.99329897 1.001131  4300
## S[3,22]   0.97620869 0.01066552  0.9519324  0.9701639  0.97769399  0.98383063  0.99254076 1.001204  3700
## S[4,22]   0.97620869 0.01066552  0.9519324  0.9701639  0.97769399  0.98383063  0.99254076 1.001204  3700
## S[7,22]   0.97620869 0.01066552  0.9519324  0.9701639  0.97769399  0.98383063  0.99254076 1.001204  3700
## S[11,22]  0.97620869 0.01066552  0.9519324  0.9701639  0.97769399  0.98383063  0.99254076 1.001204  3700
## S[12,22]  0.97620869 0.01066552  0.9519324  0.9701639  0.97769399  0.98383063  0.99254076 1.001204  3700
## S[13,22]  0.97620869 0.01066552  0.9519324  0.9701639  0.97769399  0.98383063  0.99254076 1.001204  3700
## S[14,22]  0.97620869 0.01066552  0.9519324  0.9701639  0.97769399  0.98383063  0.99254076 1.001204  3700
## S[16,22]  0.97620869 0.01066552  0.9519324  0.9701639  0.97769399  0.98383063  0.99254076 1.001204  3700
## S[3,23]   0.97470213 0.01099836  0.9498132  0.9682789  0.97620638  0.98261158  0.99170894 1.001315  3100
## S[4,23]   0.97470213 0.01099836  0.9498132  0.9682789  0.97620638  0.98261158  0.99170894 1.001315  3100
## S[7,23]   0.97470213 0.01099836  0.9498132  0.9682789  0.97620638  0.98261158  0.99170894 1.001315  3100
## S[11,23]  0.97470213 0.01099836  0.9498132  0.9682789  0.97620638  0.98261158  0.99170894 1.001315  3100
## S[12,23]  0.97470213 0.01099836  0.9498132  0.9682789  0.97620638  0.98261158  0.99170894 1.001315  3100
## S[13,23]  0.97470213 0.01099836  0.9498132  0.9682789  0.97620638  0.98261158  0.99170894 1.001315  3100
## S[14,23]  0.97470213 0.01099836  0.9498132  0.9682789  0.97620638  0.98261158  0.99170894 1.001315  3100
## S[16,23]  0.97470213 0.01099836  0.9498132  0.9682789  0.97620638  0.98261158  0.99170894 1.001315  3100
## S[17,23]  0.97470213 0.01099836  0.9498132  0.9682789  0.97620638  0.98261158  0.99170894 1.001315  3100
## S[3,24]   0.97304322 0.01146976  0.9469169  0.9662583  0.97453909  0.98133236  0.99084858 1.001456  2500
## S[4,24]   0.97304322 0.01146976  0.9469169  0.9662583  0.97453909  0.98133236  0.99084858 1.001456  2500
## S[7,24]   0.97304322 0.01146976  0.9469169  0.9662583  0.97453909  0.98133236  0.99084858 1.001456  2500
## S[11,24]  0.97304322 0.01146976  0.9469169  0.9662583  0.97453909  0.98133236  0.99084858 1.001456  2500
## S[12,24]  0.97304322 0.01146976  0.9469169  0.9662583  0.97453909  0.98133236  0.99084858 1.001456  2500
## S[13,24]  0.97304322 0.01146976  0.9469169  0.9662583  0.97453909  0.98133236  0.99084858 1.001456  2500
## S[14,24]  0.97304322 0.01146976  0.9469169  0.9662583  0.97453909  0.98133236  0.99084858 1.001456  2500
## S[16,24]  0.97304322 0.01146976  0.9469169  0.9662583  0.97453909  0.98133236  0.99084858 1.001456  2500
## S[17,24]  0.97304322 0.01146976  0.9469169  0.9662583  0.97453909  0.98133236  0.99084858 1.001456  2500
## S[3,25]   0.97121512 0.01211199  0.9439049  0.9639588  0.97290393  0.98006773  0.99013800 1.001610  2100
## S[4,25]   0.97121512 0.01211199  0.9439049  0.9639588  0.97290393  0.98006773  0.99013800 1.001610  2100
## S[7,25]   0.97121512 0.01211199  0.9439049  0.9639588  0.97290393  0.98006773  0.99013800 1.001610  2100
## S[11,25]  0.97121512 0.01211199  0.9439049  0.9639588  0.97290393  0.98006773  0.99013800 1.001610  2100
## S[12,25]  0.97121512 0.01211199  0.9439049  0.9639588  0.97290393  0.98006773  0.99013800 1.001610  2100
## S[13,25]  0.97121512 0.01211199  0.9439049  0.9639588  0.97290393  0.98006773  0.99013800 1.001610  2100
## S[14,25]  0.97121512 0.01211199  0.9439049  0.9639588  0.97290393  0.98006773  0.99013800 1.001610  2100
## S[16,25]  0.97121512 0.01211199  0.9439049  0.9639588  0.97290393  0.98006773  0.99013800 1.001610  2100
## S[17,25]  0.97121512 0.01211199  0.9439049  0.9639588  0.97290393  0.98006773  0.99013800 1.001610  2100
## S[3,26]   0.96919901 0.01296107  0.9397846  0.9614202  0.97091171  0.97871731  0.98947024 1.001754  1800
## S[4,26]   0.96919901 0.01296107  0.9397846  0.9614202  0.97091171  0.97871731  0.98947024 1.001754  1800
## S[7,26]   0.96919901 0.01296107  0.9397846  0.9614202  0.97091171  0.97871731  0.98947024 1.001754  1800
## S[11,26]  0.96919901 0.01296107  0.9397846  0.9614202  0.97091171  0.97871731  0.98947024 1.001754  1800
## S[12,26]  0.96919901 0.01296107  0.9397846  0.9614202  0.97091171  0.97871731  0.98947024 1.001754  1800
## S[13,26]  0.96919901 0.01296107  0.9397846  0.9614202  0.97091171  0.97871731  0.98947024 1.001754  1800
## S[14,26]  0.96919901 0.01296107  0.9397846  0.9614202  0.97091171  0.97871731  0.98947024 1.001754  1800
## S[16,26]  0.96919901 0.01296107  0.9397846  0.9614202  0.97091171  0.97871731  0.98947024 1.001754  1800
## S[17,26]  0.96919901 0.01296107  0.9397846  0.9614202  0.97091171  0.97871731  0.98947024 1.001754  1800
## S[3,27]   0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[4,27]   0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[7,27]   0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[11,27]  0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[12,27]  0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[13,27]  0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[14,27]  0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[16,27]  0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[17,27]  0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[18,27]  0.96697398 0.01405631  0.9348640  0.9584942  0.96885232  0.97729968  0.98898430 1.001868  1700
## S[3,28]   0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[4,28]   0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[7,28]   0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[11,28]  0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[12,28]  0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[13,28]  0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[14,28]  0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[16,28]  0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[17,28]  0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[18,28]  0.96451677 0.01543991  0.9297491  0.9554294  0.96660006  0.97584782  0.98850037 1.001934  1600
## S[3,29]   0.96180173 0.01715724  0.9229792  0.9519346  0.96424227  0.97454927  0.98825863 1.001946  1600
## S[4,29]   0.96180173 0.01715724  0.9229792  0.9519346  0.96424227  0.97454927  0.98825863 1.001946  1600
## S[7,29]   0.96180173 0.01715724  0.9229792  0.9519346  0.96424227  0.97454927  0.98825863 1.001946  1600
## S[11,29]  0.96180173 0.01715724  0.9229792  0.9519346  0.96424227  0.97454927  0.98825863 1.001946  1600
## S[12,29]  0.96180173 0.01715724  0.9229792  0.9519346  0.96424227  0.97454927  0.98825863 1.001946  1600
## S[13,29]  0.96180173 0.01715724  0.9229792  0.9519346  0.96424227  0.97454927  0.98825863 1.001946  1600
## S[14,29]  0.96180173 0.01715724  0.9229792  0.9519346  0.96424227  0.97454927  0.98825863 1.001946  1600
## S[16,29]  0.96180173 0.01715724  0.9229792  0.9519346  0.96424227  0.97454927  0.98825863 1.001946  1600
## S[17,29]  0.96180173 0.01715724  0.9229792  0.9519346  0.96424227  0.97454927  0.98825863 1.001946  1600
## S[3,30]   0.95880066 0.01925751  0.9142769  0.9476188  0.96163088  0.97320479  0.98800066 1.001904  1600
## S[4,30]   0.95880066 0.01925751  0.9142769  0.9476188  0.96163088  0.97320479  0.98800066 1.001904  1600
## S[7,30]   0.95880066 0.01925751  0.9142769  0.9476188  0.96163088  0.97320479  0.98800066 1.001904  1600
## S[11,30]  0.95880066 0.01925751  0.9142769  0.9476188  0.96163088  0.97320479  0.98800066 1.001904  1600
## S[12,30]  0.95880066 0.01925751  0.9142769  0.9476188  0.96163088  0.97320479  0.98800066 1.001904  1600
## S[13,30]  0.95880066 0.01925751  0.9142769  0.9476188  0.96163088  0.97320479  0.98800066 1.001904  1600
## S[14,30]  0.95880066 0.01925751  0.9142769  0.9476188  0.96163088  0.97320479  0.98800066 1.001904  1600
## S[16,30]  0.95880066 0.01925751  0.9142769  0.9476188  0.96163088  0.97320479  0.98800066 1.001904  1600
## S[17,30]  0.95880066 0.01925751  0.9142769  0.9476188  0.96163088  0.97320479  0.98800066 1.001904  1600
## S[3,31]   0.95548287 0.02179495  0.9036054  0.9430302  0.95886049  0.97164394  0.98789430 1.001819  1700
## S[4,31]   0.95548287 0.02179495  0.9036054  0.9430302  0.95886049  0.97164394  0.98789430 1.001819  1700
## S[7,31]   0.95548287 0.02179495  0.9036054  0.9430302  0.95886049  0.97164394  0.98789430 1.001819  1700
## S[11,31]  0.95548287 0.02179495  0.9036054  0.9430302  0.95886049  0.97164394  0.98789430 1.001819  1700
## S[12,31]  0.95548287 0.02179495  0.9036054  0.9430302  0.95886049  0.97164394  0.98789430 1.001819  1700
## S[13,31]  0.95548287 0.02179495  0.9036054  0.9430302  0.95886049  0.97164394  0.98789430 1.001819  1700
## S[14,31]  0.95548287 0.02179495  0.9036054  0.9430302  0.95886049  0.97164394  0.98789430 1.001819  1700
## S[16,31]  0.95548287 0.02179495  0.9036054  0.9430302  0.95886049  0.97164394  0.98789430 1.001819  1700
## S[17,31]  0.95548287 0.02179495  0.9036054  0.9430302  0.95886049  0.97164394  0.98789430 1.001819  1700
## S[3,32]   0.95181537 0.02482975  0.8916186  0.9379270  0.95574133  0.97037158  0.98776741 1.001705  1900
## S[11,32]  0.95181537 0.02482975  0.8916186  0.9379270  0.95574133  0.97037158  0.98776741 1.001705  1900
## S[12,32]  0.95181537 0.02482975  0.8916186  0.9379270  0.95574133  0.97037158  0.98776741 1.001705  1900
## S[13,32]  0.95181537 0.02482975  0.8916186  0.9379270  0.95574133  0.97037158  0.98776741 1.001705  1900
## S[16,32]  0.95181537 0.02482975  0.8916186  0.9379270  0.95574133  0.97037158  0.98776741 1.001705  1900
## S[17,32]  0.95181537 0.02482975  0.8916186  0.9379270  0.95574133  0.97037158  0.98776741 1.001705  1900
## S[3,33]   0.94776329 0.02842829  0.8780265  0.9326219  0.95268437  0.96914983  0.98768910 1.001578  2200
## S[12,33]  0.94776329 0.02842829  0.8780265  0.9326219  0.95268437  0.96914983  0.98768910 1.001578  2200
## S[13,33]  0.94776329 0.02842829  0.8780265  0.9326219  0.95268437  0.96914983  0.98768910 1.001578  2200
## S[17,33]  0.94776329 0.02842829  0.8780265  0.9326219  0.95268437  0.96914983  0.98768910 1.001578  2200
## S[3,34]   0.94329069 0.03266155  0.8622690  0.9264359  0.94923081  0.96789924  0.98774182 1.001455  2500
## S[12,34]  0.94329069 0.03266155  0.8622690  0.9264359  0.94923081  0.96789924  0.98774182 1.001455  2500
## S[13,34]  0.94329069 0.03266155  0.8622690  0.9264359  0.94923081  0.96789924  0.98774182 1.001455  2500
## S[3,35]   0.93836174 0.03760075  0.8461267  0.9194791  0.94569238  0.96661028  0.98780575 1.001350  2900
## S[12,35]  0.93836174 0.03760075  0.8461267  0.9194791  0.94569238  0.96661028  0.98780575 1.001350  2900
## S[13,35]  0.93836174 0.03760075  0.8461267  0.9194791  0.94569238  0.96661028  0.98780575 1.001350  2900
## S[3,36]   0.93294246 0.04330932  0.8259062  0.9118177  0.94221191  0.96499738  0.98784557 1.001818  3300
## S[12,36]  0.93294246 0.04330932  0.8259062  0.9118177  0.94221191  0.96499738  0.98784557 1.001818  3300
## S[13,36]  0.93294246 0.04330932  0.8259062  0.9118177  0.94221191  0.96499738  0.98784557 1.001818  3300
## S[3,37]   0.92700271 0.04983190  0.8031810  0.9035087  0.93831368  0.96353269  0.98792541 1.003055  3500
## S[13,37]  0.92700271 0.04983190  0.8031810  0.9035087  0.93831368  0.96353269  0.98792541 1.003055  3500
## S[3,38]   0.92051835 0.05718280  0.7737472  0.8947122  0.93402574  0.96221119  0.98808159 1.004967  3500
## S[13,38]  0.92051835 0.05718280  0.7737472  0.8947122  0.93402574  0.96221119  0.98808159 1.004967  3500
## S[3,39]   0.91347296 0.06533849  0.7459431  0.8850515  0.92985588  0.96068583  0.98827237 1.007569  3300
## S[13,39]  0.91347296 0.06533849  0.7459431  0.8850515  0.92985588  0.96068583  0.98827237 1.007569  3300
## beta[1]   5.35727512 1.43898795  2.9519872  4.3166322  5.25858215  6.26469962  8.28716447 1.002601  1600
## beta[2]  -0.06985192 0.05365924 -0.1757258 -0.1049163 -0.06850355 -0.03237192  0.02650618 1.004691  1600
## deviance 43.01945224 2.05971475 41.0286588 41.6027701 42.40457333 43.75860909 48.25115931 1.007572   840
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]  5.35727512 1.43898795  2.9519872 8.28716447 1.002601  1600
## beta[2] -0.06985192 0.05365924 -0.1757258 0.02650618 1.004691  1600