/
02_Fit SSM in foieGras.R
365 lines (254 loc) · 11.8 KB
/
02_Fit SSM in foieGras.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#################################################################################
### Fit continuous-time state-space model while accounting for location error ###
#################################################################################
library(tidyverse)
library(lubridate)
library(foieGras) #v1.0-7
library(sf) #v1.0.7
library(rnaturalearth)
library(tictoc)
library(plotly)
#### Load data ####
dat <- read.csv('Processed_data/Cleaned_FDN Cmydas tracks.csv')
glimpse(dat); str(dat)
summary(dat)
#### Wrangle data for analysis using {foieGras} ####
# Convert all 'Quality' values to "G" for FastGPS data and 'Date' to datetime format
dat <- dat %>%
mutate(Quality = ifelse(Type == 'FastGPS', 'G', Quality),
Date = as_datetime(Date))
# Rename columns for {foieGras}
dat2<- dat %>%
rename(id = Ptt, date = Date, lc = Quality, lon = Longitude, lat = Latitude,
eor = Error.Ellipse.orientation, smaj = Error.Semi.major.axis,
smin = Error.Semi.minor.axis) %>%
dplyr::select(id, date, lc, lon, lat, smaj, smin, eor) #reorders and subsets the columns
glimpse(dat2)
#### Inspect time steps of transmissions for making predictions ####
tmp <- dat2 %>%
split(.$id) %>%
purrr::map(., ~mutate(.x,
dt = difftime(c(date[-1], NA),
date,
units = "secs") %>%
as.numeric())
) %>%
bind_rows()
ggplot(tmp, aes(date, dt)) +
geom_point() +
theme_bw() +
facet_wrap(~id, scales = "free_x")
# some outliers present, but nothing to worry about for now
# Determine primary time step
ggplot(tmp) +
geom_histogram(aes(dt), binwidth = 3600) +
theme_bw() +
xlim(0, 3600*24)
tmp %>%
group_by(id) %>%
summarize(mean = mean(dt, na.rm = TRUE),
median = median(dt, na.rm = TRUE))
# Mean/median time step is ~1 hr
#################
#### Run SSM ####
#################
# Change `id` to character to avoid problems during model runs
dat2$id <- as.character(dat2$id)
#### Account for location error at observed irregular time interval ####
# Estimate 'true' locations on irregular sampling interval (by setting `time.step = NA`)
tic()
fit_crw_fitted <- fit_ssm(dat2, vmax = 3, model = "crw", time.step = NA,
control = ssm_control(verbose = 1))
toc() #took 30 sec where time.step = NA
print(fit_crw_fitted)
# Viz the filtered outliers (gold), raw observations (blue), and estimated locations (red), along with associated uncertainty (red shading)
plot(fit_crw_fitted, what = "fitted", type = 1, ask = TRUE)
plot(fit_crw_fitted, what = "fitted", type = 2, ask = TRUE)
foieGras::map(fit_crw_fitted,
what = "fitted",
by.id = TRUE)
# Estimate behavioral state (i.e., move persistence; gamma)
# Joint move persistence model ('jmpm') uses hierarchical approach across IDs
tic()
fit_crw_jmpm_fitted <- fit_mpm(fit_crw_fitted, what = "fitted", model = "jmpm",
control = mpm_control(verbose = 1))
toc() #took 48 sec to fit
print(fit_crw_jmpm_fitted)
plot(fit_crw_jmpm_fitted)
# Grab results and plot
res_crw_fitted<- join(ssm = fit_crw_fitted,
mpm = fit_crw_jmpm_fitted,
what.ssm = "fitted")
# Compare raw tracks vs fitted tracks (for adults tagged at Fernando de Noronha)
brazil<- ne_countries(scale = 50, country = "Brazil", returnclass = 'sf')
ggplot() +
geom_sf(data = brazil) +
geom_path(data = dat2, aes(lon, lat, group = id), color = 'black') + #raw tracks
geom_path(data = res_crw_fitted, aes(lon, lat, group = id), color = "blue") + #modeled tracks
theme_bw() +
facet_wrap(~id) +
coord_sf(xlim = c(-42, -32), ylim = c(-8, -1))
# Viz modeled tracks together
plotly::ggplotly(
ggplot() +
geom_sf(data = brazil, fill = "grey60") +
geom_path(data = res_crw_fitted, aes(lon, lat, group = id, color = id), size = 0.75, alpha = 0.8) +
scale_color_viridis_d() +
theme_bw() +
theme(panel.grid = element_blank()) +
coord_sf(xlim = c(-42, -32), ylim = c(-8, -2))
)
# Viz modeled tracks together w/ behavior plotted
plotly::ggplotly(
ggplot() +
geom_sf(data = brazil, fill = "grey60") +
geom_point(data = res_crw_fitted, aes(lon, lat, group = id, color = g), size = 0.75, alpha = 0.8) +
scale_color_viridis_c(option = "inferno") +
theme_bw() +
theme(panel.grid = element_blank()) +
coord_sf(xlim = c(-42, -32), ylim = c(-8, -2))
)
#interactively explore using projected coordinates (World Mercator Projection; EPSG:3395, units = km)
res_crw_fitted %>%
dplyr::select(id, date, x, y, s, g) %>%
data.frame() %>% #current version of shiny_tracks won't work w/ tibble format or when a column has all NAs
bayesmove::shiny_tracks(., "+proj=merc +lon_0=0 +datum=WGS84 +units=km +no_defs")
#interactively explore using lat/long (EPSG:4326)
res_crw_fitted %>%
dplyr::select(-c(x, y, s.se)) %>%
rename(x = lon, y = lat) %>%
dplyr::select(id, date, x, y, s, g) %>%
data.frame() %>% #current version of shiny_tracks won't work w/ tibble format or when a column has all NAs
bayesmove::shiny_tracks(., 4326)
# Check model fit w/ diagnostic "one-step-ahead residuals"; takes long time!
# diag_crw <- osar(fit_crw_fitted)
#
# plot(diag_crw, type = "ts") #time series of residuals
# plot(diag_crw, type = "qq") #Q-Q plot
# plot(diag_crw, type = "acf") #Autocorrelation function plot
#### Account for location error at regularized time interval with correlated random walk ####
# Estimate 'true' locations on regular sampling interval of 8 hrs
tic()
fit_crw_8hr <- fit_ssm(dat2, vmax = 3, model = "crw", time.step = 8,
control = ssm_control(verbose = 1))
toc() #took 34 sec to fit model
print(fit_crw_8hr)
# Viz the filtered outliers (gold), raw observations (blue), and estimated locations (red), along with associated uncertainty (red shading)
plot(fit_crw_8hr, what = "predicted", type = 1, ask = TRUE)
plot(fit_crw_8hr, what = "predicted", type = 2, ask = TRUE)
# plot(fit_crw_8hr, what = "fitted", type = 1, ask = TRUE)
# plot(fit_crw_8hr, what = "fitted", type = 2, ask = TRUE)
# Estimate behavioral state (i.e., move persistence; gamma)
# Individual move persistence model ('mpm') estimates behavioral states separately across IDs
tic()
fit_crw_mpm_8hr <- fit_mpm(fit_crw_8hr, what = "predicted", model = "mpm",
control = mpm_control(verbose = 1))
toc() #took 6 sec to fit
#if issues w/ model not converging, try changing time step of SSM and re-running
print(fit_crw_mpm_8hr)
plot(fit_crw_mpm_8hr)
# Grab results and plot
res_crw_8hr<- join(ssm = fit_crw_8hr,
mpm = fit_crw_mpm_8hr,
what.ssm = "predicted")
# Compare raw tracks vs fitted tracks (for adults tagged at Fernando de Noronha)
ggplot() +
geom_sf(data = brazil) +
geom_path(data = dat2, aes(lon, lat, group = id), color = 'black') + #raw tracks
geom_path(data = res_crw_8hr, aes(lon, lat, group = id), color = "blue") + #modeled tracks
theme_bw() +
facet_wrap(~id) +
coord_sf(xlim = c(-42, -32), ylim = c(-8, -1))
# Viz modeled tracks together
plotly::ggplotly(
ggplot() +
geom_sf(data = brazil, fill = "grey60") +
geom_path(data = res_crw_8hr, aes(lon, lat, group = id, color = id), size = 0.75, alpha = 0.8) +
scale_color_viridis_d() +
theme_bw() +
theme(panel.grid = element_blank()) +
coord_sf(xlim = c(-42, -32), ylim = c(-8, -2))
)
# Viz modeled tracks together w/ behavior plotted
plotly::ggplotly(
ggplot() +
geom_sf(data = brazil, fill = "grey60") +
geom_point(data = res_crw_8hr, aes(lon, lat, group = id, color = g), size = 0.75, alpha = 0.8) +
scale_color_viridis_c(option = "inferno") +
theme_bw() +
theme(panel.grid = element_blank()) +
coord_sf(xlim = c(-42, -32), ylim = c(-8, -2))
)
# Compare predicted tracks against tracks fitted at irregular time step
plotly::ggplotly(
ggplot() +
geom_sf(data = brazil, fill = "grey60") +
geom_path(data = res_crw_fitted, aes(lon, lat, group = id, color = id), size = 0.75, alpha = 0.3) +
geom_path(data = res_crw_8hr, aes(lon, lat, group = id, color = id), size = 0.75, alpha = 0.8) +
scale_color_viridis_d() +
theme_bw() +
theme(panel.grid = element_blank()) +
coord_sf(xlim = c(-42, -32), ylim = c(-8, -2))
)
#### Account for location error at regularized time interval w/ move persistence model ####
# Estimate 'true' locations on regular sampling interval of 8 hrs using 'move persistence' model
tic()
fit_mp_8hr <- fit_ssm(dat2, vmax = 3, model = "mp", time.step = 8,
control = ssm_control(verbose = 1))
toc() #took 1.25 min to fit model
print(fit_mp_8hr)
# Viz the filtered outliers (gold), raw observations (blue), and estimated locations (red), along with associated uncertainty (red shading)
plot(fit_mp_8hr, what = "predicted", type = 1, ask = TRUE)
plot(fit_mp_8hr, what = "predicted", type = 2, ask = TRUE)
plot(fit_mp_8hr, what = "predicted", type = 3, ask = TRUE)
# plot(fit_mp_8hr, what = "fitted", type = 1, ask = TRUE)
# plot(fit_mp_8hr, what = "fitted", type = 2, ask = TRUE)
# plot(fit_mp_8hr, what = "fitted", type = 3, ask = TRUE)
# Grab results and plot
res_mp_8hr_pred<- grab(fit_mp_8hr, what = "predicted")
# Viz modeled tracks together
plotly::ggplotly(
ggplot() +
geom_sf(data = brazil, fill = "grey60") +
geom_path(data = res_mp_8hr_pred, aes(lon, lat, group = id, color = id), size = 0.75,
alpha = 0.8) +
scale_color_viridis_d() +
theme_bw() +
theme(panel.grid = element_blank()) +
coord_sf(xlim = c(-42, -32), ylim = c(-8, -2))
)
# Viz modeled tracks together w/ behavior plotted
plotly::ggplotly(
ggplot() +
geom_sf(data = brazil, fill = "grey60") +
geom_point(data = res_mp_8hr_pred, aes(lon, lat, group = id, color = g), size = 0.75,
alpha = 0.8) +
scale_color_viridis_c(option = "inferno") +
theme_bw() +
theme(panel.grid = element_blank()) +
coord_sf(xlim = c(-42, -32), ylim = c(-8, -2))
)
### Compare estimates across the different approaches ###
# Behavioral states
ggplot() +
geom_line(data = res_crw_fitted, aes(date, g, color = "CRW_Irregular")) +
geom_line(data = res_crw_8hr, aes(date, g, color = "CRW_8hr")) +
geom_line(data = res_mp_8hr_pred, aes(date, g, color = "MP_8hr")) +
scale_color_manual(values = RColorBrewer::brewer.pal(3, "Dark2")) +
theme_bw() +
facet_wrap(~id, scales = "free_x")
## Of these 3 different models, the CRW model w/ 8 hr prediction time step seems to give best broad scale state estimates
# Track relocations
ggplot() +
geom_path(data = res_crw_fitted, aes(x, y, color = "CRW_Irregular")) +
geom_path(data = res_crw_8hr, aes(x, y, color = "CRW_8hr")) +
geom_path(data = res_mp_8hr_pred, aes(x, y, color = "MP_8hr")) +
scale_color_manual(values = RColorBrewer::brewer.pal(3, "Dark2")) +
theme_bw() +
facet_wrap(~id, scales = "free")
## Of these 3 approaches, the move persistence model seems to provide the best estimates when the individual appears to be stationary (i.e., fewer unnecessary looping movements)
### Overall verdict: for relatively fine-scale movements (time step > 1 hr & < 1 d), the CRW model seems to provide the best behavioral state estimates. Whlie all 3 models provided very similar estimates for the 'true' animal movements, the MP model seems to provide the best estimates, particularly when the animal appears to be encamped within a given site
### Since the other behavioral state models do not account for location error and the analysis of step lengths and turning angles (if used) must be at a regular time interval, we will export the tracks from the move persistence model for further use.
### Export fitted tracks ###
write.csv(res_mp_8hr_pred, "Processed_data/SSM_mp8hr_FDN Cmydas tracks.csv", row.names = FALSE)
save(fit_crw_8hr, fit_crw_mpm_8hr, file = "Processed_data/SSM_model_fits.RData")