-
Notifications
You must be signed in to change notification settings - Fork 0
/
Marine birds simulation model code.R
213 lines (142 loc) · 4.26 KB
/
Marine birds simulation model code.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
#-----------#
#-Libraries-#
#-----------#
library(tidyverse)
library(nimble)
library(coda)
#-----------#
#-Functions-#
#-----------#
#Generate composition proportions
comp.fun <- function(nspecies)
{
pi <- runif(nspecies, 0, 1)
pi <- pi/sum(pi)
return(pi)
}
#Generate miss ID rates
missID.fun <- function(nspecies, nmissID)
{
phi.psi <- matrix(0, ncol = nspecies, nrow = nspecies)
phi.psi[sample(x = 1:nspecies^2, size = nmissID)] <- runif(nmissID, 0, 0.25)
diag(phi.psi) <- runif(nspecies, 0.8, 1)
phi.psi <- phi.psi/apply(phi.psi, MARGIN = 1, sum)
return(phi.psi)
}
#----------------#
#-Set parameters-#
#----------------#
#Number of species
nspecies <- 5
#Number of sites
nsites <- 500
#Community composition
pi <- comp.fun(nspecies)
#Community expected abundance
lambda.total <- runif(1, 10000, 20000)
#Species-specific abundance
lambda <- lambda.total * pi
alpha <- rnorm(nspecies, runif(1, 0.5, 1), 0.1)
#Difference in field-of-view
observer.offset <- runif(1,1,1.5)
alpha.OBS <- alpha * observer.offset
#Expected movement rate
E.alpha.OBS <- sum(pi * alpha.OBS)
#Expected miss ID rate
phi.psi <- missID.fun(nspecies, nmissID = 8)
#Detection probability
p <- runif(1, 0.25, 1)
#Derived product of movement and detection
E.epsilon <- E.alpha.OBS * p
#---------------#
#-Simulate data-#
#---------------#
#Front facing camera, point of view camera, latent correct ID abundance, latent miss ID abundance
FF <- N <- C <- matrix(NA, ncol = nspecies, nrow = nsites)
#Observer data
OBS <- array(NA, dim = c(nsites, nspecies, 2))
#Confusion matrix
confusion.matrix <- array(NA, dim = c(nsites, nspecies, nspecies))
for(j in 1:nsites){
#Front facing camera data
FF[j,] <- rpois(nspecies, lambda.total * pi)
#Latent abundance w/correct ID
N[j,] <- rpois(nspecies, lambda.total * pi * alpha.OBS)
for(i in 1:nspecies){
confusion.matrix[j,i,] <- rmultinom(1, N[j,i], phi.psi[i,])
}
#Latent abundance w/miss ID
C[j,] <- apply(confusion.matrix[j,,], MARGIN = 2, sum)
#Observer 1 data
OBS[j,,1] <- rbinom(n = nspecies, size = C[j,], prob = p)
#Observer 2 data
OBS[j,,2] <- rbinom(n = nspecies, size = C[j,], prob = p)
}
#--------------------#
#-Estimation Model-#
#--------------------#
code <- nimbleCode({
#-Priors-#
#Composition of latent abundance (corrected for imperfect detection)
phi[1:nspecies] ~ ddirch(phi.ones[1:nspecies])
#Derived product of movement and detection
E.epsilon ~ dnorm(0, 0.01)
#-Likelihood-#
for(j in 1:nsites){
#Front facing camera composition
FF[j,1:nspecies] ~ dmulti(pi[1:nspecies], FF.total[j])
#Front facing camera total abundance
FF.total[j] ~ dpois(lambda.total)
for(o in 1:nobs){
OBS[j,1:nspecies,o] ~ dmulti(phi[1:nspecies], OBS.total[j,o])
OBS.total[j,o] ~ dpois(lambda.total * E.epsilon)
}#end o
}#end j
for(i in 1:nspecies){
pi[i] <- lambda[i]/lambda.total
#psi.ones[i] <- 1
phi.ones[i] <- 1
log(lambda[i]) <- lambda0[i]
lambda0[i] ~ dnorm(0, 0.01)
correction[i] <- E.epsilon * phi[i]/pi[i]
}#end i
lambda.total <- sum(lambda[1:nspecies])
})
#-Compile data-#
data <- list(FF = FF,
FF.total = apply(FF, 1, sum),
OBS = OBS,
OBS.total = apply(OBS, c(1,3), sum)
)
constants <- list(nspecies = nspecies, nsites = nsites, nobs = 2)
#-Initial values-#
inits <- function(){list(pi = apply(FF/apply(FF, 1, sum), 2, mean),
E.epsilon = E.epsilon,
phi = apply(apply(OBS, c(1,2), max)/apply(apply(OBS, c(1,2), max), 1, sum), 2, mean)
)}
#-Parameters to save-#
params <- c(
"pi",
"phi",
"lambda.total",
"lambda",
"E.epsilon",
"correction"
)
#-MCMC settings-#
model <- nimbleModel(code = code,
constants = constants,
data = data,
inits = inits())
MCMCconf <- configureMCMC(model, monitors = params)
MCMC <- buildMCMC(MCMCconf)
compiled.model <- compileNimble(model, MCMC)
nc <- 3
ni <- 20000
nb <- 10000
nt <- 1
#-Run model-#
out2 <- runMCMC(compiled.model$MCMC,
niter = ni, nburnin = nb,
nchains = nc, thin = nt,
samplesAsCodaMCMC = TRUE)