-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAKDE_sims.R
275 lines (190 loc) · 6.37 KB
/
AKDE_sims.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
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
library(ctmm)
# Define and simulate from a movement model -------------------------------
# The position autocorrelation timescale (in sec)
tau_p <- 1 %#% 'day'
# The velocity autocorrelation timescale (in sec)
tau_v <- 1 %#% 'hour'
# The spatial variance in m^2
sig <- 100000
# True 95% home range area
true.area <- -2*log(0.05)*pi*sig
# Generate the model
MODEL <- ctmm(tau = c(tau_p, tau_v),
isotropic = TRUE,
sigma = sig,
mu = c(0,0))
# Define the sampling schedule (e.g., 10 days sampled hourly)
st <- seq(1,10 %#% 'days', # The duration
1 %#% 'hours') # The sampling interval
sim <- simulate(MODEL, t = st)
plot(sim)
# Estimate a KDE HR from the data -----------------------------------------
START <- Sys.time() # Start time
# Fit an IID model
IID.mod <- ctmm.fit(sim,
method = "ML")
summary(IID.mod)
# KDE range estimate
kde <- akde(data = sim, CTMM = IID.mod)
END <- Sys.time() # End time
# KDE 95% range area
KDE.area <- sum(kde$CDF <= 0.95) * prod(kde$dr)
# Metrics
KDE.bias <- KDE.area - true.area
KDE.error <- KDE.bias / true.area
# Computational cost
KDE.time <- END - START
KDE.time
# Reset the clock
rm(START); rm(END)
# Estimate an AKDE HR from the data ---------------------------------------
START <- Sys.time() # Start time
# Guesstimate the parameters of an autocorrelated model
GUESS <- ctmm.guess(data = sim, interactive = FALSE)
# Fit and select autocorrelated models
OUF.mod <- ctmm.select(data = sim,
CTMM = GUESS,
method = "ML")
summary(OUF.mod)
# AKDE range estimate
AKDE <- akde(data = sim, CTMM = OUF.mod, debias = FALSE)
summary(AKDE)
END <- Sys.time() # End time
# AKDE 95% range area
AKDE.area <- sum(AKDE$CDF <= 0.95) * prod(AKDE$dr)
# Metrics
AKDE.bias <- AKDE.area - true.area
AKDE.error <- KDE.bias / true.area
# Computational cost
AKDE.time <- END - START
AKDE.time
#Reset the clock
rm(START); rm(END)
# Estimate an AKDEc HR from the data --------------------------------------
START <- Sys.time() # Start time
#Guesstimate the parameters of an autocorrelated model
GUESS <- ctmm.guess(data = sim,
interactive = FALSE)
#Fit and select autocorrelated models
OUF.mod <- ctmm.select(data = sim,
CTMM = GUESS,
method = "ML")
summary(OUF.mod)
#AKDEc range estimate
AKDEc <- akde(data = sim,
CTMM = OUF.mod,
debias = TRUE)
END <- Sys.time() # End time
# AKDEc 95% range area
AKDEc.area <- sum(AKDEc$CDF <= 0.95) * prod(AKDEc$dr)
# Metrics
AKDEc.bias <- AKDEc.area - true.area
AKDEc.error <- KDE.bias / true.area
# Computational cost
AKDEc.time <- END - START
AKDEc.time
# Reset the clock
rm(START); rm(END)
# Estimate a weighted AKDEc HR from the data ------------------------------
START <- Sys.time() # Start time
# Guesstimate the parameters of an autocorrelated model
GUESS <- ctmm.guess(data = sim,
interactive = FALSE)
# Fit and select autocorrelated models
OUF.mod <- ctmm.select(data = sim,
CTMM = GUESS,
method = "ML")
summary(OUF.mod)
# Weighted AKDEc range estimate
wAKDEc <- akde(data = sim,
CTMM = OUF.mod,
debias = TRUE,
weights = TRUE)
END <- Sys.time() # End time
# Weighted AKDEc 95% range area
wAKDEc.area <- sum(wAKDEc$CDF <= 0.95) * prod(wAKDEc$dr)
# Metrics
wAKDEc.bias <- wAKDEc.area - true.area
wAKDEc.error <- KDE.bias / true.area
# Computational cost
wAKDEc.time <- END - START
wAKDEc.time
# Reset the clock
rm(START); rm(END)
# Estimate a pHREML weighted AKDEc HR from the data -----------------------
START <- Sys.time() # Start time
# Guesstimate the parameters of an autocorrelated model
GUESS <- ctmm.guess(data = sim,
interactive = FALSE)
# Fit and select autocorrelated models
OUF.mod.phreml <- ctmm.select(data = sim,
CTMM = GUESS,
method = "pHREML")
summary(OUF.mod.phreml)
# pHREML weighted AKDEc range estimate
pHREML_wAKDEc <- akde(data = sim,
CTMM = OUF.mod.phreml,
debias = TRUE,
weights = TRUE)
END <- Sys.time() # End time
# pHREML weighted AKDEc 95% range area
pHREML_wAKDEc.area <- sum(pHREML_wAKDEc$CDF <= 0.95) *
prod(pHREML_wAKDEc$dr)
# Metrics
pHREML_wAKDEc.bias <- pHREML_wAKDEc.area - true.area
pHREML_wAKDEc.error <- KDE.bias / true.area
# Computational cost
pHREML_wAKDEc.time <- END - START
pHREML_wAKDEc.time
# Reset the clock
rm(START); rm(END)
# Estimate a bootstrapped pHREML weighted AKDEc HR from the data ----------
START <- Sys.time() # Start time
# Guesstimate the parameters of an autocorrelated model
GUESS <- ctmm.guess(data = sim,
interactive = FALSE)
# Fit and select autocorrelated models
OUF.mod.phreml <- ctmm.select(data = sim,
CTMM = GUESS,
method = "pHREML")
summary(OUF.mod.phreml)
# Bootstrap the fit
boot.fit <- ctmm.boot(data = sim,
CTMM = OUF.mod.phreml,
method = "pHREML",
iterate = TRUE)
summary(boot.fit)
# Bootstrapped weighted AKDEc range estimate
boot_pHREML_wAKDEc <- akde(data = sim,
CTMM = boot.fit,
debias = TRUE,
weights = TRUE)
END <- Sys.time() # End time
# Bootstrapped pHREML weighted AKDEc 95% range area
boot_pHREML_wAKDEc.area <- sum(boot_pHREML_wAKDEc$CDF <= 0.95) *
prod(boot_pHREML_wAKDEc$dr)
# Metrics
boot_pHREML_wAKDEc.bias <- boot_pHREML_wAKDEc.area - true.area
boot_pHREML_wAKDEc.error <- KDE.bias / true.area
# Computational cost
boot_pHREML_wAKDEc.time <- END - START
boot_pHREML_wAKDEc.time
# Reset the clock
rm(START); rm(END)
# Method comparisons ------------------------------------------------------
# Compare all of the relative area error side by side
ERROR <- c(KDE.error,
AKDE.error,
AKDEc.error,
wAKDEc.error,
pHREML_wAKDEc.error,
boot_pHREML_wAKDEc.error)
ERROR
# Compare all of computation times side by side
COST <- c(KDE.time,
AKDE.time,
AKDEc.time,
wAKDEc.time,
pHREML_wAKDEc.time,
boot_pHREML_wAKDEc.time)
COST