# Nest survival model using JAGS 
# Here we want nest survival to be a function of nest age
# The variable NestAge is computed based on the Day and AgeDay1 variables
# 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: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 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 ~ NestAge, data=nesttime)

head(fe.design)
##   (Intercept) NestAge
## 1           1       0
## 2           1       1
## 3           1       2
## 4           1       3
## 5           1       4
## 6           1       5
# 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: 1324
## 
## Initializing model
# 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.9738396 0.02021652 0.9190708 0.9655924 0.9790807 0.9879974 0.9968634 1.014300   280
## 2          1   2  /*A*/          1           9           9    0    1       0       1  324  Early       1 0.9742299 0.01865337 0.9244748 0.9661334 0.9787153 0.9875530 0.9964416 1.013300   290
## 3          1   3  /*A*/          1           9           9    0    1       0       1  289  Early       2 0.9745562 0.01724425 0.9294622 0.9665765 0.9784623 0.9869711 0.9960018 1.012171   310
## 4          1   4  /*A*/          1           9           9    0    1       0       1  256  Early       3 0.9748199 0.01597990 0.9343592 0.9669466 0.9781707 0.9864068 0.9955984 1.010921   330
## 5          1   5  /*A*/          1           9           9    0    1       0       1  225  Early       4 0.9750225 0.01485262 0.9384428 0.9674183 0.9778947 0.9859641 0.9950693 1.009564   350
## 6          1   6  /*A*/          1           9           9    0    1       0       1  196  Early       5 0.9751651 0.01385660 0.9415965 0.9676466 0.9776239 0.9854211 0.9946486 1.008123   400
# in this case, we fit a S~NestAge model, which is the same for all nests, so just extract
# DSR at each ageand plot
plotdata <- plyr::ddply(fitted.model$nesttime, "NestAge", function(x){ x[1,]})
ggplot(data=plotdata, aes(x=NestAge, y=mean))+
  ggtitle("Estimated DSR as a function of NestAge")+
  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]  3.90994722 0.83407749  2.429789  3.33446363  3.84593961 4.41056075 5.7614877 1.006835   330
## beta[2] -0.01470816 0.05383181 -0.118304 -0.05018079 -0.01540165 0.01973208 0.0964004 1.007686   290
#######################################

# get the full summary table
results$BUGSoutput$summary
##                 mean         sd       2.5%         25%         50%         75%      97.5%     Rhat n.eff
## S[1,1]    0.97383957 0.02021652  0.9190708  0.96559237  0.97908065  0.98799745  0.9968634 1.014300   280
## S[1,2]    0.97422991 0.01865337  0.9244748  0.96613339  0.97871526  0.98755295  0.9964416 1.013300   290
## S[1,3]    0.97455615 0.01724425  0.9294622  0.96657650  0.97846228  0.98697115  0.9960018 1.012171   310
## S[6,3]    0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[1,4]    0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[6,4]    0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[1,5]    0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[2,5]    0.97455615 0.01724425  0.9294622  0.96657650  0.97846228  0.98697115  0.9960018 1.012171   310
## S[3,5]    0.97422991 0.01865337  0.9244748  0.96613339  0.97871526  0.98755295  0.9964416 1.013300   290
## S[6,5]    0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[1,6]    0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[2,6]    0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[3,6]    0.97455615 0.01724425  0.9294622  0.96657650  0.97846228  0.98697115  0.9960018 1.012171   310
## S[6,6]    0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[1,7]    0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[2,7]    0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[3,7]    0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[5,7]    0.97455615 0.01724425  0.9294622  0.96657650  0.97846228  0.98697115  0.9960018 1.012171   310
## S[6,7]    0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[1,8]    0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[2,8]    0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[3,8]    0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[6,8]    0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[7,8]    0.97383957 0.02021652  0.9190708  0.96559237  0.97908065  0.98799745  0.9968634 1.014300   280
## S[9,8]    0.97383957 0.02021652  0.9190708  0.96559237  0.97908065  0.98799745  0.9968634 1.014300   280
## S[3,9]    0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[4,9]    0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[6,9]    0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[7,9]    0.97422991 0.01865337  0.9244748  0.96613339  0.97871526  0.98755295  0.9964416 1.013300   290
## S[9,9]    0.97422991 0.01865337  0.9244748  0.96613339  0.97871526  0.98755295  0.9964416 1.013300   290
## S[3,10]   0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[4,10]   0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[6,10]   0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[7,10]   0.97455615 0.01724425  0.9294622  0.96657650  0.97846228  0.98697115  0.9960018 1.012171   310
## S[9,10]   0.97455615 0.01724425  0.9294622  0.96657650  0.97846228  0.98697115  0.9960018 1.012171   310
## S[3,11]   0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[4,11]   0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[6,11]   0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[7,11]   0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[9,11]   0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[3,12]   0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[4,12]   0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[6,12]   0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[7,12]   0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[9,12]   0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[3,13]   0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[4,13]   0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[6,13]   0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[7,13]   0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[9,13]   0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[10,13]  0.97383957 0.02021652  0.9190708  0.96559237  0.97908065  0.98799745  0.9968634 1.014300   280
## S[3,14]   0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[4,14]   0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[6,14]   0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[7,14]   0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[8,14]   0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[11,14]  0.97383957 0.02021652  0.9190708  0.96559237  0.97908065  0.98799745  0.9968634 1.014300   280
## S[3,15]   0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[4,15]   0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[7,15]   0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[8,15]   0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[11,15]  0.97422991 0.01865337  0.9244748  0.96613339  0.97871526  0.98755295  0.9964416 1.013300   290
## S[12,15]  0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[3,16]   0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[4,16]   0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[7,16]   0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[11,16]  0.97455615 0.01724425  0.9294622  0.96657650  0.97846228  0.98697115  0.9960018 1.012171   310
## S[12,16]  0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[13,16]  0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[14,16]  0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[15,16]  0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[3,17]   0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[4,17]   0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[7,17]   0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[11,17]  0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[12,17]  0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[13,17]  0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[14,17]  0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[3,18]   0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[4,18]   0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[7,18]   0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[11,18]  0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[12,18]  0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[13,18]  0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[14,18]  0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[3,19]   0.97331934 0.01122317  0.9470073  0.96680464  0.97480413  0.98143856  0.9910039 1.000907  4500
## S[4,19]   0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[7,19]   0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[11,19]  0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[12,19]  0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[13,19]  0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[14,19]  0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[3,20]   0.97278672 0.01177481  0.9452478  0.96597098  0.97430086  0.98124425  0.9910692 1.001250  3400
## S[4,20]   0.97331934 0.01122317  0.9470073  0.96680464  0.97480413  0.98143856  0.9910039 1.000907  4500
## S[7,20]   0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[11,20]  0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[12,20]  0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[13,20]  0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[14,20]  0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[3,21]   0.97218231 0.01248625  0.9428015  0.96526475  0.97379201  0.98122974  0.9914843 1.001683  2000
## S[4,21]   0.97278672 0.01177481  0.9452478  0.96597098  0.97430086  0.98124425  0.9910692 1.001250  3400
## S[7,21]   0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[11,21]  0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[12,21]  0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[13,21]  0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[14,21]  0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[16,21]  0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[3,22]   0.97150272 0.01335928  0.9399174  0.96414641  0.97323188  0.98110483  0.9918187 1.002151  1300
## S[4,22]   0.97218231 0.01248625  0.9428015  0.96526475  0.97379201  0.98122974  0.9914843 1.001683  2000
## S[7,22]   0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[11,22]  0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[12,22]  0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[13,22]  0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[14,22]  0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[16,22]  0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[3,23]   0.97074417 0.01439680  0.9363598  0.96272700  0.97269143  0.98122663  0.9921283 1.002612  1000
## S[4,23]   0.97150272 0.01335928  0.9399174  0.96414641  0.97323188  0.98110483  0.9918187 1.002151  1300
## S[7,23]   0.97331934 0.01122317  0.9470073  0.96680464  0.97480413  0.98143856  0.9910039 1.000907  4500
## S[11,23]  0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[12,23]  0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[13,23]  0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[14,23]  0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[16,23]  0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[17,23]  0.97516506 0.01385660  0.9415965  0.96764659  0.97762387  0.98542114  0.9946486 1.008123   400
## S[3,24]   0.96990246 0.01560335  0.9319683  0.96133264  0.97209452  0.98130253  0.9926337 1.003035   850
## S[4,24]   0.97074417 0.01439680  0.9363598  0.96272700  0.97269143  0.98122663  0.9921283 1.002612  1000
## S[7,24]   0.97278672 0.01177481  0.9452478  0.96597098  0.97430086  0.98124425  0.9910692 1.001250  3400
## S[11,24]  0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[12,24]  0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[13,24]  0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[14,24]  0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[16,24]  0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[17,24]  0.97524843 0.01298812  0.9441429  0.96804418  0.97740985  0.98484435  0.9940749 1.006635   460
## S[3,25]   0.96897302 0.01698541  0.9278622  0.95982672  0.97173032  0.98141618  0.9930116 1.003403   730
## S[4,25]   0.96990246 0.01560335  0.9319683  0.96133264  0.97209452  0.98130253  0.9926337 1.003035   850
## S[7,25]   0.97218231 0.01248625  0.9428015  0.96526475  0.97379201  0.98122974  0.9914843 1.001683  2000
## S[11,25]  0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[12,25]  0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[13,25]  0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[14,25]  0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[16,25]  0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[17,25]  0.97527317 0.01224567  0.9466631  0.96798520  0.97724271  0.98426304  0.9936687 1.005150   550
## S[3,26]   0.96795086 0.01855151  0.9219906  0.95839118  0.97126363  0.98157786  0.9935259 1.003708   660
## S[4,26]   0.96897302 0.01698541  0.9278622  0.95982672  0.97173032  0.98141618  0.9930116 1.003403   730
## S[7,26]   0.97150272 0.01335928  0.9399174  0.96414641  0.97323188  0.98110483  0.9918187 1.002151  1300
## S[11,26]  0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[12,26]  0.97331934 0.01122317  0.9470073  0.96680464  0.97480413  0.98143856  0.9910039 1.000907  4500
## S[13,26]  0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[14,26]  0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[16,26]  0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[17,26]  0.97523951 0.01163000  0.9484288  0.96837683  0.97694746  0.98374818  0.9931312 1.003730   710
## S[3,27]   0.96683066 0.02031212  0.9163687  0.95669966  0.97070910  0.98163232  0.9939670 1.003949   610
## S[4,27]   0.96795086 0.01855151  0.9219906  0.95839118  0.97126363  0.98157786  0.9935259 1.003708   660
## S[7,27]   0.97074417 0.01439680  0.9363598  0.96272700  0.97269143  0.98122663  0.9921283 1.002612  1000
## S[11,27]  0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[12,27]  0.97278672 0.01177481  0.9452478  0.96597098  0.97430086  0.98124425  0.9910692 1.001250  3400
## S[13,27]  0.97331934 0.01122317  0.9470073  0.96680464  0.97480413  0.98143856  0.9910039 1.000907  4500
## S[14,27]  0.97331934 0.01122317  0.9470073  0.96680464  0.97480413  0.98143856  0.9910039 1.000907  4500
## S[16,27]  0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[17,27]  0.97514737 0.01114387  0.9497666  0.96850192  0.97667451  0.98328192  0.9924951 1.002706   980
## S[18,27]  0.97481990 0.01597990  0.9343592  0.96694662  0.97817069  0.98640684  0.9955984 1.010921   330
## S[3,28]   0.96560676 0.02227952  0.9099740  0.95487698  0.97029904  0.98177800  0.9943081 1.004131   580
## S[4,28]   0.96683066 0.02031212  0.9163687  0.95669966  0.97070910  0.98163232  0.9939670 1.003949   610
## S[7,28]   0.96990246 0.01560335  0.9319683  0.96133264  0.97209452  0.98130253  0.9926337 1.003035   850
## S[11,28]  0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[12,28]  0.97218231 0.01248625  0.9428015  0.96526475  0.97379201  0.98122974  0.9914843 1.001683  2000
## S[13,28]  0.97278672 0.01177481  0.9452478  0.96597098  0.97430086  0.98124425  0.9910692 1.001250  3400
## S[14,28]  0.97278672 0.01177481  0.9452478  0.96597098  0.97430086  0.98124425  0.9910692 1.001250  3400
## S[16,28]  0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[17,28]  0.97499635 0.01079170  0.9508896  0.96847306  0.97636036  0.98286708  0.9920307 1.001971  1500
## S[18,28]  0.97502249 0.01485262  0.9384428  0.96741830  0.97789466  0.98596412  0.9950693 1.009564   350
## S[3,29]   0.96427320 0.02446747  0.9033579  0.95294224  0.96985111  0.98205814  0.9946054 1.004259   560
## S[4,29]   0.96560676 0.02227952  0.9099740  0.95487698  0.97029904  0.98177800  0.9943081 1.004131   580
## S[7,29]   0.96897302 0.01698541  0.9278622  0.95982672  0.97173032  0.98141618  0.9930116 1.003403   730
## S[11,29]  0.97331934 0.01122317  0.9470073  0.96680464  0.97480413  0.98143856  0.9910039 1.000907  4500
## S[12,29]  0.97150272 0.01335928  0.9399174  0.96414641  0.97323188  0.98110483  0.9918187 1.002151  1300
## S[13,29]  0.97218231 0.01248625  0.9428015  0.96526475  0.97379201  0.98122974  0.9914843 1.001683  2000
## S[14,29]  0.97218231 0.01248625  0.9428015  0.96526475  0.97379201  0.98122974  0.9914843 1.001683  2000
## S[16,29]  0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[17,29]  0.97478570 0.01057892  0.9510086  0.96856694  0.97610783  0.98256573  0.9917346 1.001359  2900
## S[3,30]   0.96282383 0.02689086  0.8954728  0.95069877  0.96938123  0.98218645  0.9950191 1.004340   550
## S[4,30]   0.96427320 0.02446747  0.9033579  0.95294224  0.96985111  0.98205814  0.9946054 1.004259   560
## S[7,30]   0.96795086 0.01855151  0.9219906  0.95839118  0.97126363  0.98157786  0.9935259 1.003708   660
## S[11,30]  0.97278672 0.01177481  0.9452478  0.96597098  0.97430086  0.98124425  0.9910692 1.001250  3400
## S[12,30]  0.97074417 0.01439680  0.9363598  0.96272700  0.97269143  0.98122663  0.9921283 1.002612  1000
## S[13,30]  0.97150272 0.01335928  0.9399174  0.96414641  0.97323188  0.98110483  0.9918187 1.002151  1300
## S[14,30]  0.97150272 0.01335928  0.9399174  0.96414641  0.97323188  0.98110483  0.9918187 1.002151  1300
## S[16,30]  0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[17,30]  0.97451432 0.01051117  0.9506561  0.96842421  0.97574889  0.98220316  0.9913584 1.000927  4500
## S[3,31]   0.96125235 0.02956523  0.8858342  0.94834663  0.96904977  0.98237800  0.9954345 1.004382   540
## S[4,31]   0.96282383 0.02689086  0.8954728  0.95069877  0.96938123  0.98218645  0.9950191 1.004340   550
## S[7,31]   0.96683066 0.02031212  0.9163687  0.95669966  0.97070910  0.98163232  0.9939670 1.003949   610
## S[11,31]  0.97218231 0.01248625  0.9428015  0.96526475  0.97379201  0.98122974  0.9914843 1.001683  2000
## S[12,31]  0.96990246 0.01560335  0.9319683  0.96133264  0.97209452  0.98130253  0.9926337 1.003035   850
## S[13,31]  0.97074417 0.01439680  0.9363598  0.96272700  0.97269143  0.98122663  0.9921283 1.002612  1000
## S[14,31]  0.97074417 0.01439680  0.9363598  0.96272700  0.97269143  0.98122663  0.9921283 1.002612  1000
## S[16,31]  0.97331934 0.01122317  0.9470073  0.96680464  0.97480413  0.98143856  0.9910039 1.000907  4500
## S[17,31]  0.97418074 0.01059354  0.9495752  0.96805361  0.97538789  0.98196896  0.9910313 1.000709  4500
## S[3,32]   0.95955242 0.03250623  0.8763553  0.94569909  0.96830357  0.98269955  0.9958377 1.004392   540
## S[11,32]  0.97150272 0.01335928  0.9399174  0.96414641  0.97323188  0.98110483  0.9918187 1.002151  1300
## S[12,32]  0.96897302 0.01698541  0.9278622  0.95982672  0.97173032  0.98141618  0.9930116 1.003403   730
## S[13,32]  0.96990246 0.01560335  0.9319683  0.96133264  0.97209452  0.98130253  0.9926337 1.003035   850
## S[16,32]  0.97278672 0.01177481  0.9452478  0.96597098  0.97430086  0.98124425  0.9910692 1.001250  3400
## S[17,32]  0.97378315 0.01082998  0.9483426  0.96763139  0.97510019  0.98176139  0.9908962 1.000711  4500
## S[3,33]   0.95771781 0.03572900  0.8638914  0.94298253  0.96774859  0.98303035  0.9962093 1.004376   540
## S[12,33]  0.96795086 0.01855151  0.9219906  0.95839118  0.97126363  0.98157786  0.9935259 1.003708   660
## S[13,33]  0.96897302 0.01698541  0.9278622  0.95982672  0.97173032  0.98141618  0.9930116 1.003403   730
## S[17,33]  0.97331934 0.01122317  0.9470073  0.96680464  0.97480413  0.98143856  0.9910039 1.000907  4500
## S[3,34]   0.95574248 0.03924749  0.8523338  0.94010979  0.96707852  0.98335723  0.9965040 1.004339   550
## S[12,34]  0.96683066 0.02031212  0.9163687  0.95669966  0.97070910  0.98163232  0.9939670 1.003949   610
## S[13,34]  0.96795086 0.01855151  0.9219906  0.95839118  0.97126363  0.98157786  0.9935259 1.003708   660
## S[3,35]   0.95362083 0.04307370  0.8384427  0.93753638  0.96624335  0.98358261  0.9968649 1.004287   550
## S[12,35]  0.96560676 0.02227952  0.9099740  0.95487698  0.97029904  0.98177800  0.9943081 1.004131   580
## S[13,35]  0.96683066 0.02031212  0.9163687  0.95669966  0.97070910  0.98163232  0.9939670 1.003949   610
## S[3,36]   0.95134779 0.04721694  0.8226221  0.93483060  0.96580411  0.98377340  0.9971895 1.004409   560
## S[12,36]  0.96427320 0.02446747  0.9033579  0.95294224  0.96985111  0.98205814  0.9946054 1.004259   560
## S[13,36]  0.96560676 0.02227952  0.9099740  0.95487698  0.97029904  0.98177800  0.9943081 1.004131   580
## S[3,37]   0.94891904 0.05168310  0.8056079  0.93180456  0.96525022  0.98409776  0.9974084 1.004555   580
## S[13,37]  0.96427320 0.02446747  0.9033579  0.95294224  0.96985111  0.98205814  0.9946054 1.004259   560
## S[3,38]   0.94633118 0.05647400  0.7873182  0.92833751  0.96461730  0.98441398  0.9976301 1.004710   590
## S[13,38]  0.96282383 0.02689086  0.8954728  0.95069877  0.96938123  0.98218645  0.9950191 1.004340   550
## S[3,39]   0.94358188 0.06158685  0.7711910  0.92471204  0.96393019  0.98458925  0.9978262 1.004873   600
## S[13,39]  0.96125235 0.02956523  0.8858342  0.94834663  0.96904977  0.98237800  0.9954345 1.004382   540
## beta[1]   3.90994722 0.83407749  2.4297889  3.33446363  3.84593961  4.41056075  5.7614877 1.006835   330
## beta[2]  -0.01470816 0.05383181 -0.1183040 -0.05018079 -0.01540165  0.01973208  0.0964004 1.007686   290
## deviance 44.42381116 2.05713574 42.4322338 42.95302152 43.79784018 45.19890195 49.9933623 1.001574  2200
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]  3.90994722 0.83407749  2.429789 5.7614877 1.006835   330
## beta[2] -0.01470816 0.05383181 -0.118304 0.0964004 1.007686   290