-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.SMR.Dcov.Generalized.Mb.R
More file actions
249 lines (231 loc) · 8.57 KB
/
init.SMR.Dcov.Generalized.Mb.R
File metadata and controls
249 lines (231 loc) · 8.57 KB
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
e2dist <- function (x, y){
i <- sort(rep(1:nrow(y), nrow(x)))
dvec <- sqrt((x[, 1] - y[i, 1])^2 + (x[, 2] - y[i, 2])^2)
matrix(dvec, nrow = nrow(x), ncol = nrow(y), byrow = F)
}
init.SMR.Dcov.Generalized.Mb <- function(data,inits=NA,M=NA){
library(abind)
#extract observed data
y.mark <- data$y.mark
y.mID <- data$y.mID #marked detections
y.mnoID <- data$y.mnoID #marked with no ID samples
y.um <- data$y.um #unmarked samples
y.unk <- data$y.unk #unknown marked status samples
n.marked <- data$n.marked
X.mark <- as.matrix(data$X.mark)
J.mark <- nrow(X.mark)
K.mark <- data$K.mark
K2D.mark <- data$K2D.mark
X.sight <- as.matrix(data$X.sight)
J.sight <- nrow(X.sight)
K.sight <- data$K.sight
K1D.sight <- data$K1D.sight
locs <- data$locs
xlim <- data$xlim
ylim <- data$ylim
##pull out initial values
p0.p <- inits$p0.p
p0.c <- inits$p0.c
lam0 <- inits$lam0
sigma <- inits$sigma
#augment marking process capture history
y.aug <- array(0,dim=c(M,J.mark,K.mark))
y.aug[1:n.marked,,] <- y.mark
y.mark <- y.aug
#restructure data into first and subsequent capture structures
y.mark.p <- y.mark.c <- K1D.mark.p <- K1D.mark.c <- matrix(0,M,J.mark)
for(i in 1:M){
for(j in 1:J.mark){
position <- Position(y.mark[i,j,],f=function(x){x>0})#occasion of first capture, NA if no capture
if(is.na(position)){#no capture
K1D.mark.p[i,j] <- sum(K2D.mark[j,1:K.mark]) #sum trap op
K1D.mark.c[i,j] <- 0 #no subsequent capture events
}else{#there is a first capture
K1D.mark.p[i,j] <- sum(K2D.mark[j,1:position]) #sum trap op up to first capture event
if(position<K.mark){#was first capture not last occasion?
K1D.mark.c[i,j] <- sum(K2D.mark[j,(position+1):K.mark]) #sum trap op after first capture event for subsequent capture
}else{#otherwise, no subsequent capture events
K1D.mark.c[i,j] <- 0
}
y.mark.p[i,j] <- y.mark[i,j,position]
}
}
}
y.mark2D <- apply(y.mark,c(1,2),sum)
y.mark.c <- y.mark2D-y.mark.p
#I have not tested that the algorithm above is correct in all cases of trap operation entered.
##Check for bugs here##
#I've never seen errors in this algorithm when the data is properly formatted. Errors may just be formatting errors.
for(i in 1:M){
for(j in 1:J.mark){
y.check <- y.mark[i,j,]
if(sum(y.check)>0){
if(y.mark.p[i,j]!=1)stop("bug in y1")
if(y.mark.c[i,j]!=(sum(y.check)-1))stop("bug in y2")
first.cap.k.on <- which(y.check[K2D.mark[j,]==1]==1)[1] #first capture occasion counting only operable occasions
if(K1D.mark.p[i,j]!=first.cap.k.on)stop("bug in K1D.p")
if(K1D.mark.c[i,j]!=(sum(K2D.mark[j,1:K.mark])-first.cap.k.on))stop("bug in K1D.c")
}else{
if(y.mark.p[i,j]!=0)stop("bug in y1")
if(y.mark.c[i,j]!=0)stop("bug in y2")
if(K1D.mark.p[i,j]!=sum(K2D.mark[j,1:K.mark]))stop("bug in K1D.p")
if(K1D.mark.c[i,j]!=0)stop("bug in K1D.c")
}
}
}
#assign random locations to assign latent ID samples to individuals
s.init <- cbind(runif(M,xlim[1],xlim[2]), runif(M,ylim[1],ylim[2]))
#but update s.inits for marked individuals before assigning latent detections
y.both <- cbind(y.mark2D[1:n.marked,],y.mID)
X.both <- rbind(X.mark,X.sight)
idx <- which(rowSums(y.both)>0)
for(i in idx){
trps <- matrix(X.both[which(y.both[i,]>0),1:2],ncol=2,byrow=FALSE)
if(nrow(trps)>1){
s.init[i,] <- c(mean(trps[,1]),mean(trps[,2]))
}else{
s.init[i,] <- trps
}
}
#update using telemetry if you have it
if(!is.null(dim(data$locs))){
max.locs <- dim(locs)[2]
if(n.marked>1){
tel.inds <- which(rowSums(is.na(locs[,,1]))<max.locs)
n.locs.ind <- rowSums(!is.na(locs[,,1]))
}else{
tel.inds <- which(sum(is.na(locs[,,1]))<max.locs)
n.locs.ind <- sum(!is.na(locs[,,1]))
}
print("using telemetry to initialize telemetered s. Remove from data if not using in the model.")
#update using telemetry if you have it
for(i in tel.inds){
if(n.locs.ind[i]>1){
s.init[i,] <- colMeans(locs[i,1:n.locs.ind[i],])
}else{
s.init[i,] <- locs[i,1,]
}
#make sure s is in state space
if(s.init[i,1]<xlim[1]){
s.init[i,1] <- xlim[1] + 0.01
}
if(s.init[i,1]>xlim[2]){
s.init[i,1] <- xlim[2] - 0.01
}
if(s.init[i,2]<ylim[1]){
s.init[i,2] <- ylim[1] + 0.01
}
if(s.init[i,2]>ylim[2]){
s.init[i,2] <- ylim[2] - 0.01
}
}
n.locs.ind <- n.locs.ind[tel.inds]
}else{
tel.inds <- NA
n.locs.ind <- NA
}
#build plausible true sighting history to better initialize s
D.sight <- e2dist(s.init, X.sight)
lamd <- lam0*exp(-D.sight*D.sight/(2*sigma*sigma))
y.true <- matrix(0,M,J.sight)
y.true[1:n.marked,] <- y.mID
for(j in 1:J.sight){
#add marked no ID
prob <- lamd[1:n.marked,j]
prob <- prob/sum(prob)
y.true[1:n.marked,j] <- y.true[1:n.marked,j] + rmultinom(1,y.mnoID[j],prob=prob)
#add unmarked
prob <- c(rep(0,n.marked),lamd[(n.marked+1):M,j])
prob <- prob/sum(prob)
y.true[,j] <- y.true[,j] + rmultinom(1,y.um[j],prob=prob)
#add unk
prob <- lamd[,j]
prob <- prob/sum(prob)
y.true[,j] <- y.true[,j] + rmultinom(1,y.unk[j],prob=prob)
}
z.init <- 1*(rowSums(y.true)>0)
z.init[1:n.marked] <- 1
#update s.init given marking and sighting histories
y.mark2D <- apply(y.mark,c(1,2),sum)
y.both <- cbind(y.mark2D,y.true)
X.both <- rbind(X.mark,X.sight)
idx <- which(rowSums(y.both)>0)
for(i in idx){
trps <- matrix(X.both[y.both[i,]>0,1:2],ncol=2,byrow=FALSE)
if(nrow(trps)>1){
s.init[i,] <- c(mean(trps[,1]),mean(trps[,2]))
}else{
s.init[i,] <- trps
}
}
#If using a habitat mask, move any s's initialized in non-habitat above to closest habitat
e2dist <- function (x, y){
i <- sort(rep(1:nrow(y), nrow(x)))
dvec <- sqrt((x[, 1] - y[i, 1])^2 + (x[, 2] - y[i, 2])^2)
matrix(dvec, nrow = nrow(x), ncol = nrow(y), byrow = F)
}
getCell <- function(s,res,cells){
cells[trunc(s[1]/res)+1,trunc(s[2]/res)+1]
}
alldists <- e2dist(s.init,data$dSS)
alldists[,data$InSS==0] <- Inf
for(i in 1:M){
this.cell <- data$cells[trunc(s.init[i,1]/data$res)+1,trunc(s.init[i,2]/data$res)+1]
if(data$InSS[this.cell]==0){
cands <- alldists[i,]
new.cell <- which(alldists[i,]==min(alldists[i,]))
s.init[i,] <- data$dSS[new.cell,]
}
}
#check starting logProbs
D.mark <- e2dist(s.init, X.mark)
pd.p <- p0.p*exp(-D.mark*D.mark/(2*sigma*sigma))
pd.c <- p0.c*exp(-D.mark*D.mark/(2*sigma*sigma))
D.sight <- e2dist(s.init, X.sight)
lamd <- lam0*exp(-D.sight*D.sight/(2*sigma*sigma))
#marking process
logProb <- array(0,dim=c(M,J.mark))
for(i in 1:M){
for(j in 1:J.mark){
logProb[i,j] <- dbinom(y.mark.p[i,j],K1D.mark.p[i,j],pd.p[i,j],log=TRUE)
logProb[i,j] <- logProb[i,j] + dbinom(y.mark.c[i,j],K1D.mark.c[i,j],pd.c[i,j],log=TRUE)
}
}
if(!is.finite(sum(logProb)))stop("Starting observation model likelihood not finite. Marking process.")
#marked with ID obs
logProb <- array(0,dim=c(n.marked,J.sight))
for(i in 1:n.marked){
for(j in 1:J.sight){
logProb[i,j] <- dpois(y.mID[i,j],lamd[i,j]*K1D.sight[j],log=TRUE)
}
}
if(!is.finite(sum(logProb)))stop("Starting observation model likelihood not finite. Marked with ID observations.")
#marked no ID obs
logProb <- rep(0,J.sight)
if(n.marked>1){
lamd.mnoID <- colSums(lamd[1:n.marked,])
}else{
lamd.mnoID <- lamd[n.marked,]
}
for(j in 1:J.sight){
logProb[j] <- dpois(y.mnoID[j],lamd.mnoID[j]*K1D.sight[j])
}
if(!is.finite(sum(logProb)))stop("Starting observation model likelihood not finite. Marked no ID observations.")
#um obs
logProb <- rep(0,J.sight)
lamd.um <- colSums(lamd[(n.marked+1):M,])
for(j in 1:J.sight){
logProb[j] <- dpois(y.um[j],lamd.um[j]*K1D.sight[j])
}
if(!is.finite(sum(logProb)))stop("Starting observation model likelihood not finite. Unmarked observations.")
#unk obs
logProb <- rep(0,J.sight)
lamd.unk <- colSums(lamd[1:M,])
for(j in 1:J.sight){
logProb[j] <- dpois(y.unk[j],lamd.unk[j]*K1D.sight[j])
}
if(!is.finite(sum(logProb)))stop("Starting observation model likelihood not finite. Unknown marked status observations.")
return(list(s=s.init,z=z.init,K1D.mark.p=K1D.mark.p,K1D.mark.c=K1D.mark.c,K1D.sight=K1D.sight,
y.mark.p=y.mark.p,y.mark.c=y.mark.c,y.mID=y.mID,y.mnoID=y.mnoID,y.um=y.um,y.unk=y.unk,
xlim=xlim,ylim=ylim,locs=locs,tel.inds=tel.inds,n.locs.ind=n.locs.ind))
}