Created
February 23, 2022 00:55
-
-
Save ito4303/55eea5587fa46a2a3c888ee09f933122 to your computer and use it in GitHub Desktop.
Cormack-Jolly-Seber (CJS) model using nimbleEcology
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "R Notebook" | |
output: html_notebook | |
--- | |
# Cormack-Jolly-Seber (CJS) model | |
```{r setup} | |
library(AHMbook) | |
library(nimbleEcology) | |
``` | |
## Generation of simulated data | |
Simulated data are generated using the `simOcc` function of the `AHMbook` package. | |
- Number of occasions: 6 | |
- Number of marked individuals: 20 | |
- Apparent suvival probability : 0.7 | |
- Recapture probability: 0.4 | |
```{r gen_data} | |
set.seed(123) | |
cjs_data <- AHMbook::simCJS( | |
n.occ = 6, | |
n.marked = 20, | |
phi = 0.7, | |
p = 0.4, | |
show.plot = FALSE) | |
``` | |
## View data | |
Number of living individuals | |
```{r view_total} | |
cjs_data$n.alive | |
``` | |
Number of individuals captured or recaptured | |
```{r view_data1} | |
apply(cjs_data$ch, 2, sum) | |
``` | |
## Model | |
```{r model} | |
mc <- nimbleCode({ | |
for (n in 1:N) { | |
ch[n, F[n]:T] ~ dCJS_ss(phi, p, len = T - F[n] + 1) | |
} | |
# priors | |
phi ~ dunif(0, 1) | |
p ~ dunif(0, 1) | |
}) | |
``` | |
## Fitting | |
```{r fit} | |
nimble_const <- list(N = cjs_data$n.ind, | |
T = cjs_data$n.occ, | |
F = cjs_data$f) | |
nimble_data <- list(ch = cjs_data$ch) | |
nimble_init <- function() { | |
list(phi = runif(1, 0, 1), | |
p = runif(1, 0, 1))} | |
nimble_fit <- nimbleMCMC(code = mc, | |
constants = nimble_const, | |
data = nimble_data, | |
inits = nimble_init, | |
monitors = c("phi", "p"), | |
niter = 8000, | |
nburnin = 4000, | |
nchains = 3, | |
setSeed = 1:3, | |
summary = TRUE, | |
samples = TRUE) | |
``` | |
### Results | |
```{r results} | |
nimble_fit$summary | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment