-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappendix_paper_IHCMR_partB.Rmd
1415 lines (1210 loc) · 40 KB
/
appendix_paper_IHCMR_partB.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
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
---
title: 'Appendix B: Individual heterogeneity in capture-recapture models - Bayesian
approach using Jags'
output:
word_document: default
html_notebook: default
pdf_document: default
html_document: default
---
## Introduction
In this appendix, we introduce three methods to cope with individual heterogeneity in capture-recapture models, which we implement in a Bayesian framework using MCMC methods. First, we present multistate models in which heterogeneity is measured on individuals using states. Then, we illustrate models with individual random effects and finite mixtures that can help in dealing with hidden heterogeneity. We refer to the paper for a formal presentation of these models and a list of references using them. Throughout this appendix, we use R to simulate data and program Jags is called from R using package Rjags to fit models. We do our best to ensure reproducibility. Note that the frequentist approach can be used instead, and implemented either with program E-SURGE (appendix C) or program Mark (appendix A).
## Multistate models
In this section, we aim at illustrating how not accounting for individual heterogeneity may obscure the detection of life-history tradeoffs.
In details, we consider two states for the individuals of our fake population, non-breeding (NB) and breeding (B). To mimic
individual heterogeneity, we simulate a bunch of good individuals with survival $\phi_{NB}=0.7$ and $\phi_{B}=0.8$ and a bunch of bad individuals with survival $\phi_{NB}=0.7$ and $\phi_{B}=0.6$. Overall, the cost of breeding on survival should be detected only in bad individuals after accounting for individual heterogeneity through quality. For each group of bad vs. good individuals, we consider the same detection probability $p=0.9$, the same transition probabilities between breeding states $\psi_{NB,B}=0.8$ and $\psi_{B,NB}=0.3$, and 100 newly marked individuals for each group in each year of the 6-year study.
### Data simulation
Using R code from [Kéry and Schaub (2012)](http://www.vogelwarte.ch/de/projekte/publikationen/bpa/) book (chapter 9), we first define a function to simulate multistate capture-recapture data:
```{r}
# Define function to simulate multistate capture-recapture data
simul.ms <- function(PSI.STATE, PSI.OBS, marked, unobservable = NA){
# Unobservable: number of state that is unobservable
n.occasions <- dim(PSI.STATE)[4] + 1
CH <- CH.TRUE <- matrix(NA, ncol = n.occasions, nrow = sum(marked))
# Define a vector with the occasion of marking
mark.occ <- matrix(0, ncol = dim(PSI.STATE)[1], nrow = sum(marked))
g <- colSums(marked)
for (s in 1:dim(PSI.STATE)[1]){
if (g[s]==0) next # To avoid error message if nothing to replace
mark.occ[(cumsum(g[1:s])-g[s]+1)[s]:cumsum(g[1:s])[s],s] <-
rep(1:n.occasions, marked[1:n.occasions,s])
} #s
for (i in 1:sum(marked)){
for (s in 1:dim(PSI.STATE)[1]){
if (mark.occ[i,s]==0) next
first <- mark.occ[i,s]
CH[i,first] <- s
CH.TRUE[i,first] <- s
} #s
for (t in (first+1):n.occasions){
# Multinomial trials for state transitions
if (first==n.occasions) next
state <- which(rmultinom(1, 1, PSI.STATE[CH.TRUE[i,t-1],,i,t-1])==1)
CH.TRUE[i,t] <- state
# Multinomial trials for observation process
event <- which(rmultinom(1, 1, PSI.OBS[CH.TRUE[i,t],,i,t-1])==1)
CH[i,t] <- event
} #t
} #i
# Replace the NA and the highest state number (dead) in the file by 0
CH[is.na(CH)] <- 0
CH[CH==dim(PSI.STATE)[1]] <- 0
CH[CH==unobservable] <- 0
id <- numeric(0)
for (i in 1:dim(CH)[1]){
z <- min(which(CH[i,]!=0))
ifelse(z==dim(CH)[2], id <- c(id,i), id <- c(id))
}
return(list(CH=CH[-id,], CH.TRUE=CH.TRUE[-id,]))
# CH: capture histories to be used
# CH.TRUE: capture histories with perfect observation
}
```
Second, we use this function to simulate the two datasets of good and bad individuals:
```{r}
set.seed(1) # for reproducibility
p = 0.9
R = 100
#------------------------------
#---- good quality individuals
#------------------------------
# Define mean survival, transitions, recapture, as well as number of occasions, states, observations and released individuals
phiA <- 0.7
phiB <- 0.8
psiAB <- 0.8
psiBA <- 0.3
pA <- p
pB <- p
n.occasions <- 6
n.states <- 3
n.obs <- 3
marked <- matrix(NA, ncol = n.states, nrow = n.occasions)
marked[,1] <- rep(R, n.occasions)
marked[,2] <- rep(R, n.occasions)
marked[,3] <- rep(0, n.occasions)
# Define matrices with survival, transition and recapture probabilities
# 1. State process matrix
totrel <- sum(marked)*(n.occasions-1)
PSI.STATE <- array(NA, dim=c(n.states, n.states, totrel, n.occasions-1))
for (i in 1:totrel){
for (t in 1:(n.occasions-1)){
PSI.STATE[,,i,t] <- matrix(c(
phiA*(1-psiAB), phiA*psiAB, 1-phiA,
phiB*psiBA, phiB*(1-psiBA), 1-phiB,
0, 0, 1 ), nrow = n.states, byrow = TRUE)
} #t
} #i
# 2.Observation process matrix
PSI.OBS <- array(NA, dim=c(n.states, n.obs, totrel, n.occasions-1))
for (i in 1:totrel){
for (t in 1:(n.occasions-1)){
PSI.OBS[,,i,t] <- matrix(c(
pA, 0, 1-pA,
0, pB, 1-pB,
0, 0, 1 ), nrow = n.states, byrow = TRUE)
} #t
} #i
# Execute function
sim <- simul.ms(PSI.STATE, PSI.OBS, marked)
CH <- sim$CH
his1 = CH[!apply(CH,1,sum)==0,] # remove lines of 0s
#------------------------------
#---- bad quality individuals
#------------------------------
# Define mean survival, transitions, recapture, as well as number of occasions, states, observations and released individuals
phiA <- 0.7
phiB <- 0.6
psiAB <- 0.8
psiBA <- 0.3
pA <- p
pB <- p
n.occasions <- 6
n.states <- 3
n.obs <- 3
marked <- matrix(NA, ncol = n.states, nrow = n.occasions)
marked[,1] <- rep(R, n.occasions)
marked[,2] <- rep(R, n.occasions)
marked[,3] <- rep(0, n.occasions)
# Define matrices with survival, transition and recapture probabilities
# 1. State process matrix
totrel <- sum(marked)*(n.occasions-1)
PSI.STATE <- array(NA, dim=c(n.states, n.states, totrel, n.occasions-1))
for (i in 1:totrel){
for (t in 1:(n.occasions-1)){
PSI.STATE[,,i,t] <- matrix(c(
phiA*(1-psiAB), phiA*psiAB, 1-phiA,
phiB*psiBA, phiB*(1-psiBA), 1-phiB,
0, 0, 1 ), nrow = n.states, byrow = TRUE)
} #t
} #i
# 2.Observation process matrix
PSI.OBS <- array(NA, dim=c(n.states, n.obs, totrel, n.occasions-1))
for (i in 1:totrel){
for (t in 1:(n.occasions-1)){
PSI.OBS[,,i,t] <- matrix(c(
pA, 0, 1-pA,
0, pB, 1-pB,
0, 0, 1 ), nrow = n.states, byrow = TRUE)
} #t
} #i
# Execute function
sim <- simul.ms(PSI.STATE, PSI.OBS, marked)
CH <- sim$CH
his2 = CH[!apply(CH,1,sum)==0,] # remove lines of 0s
```
Last, we pool these two datasets together:
```{r}
his = rbind(his1,his2)
head(his) # display first lines
tail(his) # display last lines
```
### Model fitting
We adapted R code from [Kéry and Schaub (2012)](http://www.vogelwarte.ch/de/projekte/publikationen/bpa/) book (chapter 9) to fit multistate capture-recapture data.
Let us get the occasion of first capture for each individual:
```{r}
get.first <- function(x) min(which(x!=0))
f <- apply(his, 1, get.first)
f
```
Recode the data such that 1 = seen alive in A, 2 = seen alive in B, 3 = not seen:
```{r}
his_recoded <- his
his_recoded[his_recoded==0] <- 3
```
Now we fit a multistate model: we assume that survival depends on the breeding states,
transition probabilities are constant over time, as well as the detection probability:
```{r}
sink("state_on_survival.jags")
cat("
model {
# -------------------------------------------------
# Parameters:
# phiA: survival probability at site A
# phiB: survival probability at site B
# psiAB: movement probability from site A to site B
# psiBA: movement probability from site B to site A
# p: recapture probability at site A or B
# -------------------------------------------------
# States (S):
# 1 alive at A
# 2 alive at B
# 3 dead
# Observations (O):
# 1 seen at A
# 2 seen at B
# 3 not seen
# -------------------------------------------------
# Priors
phiA ~ dunif(0, 1)
phiB ~ dunif(0, 1)
psiAB ~ dunif(0, 1)
psiBA ~ dunif(0, 1)
p ~ dunif(0, 1)
# Define state-transition and observation matrices
for (i in 1:nind){
# Define probabilities of state S(t+1) given S(t)
for (t in f[i]:(n.occasions-1)){
ps[1,i,t,1] <- phiA * (1-psiAB)
ps[1,i,t,2] <- phiA * psiAB
ps[1,i,t,3] <- 1-phiA
ps[2,i,t,1] <- phiB * psiBA
ps[2,i,t,2] <- phiB * (1-psiBA)
ps[2,i,t,3] <- 1-phiB
ps[3,i,t,1] <- 0
ps[3,i,t,2] <- 0
ps[3,i,t,3] <- 1
# Define probabilities of O(t) given S(t)
po[1,i,t,1] <- p
po[1,i,t,2] <- 0
po[1,i,t,3] <- 1-p
po[2,i,t,1] <- 0
po[2,i,t,2] <- p
po[2,i,t,3] <- 1-p
po[3,i,t,1] <- 0
po[3,i,t,2] <- 0
po[3,i,t,3] <- 1
} #t
} #i
# Likelihood
for (i in 1:nind){
# Define latent state at first capture
z[i,f[i]] <- y[i,f[i]]
for (t in (f[i]+1):n.occasions){
# State process: draw S(t) given S(t-1)
z[i,t] ~ dcat(ps[z[i,t-1], i, t-1,])
# Observation process: draw O(t) given S(t)
y[i,t] ~ dcat(po[z[i,t], i, t-1,])
} #t
} #i
}
",fill = TRUE)
sink()
```
```{r}
# Function to create known latent states z
known.state.ms <- function(ms, notseen){
# notseen: label for ënot seení
state <- ms
state[state==notseen] <- NA
for (i in 1:dim(ms)[1]){
m <- min(which(!is.na(state[i,])))
state[i,m] <- NA
}
return(state)
}
# Function to create initial values for unknown z
ms.init.z <- function(ch, f){
for (i in 1:dim(ch)[1]){ch[i,1:f[i]] <- NA}
states <- max(ch, na.rm = TRUE)
known.states <- 1:(states-1)
v <- which(ch==states)
ch[-v] <- NA
ch[v] <- sample(known.states, length(v), replace = TRUE)
return(ch)
}
# Bundle data
jags.data <- list(y = his_recoded, f = f, n.occasions = dim(his_recoded)[2], nind = dim(his_recoded)[1], z = known.state.ms(his_recoded, 3))
# Initial values
inits <- function(){list(phiA = runif(1, 0, 1), psiAB = runif(1, 0, 1), p = runif(1, 0, 1), z = ms.init.z(his_recoded, f))}
# Parameters monitored
parameters <- c("phiA", "phiB", "psiAB", "psiBA", "p")
# MCMC settings
ni <- 5000
nb <- 2000
nc <- 2
# Call JAGS from R
library(R2jags)
ms <- jags(jags.data, inits, parameters, "state_on_survival.jags", n.chains = nc, n.iter = ni, n.burnin = nb)
print(ms, digits = 3)
```
Run same model without state effect on survival:
```{r}
sink("no_state_on_survival.jags")
cat("
model {
# -------------------------------------------------
# Parameters:
# phi: survival probability at site A or B
# psiAB: movement probability from site A to site B
# psiBA: movement probability from site B to site A
# p: recapture probability at site A or B
# -------------------------------------------------
# States (S):
# 1 alive at A
# 2 alive at B
# 3 dead
# Observations (O):
# 1 seen at A
# 2 seen at B
# 3 not seen
# -------------------------------------------------
# Priors
phi ~ dunif(0, 1)
psiAB ~ dunif(0, 1)
psiBA ~ dunif(0, 1)
p ~ dunif(0, 1)
# Define state-transition and observation matrices
for (i in 1:nind){
# Define probabilities of state S(t+1) given S(t)
for (t in f[i]:(n.occasions-1)){
ps[1,i,t,1] <- phi * (1-psiAB)
ps[1,i,t,2] <- phi * psiAB
ps[1,i,t,3] <- 1-phi
ps[2,i,t,1] <- phi * psiBA
ps[2,i,t,2] <- phi * (1-psiBA)
ps[2,i,t,3] <- 1-phi
ps[3,i,t,1] <- 0
ps[3,i,t,2] <- 0
ps[3,i,t,3] <- 1
# Define probabilities of O(t) given S(t)
po[1,i,t,1] <- p
po[1,i,t,2] <- 0
po[1,i,t,3] <- 1-p
po[2,i,t,1] <- 0
po[2,i,t,2] <- p
po[2,i,t,3] <- 1-p
po[3,i,t,1] <- 0
po[3,i,t,2] <- 0
po[3,i,t,3] <- 1
} #t
} #i
# Likelihood
for (i in 1:nind){
# Define latent state at first capture
z[i,f[i]] <- y[i,f[i]]
for (t in (f[i]+1):n.occasions){
# State process: draw S(t) given S(t-1)
z[i,t] ~ dcat(ps[z[i,t-1], i, t-1,])
# Observation process: draw O(t) given S(t)
y[i,t] ~ dcat(po[z[i,t], i, t-1,])
} #t
} #i
}
",fill = TRUE)
sink()
```
```{r}
# Function to create known latent states z
known.state.ms <- function(ms, notseen){
# notseen: label for ënot seení
state <- ms
state[state==notseen] <- NA
for (i in 1:dim(ms)[1]){
m <- min(which(!is.na(state[i,])))
state[i,m] <- NA
}
return(state)
}
# Function to create initial values for unknown z
ms.init.z <- function(ch, f){
for (i in 1:dim(ch)[1]){ch[i,1:f[i]] <- NA}
states <- max(ch, na.rm = TRUE)
known.states <- 1:(states-1)
v <- which(ch==states)
ch[-v] <- NA
ch[v] <- sample(known.states, length(v), replace = TRUE)
return(ch)
}
# Bundle data
jags.data <- list(y = his_recoded, f = f, n.occasions = dim(his_recoded)[2], nind = dim(his_recoded)[1], z = known.state.ms(his_recoded, 3))
# Initial values
inits <- function(){list(phi = runif(1, 0, 1), psiAB = runif(1, 0, 1), p = runif(1, 0, 1), z = ms.init.z(his_recoded, f))}
# Parameters monitored
parameters <- c("phi", "psiAB", "psiBA", "p")
# MCMC settings
ni <- 5000
nb <- 2000
nc <- 2
# Call JAGS from R
library(R2jags)
ms_without <- jags(jags.data, inits, parameters, "no_state_on_survival.jags", n.chains = nc, n.iter = ni, n.burnin = nb)
print(ms_without, digits = 3)
```
If you compare DIC values, it sounds like the difference in survival of breeding vs. non-breeding individuals is not detected.
Let's add individual heterogeneity through an individual covariate for bad vs. good individuals:
```{r}
quality=c(rep(0,nrow(his1)),rep(1,nrow(his2))) # 0 for good, 1 for bad
quality
```
Now we fit again the two models from above, including the effect of individual heterogeneity.
```{r}
sink("state_on_survival_withquality.jags")
cat("
model {
# -------------------------------------------------
# Parameters:
# alpha, beta: regression parameters for survival probability at site A/B
# psiAB: movement probability from site A to site B
# psiBA: movement probability from site B to site A
# p: recapture probability
# -------------------------------------------------
# States (S):
# 1 alive at A
# 2 alive at B
# 3 dead
# Observations (O):
# 1 seen at A
# 2 seen at B
# 3 not seen
# -------------------------------------------------
# Priors
alphaA ~ dnorm(0, 0.01)
betaA ~ dnorm(0, 0.01)
alphaB ~ dnorm(0, 0.01)
betaB ~ dnorm(0, 0.01)
for (i in 1:nind){
logit(phiA[i]) <- alphaA + betaA * quality[i]
logit(phiB[i]) <- alphaB + betaB * quality[i]}
psiAB ~ dunif(0, 1)
psiBA ~ dunif(0, 1)
p ~ dunif(0, 1)
# Define state-transition and observation matrices
for (i in 1:nind){
# Define probabilities of state S(t+1) given S(t)
for (t in f[i]:(n.occasions-1)){
ps[1,i,t,1] <- phiA[i] * (1-psiAB)
ps[1,i,t,2] <- phiA[i] * psiAB
ps[1,i,t,3] <- 1-phiA[i]
ps[2,i,t,1] <- phiB[i] * psiBA
ps[2,i,t,2] <- phiB[i] * (1-psiBA)
ps[2,i,t,3] <- 1-phiB[i]
ps[3,i,t,1] <- 0
ps[3,i,t,2] <- 0
ps[3,i,t,3] <- 1
# Define probabilities of O(t) given S(t)
po[1,i,t,1] <- p
po[1,i,t,2] <- 0
po[1,i,t,3] <- 1-p
po[2,i,t,1] <- 0
po[2,i,t,2] <- p
po[2,i,t,3] <- 1-p
po[3,i,t,1] <- 0
po[3,i,t,2] <- 0
po[3,i,t,3] <- 1
} #t
} #i
# Likelihood
for (i in 1:nind){
# Define latent state at first capture
z[i,f[i]] <- y[i,f[i]]
for (t in (f[i]+1):n.occasions){
# State process: draw S(t) given S(t-1)
z[i,t] ~ dcat(ps[z[i,t-1], i, t-1,])
# Observation process: draw O(t) given S(t)
y[i,t] ~ dcat(po[z[i,t], i, t-1,])
} #t
} #i
}
",fill = TRUE)
sink()
# Function to create known latent states z
known.state.ms <- function(ms, notseen){
# notseen: label for ënot seení
state <- ms
state[state==notseen] <- NA
for (i in 1:dim(ms)[1]){
m <- min(which(!is.na(state[i,])))
state[i,m] <- NA
}
return(state)
}
# Function to create initial values for unknown z
ms.init.z <- function(ch, f){
for (i in 1:dim(ch)[1]){ch[i,1:f[i]] <- NA}
states <- max(ch, na.rm = TRUE)
known.states <- 1:(states-1)
v <- which(ch==states)
ch[-v] <- NA
ch[v] <- sample(known.states, length(v), replace = TRUE)
return(ch)
}
# Bundle data
jags.data <- list(y = his_recoded, f = f, quality = quality, n.occasions = dim(his_recoded)[2], nind = dim(his_recoded)[1], z = known.state.ms(his_recoded, 3))
# Initial values
inits <- function(){list(alphaA = rnorm(1, 0, 1), betaA = rnorm(1, 0, 1), psiAB = runif(1, 0, 1), p = runif(1, 0, 1), z = ms.init.z(his_recoded, f))}
# Parameters monitored
parameters <- c("alphaA", "alphaB","betaA", "betaB", "psiAB", "psiBA", "p")
# MCMC settings
ni <- 5000
nb <- 2000
nc <- 2
# Call JAGS from R
library(R2jags)
ms_quality <- jags(jags.data, inits, parameters, "state_on_survival_withquality.jags", n.chains = nc, n.iter = ni, n.burnin = nb)
print(ms_quality, digits = 3)
```
Same model without state effect on survival:
```{r}
sink("no_state_on_survival_withquality.jags")
cat("
model {
# -------------------------------------------------
# Parameters:
# alpha, beta: regression parameters for survival probability at site A/B
# psiAB: movement probability from site A to site B
# psiBA: movement probability from site B to site A
# pA: recapture probability
# -------------------------------------------------
# States (S):
# 1 alive at A
# 2 alive at B
# 3 dead
# Observations (O):
# 1 seen at A
# 2 seen at B
# 3 not seen
# -------------------------------------------------
# Priors
alpha ~ dnorm(0, 0.01)
beta ~ dnorm(0, 0.01)
for (i in 1:nind){logit(phi[i]) <- alpha + beta * quality[i]}
psiAB ~ dunif(0, 1)
psiBA ~ dunif(0, 1)
p ~ dunif(0, 1)
# Define state-transition and observation matrices
for (i in 1:nind){
# Define probabilities of state S(t+1) given S(t)
for (t in f[i]:(n.occasions-1)){
ps[1,i,t,1] <- phi[i] * (1-psiAB)
ps[1,i,t,2] <- phi[i] * psiAB
ps[1,i,t,3] <- 1-phi[i]
ps[2,i,t,1] <- phi[i] * psiBA
ps[2,i,t,2] <- phi[i] * (1-psiBA)
ps[2,i,t,3] <- 1-phi[i]
ps[3,i,t,1] <- 0
ps[3,i,t,2] <- 0
ps[3,i,t,3] <- 1
# Define probabilities of O(t) given S(t)
po[1,i,t,1] <- p
po[1,i,t,2] <- 0
po[1,i,t,3] <- 1-p
po[2,i,t,1] <- 0
po[2,i,t,2] <- p
po[2,i,t,3] <- 1-p
po[3,i,t,1] <- 0
po[3,i,t,2] <- 0
po[3,i,t,3] <- 1
} #t
} #i
# Likelihood
for (i in 1:nind){
# Define latent state at first capture
z[i,f[i]] <- y[i,f[i]]
for (t in (f[i]+1):n.occasions){
# State process: draw S(t) given S(t-1)
z[i,t] ~ dcat(ps[z[i,t-1], i, t-1,])
# Observation process: draw O(t) given S(t)
y[i,t] ~ dcat(po[z[i,t], i, t-1,])
} #t
} #i
}
",fill = TRUE)
sink()
# Function to create known latent states z
known.state.ms <- function(ms, notseen){
# notseen: label for ënot seení
state <- ms
state[state==notseen] <- NA
for (i in 1:dim(ms)[1]){
m <- min(which(!is.na(state[i,])))
state[i,m] <- NA
}
return(state)
}
# Function to create initial values for unknown z
ms.init.z <- function(ch, f){
for (i in 1:dim(ch)[1]){ch[i,1:f[i]] <- NA}
states <- max(ch, na.rm = TRUE)
known.states <- 1:(states-1)
v <- which(ch==states)
ch[-v] <- NA
ch[v] <- sample(known.states, length(v), replace = TRUE)
return(ch)
}
# Bundle data
jags.data <- list(y = his_recoded, f = f, quality = quality, n.occasions = dim(his_recoded)[2], nind = dim(his_recoded)[1], z = known.state.ms(his_recoded, 3))
# Initial values
inits <- function(){list(alpha = rnorm(1, 0, 1), beta = rnorm(1, 0, 1), psiAB = runif(1, 0, 1), p = runif(1, 0, 1), z = ms.init.z(his_recoded, f))}
# Parameters monitored
parameters <- c("alpha","beta", "psiAB", "psiBA", "p")
# MCMC settings
ni <- 5000
nb <- 2000
nc <- 2
# Call JAGS from R
library(R2jags)
ms_quality_without <- jags(jags.data, inits, parameters, "no_state_on_survival_withquality.jags", n.chains = nc, n.iter = ni, n.burnin = nb)
print(ms_quality_without, digits = 3)
```
Clearly, the inclusion of quality improves the DIC. Also, the model with a difference in survival between breeders and non-breeders is better supported by the data when individual heterogeneity is accounted for.
# Models with individual random effects
Here, we aim at illustrating how not accounting for individual heterogeneity may obscure the detection of senescence in survival.
More specifically, we consider a single cohort of 500 individuals with survival decreasing as they age over a 20-year study.
We also add a frailty for each individual under the form of a normal distribution. Specifically, we specify $logit(\phi_i(a))=\beta_0+\beta_1 a + \varepsilon_i$ where $\varepsilon_i \sim N(0,\sigma^2)$. We use $\beta_0 = 1$, $\beta_1 = -0.05$ and $\sigma = 1$. If we condition upon the random effect, survival is decreasing as age increases. Note that we consider the same detection probability $p=0.5$ for all individuals.
### Data simulation
First, we simulate survival for each individual then plot the individual trajectories (in grey) as well as survival conditional on the random effect (in red):
```{r}
rm(list=ls())
r = set.seed(3) # for reproducibility
p = 0.5 # detection
intercept_phi = 1
slope_phi = -0.05
sigmaphi = 1
nind = 500 # nb of individuals
nyear = 20 # duration of the study
expit<-function(x){exp(x)/(1+exp(x))} # reciprocal logit function
z<-data<-x<-matrix(NA,nrow=nind,ncol=nyear)
first<-rep(1,nind)
age = matrix(NA,nind,nyear)
phi = matrix(NA,nind,nyear)
# simulate age-varying survival for each individual
for (i in 1:nind){
mask <- first[i]:nyear
age[i,mask] <- mask - first[i] + 1
phi[i,mask] <- expit(intercept_phi + slope_phi * age[i,mask] + rnorm(1,0,sigmaphi))
}
plot(age[1,],phi[1,],type='l',col='grey',ylim=c(0,1),xlab='age',ylab='estimated survival')
for (i in 2:nind){
lines(age[i,],phi[i,],type='l',col='grey')
}
lines(1:nyear,expit(intercept_phi + slope_phi * 1:nyear),col='red',lwd=2)
```
Now simulate the encounter histories:
```{r}
for(i in 1:nind){
z[i,first[i]] <- x[i,first[i]] <- 1
for(j in (first[i]+1):nyear){
z[i,j]<-rbinom(1,1,phi[i,j-1]*z[i,j-1])
x[i,j]<-rbinom(1,1,z[i,j]*p)
}
}
his = x
his[is.na(his)]=0 # remove lines with 0's
```
### Model fitting
We fit the model with an age effect but no individual heterogeneity to the simulated dataset:
```{r}
sink("cjs_age.jags")
cat("
model {
# Priors and constraints
for (i in 1:nind){
for (t in f[i]:(n.occasions-1)){
logit(phi[i,t]) <- alpha + beta * t # a single cohort is used here, so that age = time elapsed since first capture = time
p[i,t] <- mean.p
} #t
} #i
alpha ~ dnorm(0, 0.01) # Prior for intercept of age effect
beta ~ dnorm(0, 0.01) # Prior for slope of age effect
mean.p ~ dunif(0, 1) # Prior for mean recapture
# Likelihood
for (i in 1:nind){
# Define latent state at first capture
z[i,f[i]] <- 1
for (t in (f[i]+1):n.occasions){
# State process
z[i,t] ~ dbern(mu1[i,t])
mu1[i,t] <- phi[i,t-1] * z[i,t-1]
# Observation process
y[i,t] ~ dbern(mu2[i,t])
mu2[i,t] <- p[i,t-1] * z[i,t]
} #t
} #i
}
",fill = TRUE)
sink()
# Bundle data
known.state.cjs <- function(ch){
state <- ch
for (i in 1:dim(ch)[1]){
n1 <- min(which(ch[i,]==1))
n2 <- max(which(ch[i,]==1))
state[i,n1:n2] <- 1
state[i,n1] <- NA
}
state[state==0] <- NA
return(state)
}
jags.data <- list(y = his, f = first, nind = dim(his)[1], n.occasions = dim(his)[2], z = known.state.cjs(his))
# Initial values
cjs.init.z <- function(ch,f){
for (i in 1:dim(ch)[1]){
if (sum(ch[i,])==1) next
n2 <- max(which(ch[i,]==1))
ch[i,f[i]:n2] <- NA
}
for (i in 1:dim(ch)[1]){
ch[i,1:f[i]] <- NA
}
return(ch)
}
inits <- function(){list(z = cjs.init.z(his, first), alpha = rnorm(1, 0, 1), mean.p = runif(1, 0, 1), beta = rnorm(1, 0, 1))}
# Parameters monitored
parameters <- c("alpha", "beta", "mean.p")
# MCMC settings
ni <- 5000
nb <- 2000
nc <- 2
# Call JAGS from R
library(R2jags)
cjs_age <- jags(jags.data, inits, parameters, "cjs_age.jags", n.chains = nc, n.iter = ni, n.burnin = nb)
# Summarize posteriors
print(cjs_age, digits = 3)
```
Having a look to the parameter estimates, it sounds like the slope of the age effect on survival is estimated positive...
Which means that at the population level, whenever individual heterogeneity is ignored, then senescence (in red) is completely masked. Even worse, survival is increasing with increasing age (in blue).
```{r}
plot(age[1,],phi[1,],type='l',col='grey',ylim=c(0,1),xlab='age',ylab='estimated survival')
for (i in 2:nind){
lines(age[i,],phi[i,],type='l',col='grey')
}
lines(1:nyear,expit(intercept_phi + slope_phi * 1:nyear),col='red',lwd=2)
lines(1:nyear,expit(mean(cjs_age$BUGSoutput$sims.matrix[,'alpha']) + mean(cjs_age$BUGSoutput$sims.matrix[,'beta']) * 1:nyear),col='blue',lwd=2)
```
Now we fit the model with a random effect in the survival process.
```{r}
sink("cjs_age_re.jags")
cat("
model {
# Priors and constraints
for (i in 1:nind){
for (t in f[i]:(n.occasions-1)){
logit(phi[i,t]) <- alpha + beta * t + epsilon[i] # a single cohort is used here, so that age = time elapsed since first capture = time
p[i,t] <- mean.p
} #t
} #i
for (i in 1:nind){
epsilon[i] ~ dnorm(0, tau)
}
alpha ~ dnorm(0, 0.01) # Prior for intercept of age effect
beta ~ dnorm(0, 0.01) # Prior for slope of age effect
sigma ~ dunif(0, 5) # Prior for standard deviation
tau <- pow(sigma, -2)
sigma2 <- pow(sigma, 2)
mean.p ~ dunif(0, 1) # Prior for mean recapture
# Likelihood
for (i in 1:nind){
# Define latent state at first capture
z[i,f[i]] <- 1
for (t in (f[i]+1):n.occasions){
# State process
z[i,t] ~ dbern(mu1[i,t])
mu1[i,t] <- phi[i,t-1] * z[i,t-1]
# Observation process
y[i,t] ~ dbern(mu2[i,t])
mu2[i,t] <- p[i,t-1] * z[i,t]
} #t
} #i
}
",fill = TRUE)
sink()
# Bundle data
jags.data <- list(y = his, f = first, nind = dim(his)[1], n.occasions = dim(his)[2], z = known.state.cjs(his))
# Initial values
inits <- function(){list(z = cjs.init.z(his, first), alpha = rnorm(1, 0, 1), beta = rnorm(1, 0, 1),mean.p = runif(1, 0, 1), sigma = runif(1, 0, 2))}
# Parameters monitored
parameters <- c("alpha", "beta", "mean.p", "sigma2")
# MCMC settings
ni <- 5000
nb <- 2000
nc <- 2
# Call JAGS from R
cjs_age_re <- jags(jags.data, inits, parameters, "cjs_age_re.jags", n.chains = nc, n.iter = ni, n.burnin = nb)
# Summarize posteriors
print(cjs_age_re, digits = 3)
```
The intercept and slope of the age-survival relationship are quite close to the values we used to simulate the data.
Now we add to our previous plot the survival as estimated when individual heterogeneity is explicitely accounted for using individual random effects (in green):
```{r}
plot(age[1,],phi[1,],type='l',col='grey',ylim=c(0,1),xlab='age',ylab='estimated survival')
for (i in 2:nind){
lines(age[i,],phi[i,],type='l',col='grey')
}
lines(1:nyear,expit(intercept_phi + slope_phi * 1:nyear),col='red',lwd=2)
lines(1:nyear,expit(mean(cjs_age$BUGSoutput$sims.matrix[,'alpha']) + mean(cjs_age$BUGSoutput$sims.matrix[,'beta']) * 1:nyear),col='blue',lwd=2)
lines(1:nyear,expit(mean(cjs_age_re$BUGSoutput$sims.matrix[,'alpha']) + mean(cjs_age_re$BUGSoutput$sims.matrix[,'beta']) * 1:nyear),col='green',lwd=2)
```
## Models with finite mixtures
Here, we again aim at illustrating how not accounting for individual heterogeneity may obscure the detection of senescence in survival. In contrast with the previous section, we now use finite mixtures to deal with heterogeneity. More specifically, we consider a cohort of 1000 individuals that are split into a group of robust individuals in proportion $\pi$ with constant high survival $\phi_R$ and a group of frail individuals with survival $\phi_F$ that senesce over the 20 years of the study according to the relationship $logit(\phi_F(a))=\beta_0+\beta_1 a$. We use $\pi = 0.3$, $\phi_R = 0.85$, $\beta_0 = 0$ and $\beta_1 = -0.07$. Note that we consider the same detection probability $p=0.5$ for all individuals.
### Data simulation
First simulate data
```{r}
rm(list=ls())
r = set.seed(3) # for reproducibility
p = 0.5 # detection
prop_class1 = 0.3 # pi
phi_class1 = 0.85 # survival or robust ind
intercept_phi_class2 = 0 #beta_0
slope_phi_class2 = -0.05 # beta_1
nind = 1000 # nb of ind
nyear = 20 # duration of the study
expit<-function(x){exp(x)/(1+exp(x))} # reciprocal of the logit function
z<-data<-x<-matrix(NA,nrow=nind,ncol=nyear)
first<-rep(1,nind)
age = matrix(NA,nind,nyear)
phi = matrix(NA,nind,nyear)
which_mixture = rep(NA,nind)
# simulate age-varying survival for each individual,
# by first assigning them to the robust or frail class, then using the corresponding
# survival
for (i in 1:nind){
mask <- first[i]:nyear
age[i,mask] <- mask - first[i] + 1
which_mixture[i] <- rbinom(1,1,prop_class1) # assign ind i to a class with prob pi
if (which_mixture[i] == 1){
phi[i,mask] <- phi_class1 # robust
} else {
phi[i,mask] <- expit(intercept_phi_class2 + slope_phi_class2 * age[i,mask])} # frail
}
```
Represent graphically survival over time in the two classes:
```{r}
plot(age[1,],phi[1,],type='l',col='grey',ylim=c(0,1),xlab='age',ylab='estimated survival')
for (i in 2:nind){
lines(age[i,],phi[i,],type='l',col='grey')
}
```
Now simulate the encounter histories:
```{r}
for(i in 1:nind){
z[i,first[i]] <- x[i,first[i]] <- 1
for(j in (first[i]+1):nyear){
z[i,j]<-rbinom(1,1,phi[i,j-1]*z[i,j-1])
x[i,j]<-rbinom(1,1,z[i,j]*p)
}
}
his = x
his[is.na(his)]=0
```
### Model fitting
Let's fit two models assuming homogeneity, first one with constant survival probability, second one with an age effect:
```{r}
sink("Mct.jags")
cat("
model
{
# notation used
# STATES
# V for alive
# D for dead
# OBSERVATIONS
# 0 = non-observed (coded 1)
# 1 = observed (coded 2)
# prior on survival for V1 and V2
phi ~ dunif(0,1)
# prior on detection
p ~ dunif(0,1)
# probabilities for each initial state
px0[1] <- 1 # prob. of being in initial state V
px0[2] <- 0 # prob. of being in initial state D
# define probabilities of observations at t given states at t
po[1,1] <- 1-p
po[1,2] <- p
po[2,1] <- 1
po[2,2] <- 0
po.init[1,1] <- 0
po.init[1,2] <- 1
po.init[2,1] <- 1
po.init[2,2] <- 0
# define probabilities of states at t given states at t-1
px[1,1] <- phi
px[1,2] <- 1-phi
px[2,1] <- 0
px[2,2] <- 1
for (i in 1:nind){ # for each indiv
# initial states
z[i,first[i]] ~ dcat(px0[1:2])