-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.SMR.Dcov.Generalized.R
More file actions
200 lines (186 loc) · 6.34 KB
/
init.SMR.Dcov.Generalized.R
File metadata and controls
200 lines (186 loc) · 6.34 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
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 <- 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
K1D.mark <- data$K1D.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 <- inits$p0
lam0 <- inits$lam0
sigma <- inits$sigma
#augment marking process capture history
y.aug <- matrix(0,M,J.mark)
y.aug[1:n.marked,] <- y.mark
y.mark <- y.aug
#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.mark[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.both <- cbind(y.mark,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 <- p0*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[i,j],K1D.mark[j],pd[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=K1D.mark,K1D.sight=K1D.sight,
y.mark=y.mark,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))
}