-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
executable file
·874 lines (766 loc) · 28.5 KB
/
index.Rmd
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
---
title: "Estimation of the Pyrenean brown bear population"
output:
html_document:
code_folding: show
df_print: paged
highlight: tango
number_sections: yes
theme: united
toc: yes
toc_depth: 2
pdf_document:
toc: yes
toc_depth: '2'
date: "November 2021"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE, message = FALSE, warning = FALSE, dpi = 600)
```
# Motivation
We estimate abundance of the Pyrenean brown bear population with data from France and Spain using robust-design capture-recapture models. We consider data from 2008 to 2020 considering the period May to September.
A nice introduction to robust design models can be found [here](http://www.phidot.org/software/mark/docs/book/pdf/chap15.pdf).
Here, we adopt a frequentist approach to explore effects on survival and detection and the temporary emigration structure. Analyses were performed using the `R` package `RMark` ([Laake 2013](http://www.afsc.noaa.gov/Publications/ProcRpt/PR2013-01.pdf)) that allows calling program `Mark` from `R`.
Load a few packages we will need.
```{r}
library(tidyverse)
theme_set(theme_light(base_size=16))
library(lubridate)
library(zoo)
library(RMark)
library(R2jags)
```
# Data preparation
Read in the data and inspect them:
```{r}
rawdata <- read_csv2("dat/databaseCMR2021.csv", col_names = FALSE)
rawdata
```
The columns have no name, we're gonna add some. Let's start by the obvious ones:
```{r}
dataprocessed <- rawdata %>%
rename(id = 'X1',
sex = 'X2',
birth_year = 'X3',
first_capture = 'X4',
age_first_capture = 'X5')
dataprocessed
```
Now we need the dates to name the remaining columns:
```{r}
dates <- seq(from = as.Date("2008/5/1"), to = as.Date("2020/9/1"), by = "month") %>%
enframe(name = NULL) %>% # make it a tibble
rename(date = value) %>% # rename column
mutate(month = month(date),
year = year(date)) %>% # select month and year
filter(month %in% c(5,6,7,8,9)) # filter May -> September
dates
```
Rename the columns, at last:
```{r}
dataprocessed <- dataprocessed %>%
rename_at(vars(starts_with('X')), ~paste0(dates$month, '/', dates$year)) %>%
mutate(id = as_factor(id),
sex = as_factor(sex))
dataprocessed
```
# Data exploration
First, we tidy the data:
```{r}
tidydata <- dataprocessed %>%
pivot_longer(-c(id,sex,birth_year,first_capture,age_first_capture), names_to = 'date', values_to = '(non-)detections')
tidydata
```
Now we may compute the number of detections per occasion:
```{r}
tidydata %>%
mutate(date = as.Date(as.yearmon(date, "%m/%Y")),
month = month(date),
year = year(date)) %>%
group_by(year,month) %>%
summarise(emr = sum(`(non-)detections`)) %>%
ggplot() +
geom_col(aes(x = as_factor(year), y = emr, fill = as_factor(month))) +
scale_fill_viridis_d(name = 'month') +
labs(x = 'year', y = "number of detections") +
coord_flip() +
theme(legend.position = "bottom")
```
How many individuals have been identified:
```{r}
tidydata %>%
pivot_wider(names_from = date, values_from = '(non-)detections') %>%
pull(id) %>%
length()
```
How many males, females and individuals of unknown sex do we have:
```{r}
tidydata %>%
pivot_wider(names_from = date, values_from = '(non-)detections') %>%
count(sex)
```
# Formating data for capture-recapture analyses
To format the data for capture-recapture analyses, we first double-check whether all individuals have at least a detection:
```{r}
dataprocessed <- dataprocessed %>%
mutate(sum = rowSums(across(contains("20"))), .before = id) %>%
filter(sum > 0)
```
We need to paste the columns of (non-)detections altogether, which can be achieved as follows:
```{r}
ch <- dataprocessed %>%
select(-c(sum, id,sex,birth_year,first_capture,age_first_capture)) %>%
unite(col = 'ch', sep= '')
ch
```
We now define the structure of the robust design. We have 13 primary occasions (the years, from 2008 to 2020) and 5 secondary occasions (from May to September). From the help file (see ?robust), we read that 'The 0 time intervals represent the secondary sessions in which the population is assumed to be closed. The non-zero values are the time intervals between the primary occasions.'. We therefore write:
```{r}
time.intervals <- c(0,0,0,0,1, # 2008
0,0,0,0,1, # 2009
0,0,0,0,1, # 2010
0,0,0,0,1, # 2011
0,0,0,0,1, # 2012
0,0,0,0,1, # 2013
0,0,0,0,1, # 2014
0,0,0,0,1, # 2015
0,0,0,0,1, # 2016
0,0,0,0,1, # 2017
0,0,0,0,1, # 2018
0,0,0,0,1, # 2019
0,0,0,0) # 2020
```
Because we want to have age effects on survival, we need to create an age variable bearing in mind that we have age at first capture. Cubs (< 2 year old) are coded 1, subadults are coded 2 (2 or 3 years old) and adults 3 (> 3 years old):
```{r}
ageclass <- dataprocessed %>%
mutate(aged = case_when(
age_first_capture < 2 ~ '1',
age_first_capture == 2 | age_first_capture == 3 ~ '2',
age_first_capture > 3 ~ '3')) %>%
pull(aged) %>%
as_factor() %>%
fct_inseq() # reorder factor by numeric value of level
ageclass
```
For individuals named `Balou`, `Bercero2013`, `Cachou`, `Gribouille`, `Melloux`, `New20_01`, `S28Slo3`, `ourson_Caramellita_2020_2` and `ourson_Caramellita_2020_3` we know the date of death, let's use this information and right censor them. First, locate them:
```{r}
dataprocessed %>%
mutate(id = as.character(id)) %>%
pull(id) -> id
mask <- which(id %in% c("Balou",
"Bercero2013",
"Cachou",
"Gribouille",
"Melloux",
"New20_01",
"S28Slo3",
"ourson_Caramellita_2020_2",
"ourson_Caramellita_2020_3"))
```
```{r}
freq <- rep(1, nrow(dataprocessed))
freq[mask] <- -1
```
Now put together the encounter histories and age class
```{r}
brownbear <- data.frame(ch = ch,
freq = freq,
age = ageclass)
brownbear
```
Initialize the number of capture occasions, the time intervals and create an age structure, for both standard robust design and robust design with heterogeneity:
```{r}
# standard RD
bear.process <- process.data(brownbear,
model = "Robust", # standard robust design
time.intervals = time.intervals, # primary/secondary occasions
groups = "age",
initial.age = c(0,2,4), # specifies age at first capture for each age class
begin.time = 2008)
# RD with heterogeneity
bear.process.mix <- process.data(brownbear,
model = "RDHet", # robust design with heterogeneity
time.intervals = time.intervals, # primary/secondary occasions
groups = "age",
initial.age = c(0,2,4), # specifies age at first capture for each age class
begin.time = 2008)
```
Create design matrix:
```{r}
bear.ddl <- make.design.data(bear.process)
bear.ddl.mix <- make.design.data(bear.process.mix)
```
Create a binned age variable in the design matrix (see, e.g., [here](http://www.phidot.org/forum/viewtopic.php?f=21&t=2591)):
```{r}
bear.ddl <- add.design.data(bear.process,
bear.ddl,
parameter = "S",
type = "age",
bins = c(0, 2, 4, 21),
name = "ageclass",
right = FALSE, # to get an interval closed on the left and open on the right
replace = TRUE)
# compare bear.ddl$S$age with bear.ddl$S$ageclass
bear.ddl.mix <- add.design.data(bear.process.mix,
bear.ddl.mix,
parameter = "S",
type = "age",
bins = c(0, 2, 4, 20),
name = "ageclass",
right = FALSE, # to get an interval closed on the left and open on the right
replace = TRUE)
```
Last, specify structure on parameters, namely we consider survival constant or age-dependent, detection constant, time-dependent (we consider variation between primary occasions and within primary occasions) or heterogeneous, and emigration Markovian, random or no emigration:
```{r}
S <- list(formula=~1) # survival is constant
S.age <- list(formula=~ageclass) # survival is age-dependent (3 age classes)
p <- list(formula=~1, share = TRUE) # detection is constant, share = TRUE is to force c and p to share same columns
p.time <- list(formula=~time, share = TRUE) # detection is time-varying where time is the occasions within primary occasions
p.session <- list(formula=~session, share = TRUE) # detection is time-varying where session is the primary occasion
p.mix <- list(formula=~mixture, share = TRUE) # detection is heterogeneous
GammaDoublePrimeNE <- list(formula=~1, share = TRUE, fixed = 0) # gamma' = gamma'' = 0, no emigration
GammaDoublePrimeMK <- list(formula=~1) # Markovian emigration
GammaPrimeMK <- list(formula=~1) # Markovian emigration
GammaDoublePrimeAL <- list(formula=~1, share = TRUE) # gamma' = gamma'', random emigration
```
# Robust-design capture-recapture analyses
Before starting the analyses, it doesn't hurt to have a look to the relevant help file:
```{r}
?robust
```
Now let's fit a bunch of models.
## No emigration
```{r}
modelSp_noemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S,
GammaDoublePrime = GammaDoublePrimeNE,
p = p),
threads = 2,
output = FALSE)
modelSpt_noemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S,
GammaDoublePrime = GammaDoublePrimeNE,
p = p.time),
threads = 2,
output = FALSE)
modelSpprimary_noemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S,
GammaDoublePrime = GammaDoublePrimeNE,
p = p.session),
threads = 2,
output = FALSE)
modelSph_noemig <- mark(bear.process.mix,
bear.ddl.mix,
model.parameters=list(S = S,
GammaDoublePrime = GammaDoublePrimeNE,
p = p.mix),
threads = 2,
output = FALSE)
modelSap_noemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S.age,
GammaDoublePrime = GammaDoublePrimeNE,
p = p),
threads = 2,
output = FALSE)
modelSapt_noemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S.age,
GammaDoublePrime = GammaDoublePrimeNE,
p = p.time),
threads = 2,
output = FALSE)
modelSapprimary_noemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S.age,
GammaDoublePrime = GammaDoublePrimeNE,
p = p.session),
threads = 2,
output = FALSE)
modelSaph_noemig <- mark(bear.process.mix,
bear.ddl.mix,
model.parameters=list(S = S.age,
GammaDoublePrime = GammaDoublePrimeNE,
p = p.mix),
threads = 2,
output = FALSE)
```
## Markovian emigration
```{r}
modelSp_mkemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S,
GammaPrime = GammaPrimeMK,
GammaDoublePrime = GammaDoublePrimeMK,
p = p),
threads = 2,
output = FALSE)
modelSpt_mkemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S=S,
GammaPrime = GammaPrimeMK,
GammaDoublePrime = GammaDoublePrimeMK,
p=p.time),
threads = 2,
output = FALSE)
modelSpprimary_mkemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S=S,
GammaPrime = GammaPrimeMK,
GammaDoublePrime = GammaDoublePrimeMK,
p=p.session),
threads = 2,
output = FALSE)
modelSph_mkemig <- mark(bear.process.mix,
bear.ddl.mix,
model.parameters=list(S=S,
GammaPrime = GammaPrimeMK,
GammaDoublePrime = GammaDoublePrimeMK,
p = p.mix),
threads = 2,
output = FALSE)
modelSap_mkemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S.age,
GammaPrime = GammaPrimeMK,
GammaDoublePrime = GammaDoublePrimeMK,
p = p),
threads = 2,
output = FALSE)
modelSapt_mkemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S.age,
GammaPrime = GammaPrimeMK,
GammaDoublePrime = GammaDoublePrimeMK,
p = p.time),
threads = 2,
output = FALSE)
modelSapprimary_mkemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S.age,
GammaPrime = GammaPrimeMK,
GammaDoublePrime = GammaDoublePrimeMK,
p = p.session),
threads = 2,
output = FALSE)
modelSaph_mkemig <- mark(bear.process.mix,
bear.ddl.mix,
model.parameters=list(S = S.age,
GammaPrime = GammaPrimeMK,
GammaDoublePrime = GammaDoublePrimeMK,
p = p.mix),
threads = 2,
output = FALSE)
```
## Random emigration
```{r}
modelSp_rdemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S,
GammaDoublePrime = GammaDoublePrimeAL,
p = p),
threads = 2,
output = FALSE)
modelSpt_rdemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S,
GammaDoublePrime = GammaDoublePrimeAL,
p = p.time),
threads = 2,
output = FALSE)
modelSpprimary_rdemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S,
GammaDoublePrime = GammaDoublePrimeAL,
p = p.session),
threads = 2,
output = FALSE)
modelSph_rdemig <- mark(bear.process.mix,
bear.ddl.mix,
model.parameters=list(S = S,
GammaDoublePrime = GammaDoublePrimeAL,
p = p.mix),
threads = 2,
output = FALSE)
modelSap_rdemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S.age,
GammaDoublePrime = GammaDoublePrimeAL,
p = p),
threads = 2,
output = FALSE)
modelSapt_rdemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S.age,
GammaDoublePrime = GammaDoublePrimeAL,
p = p.time),
threads = 2,
output = FALSE)
modelSapprimary_rdemig <- mark(bear.process,
bear.ddl,
model.parameters=list(S = S.age,
GammaDoublePrime = GammaDoublePrimeAL,
p = p.session),
threads = 2,
output = FALSE)
modelSaph_rdemig <- mark(bear.process.mix,
bear.ddl.mix,
model.parameters=list(S = S.age,
GammaDoublePrime = GammaDoublePrimeAL,
p = p.mix),
threads = 2,
output = FALSE)
```
## Model selection
Next step is to try and make sense of the models we fitted. We first collect them all:
```{r}
name_models <- c('modelSp_noemig',
'modelSpt_noemig',
'modelSpprimary_noemig',
'modelSph_noemig',
'modelSap_noemig',
'modelSapt_noemig',
'modelSapprimary_noemig',
'modelSaph_noemig',
'modelSp_mkemig',
'modelSpt_mkemig',
'modelSpprimary_mkemig',
'modelSph_mkemig',
'modelSap_mkemig',
'modelSapt_mkemig',
'modelSapprimary_mkemig',
'modelSaph_mkemig',
'modelSp_rdemig',
'modelSpt_rdemig',
'modelSpprimary_rdemig',
'modelSph_rdemig',
'modelSap_rdemig',
'modelSapt_rdemig',
'modelSapprimary_rdemig',
'modelSaph_rdemig')
AICcvalues <- rep(NA, length(name_models))
AICcvalues[1] <- eval(parse(text = paste0(name_models[1],'$results$AICc')))
for (i in 2:length(name_models)){
AICcvalues[i] <- eval(parse(text = paste0(name_models[i],'$results$AICc')))
}
ord <- order(AICcvalues)
model_table <- data.frame(model = name_models[ord], AICc = AICcvalues[ord])
model_table
```
We fitted 24 models in total, with 4 detection structures, 2 survival structures and 3 emigration structures. It appears that the models with age-dependent survival and heterogeneous detection are best supported by the data. There is emigration, but it is difficult to distinguish between random or Markovian emigration (the difference in AICc between the two top ranked models is lower than 2 units). There is no need to carry out model averaging because the AICc of these two models is much lower than the other models.
Let's inspect the parameter estimates of the two best models:
```{r}
modelSaph_rdemig$results$real[1:7,]
modelSaph_mkemig$results$real[1:8,]
```
The estimates of survival and detection probabilities are indistinguishable, which will make our life easier as we can rely on either models to get abundance estimates. More precisely, survival of cubs is around $84\%$, survival of subadults is $95\%$, and that of adults is $96\%$. Regarding the observation process, we have a mixture of lowly and highly detectable individuals. More precisely, we have a proportion $0.72$ of individuals with detection $42\%$ and a proportion $0.28$ of individuals with detection $85\%$.
Clean up:
```{r}
cleanup(ask = FALSE)
```
# Population size
## Formating data for capture-recapture analyses
Read again and format raw data.
```{r}
rawdata <- read_csv2("dat/databaseCMR2021.csv", col_names = FALSE)
dataprocessed <- rawdata %>%
rename(id = 'X1',
sex = 'X2',
birth_year = 'X3',
first_capture = 'X4',
age_first_capture = 'X5') %>%
mutate(sum = rowSums(across(starts_with("X"))), .before = id) %>%
filter(sum > 0) %>%
select(-sum)
dates <- seq(from = as.Date("2008/5/1"), to = as.Date("2020/9/1"), by = "month") %>%
enframe(name = NULL) %>% # make it a tibble
rename(date = value) %>% # rename column
mutate(month = month(date),
year = year(date)) %>% # select month and year
filter(month %in% c(5,6,7,8,9)) # filter May -> September
brownbear <- dataprocessed %>%
rename_at(vars(starts_with('X')), ~paste0(dates$month, '/', dates$year)) %>%
mutate(id = as_factor(id),
sex = as_factor(sex))
n.ind <- nrow(brownbear) # number of individuals
freq <- rep(1, n.ind)
brownbear %>%
mutate(id = as.character(id)) %>%
pull(id) -> id
mask <- which(id %in% c("Balou",
"Bercero2013",
"Cachou",
"Gribouille",
"Melloux",
"New20_01",
"S28Slo3",
"ourson_Caramellita_2020_2",
"ourson_Caramellita_2020_3"))
```
We compute several quantities that we will need:
```{r}
n.primary <- 13 # number of primary occasions
n.secondary <- rep(5, n.primary) # number of secondary occasions per primary occasion
index <- list(1:5,
6:10,
11:15,
16:20,
21:25,
26:30,
31:35,
36:40,
41:45,
46:50,
51:55,
56:60,
61:65) # the secondary occasions
```
We calculate the number of individuals caught in each primary occasion, which we will need to get an estimate of population size:
```{r}
encounter <- brownbear %>%
select(-c(id, sex, birth_year, first_capture, age_first_capture)) %>%
as.matrix()
caught <- rep(NA, n.primary)
for (i in 1:n.primary){
tmp <- encounter[,index[[i]]]
caught[i] <- nrow(tmp[rowSums(tmp)!=0,])
}
caught
```
We format the data as an array with dimensions the number of individuals times the number of primary occasions times the number of secondary occasions:
```{r}
obs <- array(NA, dim = c(n.ind, n.primary, max(n.secondary)))
for (i in 1:n.primary){
obs[,i,1:n.secondary[i]] <- encounter[,index[[i]]]
}
dim(obs)
```
Now we format the data as required in the Bayesian implementation of the robust design:
```{r}
ch <- matrix(NA, n.ind, n.primary)
for (i in 1:n.ind){
for (t in 1:n.primary){
ifelse(any(obs[i,t,1:n.secondary[t]] == 1), ch[i,t] <- 1, ch[i,t] <- 2)
}
}
```
Get first occasion of capture for each individual:
```{r}
get.first <- function(x)min(which (x != 2))
first <- apply(ch,1,get.first)
first[first == "Inf"] <- NA
```
Get last occasion of capture for each individual:
```{r}
#ch[mask,]
last <- rep(ncol(ch), nrow(ch))
last[mask] <- c(7, 6, 12, 13, 11, 13, 9, 13, 13)
```
Build an age matrix to be applied on survival:
```{r}
age <- matrix(NA, nrow = n.ind, ncol = n.primary - 1)
agefirst <- brownbear$age_first_capture
for (i in 1:n.ind){
tmp <- agefirst[i]
for (t in wrapr::seqi(first[i],n.primary-1)){
age[i,t] <- tmp
tmp <- tmp + 1
} #t
} #i
ageclass <- age
ageclass[age == 0 | age == 1] <- 1
ageclass[age == 2 | age == 3] <- 2
ageclass[age > 3] <- 3
head(ageclass)
```
```{r}
total <- matrix(NA, n.ind, n.primary)
for (i in 1:n.ind){
for (t in first[i]:n.primary){
total[i,t] <- sum(obs[i,t,1:n.secondary[t]])
}}
total[is.na(total)] <- 0
```
```{r}
avail <- array(NA, dim = c(n.ind, n.primary, max(n.secondary)))
for (i in 1:n.ind){
for (t in first[i]:n.primary){
for (j in 1:n.secondary[t]){
if(total[i,t] > 1){
avail[i,t,j] <- 1
}
if(total[i,t] == 1){
avail[i,t,j] <- 1
}
if(total[i,t] == 1 & obs[i,t,j] == 1){
avail[i,t,j] <- 0
obs[i,t,j] <- 0
}
}
}
}
avail[is.na(avail)] <- 0
```
Cut individuals released in last primary occasion:
```{r}
cut <- which(first != n.primary)
ch <- ch[c(cut),]
avail <- avail[c(cut),,]
obs <- obs[c(cut),,]
first <- first[c(cut)]
ageclass <- ageclass[c(cut),]
last <- last[c(cut)]
```
## Model fitting
We consider a capture-recapture model with robust design in which temporary emigration is random, survival is age-dependent survival and there is heterogeneity in the detection process (2-class finite mixture). This is the model best supported by the data, see part 1 of our analyses. We also right censored individuals for which date of death is known.
The code:
```{r}
model <- function() {
# priors
for (i in 1:n.ind){
for (t in first[i]:(n.years - 1)){
phi[i,t] <- beta[age[i,t]] # survival
}
}
for (u in 1:3){
beta[u] ~ dunif(0, 1) # Priors for age-specific survival
}
gamma ~ dunif(0,1) # gamma
mu0[1] ~ dunif(0,1)
mu0[2] ~ dunif(0,1)
mu[1:2] <- sort(mu0) # to handle label switching issue
prop ~ ddirich(alpha)
# secondary occasions p's
for (i in 1:n.ind){
eta[i] ~ dcat(prop[]) # indicator of whether you belong to class 1 or 2
for (t in 1:n.years){
for (j in 1:max(n.sec[1:n.years])){
p[i,t,j] <- mu[eta[i]] # detection is either mu1 or mu2
}
}
}
# primary occasions p's or pooled detection probability
for (i in 1:n.ind){
for (t in 1:n.years){
upstar[i,t] <- 1 - prod(1 - p[i,t,1:n.sec[t]])
}
}
# averaged detection over individuals
for (t in 1:n.years){
pstar[t] <- mean(upstar[1:n.ind,t])
}
# likelihood
for (i in 1:n.ind){
z[i,first[i]] <- ch[i,first[i]]
for (t in first[i]:last[i]){
for (j in 1:n.sec[t]){
mu3[i,t,j] <- avail[i,t,j] * p[i,t,j]
obs[i,t,j] ~ dbern(mu3[i,t,j])
}
}
for (t in (first[i]+1):last[i]){
mu1[i,t] <- z[i,t-1] * phi[i,t-1]
mu2[i,t] <- z[i,t] * (1 - gamma) * upstar[i,t]
z[i,t] ~ dbern(mu1[i,t])
ch[i,t] ~ dbern(mu2[i,t])
}
}
}
```
The data
```{r}
ch[ch == 2] <- 0 # Bernoulli likelihood
dat <- list(first = first,
last = last,
ch = ch,
n.sec = n.secondary,
n.years = ncol(ch),
n.ind = nrow(ch),
avail = avail,
obs = obs,
age = ageclass,
alpha = c(1,1))
```
Then initial values, parameters to monitor, MCMC settings:
```{r}
# initial values for the latent states:
z.init <- matrix(NA, nrow(ch), ncol(ch))
for (i in 1:nrow(ch)){
if(first[i] < last[i]){
z.init[i,(first[i] + 1):last[i]] <- 1
}
}
inits <- function(){list(z = z.init)}
# parameters
#pars <- c('pstar','mean.p','beta','gamma','sdeps')
pars <- c('pstar','mu','beta','gamma','prop')
n.chains <- 3
n.iter <- 20000
n.burnin <- 5000
```
We are ready to fit the model to the data:
```{r}
res_random <- jags(data = dat,
inits = inits,
parameters.to.save = pars,
model.file = model,
n.chains = n.chains,
n.iter = n.iter,
n.burnin = n.burnin)
```
Posterior density distribution of the parameters:
```{r out.width = "100%"}
jagsfit.mcmc <- as.mcmc(res_random)
library(lattice)
densityplot(jagsfit.mcmc)
```
Display the results:
```{r}
summary(jagsfit.mcmc)
```
## Get abundance
Provide posterior means for population size:
```{r}
Nmcmc <- matrix(NA, nrow(res_random$BUGSoutput$sims.list$pstar), n.primary)
for (i in 1:n.primary){
Nmcmc[,i] <- caught[i] / res_random$BUGSoutput$sims.list$pstar[,i]
}
Nmean <- apply(Nmcmc,2,mean)
N25 <- apply(Nmcmc,2,quantile,probs = 2.5/100)
N975 <- apply(Nmcmc,2,quantile,probs = 97.5/100)
```
Now compare the estimates with credible intervals to the counts with no correction for imperfect detection:
```{r}
res <- data.frame(year = 2008:2020,
Nhat = Nmean,
lwr = N25,
upr = N975,
counts = caught)
res
```
The abundance estimates are higher than the naive counts, which is what we expect as we correct for imperfect detection and individual heterogeneity in it (e.g. [Cubaynes et al. 2010](https://oliviergimenez.github.io/pubs/Cubaynesetal2010.pdf)).
Visually, we obtain:
```{r}
res %>%
ggplot(aes(year, Nhat)) +
geom_point(color = 'firebrick', size = 2) +
geom_line(color = 'firebrick', size = 0.5) +
geom_ribbon(aes(ymin = lwr,
ymax = upr),
alpha=0.3) +
scale_x_continuous(breaks = 2008:2020,
labels = 2008:2020) +
scale_y_continuous(breaks = seq(0,100,by=5),
labels = seq(0,100,by=5)) +
ylab("Estimated abundance") +
xlab("Year") +
labs(
title = 'Estimated abundance of Pyrenean brown bear',
subtitle = 'w/ a Bayesian robust design capture-recapture model')
```
# R version
```{r}
sessionInfo()
```