1
1
# Schaub & Kéry (2022) Integrated Population Models
2
2
# Chapter 2 : Bayesian statistical modeling using JAGS
3
3
# ----------------------------------------------------
4
- # Code from proofs.
5
4
6
5
# Run time approx. 3 mins
7
6
@@ -15,7 +14,7 @@ library(IPMbook)
15
14
16
15
# Define imaginary counts and elevation data for 10 sites
17
16
original.elev <- c(500 , 400 , 700 , 500 , 600 , 800 , 1000 , 900 , 1000 , 900 )
18
- C <- c(6 , 10 , 2 , 7 , 4 , 1 , 1 , 2 , 0 , 0 ) # Counts
17
+ C <- c(6 , 10 , 2 , 7 , 4 , 1 , 1 , 2 , 0 , 0 ) # Counts
19
18
20
19
# Look at data and produce summaries of counts (not shown)
21
20
cbind(original.elev , C )
@@ -34,6 +33,7 @@ library(jagsUI)
34
33
35
34
# Bundle data
36
35
jags.data <- list (C = C , elev = elev , n = length(C ))
36
+
37
37
str(jags.data )
38
38
# List of 3
39
39
# $ C : num [1:10] 6 10 2 7 4 1 1 2 0 0
@@ -45,22 +45,19 @@ cat(file="model1.txt", "
45
45
model {
46
46
# Priors and linear models
47
47
# One set of vague priors: 'flat' normal
48
- alpha ~ dnorm(0, 1.0E-06) # log-linear intercept
49
- beta ~ dnorm(0, 1.0E-06) # log-linear slope
50
-
48
+ alpha ~ dnorm(0, 1.0E-06) # log-linear intercept
49
+ beta ~ dnorm(0, 1.0E-06) # log-linear slope
51
50
# Another possible set of vague priors: suitably wide uniform
52
- # alpha ~ dunif(-100, 100) # log-linear intercept
53
- # beta ~ dunif(-100, 100) # log-linear slope
54
-
51
+ # alpha ~ dunif(-100, 100) # log-linear intercept
52
+ # beta ~ dunif(-100, 100) # log-linear slope
55
53
# Likelihood for the Poisson GLM
56
54
for (i in 1:n){
57
55
C[i] ~ dpois(lambda[i]) # Stochastic part
58
- log(lambda[i]) <- alpha + beta * elev[i] # Link function and linear pred.
59
- # lambda[i] <- exp(alpha + beta * elev[i]) # same written differently
56
+ log(lambda[i]) <- alpha + beta * elev[i] # Link function and linear pred.
57
+ # lambda[i] <- exp(alpha + beta * elev[i]) # Same written differently
60
58
}
61
-
62
59
# Derived quantities
63
- mean.exp.count <- exp(alpha) # Backtransformed intercept
60
+ mean.exp.count <- exp(alpha) # Backtransformed intercept
64
61
}
65
62
" )
66
63
@@ -84,37 +81,73 @@ out1 <- jags(jags.data, inits, parameters, "model1.txt", n.iter=ni, n.burnin=nb,
84
81
# Produce an overview of the R object created by jagsUI
85
82
str(out1 ) # Only a small portion of total output is shown here!
86
83
87
- # par(mfrow=c(2, 2)); jagsUI::traceplot(out1) # May need jagsUI::traceplot!
88
- jagsUI :: traceplot(out1 , layout = c(2 ,2 )) # May need jagsUI::traceplot!
84
+ # List of 24
85
+ # $ sims.list :List of 5
86
+ # ..$ alpha : num [1:12000] -0.0859 -0.2759 0.5362 0.5113 ...
87
+ # ..$ beta : num [1:12000] -8.13 -7.02 -6.57 -5.51 -5.05 ...
88
+ # ..$ mean.exp.count: num [1:12000] 0.918 0.759 1.709 1.667 2.062 ...
89
+ # ..$ lambda : num [1:12000, 1:10] 5.96 3.82 7.75 5.93 6.58 ...
90
+ # ..$ deviance : num [1:12000] 35.2 39.4 31.5 29 28.7 ...
91
+ # $ mean :List of 5
92
+ # ..$ alpha : num 0.623
93
+ # ..$ beta : num -5.16
94
+ # ..$ mean.exp.count: num 1.94
95
+ # ..$ lambda : num [1:10(1d)] 6.21 10.52 2.25 6.21 3.71 ...
96
+ # ..$ deviance : num 30.7
97
+ # [... output truncated ...]
98
+ # $ summary : num [1:14, 1:11] 0.623 -5.157 1.936 6.209 10.522 ...
99
+ # ..- attr(*, "dimnames")=List of 2
100
+ # .. ..$ : chr [1:14] "alpha" "beta" "mean.exp.count" "lambda[1]" ...
101
+ # .. ..$ : chr [1:11] "mean" "sd" "2.5%" "25%" ...
102
+ # $ samples :List of 3
103
+ # ..$ : 'mcmc' num [1:4000, 1:14] -0.0859 -0.2759 0.5362 0.5113 0.7234 ...
104
+ # .. ..- attr(*, "dimnames")=List of 2
105
+ # .. .. ..$ : NULL
106
+ # .. .. ..$ : chr [1:14] "alpha" "beta" "mean.exp.count" "lambda[1]" ...
107
+ # .. ..- attr(*, "mcpar")= num [1:3] 10010 50000 10
108
+ # ..$ : 'mcmc' num [1:4000, 1:14] 0.9 1.091 0.935 0.579 0.518 ...
109
+ # .. ..- attr(*, "dimnames")=List of 2
110
+ # .. .. ..$ : NULL
111
+ # .. .. ..$ : chr [1:14] "alpha" "beta" "mean.exp.count" "lambda[1]" ...
112
+ # .. ..- attr(*, "mcpar")= num [1:3] 10010 50000 10
113
+ # ..$ : 'mcmc' num [1:4000, 1:14] 0.294 0.559 0.226 0.646 0.545 ...
114
+ # .. ..- attr(*, "dimnames")=List of 2
115
+ # .. .. ..$ : NULL
116
+ # .. .. ..$ : chr [1:14] "alpha" "beta" "mean.exp.count" "lambda[1]" ...
117
+ # .. ..- attr(*, "mcpar")= num [1:3] 10010 50000 10
118
+ # ..- attr(*, "class")= chr "mcmc.list"
119
+ # [... output truncated ...]
120
+
121
+
122
+ jagsUI :: traceplot(out1 , layout = c(2 , 2 )) # May need jagsUI::traceplot!
89
123
90
124
# ~~~~ code for Fig 2.10 ~~~~
91
- jagsUI :: traceplot(out1 , c(" alpha" , " beta" , " mean.exp.count" ), layout = c(1 ,3 )) # just some
125
+ jagsUI :: traceplot(out1 , c(" alpha" , " beta" , " mean.exp.count" ), layout = c(1 ,3 ))
92
126
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
93
127
94
128
print(out1 , 3 )
95
129
96
130
# JAGS output for model 'model1.txt', generated by jagsUI.
97
- # Estimates based on 3 chains of 50000 iterations,
98
- # adaptation = 1000 iterations (sufficient),
99
- # burn-in = 10000 iterations and thin rate = 10,
100
- # yielding 12000 total samples from the joint posterior.
131
+ # Estimates based on 3 chains of 50000 iterations, adaptation = 1000 iterations (sufficient),
132
+ # burn-in = 10000 iterations and thin rate = 10, yielding 12000 total samples from the
133
+ # joint posterior.
101
134
# MCMC ran for 0.081 minutes at time 2020-11-19 18:09:13.
102
135
103
- # mean sd 2.5% 50% 97.5% overlap0 f Rhat n.eff
104
- # alpha 0.619 0.280 0.027 0.637 1.114 FALSE 0.979 1.000 12000
105
- # beta -5.178 1.140 -7.531 -5.122 -3.087 FALSE 1.000 1.000 12000
106
- # mean.exp.count 1.929 0.520 1.028 1.892 3.048 FALSE 1.000 1.000 12000
107
- # lambda[1] 6.210 1.115 4.233 6.132 8.558 FALSE 1.000 1.000 12000
108
- # lambda[2] 10.541 2.479 6.354 10.327 16.022 FALSE 1.000 1.000 10153
109
- # lambda[3] 2.238 0.550 1.271 2.206 3.414 FALSE 1.000 1.000 12000
110
- # lambda[4] 6.210 1.115 4.233 6.132 8.558 FALSE 1.000 1.000 12000
111
- # lambda[5] 3.705 0.689 2.472 3.657 5.171 FALSE 1.000 1.000 12000
112
- # lambda[6] 1.368 0.453 0.618 1.325 2.365 FALSE 1.000 1.000 12000
113
- # lambda[7] 0.529 0.283 0.141 0.478 1.229 FALSE 1.000 1.000 12000
114
- # lambda[8] 0.846 0.363 0.296 0.795 1.692 FALSE 1.000 1.000 12000
115
- # lambda[9] 0.529 0.283 0.141 0.478 1.229 FALSE 1.000 1.000 12000
116
- # lambda[10] 0.846 0.363 0.296 0.795 1.692 FALSE 1.000 1.000 12000
117
- # deviance 30.633 2.044 28.661 30.021 36.144 FALSE 1.000 1.001 3355
136
+ # mean sd 2.5% 50% 97.5% overlap0 f Rhat n.eff
137
+ # alpha 0.619 0.280 0.027 0.637 1.114 FALSE 0.979 1.000 12000
138
+ # beta -5.178 1.140 -7.531 -5.122 -3.087 FALSE 1.000 1.000 12000
139
+ # mean.exp.count 1.929 0.520 1.028 1.892 3.048 FALSE 1.000 1.000 12000
140
+ # lambda[1] 6.210 1.115 4.233 6.132 8.558 FALSE 1.000 1.000 12000
141
+ # lambda[2] 10.541 2.479 6.354 10.327 16.022 FALSE 1.000 1.000 10153
142
+ # lambda[3] 2.238 0.550 1.271 2.206 3.414 FALSE 1.000 1.000 12000
143
+ # lambda[4] 6.210 1.115 4.233 6.132 8.558 FALSE 1.000 1.000 12000
144
+ # lambda[5] 3.705 0.689 2.472 3.657 5.171 FALSE 1.000 1.000 12000
145
+ # lambda[6] 1.368 0.453 0.618 1.325 2.365 FALSE 1.000 1.000 12000
146
+ # lambda[7] 0.529 0.283 0.141 0.478 1.229 FALSE 1.000 1.000 12000
147
+ # lambda[8] 0.846 0.363 0.296 0.795 1.692 FALSE 1.000 1.000 12000
148
+ # lambda[9] 0.529 0.283 0.141 0.478 1.229 FALSE 1.000 1.000 12000
149
+ # lambda[10] 0.846 0.363 0.296 0.795 1.692 FALSE 1.000 1.000 12000
150
+ # deviance 30.633 2.044 28.661 30.021 36.144 FALSE 1.000 1.001 3355
118
151
119
152
# Successful convergence based on Rhat values (all < 1.1).
120
153
# Rhat is the potential scale reduction factor (at convergence, Rhat=1).
@@ -148,17 +181,17 @@ predict(fm, type='response', newdata=data.frame(elev=0), se=TRUE)
148
181
149
182
# [... output truncated ...]
150
183
# Coefficients:
151
- # Estimate Std. Error z value Pr(>|z|)
184
+ # Estimate Std. Error z value Pr(>|z|)
152
185
# (Intercept) 0.6787 0.2705 2.509 0.0121 *
153
186
# elev -5.0198 1.1035 -4.549 5.4e-06 ***
154
187
# [... output truncated ...]
155
188
156
189
# $fit
157
- # 1
190
+ # 1
158
191
# 1.971256
159
192
160
193
# $se.fit
161
- # 1
194
+ # 1
162
195
# 0.5331537
163
196
164
197
# New data bundle with variant of data with bigger sample size
0 commit comments