forked from amburgey/Browntreesnake_ATTRACTANTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPredictiveSingleNight.R
182 lines (138 loc) · 5.55 KB
/
PredictiveSingleNight.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
### Integrating across space to understand how encounter probability scales to probability of detection on a given night
## Simulate a single snake with an activity center in the center of the study area
## Each treatment is applied uniformly on a given evening (i.e., every grid cell receives no lure vs. every grid cell receives lure)
## Parameters that affect detection (sigma and lam0) are from model results from each study
rm(list = ls())
library(jagsUI)
##### LURE PROJECT #####
load("Results/SCRVISlurenolure.RData")
## Establish parameters for detection, lam0[1] = no trap lure, lam0[2] = trap lure
sigma <- out$sims.list$sigma
lam0 <- out$sims.list$lam0
## Study area grid, trap locations, and number of traps
locs <- secr::make.grid(nx = 13, ny = 27, spacex = 16, spacey = 8)
pts <- as.matrix(locs)
J <- nrow(pts)
## Status of all grid cells that evening (1 = no lure, 2 = lure)
STATUS <- c(1,2)
## Define state-space of point process. (i.e., where animals live).
## "delta" just adds a fixed buffer to the outer extent of the traps.
## Don't need to estimate state-space since we know it (5 ha enclosed pop)
delta<- 11.874929
Xl<-min(locs[,1]) - delta
Xu<-max(locs[,1]) + delta
Yl<-min(locs[,2]) - delta
Yu<-max(locs[,2]) + delta
## Check area:
A <- (Xu-Xl)*(Yu-Yl)
# Number of nights trapping (just one)
K <- 1
## Snake activity center (AC) is in center of grid
S <- as.data.frame(matrix(as.matrix(locs[176,]), nrow = 1, ncol = 2))
## Function to calculate distance between two sets of (x,y) locations
e2dist <- function(A, B) {
xdif <- outer(A[, 1], B[, 1], "-")
ydif <- outer(A[, 2], B[, 2], "-")
sqrt(xdif^2 + ydif^2)
}
#Distance between each individual AC and each trap
d2 <- e2dist(S,pts)
## Simulate the encounter probabilities of this individual in every trap and repeat for every posterior sample (i.e., 6000 times) in order to calculate mean and uncertainty
p <- array(NA,dim = c(length(sigma),J,length(STATUS)))
set.seed(04042021)
for(l in 1:length(STATUS)){ ## do for a situation where every cell is no lure, lure
for(j in 1:J){ ## do for every survey location
for(s in 1:length(sigma)){## do for every element of out$sims.list$sigma
p[s,j,l] <- lam0[s,STATUS[l]]*exp(-(d2[1,j])/(2*sigma[s]*sigma[s]))
}
}
}
## Mean probability of encountering a snake on a grid with no lures
NTLp <- mean(p[,,1])
## Probability of detecting a snake across the area during an evening with no lures
NTLpstar <- vector()
for(s in 1:nrow(p)){
NTLpstar[s] <- 1-prod(1-p[s,,1])
}
mean(NTLpstar)
quantile(NTLpstar, probs=c(0.025,0.975))
## Mean probability of encountering a snake on a grid with lures
TLp <- mean(p[,,2])
## Probability of detecting a snake across the area during an evening with lures
TLpstar <- vector()
for(s in 1:nrow(p)){
TLpstar[s] <- 1-prod(1-p[s,,2])
}
mean(TLpstar)
quantile(TLpstar, probs=c(0.025,0.975))
##### SCENT PROJECT #####
rm(list = ls())
load("Results/SCRVISscentnoscent.RData")
## Establish parameters for detection, lam0[1] = no scent, lam0[2] = fresh scent, lam0[3] = old scent
sigma <- out$sims.list$sigma
lam0 <- out$sims.list$lam0
## Study area grid, trap locations, and number of traps
locs <- secr::make.grid(nx = 13, ny = 27, spacex = 16, spacey = 8)
pts <- as.matrix(locs)
J <- nrow(pts)
## Status of all grid cells that evening (1 = no scent, 2 = fresh scent, 3 = old scent)
STATUS <- c(1,2,3)
## Define state-space of point process. (i.e., where animals live).
## "delta" just adds a fixed buffer to the outer extent of the traps.
## Don't need to estimate state-space since we know it (5 ha enclosed pop)
delta<- 11.874929
Xl<-min(locs[,1]) - delta
Xu<-max(locs[,1]) + delta
Yl<-min(locs[,2]) - delta
Yu<-max(locs[,2]) + delta
## Check area:
A <- (Xu-Xl)*(Yu-Yl)
# Number of nights trapping (just one)
K <- 1
## Snake activity center (AC) is in center of grid
S <- as.data.frame(matrix(as.matrix(locs[176,]), nrow = 1, ncol = 2))
## Function to calculate distance between two sets of (x,y) locations
e2dist <- function(A, B) {
xdif <- outer(A[, 1], B[, 1], "-")
ydif <- outer(A[, 2], B[, 2], "-")
sqrt(xdif^2 + ydif^2)
}
#Distance between each individual AC and each each trap
d2 <- e2dist(S,pts)
## Simulate the encounter probabilities of this individual in every trap and repeat for every posterior sample (i.e., 6000 times) in order to calculate mean and uncertainty
p <- array(NA,dim = c(length(sigma),J,length(STATUS)))
set.seed(04042021)
for(l in 1:length(STATUS)){ ## do for a situation where every cell is no lure, lure
for(j in 1:J){ ## do for every survey location
for(s in 1:length(sigma)){## do for every element of out$sims.list$sigma
p[s,j,l] <- lam0[s,STATUS[l]]*exp(-(d2[1,j])/(2*sigma[s]*sigma[s]))
}
}
}
## Mean probability of encountering a snake on a grid with no scent
NSp <- mean(p[,,1])
## Probability of detecting a snake across the area during an evening with no scent
NSpstar <- vector()
for(s in 1:nrow(p)){
NSpstar[s] <- 1-prod(1-p[s,,1])
}
mean(NSpstar)
quantile(NSpstar, probs=c(0.025,0.975))
## Mean probability of encountering a snake on a grid with fresh scent
FSp <- mean(p[,,2])
## Probability of detecting a snake across the area during an evening with lures
FSpstar <- vector()
for(s in 1:nrow(p)){
FSpstar[s] <- 1-prod(1-p[s,,2])
}
mean(FSpstar)
quantile(FSpstar, probs=c(0.025,0.975))
## Mean probability of encountering a snake on a grid with old scent
OSp <- mean(p[,,3])
## Probability of detecting a snake across the area during an evening with lures
OSpstar <- vector()
for(s in 1:nrow(p)){
OSpstar[s] <- 1-prod(1-p[s,,3])
}
mean(OSpstar)
quantile(OSpstar, probs=c(0.025,0.975))