-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod2.txt
327 lines (249 loc) · 11.9 KB
/
mod2.txt
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
model{
###########################################
### Spatial Dynamic Co-Occurrence Model ###
###########################################
# State 1= Unoccupied(U), State 2= rodent(A), State 3 = mustelid(B), State 4 = rodent & mustelid(AB)
###########################################
##############
## Priors ##
##############
beta0_gamA ~ dnorm(0,1)
beta0_gamAB ~ dnorm(0,1)
beta0_gamB ~ dnorm(0,1)
beta0_gamBA ~ dnorm(0,1)
beta0_epsA ~ dnorm(0,1)
beta0_epsAB ~ dnorm(0,1)
beta0_epsB ~ dnorm(0,1)
beta0_epsBA ~ dnorm(0,1)
beta1_gamA ~ dnorm(0,1)
beta1_gamAB ~ dnorm(0,1)
beta1_gamB ~ dnorm(0,1)
beta1_gamBA ~ dnorm(0,1)
beta1_epsA ~ dnorm(0,1)
beta1_epsAB ~ dnorm(0,1)
beta1_epsB ~ dnorm(0,1)
beta1_epsBA ~ dnorm(0,1)
beta0_GamA ~ dnorm(0,1)
beta0_GamAB ~ dnorm(0,1)
beta0_GamB ~ dnorm(0,1)
beta0_GamBA ~ dnorm(0,1)
beta0_EpsA ~ dnorm(0,1)
beta0_EpsAB ~ dnorm(0,1)
beta0_EpsB ~ dnorm(0,1)
beta0_EpsBA ~ dnorm(0,1)
# interscept det prob
alphaA0 ~ dnorm(0,1)
alphaB0 ~ dnorm(0,1)
# initial state parameters
for(b in 1:nblock){
for(i in 1:3){
psi[b,i] ~ dunif(0,0.5) # site
}
# for block, which is just a function of the states of the sites within each block
x[b,1] <- ifelse(sum(z[,b,1]==1) == nsite, 1,
ifelse(sum(z[,b,1]==2) + sum(z[,b,1]==1) == nsite, 2,
ifelse(sum(z[,b,1]==3) + sum(z[,b,1]==1) == nsite, 3, 4) ) )
#site
for(j in 1:nsite){
fsm[j, b, 1] <- 1-psi[b,1]-psi[b,2]-psi[b,3] #-----------|U
fsm[j, b, 2] <- psi[b,1] #-----------|A
fsm[j, b, 3] <- psi[b,2] #-----------|B
fsm[j, b, 4] <- psi[b,3] #-----------|AB
# first season latent state
# for sites
z[j, b, 1] ~ dcat( fsm[j, b, ( 1:nout )])
} #close site loop
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
###############################################
# btpm = block transition probability matrix. #
# All columns sum to 1. #
###############################################
for(t in 1:(nseason-1)){
# U to ...
btpm[b,t, 1, 1] <- (1-GamA[b,t]) * (1-GamB[b,t]) #--|U
btpm[b,t, 2, 1] <- GamA[b,t] * (1-GamB[b,t]) #--|A
btpm[b,t, 3, 1] <- (1-GamA[b,t]) * GamB[b,t] #--|B
btpm[b,t, 4, 1] <- GamA[b,t] * GamB[b,t] #--|AB
# A to ...
btpm[b,t, 1, 2] <- EpsA[b,t] * (1-GamBA[b,t]) #--|U
btpm[b,t, 2, 2] <- (1-EpsA[b,t]) * (1-GamBA[b,t]) #--|A
btpm[b,t, 3, 2] <- EpsA[b,t] * GamBA[b,t] #--|B
btpm[b,t, 4, 2] <- (1-EpsA[b,t]) * GamBA[b,t] #--|AB
# B to ...
btpm[b,t, 1, 3] <- (1-GamAB[b,t]) * EpsB[b,t] #--|U
btpm[b,t, 2, 3] <- GamAB[b,t] * EpsB[b,t] #--|A
btpm[b,t, 3, 3] <- (1-GamAB[b,t]) * (1-EpsB[b,t]) #--|B
btpm[b,t, 4, 3] <- GamAB[b,t] * (1-EpsB[b,t]) #--|AB
# AB to ..
btpm[b,t, 1, 4] <- EpsAB[b,t] * EpsBA[b,t] #--|U
btpm[b,t, 2, 4] <- (1-EpsAB[b,t]) * EpsBA[b,t] #--|A
btpm[b,t, 3, 4] <- EpsAB[b,t] * (1-EpsBA[b,t]) #--|B
btpm[b,t, 4, 4] <- (1-EpsAB[b,t]) * (1-EpsBA[b,t]) #--|AB
# latent block state for the rest of the seasons
x[b, t+1] ~ dcat(btpm[b, t, (1:nout), x[b, t]])
####################################################
## stpm = site transition probability matrix. ##
## These are dependent on the block level state ##
## All columns sum to 1. ##
####################################################
## Logit links for col and ext probabilities
logit(GamA[b,t]) <- beta0_GamA
logit(GamAB[b,t]) <- beta0_GamAB
logit(GamB[b,t]) <- beta0_GamB
logit(GamBA[b,t]) <- beta0_GamBA
logit(EpsA[b,t]) <- beta0_EpsA
logit(EpsAB[b,t]) <- beta0_EpsAB
logit(EpsB[b,t]) <- beta0_EpsB
logit(EpsBA[b,t]) <- beta0_EpsBA
for(j in 1:nsite){
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# logit links
logit(gamA[b, j, t]) <- beta0_gamA
logit(gamAB[b, j, t]) <- beta0_gamAB
logit(gamB[b, j, t]) <- beta0_gamB
logit(gamBA[b, j, t]) <- beta0_gamBA
logit(epsA[b, j, t]) <- beta0_epsA
logit(epsAB[b, j, t]) <- beta0_epsAB
logit(epsB[b, j, t]) <- beta0_epsB
logit(epsBA[b, j, t]) <- beta0_epsBA
# site transition matrix
# blocks state (x) = U
# U to ...
stpm[b, j, t, 1, 1, 1] <- 1 #--|U
stpm[b, j, t, 2, 1, 1] <- 0 #--|A
stpm[b, j, t, 3, 1, 1] <- 0 #--|B
stpm[b, j, t, 4, 1, 1] <- 0 #--|AB
# A to ...
stpm[b, j, t, 1, 2, 1] <- 1 #--|U
stpm[b, j, t, 2, 2, 1] <- 0 #--|A
stpm[b, j, t, 3, 2, 1] <- 0 #--|B
stpm[b, j, t, 4, 2, 1] <- 0 #--|AB
# B to ...
stpm[b, j, t, 1, 3, 1] <- 1 #--|U
stpm[b, j, t, 2, 3, 1] <- 0 #--|A
stpm[b, j, t, 3, 3, 1] <- 0 #--|B
stpm[b, j, t, 4, 3, 1] <- 0 #--|AB
# AB to ..
stpm[b, j, t, 1, 4, 1] <- 1 #--|U
stpm[b, j, t, 2, 4, 1] <- 0 #--|A
stpm[b, j, t, 3, 4, 1] <- 0 #--|B
stpm[b, j, t, 4, 4, 1] <- 0 #--|AB
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# blocks state (x) = A
# U to ...
stpm[b, j, t, 1, 1, 2] <- (1-gamA[b, j, t]) #--|U
stpm[b, j, t, 2, 1, 2] <- gamA[b, j, t] #--|A
stpm[b, j, t, 3, 1, 2] <- 0 #--|B
stpm[b, j, t, 4, 1, 2] <- 0 #--|AB
# A to ...
stpm[b, j, t, 1, 2, 2] <- epsA[b, j, t] #--|U
stpm[b, j, t, 2, 2, 2] <- (1-epsA[b, j, t]) #--|A
stpm[b, j, t, 3, 2, 2] <- 0 #--|B
stpm[b, j, t, 4, 2, 2] <- 0 #--|AB
# B to ...
stpm[b, j, t, 1, 3, 2] <- (1-gamAB[b, j, t]) #--|U
stpm[b, j, t, 2, 3, 2] <- gamAB[b, j, t] #--|A
stpm[b, j, t, 3, 3, 2] <- 0 #--|B
stpm[b, j, t, 4, 3, 2] <- 0 #--|AB
# AB to ..
stpm[b, j, t, 1, 4, 2] <- epsAB[b, j, t] #--|U
stpm[b, j, t, 2, 4, 2] <- (1-epsAB[b, j, t]) #--|A
stpm[b, j, t, 3, 4, 2] <- 0 #--|B
stpm[b, j, t, 4, 4, 2] <- 0 #--|AB
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# blocks state (x) = B
# U to ...
stpm[b, j, t, 1, 1, 3] <- (1-gamB[b, j, t]) #--|U
stpm[b, j, t, 2, 1, 3] <- 0 #--|A
stpm[b, j, t, 3, 1, 3] <- gamB[b, j, t] #--|B
stpm[b, j, t, 4, 1, 3] <- 0 #--|AB
# A to ...
stpm[b, j, t, 1, 2, 3] <- (1-gamBA[b, j, t]) #--|U
stpm[b, j, t, 2, 2, 3] <- 0 #--|A
stpm[b, j, t, 3, 2, 3] <- gamBA[b, j, t] #--|B
stpm[b, j, t, 4, 2, 3] <- 0 #--|AB
# B to ...
stpm[b, j, t, 1, 3, 3] <- epsB[b, j, t] #--|U
stpm[b, j, t, 2, 3, 3] <- 0 #--|A
stpm[b, j, t, 3, 3, 3] <- (1-epsB[b, j, t]) #--|B
stpm[b, j, t, 4, 3, 3] <- 0 #--|AB
# AB to ..
stpm[b, j, t, 1, 4, 3] <- epsBA[b, j, t] #--|U
stpm[b, j, t, 2, 4, 3] <- 0 #--|A
stpm[b, j, t, 3, 4, 3] <- (1-epsBA[b, j, t]) #--|B
stpm[b, j, t, 4, 4, 3] <- 0 #--|AB
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# blocks state (x) = AB
# U to ...
stpm[b, j, t, 1, 1, 4] <- (1-gamA[b, j, t]) * (1-gamB[b, j, t]) #--|U
stpm[b, j, t, 2, 1, 4] <- gamA[b, j, t] * (1-gamB[b, j, t]) #--|A
stpm[b, j, t, 3, 1, 4] <- (1-gamA[b, j, t]) * gamB[b, j, t] #--|B
stpm[b, j, t, 4, 1, 4] <- gamA[b, j, t] * gamB[b, j, t] #--|AB
# A to ...
stpm[b, j, t, 1, 2, 4] <- epsA[b, j, t] * (1-gamBA[b, j, t]) #--|U
stpm[b, j, t, 2, 2, 4] <- (1-epsA[b, j, t]) * (1-gamBA[b, j, t]) #--|A
stpm[b, j, t, 3, 2, 4] <- epsA[b, j, t] * gamBA[b, j, t] #--|B
stpm[b, j, t, 4, 2, 4] <- (1-epsA[b, j, t]) * gamBA[b, j, t] #--|AB
# B to ...
stpm[b, j, t, 1, 3, 4] <- (1-gamAB[b, j, t] ) * epsB[b, j, t] #--|U
stpm[b, j, t, 2, 3, 4] <- gamAB[b, j, t] * epsB[b, j, t] #--|A
stpm[b, j, t, 3, 3, 4] <- (1-gamAB[b, j, t] ) * (1-epsB[b, j, t]) #--|B
stpm[b, j, t, 4, 3, 4] <- gamAB[b, j, t] * (1-epsB[b, j, t]) #--|AB
# AB to ..
stpm[b, j, t, 1, 4, 4] <- epsAB[b, j, t] * epsBA[b, j, t] #--|U
stpm[b, j, t, 2, 4, 4] <- (1-epsAB[b, j, t]) * epsBA[b, j, t] #--|A
stpm[b, j, t, 3, 4, 4] <- epsAB[b, j, t] * (1-epsBA[b, j, t]) #--|B
stpm[b, j, t, 4, 4, 4] <- (1-epsAB[b, j, t]) * (1-epsBA[b, j, t]) #--|AB
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# latent site state for the rest of the seasons
z[j, b, t+1] ~ dcat( stpm[b, j, t, ( 1:nout ) , z[ j, b, t], x[b,t+1]] + 0.01) # +0.01 to avoide giving the dcat a prob of 0
for(day in 1:nsurvey) {
y[j, b, t, day] ~ dcat( dpm[b, j, t, ( 1:nout ) , z[j, b, t]] + 0.01) # +0.01 to avoide giving the dcat a prob of 0
} #end survey loop
#############################################################
## detection matrix (OS = observed state, TS = true state) ##
#############################################################
# TS = U
dpm[b, j, t, 1, 1] <- 1 #--|OS = U
dpm[b, j, t, 2, 1] <- 0 #--|OS = A
dpm[b, j, t, 3, 1] <- 0 #--|OS = B
dpm[b, j, t, 4, 1] <- 0 #--|OS = AB
# TS = A
dpm[b, j, t, 1, 2] <- 1-pA[b, j, t] #--|OS = U
dpm[b, j, t, 2, 2] <- pA[b, j, t] #--|OS = A
dpm[b, j, t, 3, 2] <- 0 #--|OS = B
dpm[b, j, t, 4, 2] <- 0 #--|OS = AB
# TS = B
dpm[b, j, t, 1, 3] <- 1-pB[b, j, t] #--|OS = U
dpm[b, j, t, 2, 3] <- 0 #--|OS = A
dpm[b, j, t, 3, 3] <- pB[b, j, t] #--|OS = B
dpm[b, j, t, 4, 3] <- 0 #--|OS = AB
# TS = AB
dpm[b, j, t, 1, 4] <- (1-pA[b, j, t]) * (1-pB[b, j, t]) #--|OS = U
dpm[b, j, t, 2, 4] <- pA[b, j, t] * (1-pB[b, j, t]) #--|OS = A
dpm[b, j, t, 3, 4] <- (1-pA[b, j, t]) * pB[b, j, t] #--|OS = B
dpm[b, j, t, 4, 4] <- pA[b, j, t] * pB[b, j, t] #--|OS = AB
## logit links for detection probs
logit(pA[b, j, t]) <- alphaA0
logit(pB[b, j, t]) <- alphaB0
} # end site loop
} # end time loop
} #close block loop
## Derived parameters
diff_gamA <- gamA[1, 1, 1] - gamAB[1, 1, 1]
diff_gamB <- gamB[1, 1, 1] - gamBA[1, 1, 1]
diff_epsA <- epsA[1, 1, 1] - epsAB[1, 1, 1]
diff_epsB <- epsB[1, 1, 1] - epsBA[1, 1, 1]
diff_GamA <- GamA[1,1] - GamAB[1,1]
diff_GamB <- GamB[1,1] - GamBA[1,1]
diff_EpsA <- EpsA[1,1] - EpsAB[1,1]
diff_EpsB <- EpsB[1,1] - EpsBA[1,1]
ratio_gamA <- gamA[1, 1, 1] / gamAB[1, 1, 1]
ratio_gamB <- gamB[1, 1, 1] / gamBA[1, 1, 1]
ratio_epsA <- epsAB[1, 1, 1] / epsA[1, 1, 1]
ratio_epsB <- epsB[1, 1, 1] / epsBA[1, 1, 1]
ratio_GamA <- GamA[1,1] / GamAB[1,1]
ratio_GamB <- GamBA[1,1] / GamB[1,1]
ratio_EpsA <- EpsAB[1,1] / EpsA[1,1]
ratio_EpsB <- EpsB[1,1] / EpsBA[1,1]
}# end