forked from davisk93/Davis-et-al_BLTE-IPM-BPVA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BLTE_SCF_Github.R
1605 lines (1245 loc) · 53.6 KB
/
BLTE_SCF_Github.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
########################################################################################
# Integrated population model (IPM) for St. Clair Flats Black Terns, 2013 - 2022
# Kayla Davis, Sarah Saunders, Stephanie Beilke, Erin Ford, Jenni Fuller, Ava Landgraf, and Elise Zipkin
# Adapted from original scripts by Michael Schaub & Marc Kéry (2021)
# Modified by K. Davis, 2022
########################################################################################
# setup ------------------------------------------------------------------------
#load libraries
library(openxlsx)
library(tidyverse)
library(lubridate)
library(coda)
library(ggpubr)
library(MCMCvis)
library(jagsUI)
library(scales)
library(plotrix)
library(tools)
#load function to create a m-array based on capture-recapture data (CH)
marray <- function(CH){
nind <- dim(CH)[1]
n.occasions <- dim(CH)[2]
m.array <- matrix(data = 0, ncol = n.occasions+1, nrow = n.occasions)
#Calculate the number of released individuals at each time period
for (t in 1:n.occasions){
m.array[t,1] <- sum(CH[,t])
}
for (i in 1:nind){
pos <- which(CH[i,]!=0)
g <- length(pos)
for (z in 1:(g-1)){
m.array[pos[z],pos[z+1]] <- m.array[pos[z],pos[z+1]] + 1
} #z
} #i
#Calculate the number of individuals that are never recaptured
for (t in 1:n.occasions){
m.array[t,n.occasions+1] <- m.array[t,1] - sum(m.array[t,2:n.occasions])
}
out <- m.array[1:(n.occasions-1),2:(n.occasions+1)]
return(out)
}
# the data ---------------------------------------------------------------------
########################################################################
# Capture-recapture data: m-array of juveniles (HY) and adults (AHY)
########################################################################
#First read in capture histories for birds marked as chicks during 2013-2022
CH.J <- read.table("SCF_HY13to22_update.txt")
#convert to matrix
CH.J <- data.matrix(CH.J)
#read in capture histories for birds marked as adults during 2013-2022
CH.A <- read.table("SCF_AHY13to22_update.txt")
#convert to matrix
CH.A <- data.matrix(CH.A)
#create two m-arrays, one for juveniles and one for adults
cap <- apply(CH.J, 1, sum)
ind <- which(cap >= 2)
CH.J.R <- CH.J[ind,] # Juvenile CH recaptured at least once
CH.J.N <- CH.J[-ind,] # Juvenile CH never recaptured
#Remove first capture
first <- numeric()
for (i in 1:dim(CH.J.R)[1]){
first[i] <- min(which(CH.J.R[i,]==1))
}
CH.J.R1 <- CH.J.R
for (i in 1:dim(CH.J.R)[1]){
CH.J.R1[i,first[i]] <- 0
}
#Add grown-up juveniles to adults and create m-array
CH.A.m <- rbind(CH.A, CH.J.R1)
CH.A.marray <- marray(CH.A.m)
#Create CH matrix for juveniles, ignoring subsequent recaptures
second <- numeric()
for (i in 1:dim(CH.J.R1)[1]){
second[i] <- min(which(CH.J.R1[i,]==1))
}
CH.J.R2 <- matrix(0, nrow = dim(CH.J.R)[1], ncol = dim(CH.J.R)[2])
for (i in 1:dim(CH.J.R)[1]){
CH.J.R2[i,first[i]] <- 1
CH.J.R2[i,second[i]] <- 1
}
#Create m-array for these
CH.J.R.marray <- marray(CH.J.R2)
#The last column should show the number of juveniles not recaptured again and should all be zeros, since all of them are released as adults
CH.J.R.marray[,dim(CH.J)[2]] <- 0
#Create the m-array for juveniles never recaptured and add it to the previous m-array
CH.J.N.marray <- marray(CH.J.N)
CH.J.marray <- CH.J.R.marray + CH.J.N.marray
#outputs: CH.A.marray and CH.J.marray
#convert outputs to names of m-arrays used in models
#delete last 2 rows of juvenile m-array (can't recap birds released in last 2 occassions)
marray.j <- CH.J.marray[-c(7:8),]
marray.a <- CH.A.marray
# #Create NA marrays
# marray.j <- matrix(0, nrow = 7, ncol = 10)
# marray.a <- matrix(0, nrow = 9, ncol = 10)
########################################################################
# Population count data
########################################################################
dat1 <- read.xlsx("SCF-IPM_github.xlsx")
# breeding pairs and productivity
year <- dat1$Year
nyears <- length(year)
y <- dat1$MinPairs # the number of breeding pairs per year
j1 <- dat1$MinFledges # the number of fledglings recorded flying
j2 <- dat1$Nanotag # the number of fledglings recorded via nanotag
R <- y # number of pairs/broods monitored
R_tag <- dat1$NumberTagged # number of fledglings tagged
########################################################################
# Covariates
########################################################################
# read in covariate data (top supported model only)
yeareffects = read.csv("YearCovs.csv", header=T, sep=',', na.strings=T)
# adult survival covs
nao_hur = yeareffects$nao_jan.jun
# make into vector for jags
nao_hur = as.numeric(as.vector(nao_hur[1:9]))
#calculate mean nao
mnao <- mean(nao_hur)
sdnao <- sd(nao_hur)
# standardize effects
znao_hur <- as.vector(scale(nao_hur))
# the model ---------------------------------------------------------------------
#############################################################################
# Integrated population model (IPM) for St. Clair Flats Black Terns
# Code by Kayla Davis, Michigan State University, 2022
# Data provided by Detroit Audubon
# Adapted from original scripts by Michael Schaub & Marc Kéry (2021)
# Modified by K. Davis, 2022
# See main text for full description of modeling framework
#
# Notations:
# nyears = 10
# marray.j is an m-array of capture histories for individuals first banded as
# chicks during 2013 - 2022
# marray.a is an m-array of capture histories for individuals first banded as
# adults during 2013 - 2022
# y = number of breeding pairs annually
# j1 = number of fledglings observed annually from on-the-ground counts
# j2 = number of fledglings observed annually (2019 and 2021) from nanotag observations
# R = number of pairs/broods monitored annually
# R_tag = number of pre-fledged chicks tagged with nanotags in 2019 and 2021
# znao_hur = North Atlantic Oscillation index preceeding the hurricane season of year t-1
# (i.e. NAOI in January-June before breeding season of year t-1)
#############################################################################
sink("blte_ipm")
cat("
model {
#------------------------------------------------------------------------
# Integrated population model
# - Stage-structured model with 2 stages:
# juvenile (age 0-3), adults (age 4+)
# - Age at first breeding is third year
# - 1 and 2 y olds are 'invisible' (don't return to breeding grounds
# until age 3)
# - Prebreeding census, female-based
# - All vital rates are assumed to be time-dependent
# - Includes env. stochasticity thru random time effects for all params except fecundity
#------------------------------------------------------------------------
#----------------------------------------
# 1. Define the priors for the parameters
#----------------------------------------
# Initial population sizes
for (t in 1:3){
n1[t] ~ dnorm(10, 0.1)I(0,) # New 3 year olds; needs prior for first 3 years
N1[t] <- round(n1[t])
Ntot[t] <- Nad[t] + N1[t] # Initial population size of adults in first 3 years is sum of new 3yos and returning adults
}#t
N0[1] ~ dpois((f1[1]*0.5) * Ntot[1]) # Initial pop size of fledglings
nadSurv ~ dnorm(300, 0.01)I(0,) # Adults in year 1
Nad[1] <- round(nadSurv)
# Mean demographic parameters (on appropriate scale)
mphi.juv ~ dbeta(3, 12)
mphi.ad ~ dbeta(13.5, 4)
l.mphij <- log(mphi.juv / (1-mphi.juv)) # Logit transformation
l.mphia <- log(mphi.ad / (1-mphi.ad)) # Logit transformation
fec1 ~ dunif(0,3) # productivity
l.mfec1 <- log(fec1) # Log transform
fec2 ~ dunif(0,3) # productivity
l.mfec2 <- log(fec2) # Log transformation
res ~ dunif(0,1) # mean detection probability
l.p <- log(res / (1-res)) # Logit transformation
# Priors for beta coefficients
beta.phia1 ~ dnorm(0, 0.01)
# Precision of standard deviations of temporal variability
sig.phij ~ dexp(1)
tau.phij <- pow(sig.phij, -2)
sig.phia ~ dexp(1)
tau.phia <- pow(sig.phia, -2)
sig.res ~ dunif(0, 10)
tau.res <- pow(sig.res, -2)
sig.obs ~ dunif(0.5, 50) # residual variance
tau.obs <- pow(sig.obs, -2)
# Distribution of error terms (Bounded to help with convergence)
for (t in 1:(nyears-1)){
epsilon.phia[t] ~ dnorm(0, tau.phia)T(-5,5)
epsilon.res[t] ~ dnorm(0, tau.res)T(-5,5)
}
for (t in 1:(nyears-3)){
epsilon.phij[t] ~ dnorm(0, tau.phij)T(-5,5)
}
#---------------------------------------------
# 2. Constrain parameters (temp variability)
#---------------------------------------------
for (t in 1:(nyears-1)){
logit(phia[t]) <- l.mphia + beta.phia1 * znao_hur[t] + epsilon.phia[t] # epsilon.phia is random temporal effect for env. stoch.
logit(p[t]) <- l.p + epsilon.res[t]
}
for (t in 1:nyears){
log(f1[t]) <- l.mfec1 # f1 = fecundity from fledgling counts (this is used in the IPM)
log(f2[t]) <- l.mfec2 # f2 = fecundity from nanotag counts (this is estimated outside of the IPM for comparison to fledgling count)
}
for (t in 1:(nyears-3)){
logit(phij[t]) <- l.mphij + epsilon.phij[t]
}
#-----------------------
# 3. Derived parameters
#-----------------------
mphij <- exp(l.mphij)/(1+exp(l.mphij)) # Mean juvenile survival
mphia <- exp(l.mphia)/(1+exp(l.mphia)) # Mean adult survival
mfec1 <- exp(l.mfec1) # Mean productivity fledgling counts
mfec2 <- exp(l.mfec2) # Mean productivity nanotag counts
# Population growth rate (total adult breeders [3+ y olds])
for (t in 1:(nyears-1)){
lambda[t] <- Ntot[t+1] / (Ntot[t] + 0.001)
logla[t] <- log(lambda[t])
}
mlam <- exp((1/(nyears-1))*sum(logla[1:(nyears-1)])) # Geo mean all yrs
#--------------------------------------------
# 4. The likelihoods of the single data sets
#--------------------------------------------
# 4.1. Likelihood for population count data (state-space model)
# 4.1.1 System process
for (k in 4:nyears){
N1[k] ~ dbin(phij[k-3], N0[k-3])
Ntot[k] <- N1[k] + Nad[k]
}#k
for (t in 2:nyears){
mean1[t] <- (f1[t]*0.5) * Ntot[t] # all adults can breed
N0[t] ~ dpois(mean1[t]) # Fledglings
Nad[t] ~ dbin(phia[t-1], Ntot[t-1])
}
# 4.1.2 Observation process
for (t in 1:nyears){
y[t] ~ dnorm(Ntot[t], tau.obs) # all adults are counted
}
# 4.2 Likelihood for capture-recapture data: CJS model
# Multinomial likelihood
for (t in 1:(nyears-3)){
marray.j[t,1:nyears] ~ dmulti(pr.j[t,], r.j[t])
}
for (t in 1:(nyears-1)){
marray.a[t,1:nyears] ~ dmulti(pr.a[t,], r.a[t])
}
# m-array cell probabilities for juveniles
for (t in 1:(nyears-3)){
# Main diagonal
pr.j[t,(t+2)] <- phij[t]*p[t+2]
# Above main diagonal
for (j in (t+3):(nyears-1)){
pr.j[t,j] <- phij[t]*prod(phia[(t+3):j])*prod(q[(t+2):(j-1)])*p[j]
} #j
# Below main diagonal
for (j in 1:(t+1)){
pr.j[t,j] <- 0
} #j
# Last column
pr.j[t,nyears] <- 1-sum(pr.j[t,1:(nyears-1)])
} #t
# m-array cell probabilities for adults
for (t in 1:(nyears-1)){
q[t] <- 1-p[t]
# Main diagonal
pr.a[t,t] <- phia[t]*p[t]
# above main diagonal
for (j in (t+1):(nyears-1)){
pr.a[t,j] <- prod(phia[t:j])*prod(q[t:(j-1)])*p[j]
} #j
# Below main diagonal
for (j in 1:(t-1)){
pr.a[t,j] <- 0
} #j
# Last column
pr.a[t,nyears] <- 1-sum(pr.a[t,1:(nyears-1)])
} #t
# 4.3. Likelihood for productivity data: Poisson regression
for (t in 1:nyears) {
j1[t] ~ dpois(rho1[t]) # number young observed as fledged
rho1[t] <- f1[t] * R[t] # number of pairs and fecundity
j2[t] ~ dpois(rho2[t]) # number young fledged with nanotag
rho2[t] <- R_tag[t] * f2[t] # number tagged and fecundity
}
}
",fill = TRUE)
sink()
###################################################################
# Bundle data
jags.data <- list(znao_hur = znao_hur, nyears = nyears, marray.j = marray.j, marray.a = marray.a, r.j = rowSums(marray.j), r.a = rowSums(marray.a), y = y, j1 = j1, j2 = j2, R = R, R_tag = R_tag)
# Initial values
inits <- function(){list(mphi.juv = runif(0.1, 0.2, 0.4), mphi.ad = runif(1, 0.7, 0.9), fec1 = runif(1, 0, 2), fec2 = runif(1, 0, 2), res = runif(1, 0, 0.5), sig.phij = runif(1, 0.1, 5), sig.phia = runif(1, 0.1, 5),
sig.fec1 = runif(1, 0.1, 10), sig.fec2 = runif(1, 0.1, 10), sigma.obs = runif(1, 0, 1), beta.phia1 = rnorm(1,0,1))}
# Parameters monitored
parameters <- c("phij", "phia","f1", "f2", "lambda",
"mphij", "mphia","mfec1","mfec2", "mlam",
"l.mphij", "l.mphia","l.mfec1","l.mfec2", "p",
"sig.phij", "sig.phia", "sig.obs",
"N1", "Nad", "Ntot", "beta.phia1")
# MCMC settings
ni <- 500000
nt <- 10
nb <- 400000
nc <- 3
# Call JAGS from R
scf <- jags(jags.data, inits, parameters, "blte_ipm", n.chains = nc, n.thin = nt, n.iter = ni, n.burnin = nb, parallel = TRUE, store.data = TRUE)
# ~~~~ save output for use later ~~~~
save(scf, file="scf.Rdata")
#use MCMC vis to look at different credible intervals (80% and 95%)
scf
MCMCsummary(scf,
params = c("beta.phia1"),
probs = c(0.025, 0.05, 0.075, 0.1,0.25, 0.5, 0.75, 0.9, 0.925, 0.95, 0.975),
round = 2)
# BPVA ---------------------------------------------------------------------
###############################################################################################################################
## Bayesian population viability analysis with top-supported model covariates
#############################################################################################################################
###################################################
## Bootstrap method to obtain temporal variation
## in NAO values over time
##################################################
## Read in data
nao_all <- read.csv("Hist_NAO_Means.csv", stringsAsFactors = FALSE)
st.mean <- mean(nao_all$NAO_Jan_Jun_Mean[114:123])
lt.mean <- mean(nao_all$NAO_Jan_Jun_Mean[24:123])
## Bootstrap method for pulling 10 random values of historical NAO
## to calculate SD [temporal variability of NAO]
library(boot)
sdstats <- function(data, indices){
dat <- data[indices,] #allows boot to select sample
s1 <- sample(dat[,2], size=10, replace=FALSE)
sd.s <- sd(s1)
return(sd.s)
}
# bootstrapping wtih 1000 reps
set.seed(1234)
results <- boot(data=nao_all, statistic=sdstats,
R=100000)
# view results
results
mnao.sd <- mean(results$t) #mean of bootstrapped samples: 0.58
sdnao.sd <- sd(results$t) #sd of bootstrapped samples: 0.13
###################################################
# Combined mgmt scenario:
# increase adult survival 5%
# increase juv survival 10%
# double fecundity
##################################################
sink("blte_nao_bpva_all")
cat("
model {
#----------------------------------------
# 1. Define the priors for the parameters
#----------------------------------------
# Initial population sizes
for (t in 1:3){
n1[t] ~ dnorm(5, 0.1)I(0,) # New 3 year olds; needs prior for first 3 years
N1[t] <- round(n1[t])
Ntot[t] <- Nad[t] + N1[t] # Initial population size of adults in first 3 years is sum of new 3yos and returning adults
}#t
N0[1] ~ dpois(0.5 * f1[1] * Ntot[1]) # Initial pop size of fledglings
nadSurv ~ dnorm(300, 0.01)I(0,) # Adults in year 1
Nad[1] <- round(nadSurv)
# Mean demographic parameters (on appropriate scale)
mphi.juv ~ dbeta(3, 12)
mphi.ad ~ dbeta(13.5, 4)
l.mphij <- log(mphi.juv / (1-mphi.juv)) # Logit transformation
l.mphia <- log(mphi.ad / (1-mphi.ad)) # Logit transformation
fec1 ~ dunif(0,3) # productivity
l.mfec1 <- log(fec1) # Log transform
res ~ dunif(0,1) # mean detection probability
l.p <- log(res / (1-res)) # Logit transformation
# Priors for beta coefficients
beta.phia1 ~ dnorm(0, 0.01)
# Precision of standard deviations of temporal variability
sig.phij ~ dexp(1)
tau.phij <- pow(sig.phij, -2)
sig.phia ~ dexp(1)
tau.phia <- pow(sig.phia, -2)
sig.res ~ dunif(0, 10)
tau.res <- pow(sig.res, -2)
sig.obs ~ dunif(0.5, 50) # residual variance
tau.obs <- pow(sig.obs, -2)
sig.sig <- sdnao.sd #sdnao.sd is 0.13 from bootstrap results
tau.sig <- pow(sig.sig, -2)
sig.nao ~ dnorm(mnao.sd, tau.sig) #mnao.sd is 0.58 from bootstrap results
tau.nao <- pow(sig.nao, -2)
# Distribution of error terms (Bounded to help with convergence)
for (t in 1:(nyears-1+K)){
epsilon.phia[t] ~ dnorm(0, tau.phia)T(-5,5)
epsilon.res[t] ~ dnorm(0, tau.res)T(-5,5)
}
for (t in 1:(nyears-3+K)){
epsilon.phij[t] ~ dnorm(0, tau.phij)T(-5,5)
}
for (t in nyears:(nyears-1+K)){ #pull future NAO values from prior
nao.pre[t] ~ dnorm(st.mean,tau.nao) #use short term mean(st.mean) or long term mean (lt.mean) here
nao.fut[t] <- (nao.pre[t] - mnao)/sdnao
}
#---------------------------------------------
# 2. Constrain parameters (temp variability)
#---------------------------------------------
# Past
for (t in 1:(nyears-1)){
logit(phia[t]) <- l.mphia + beta.phia1 * znao_hur[t] + epsilon.phia[t] # epsilon.phia is random temporal effect for env. stoch.
}
# Future: use priors for NAO on phia and increase adult survival ~5% (0.85 --> 0.90)
for (t in nyears:(nyears-1+K)){
logit(phia[t]) <- log(0.9/(1-0.9)) + beta.phia1*nao.fut[t] + epsilon.phia[t] # Adult apparent survival (3+ y old survival)
}
#Past fecundity
for (t in 1:(nyears)){
log(f1[t]) <- l.mfec1
}
#Future fecundity
for (t in (nyears+1):(nyears+K)){
log(f1[t]) <- l.mfec1 + log(2)
}
#Past juv survival
for (t in 1:(nyears-3)){
logit(phij[t]) <- l.mphij + epsilon.phij[t]
}
#Future juv survival (increase mean juv survival by ~10% --> .08 to .18)
for (t in (nyears-2):(nyears-3+K)){
logit(phij[t]) <- log(0.18/(1-0.18)) + epsilon.phij[t]
}
for (t in 1:(nyears-1+K)){ # Same for past and future
logit(p[t]) <- l.p + epsilon.res[t]
}
#-----------------------
# 3. Derived parameters
#-----------------------
mphij <- exp(l.mphij)/(1+exp(l.mphij)) # Mean juvenile survival
mphia <- exp(l.mphia)/(1+exp(l.mphia)) # Mean adult survival
mfec1 <- exp(l.mfec1) # Mean productivity fledgling counts
#--------------------------------------------
# 4. The likelihoods of the single data sets
#--------------------------------------------
# 4.1. Likelihood for population count data (state-space model)
# 4.1.1 System process
for (k in 4:(nyears+K)){
N1[k] ~ dbin(phij[k-3], N0[k-3])
Ntot[k] <- N1[k] + Nad[k]
}#k
for (t in 2:(nyears+K)){
mean1[t] <- 0.5 * f1[t] * Ntot[t] # all adults can breed
N0[t] ~ dpois(mean1[t]) # Fledglings
Nad[t] ~ dbin(phia[t-1], Ntot[t-1])
}
# 4.1.2 Observation process
for (t in 1:nyears){
y[t] ~ dnorm(Ntot[t], tau.obs) # all adults are counted
}
# 4.2 Likelihood for capture-recapture data: CJS model
# Multinomial likelihood
for (t in 1:(nyears-3)){
marray.j[t,1:nyears] ~ dmulti(pr.j[t,], r.j[t])
}
for (t in 1:(nyears-1)){
marray.a[t,1:nyears] ~ dmulti(pr.a[t,], r.a[t])
}
# m-array cell probabilities for juveniles
for (t in 1:(nyears-3)){
# Main diagonal
pr.j[t,(t+2)] <- phij[t]*p[t+2]
# Above main diagonal
for (j in (t+3):(nyears-1)){
pr.j[t,j] <- phij[t]*prod(phia[(t+3):j])*prod(q[(t+2):(j-1)])*p[j]
} #j
# Below main diagonal
for (j in 1:(t+1)){
pr.j[t,j] <- 0
} #j
# Last column
pr.j[t,nyears] <- 1-sum(pr.j[t,1:(nyears-1)])
} #t
# m-array cell probabilities for adults
for (t in 1:(nyears-1)){
q[t] <- 1-p[t]
# Main diagonal
pr.a[t,t] <- phia[t]*p[t]
# above main diagonal
for (j in (t+1):(nyears-1)){
pr.a[t,j] <- prod(phia[t:j])*prod(q[t:(j-1)])*p[j]
} #j
# Below main diagonal
for (j in 1:(t-1)){
pr.a[t,j] <- 0
} #j
# Last column
pr.a[t,nyears] <- 1-sum(pr.a[t,1:(nyears-1)])
} #t
# 4.3. Likelihood for productivity data: Poisson regression
for (t in 1:nyears) {
j1[t] ~ dpois(rho1[t]) # number young observed as fledged
rho1[t] <- R[t] * f1[t] # number of pairs and fecundity
}
}
",fill = TRUE)
sink()
###################################################################
# Bundle data
K<-10
jags.data <- list(znao_hur = znao_hur, nyears = nyears, marray.j = marray.j,
marray.a = marray.a, r.j = rowSums(marray.j),
r.a = rowSums(marray.a), y = y, j1 = j1,
R = R, mnao.sd = mnao.sd, sdnao.sd = sdnao.sd,
mnao = mnao, sdnao=sdnao, K=K, st.mean = st.mean, lt.mean=lt.mean)
# Initial values
inits <- function(){list(mphi.juv = runif(0.1, 0.2, 0.4), mphi.ad = runif(1, 0.7, 0.9), fec1 = runif(1, 0, 2), res = runif(1, 0, 0.5), sig.phij = runif(1, 0.1, 5), sig.phia = runif(1, 0.1, 5),
sigma.obs = runif(1, 0, 1), beta.phia1 = rnorm(1,0,1))}
# Parameters monitored
parameters <- c("phij", "phia","f1", "lambda",
"mphij", "mphia","mfec1", "mlam.tot",
"mlam.hist", "l.mphij", "l.mphia","l.mfec1", "p",
"sig.phij", "sig.phia", "sig.fec","sig.obs",
"N1", "Nad", "Ntot", "beta.phia1",
"nao.fut", "nao.pre")
# MCMC settings
ni <- 500000
nt <- 10
nb <- 400000
nc <- 3
# Call JAGS from R
nao.st.all <- jags(jags.data, inits, parameters, "blte_nao_bpva_all", n.chains = nc, n.thin = nt, n.iter = ni, n.burnin = nb, parallel = TRUE, store.data = TRUE)
###################################################
# Adult survival mgmt scenario:
# increase adult survival 5%
##################################################
sink("blte_nao_bpva_phia")
cat("
model {
#----------------------------------------
# 1. Define the priors for the parameters
#----------------------------------------
# Initial population sizes
for (t in 1:3){
n1[t] ~ dnorm(5, 0.1)I(0,) # New 3 year olds; needs prior for first 3 years
N1[t] <- round(n1[t])
Ntot[t] <- Nad[t] + N1[t] # Initial population size of adults in first 3 years is sum of new 3yos and returning adults
}#t
N0[1] ~ dpois(0.5 * f1[1] * Ntot[1]) # Initial pop size of fledglings
nadSurv ~ dnorm(300, 0.01)I(0,) # Adults in year 1
Nad[1] <- round(nadSurv)
# Mean demographic parameters (on appropriate scale)
mphi.juv ~ dbeta(3, 12)
mphi.ad ~ dbeta(13.5, 4)
l.mphij <- log(mphi.juv / (1-mphi.juv)) # Logit transformation
l.mphia <- log(mphi.ad / (1-mphi.ad)) # Logit transformation
fec1 ~ dunif(0,3) # productivity
l.mfec1 <- log(fec1) # Log transform
res ~ dunif(0,1) # mean detection probability
l.p <- log(res / (1-res)) # Logit transformation
# Priors for beta coefficients
beta.phia1 ~ dnorm(0, 0.01)
# Precision of standard deviations of temporal variability
sig.phij ~ dexp(1)
tau.phij <- pow(sig.phij, -2)
sig.phia ~ dexp(1)
tau.phia <- pow(sig.phia, -2)
sig.res ~ dunif(0, 10)
tau.res <- pow(sig.res, -2)
sig.obs ~ dunif(0.5, 50) # residual variance
tau.obs <- pow(sig.obs, -2)
sig.sig <- sdnao.sd #sdnao.sd is 0.13 from bootstrap results
tau.sig <- pow(sig.sig, -2)
sig.nao ~ dnorm(mnao.sd, tau.sig) #mnao.sd is 0.58 from bootstrap results
tau.nao <- pow(sig.nao, -2)
# Distribution of error terms (Bounded to help with convergence)
for (t in 1:(nyears-1+K)){
epsilon.phia[t] ~ dnorm(0, tau.phia)T(-5,5)
epsilon.res[t] ~ dnorm(0, tau.res)T(-5,5)
}
for (t in 1:(nyears-3+K)){
epsilon.phij[t] ~ dnorm(0, tau.phij)T(-5,5)
}
for (t in nyears:(nyears-1+K)){ #pull future NAO values from prior
nao.pre[t] ~ dnorm(st.mean,tau.nao) #use short term mean(st.mean) or long term mean (lt.mean) here
nao.fut[t] <- (nao.pre[t] - mnao)/sdnao
}
#---------------------------------------------
# 2. Constrain parameters (temp variability)
#---------------------------------------------
# Past
for (t in 1:(nyears-1)){
logit(phia[t]) <- l.mphia + beta.phia1 * znao_hur[t] + epsilon.phia[t] # epsilon.phia is random temporal effect for env. stoch.
}
# Future: use priors for NAO on phia and increase adult survival ~5% (0.85 --> 0.90)
for (t in nyears:(nyears-1+K)){
logit(phia[t]) <- log(0.9/(1-0.9)) + beta.phia1*nao.fut[t] + epsilon.phia[t] # Adult apparent survival (3+ y old survival)
}
# Fecundity same for past and future
for (t in 1:(nyears+K)){
log(f1[t]) <- l.mfec1
}
#juv survival same for past and future
for (t in 1:(nyears-3+K)){
logit(phij[t]) <- l.mphij + epsilon.phij[t]
}
for (t in 1:(nyears-1+K)){ # Same for past and future
logit(p[t]) <- l.p + epsilon.res[t]
}
#-----------------------
# 3. Derived parameters
#-----------------------
mphij <- exp(l.mphij)/(1+exp(l.mphij)) # Mean juvenile survival
mphia <- exp(l.mphia)/(1+exp(l.mphia)) # Mean adult survival
mfec1 <- exp(l.mfec1) # Mean productivity fledgling counts
#--------------------------------------------
# 4. The likelihoods of the single data sets
#--------------------------------------------
# 4.1. Likelihood for population count data (state-space model)
# 4.1.1 System process
for (k in 4:(nyears+K)){
N1[k] ~ dbin(phij[k-3], N0[k-3])
Ntot[k] <- N1[k] + Nad[k]
}#k
for (t in 2:(nyears+K)){
mean1[t] <- 0.5 * f1[t] * Ntot[t] # all adults can breed
N0[t] ~ dpois(mean1[t]) # Fledglings
Nad[t] ~ dbin(phia[t-1], Ntot[t-1])
}
# 4.1.2 Observation process
for (t in 1:nyears){
y[t] ~ dnorm(Ntot[t], tau.obs) # all adults are counted
}
# 4.2 Likelihood for capture-recapture data: CJS model
# Multinomial likelihood
for (t in 1:(nyears-3)){
marray.j[t,1:nyears] ~ dmulti(pr.j[t,], r.j[t])
}
for (t in 1:(nyears-1)){
marray.a[t,1:nyears] ~ dmulti(pr.a[t,], r.a[t])
}
# m-array cell probabilities for juveniles
for (t in 1:(nyears-3)){
# Main diagonal
pr.j[t,(t+2)] <- phij[t]*p[t+2]
# Above main diagonal
for (j in (t+3):(nyears-1)){
pr.j[t,j] <- phij[t]*prod(phia[(t+3):j])*prod(q[(t+2):(j-1)])*p[j]
} #j
# Below main diagonal
for (j in 1:(t+1)){
pr.j[t,j] <- 0
} #j
# Last column
pr.j[t,nyears] <- 1-sum(pr.j[t,1:(nyears-1)])
} #t
# m-array cell probabilities for adults
for (t in 1:(nyears-1)){
q[t] <- 1-p[t]
# Main diagonal
pr.a[t,t] <- phia[t]*p[t]
# above main diagonal
for (j in (t+1):(nyears-1)){
pr.a[t,j] <- prod(phia[t:j])*prod(q[t:(j-1)])*p[j]
} #j
# Below main diagonal
for (j in 1:(t-1)){
pr.a[t,j] <- 0
} #j
# Last column
pr.a[t,nyears] <- 1-sum(pr.a[t,1:(nyears-1)])
} #t
# 4.3. Likelihood for productivity data: Poisson regression
for (t in 1:nyears) {
j1[t] ~ dpois(rho1[t]) # number young observed as fledged
rho1[t] <- R[t] * f1[t] # number of pairs and fecundity
}
}
",fill = TRUE)
sink()
###################################################################
###################################################################
# Bundle data
K<-10
jags.data <- list(znao_hur = znao_hur, nyears = nyears, marray.j = marray.j,
marray.a = marray.a, r.j = rowSums(marray.j),
r.a = rowSums(marray.a), y = y, j1 = j1,
R = R, mnao.sd = mnao.sd, sdnao.sd = sdnao.sd,
mnao = mnao, sdnao=sdnao, K=K, st.mean = st.mean, lt.mean=lt.mean)
# Initial values
inits <- function(){list(mphi.juv = runif(0.1, 0.2, 0.4), mphi.ad = runif(1, 0.7, 0.9), fec1 = runif(1, 0, 2), res = runif(1, 0, 0.5), sig.phij = runif(1, 0.1, 5), sig.phia = runif(1, 0.1, 5),
sigma.obs = runif(1, 0, 1), beta.phia1 = rnorm(1,0,1))}
# Parameters monitored
parameters <- c("phij", "phia","f1", "lambda",
"mphij", "mphia","mfec1", "mlam.tot",
"mlam.hist", "l.mphij", "l.mphia","l.mfec1", "p",
"sig.phij", "sig.phia", "sig.fec","sig.obs",
"N1", "Nad", "Ntot", "beta.phia1",
"nao.fut", "nao.pre")
# MCMC settings
ni <- 500000
nt <- 10
nb <- 400000
nc <- 3
nao.st.phia <- jags(jags.data, inits, parameters, "blte_nao_bpva_phia", n.chains = nc, n.thin = nt, n.iter = ni, n.burnin = nb, parallel = TRUE, store.data = TRUE)
###################################################
# Juvenile survival mgmt scenario:
# increase juv survival 10%
##################################################
sink("blte_nao_bpva_phij")
cat("
model {
#----------------------------------------
# 1. Define the priors for the parameters
#----------------------------------------
# Initial population sizes
for (t in 1:3){
n1[t] ~ dnorm(5, 0.1)I(0,) # New 3 year olds; needs prior for first 3 years
N1[t] <- round(n1[t])
Ntot[t] <- Nad[t] + N1[t] # Initial population size of adults in first 3 years is sum of new 3yos and returning adults
}#t
N0[1] ~ dpois(0.5 * f1[1] * Ntot[1]) # Initial pop size of fledglings
nadSurv ~ dnorm(300, 0.01)I(0,) # Adults in year 1
Nad[1] <- round(nadSurv)
# Mean demographic parameters (on appropriate scale)
mphi.juv ~ dbeta(3, 12)
mphi.ad ~ dbeta(13.5, 4)
l.mphij <- log(mphi.juv / (1-mphi.juv)) # Logit transformation
l.mphia <- log(mphi.ad / (1-mphi.ad)) # Logit transformation
fec1 ~ dunif(0,3) # productivity
l.mfec1 <- log(fec1) # Log transform
res ~ dunif(0,1) # mean detection probability
l.p <- log(res / (1-res)) # Logit transformation
# Priors for beta coefficients
beta.phia1 ~ dnorm(0, 0.01)
# Precision of standard deviations of temporal variability
sig.phij ~ dexp(1)
tau.phij <- pow(sig.phij, -2)
sig.phia ~ dexp(1)
tau.phia <- pow(sig.phia, -2)
sig.res ~ dunif(0, 10)
tau.res <- pow(sig.res, -2)
sig.obs ~ dunif(0.5, 50) # residual variance
tau.obs <- pow(sig.obs, -2)
sig.sig <- sdnao.sd #sdnao.sd is 0.13 from bootstrap results
tau.sig <- pow(sig.sig, -2)
sig.nao ~ dnorm(mnao.sd, tau.sig) #mnao.sd is 0.58 from bootstrap results
tau.nao <- pow(sig.nao, -2)
# Distribution of error terms (Bounded to help with convergence)
for (t in 1:(nyears-1+K)){
epsilon.phia[t] ~ dnorm(0, tau.phia)T(-5,5)
epsilon.res[t] ~ dnorm(0, tau.res)T(-5,5)
}
for (t in 1:(nyears-3+K)){
epsilon.phij[t] ~ dnorm(0, tau.phij)T(-5,5)
}
for (t in nyears:(nyears-1+K)){ #pull future NAO values from prior
nao.pre[t] ~ dnorm(st.mean,tau.nao) #use short term mean(st.mean) or long term mean (lt.mean) here
nao.fut[t] <- (nao.pre[t] - mnao)/sdnao
}
#---------------------------------------------
# 2. Constrain parameters (temp variability)
#---------------------------------------------
# Past adult survival
for (t in 1:(nyears-1)){
logit(phia[t]) <- l.mphia + beta.phia1 * znao_hur[t] + epsilon.phia[t] # epsilon.phia is random temporal effect for env. stoch.
}
# Future: use priors for NAO on phia
for (t in nyears:(nyears-1+K)){
logit(phia[t]) <- l.mphia + beta.phia1*nao.fut[t] + epsilon.phia[t] # Adult apparent survival (3+ y old survival)
}
# Fecundity same for past and future
for (t in 1:(nyears+K)){
log(f1[t]) <- l.mfec1
}