forked from njhostet/Hostetter_etal_2021_PlosOne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SupplementS4_CaseStudy.R
421 lines (308 loc) · 13 KB
/
SupplementS4_CaseStudy.R
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
##
##
## Case study described in Hostetter NJ, NJ Lunn, ES Richardson, EV Regehr, and SJ Converse:
## Age-structured Jolly-Seber model expands inference and improves parameter estimation from capture-recapture data.
##
## This script loads, formats, and runs the models described in the case study.
## For detailed examples of data and models, we recommend the simulation script (SupplementS1_SimulationAgeStructuredJS.R)
##
## models include
## JS without age data ("NoAge")
## JS with age data and constant survival ("Age")
## JS with age data and survival is a quadratic function of age ("AgeSurvival")
##
## 10-July-2020
##
## Although these data have been processed successfully on a computer system at the U.S. Geological Survey (USGS), no warranty expressed or implied is made regarding the display or utility of the data for other purposes, nor on all computer systems, nor shall the act of distribution constitute any such warranty. The USGS or the U.S. Government shall not be held liable for improper or incorrect use of the data described and/or contained herein.
## required packages
library(nimble)
## load data
dat <- dget("SupplementS3_CaseStudyData.txt")
list2env(dat, .GlobalEnv) # send list variable to global environment for ease of indexing
# ydatAug: capture-recapture data, inlcuding augmented individuals
# n: number of detected individuals
# nz: number of augmented individuals (note M = n+nz)
# M: augmentation value
# K: study occasions
# ageMatAugMinusOne: age matrix, including augmented individuals. Ages are 1-actual ages as individuals *enter* at actual age 2 (independence)
# centered_age: median observed age (minus 1)
# max.age: maximum age in occasion 1 (minus 1).
# zdatNoAGE: known values of z in JS model without age data
# zstNoAGE: initial values for z in JS model without age data. Prevents initialization errors
# zdatAGE: known values of z in JS model with age data
# zstAGE: initial values for z in JS model with age data. Prevents initialization errors
## MCMC settings (warning, hours of run time with full settings)
nc <- 6; nAdapt <- 500; nb <- 20000; ni <- 200000+nb; nthin <- 10
nc <- 2; nAdapt <- 500; nb <- 100; ni <- 500+nb; nthin <- 1 # demonstrate model
## select model to run
run.model <- "NoAge" # "NoAge", "AgeSurvival","Age"
if(run.model=="NoAge"){
##
##
##
## OPTION 1: no age data
##
##
##
##
code_JS <- nimbleCode({
# Priors and constraints
psi ~ dbeta(1,1) # data augmentation
p ~ dbeta(1,1) # detection probability
phi ~ dbeta(1,1) # survival
# recruitment prob at k
beta[1:K] ~ ddirch(b[1:K])
eta[1] <- beta[1]
for(k in 2:K){
eta[k] <- beta[k]/(1-sum(beta[1:(k-1)]))
}
# Likelihoods
for (i in 1:M){
w[i] ~ dbern(psi)
# At first occasion
# State process
u[i,1] ~ dbern(eta[1])
z[i,1] <- u[i,1]*w[i]
# Observation process
y[i,1] ~ dbern(z[i,1]*p)
# derived stuff
avail[i,1] <- 1- u[i,1] # still available for 'recruitment'.
recruit[i,1] <- z[i,1] # 'recruited' at k
# for occasions >1
for (t in 2:K){
# State process
u[i,t] ~ dbern(u[i,t-1]*phi + avail[i,t-1]*eta[t])
z[i,t] <-u[i,t]*w[i]
# Observation process
y[i,t] ~ dbern(z[i,t]*p)
# derived stuff
avail[i,t] <- 1- max(u[i,1:t]) # still available for 'recruitment'.
recruit[i,t] <- equals(z[i,t]-z[i,t-1],1) # 'recruited' at k
} #t
} #i
#derived population level stuff
for (t in 1:K){
N[t] <- sum(z[1:M,t]) # Annual abundance
B[t] <- sum(recruit[1:M,t]) # Number of entries
} #t
Nsuper <- sum(w[1:M]) # Superpopulation size
# Optional Freeman-Tukey GOF evaluating observed and expected counts.
for(t in 1:K){
expectedCount[t] <- N[t]*p
Count.new[t] ~ dbin(p, N[t])
## discrepancy from original
E.dat[t] <- (sqrt(Counts[t])-sqrt(expectedCount[t]) )^2
## discrepancy from new
E.new[t] <- (sqrt(Count.new[t])-sqrt(expectedCount[t]) )^2
}
fit.dat <- sum(E.dat[1:K])
fit.new <- sum(E.new[1:K])
})#END model
## Format for NIMBLE without age
nim.data<- list(u=zdatNoAGE,y = ydatAug, w=c(rep(1, n), rep(NA, nz)), b=rep(1,K) )
nim.constants<- list(K=K, M=M, Counts=apply(ydatAug, 2,sum))
params <- c("p", "phi", "beta", "psi", "Nsuper", "N","fit.dat", "fit.new")
inits <- list(phi = .80, p = .2, u = zstNoAGE , w=c(rep(NA, n), rep(0, nz)), psi = .5, beta=rep(1/K, K) )
## RUN!
## Nimble steps
Rmodel <- nimbleModel(code=code_JS, constants=nim.constants, data=nim.data, calculate=F, check=F, inits=inits)
conf <- configureMCMC(Rmodel,monitors=params, control = list(adaptInterval = nAdapt), thin=nthin, useConjugacy = TRUE)
Rmcmc <- buildMCMC(conf) #produces an uncompiled R mcmc function
Cmodel <- compileNimble(Rmodel)
Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
out <- runMCMC(Cmcmc, niter = ni, nburnin = nb, nchains = nc, inits=inits, thin=nthin,
setSeed = FALSE, progressBar = TRUE, samplesAsCodaMCMC = TRUE)
}#if(run.model=="NoAge")
if(run.model=="Age"){
##
##
##
## OPTION 2: with age data, constant survival
##
##
##
##
## NIMBLE MODEL
code_JS_AGE <- nimbleCode({
# Priors and constraints
psi~ dbeta(1,1) # data augmentation
p ~ dbeta(1,1) # detection
phi ~ dbeta(1,1) # survival
# recruitment prob at k
beta[1:K] ~ ddirch(b[1:K])
eta[1] <- beta[1]
for(k in 2:K){
eta[k] <- beta[k]/(1-sum(beta[1:(k-1)]))
}
# starting age distribution is not yet recruited (age = 0), or age if recruited
piAGE[1:max.age] ~ ddirch(a[1:max.age])
piAGEuncond[1:(max.age+1)] <- c( (1-eta[1] ), eta[1]*piAGE[1:max.age] )
# Likelihoods
for (i in 1:M){
w[i] ~ dbern(psi)
# Define latent state at first occasion
# Age process
agePlusOne[i] ~ dcat(piAGEuncond[1:(max.age+1)])
age[i,1] <- agePlusOne[i]-1
trueAge[i,1] <- age[i,1]*z[i,1] # age if alive (zero if not yet entered or dead)
# State process
u[i,1] <- step(agePlusOne[i]-1.1) # alive if age[i,1] >0
z[i,1] <- u[i,1]*w[i]
# Observation process
y[i,1] ~ dbern(z[i,1]*p)
# derived stuff
avail[i,1] <- 1- u[i,1] # still available for recruitment.
recruit[i,1] <- z[i,1] # recruited at k
# for occasions >1
for (t in 2:K){
# State process
u[i,t] ~ dbern(u[i,t-1]*phi + avail[i,t-1]*eta[t])
z[i,t] <- u[i,t]*w[i]
trueAge[i,t] <- age[i,t]*z[i,t]
# Age process
age[i,t] <- age[i,t-1] + max(u[i,1:t]) # ages by one year after recruitment
# Observation process
y[i,t] ~ dbern(z[i,t]*p)
# derived stuff
avail[i,t] <- 1- max(u[i,1:t])
recruit[i,t] <- equals(z[i,t]-z[i,t-1],1) # recruited at k
} #t
} #i
#derived population level stuff
for (t in 1:K){
N[t] <- sum(z[1:M,t]) # Annual abundance
B[t] <- sum(recruit[1:M,t]) # Number of entries
} #t
Nsuper <- sum(w[1:M]) # Superpopulation size
# Optional Freeman-Tukey GOF evaluating observed and expected counts.
for(t in 1:K){
expectedCount[t] <- N[t]*p
Count.new[t] ~ dbin(p, N[t])
## discrepancy from original
E.dat[t] <- (sqrt(Counts[t])-sqrt(expectedCount[t]) )^2
## discrepancy from new
E.new[t] <- (sqrt(Count.new[t])-sqrt(expectedCount[t]) )^2
}
fit.dat <- sum(E.dat[1:K])
fit.new <- sum(E.new[1:K])
})#END model
##
## Format for NIMBLE with age
nim.data<- list(u=zdatAGE,y = ydatAug, age=ageMatAugMinusOne, agePlusOne=ageMatAugMinusOne[,1]+1, w=c(rep(1, n), rep(NA, nz)),
b=rep(1,K), a=rep(1, max.age) )
nim.constants<- list(K=K, M=M, max.age=max.age, Counts=apply(ydatAug, 2,sum))
params <- c("p", "phi", "beta", "psi", "piAGE", "Nsuper", "N","fit.dat", "fit.new")
inits <- list(phi = .9, p = .2, psi=.6, u = zstAGE, w=c(rep(NA, n), rep(0, nz)),
beta=rep(1/K, K),agePlusOne=c(rep(NA, n), rep(1, nz)) )
## RUN!
## Nimble steps
Rmodel <- nimbleModel(code=code_JS_AGE, constants=nim.constants, data=nim.data, calculate=F, check=F, inits=inits)
conf <- configureMCMC(Rmodel,monitors=params,control = list(adaptInterval = nAdapt), thin=nthin, useConjugacy = TRUE)
Rmcmc <- buildMCMC(conf)
Cmodel <- compileNimble(Rmodel)
Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
out <- runMCMC(Cmcmc, niter = ni, nburnin = nb, nchains = nc, inits=inits, thin=nthin,
setSeed = FALSE, progressBar = TRUE, samplesAsCodaMCMC = TRUE)
}#if(run.model=="Age")
if(run.model=="AgeSurvival"){
##
##
##
##
## Option 3. with age data, survival a quadratic function of age
##
##
##
##
## NIMBLE MODEL
code_JS_AgeSurvival <- nimbleCode({
# Priors and constraints
psi~ dbeta(1,1) # data augmentation
p ~ dbeta(1,1) # detection
phi0 ~ dbeta(1,1) # survival at centered age
alpha0 <- logit(phi0)
alpha1 ~ dnorm(0, sd=10) # relationship with age[i,t]
alpha2 ~ dnorm(0, sd=10) # relationship with age[i,t]^2
# recruitment prob at k
beta[1:K] ~ ddirch(b[1:K])
eta[1] <- beta[1]
for(k in 2:K){
eta[k] <- beta[k]/(1-sum(beta[1:(k-1)]))
}
# starting age distribution is not yet recruited (age = 0), or age if alive
piAGE[1:max.age] ~ ddirch(a[1:max.age])
piAGEuncond[1:(max.age+1)] <- c( (1-eta[1] ), eta[1]*piAGE[1:max.age] )
# Likelihoods
for (i in 1:M){
w[i] ~ dbern(psi)
# Define latent state at first occasion
# Age process
agePlusOne[i] ~ dcat(piAGEuncond[1:(max.age+1)])
age[i,1] <- agePlusOne[i]-1
trueAge[i,1] <- age[i,1] * z[i,1]
# State process
u[i,1] <- step(agePlusOne[i]-1.1) # alive if age[i,1] >0
z[i,1] <- u[i,1]*w[i]
# Observation process
y[i,1] ~ dbern(z[i,1]*p)
# derived stuff
avail[i,1] <- 1- u[i,1] # still available for recruitment
recruit[i,1] <- z[i,1] # recruited at t
# for occasions >1
for (t in 2:K){
# State process
u[i,t] ~ dbern(u[i,t-1]*phi[i,t-1] + avail[i,t-1]*eta[t])
logit(phi[i,t-1]) <- alpha0 + alpha1*(age[i,t-1]-centered_age) + alpha2*(age[i,t-1]-centered_age)^2 # centered
z[i,t] <- u[i,t]*w[i]
# Age process
age[i,t] <- age[i,t-1] + max(u[i,1:t]) # ages by one year after recruitment
trueAge[i,t] <- age[i,t] * z[i,t]
# Observation process
y[i,t] ~ dbern(z[i,t]*p)
# derived stuff
avail[i,t] <- 1- max(u[i,1:t])
recruit[i,t] <- equals(z[i,t]-z[i,t-1],1) # recruited at t
} #t
} #i
#derived population level stuff
for (t in 1:K){
N[t] <- sum(z[1:M,t]) # Annual abundance
B[t] <- sum(recruit[1:M,t]) # Number of entries
} #t
Nsuper <- sum(w[1:M]) # Superpopulation size
# monitor age specific survival (buffer beyond expected max.age if interested). This could be done outside the model.
for(aa in 1:(max.age+5)){
logit(phi.age[aa]) <- alpha0 + alpha1*(aa-centered_age) + alpha2*(aa-centered_age)^2
}
# Optional Freeman-Tukey GOF evaluating observed and expected counts.
for(t in 1:K){
expectedCount[t] <- N[t]*p
Count.new[t] ~ dbin(p, N[t])
## discrepancy from original
E.dat[t] <- (sqrt(Counts[t])-sqrt(expectedCount[t]) )^2
## discrepancy from new
E.new[t] <- (sqrt(Count.new[t])-sqrt(expectedCount[t]) )^2
}
fit.dat <- sum(E.dat[1:K])
fit.new <- sum(E.new[1:K])
})#END model
##
## Format for NIMBLE
nim.data<- list(u=zdatAGE,y = ydatAug, age=ageMatAugMinusOne, agePlusOne=ageMatAugMinusOne[,1]+1, w=c(rep(1, n), rep(NA, nz)),
b=rep(1,K), a=rep(1, max.age))
nim.constants<- list(K=K, M=M, max.age=max.age, centered_age=centered_age, Counts=apply(ydatAug, 2,sum))
params <- c("p", "phi.age", "phi0","alpha0", "alpha1", "alpha2",
"beta", "piAGE","psi", "Nsuper", "N","fit.dat", "fit.new") # save trueAge to derive estimated age structure our side the model.
inits <- list(phi0 = .75, alpha1 = 0, alpha2 = 0, p = .2, psi=.6, u = zstAGE, w=c(rep(NA, n), rep(0, nz)),
beta=rep(1/K, K),agePlusOne=c(rep(NA, n), rep(1, nz)) )
## RUN!
## Nimble steps
Rmodel <- nimbleModel(code=code_JS_AgeSurvival, constants=nim.constants, data=nim.data, calculate=F, check=F, inits=inits)
conf <- configureMCMC(Rmodel,monitors=params,control = list(adaptInterval = nAdapt), thin=nthin, useConjugacy = TRUE)
Rmcmc <- buildMCMC(conf)
Cmodel <- compileNimble(Rmodel)
Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
out <- runMCMC(Cmcmc, niter = ni, nburnin = nb, nchains = nc, inits=inits, thin=nthin,
setSeed = FALSE, progressBar = TRUE, samplesAsCodaMCMC = TRUE)
}# if(run.model=="AgeSurvival")
summary(out)