-
Notifications
You must be signed in to change notification settings - Fork 0
/
iSSA.Rmd
445 lines (374 loc) · 14.6 KB
/
iSSA.Rmd
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
---
title: "Integrated step selection analysis (iSSA)"
author: "Pierre Cottais & An Hoàng"
date: "25/01/2022"
output:
pdf_document:
extra_dependencies: ["float"]
toc: yes
number_sections: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T,
results = "hide")
```
```{r packages}
library(tidyverse)
library(ggmap)
# library(sf)
library(lubridate)
library(amt)
library(stargazer)
```
# Data processing
```{r}
# oceanography dataset
load("CALDIO_MPE_2021/generalisation datasets/grid_oceano_2011.Rdata")
grid_oceano_2011 <- grid_oceano
load("CALDIO_MPE_2021/generalisation datasets/grid_oceano_2012.Rdata")
grid_oceano_2012 <- grid_oceano
remove(grid_oceano)
# related dates
load("CALDIO_MPE_2021/generalisation datasets/grid_oceano_Date_2011.Rdata")
grid_Oceano_Date_2011 <- grid_Oceano_Date
load("CALDIO_MPE_2021/generalisation datasets/grid_oceano_Date_2012.Rdata")
grid_Oceano_Date_2012 <- grid_Oceano_Date
remove(grid_Oceano_Date)
# Scopoli's sheawater datasets
load("CALDIO_MPE_2021/training datasets/CALDIO_2011_oceano.Rdata")
load("CALDIO_MPE_2021/training datasets/CALDIO_2012_oceano.Rdata")
ALL <- bind_rows(ALL2011, ALL2012)
ALL <- ALL %>% mutate(Year = as.factor(year(ymd_hms(Time))))
# remove(ALL2011, ALL2012)
# load functions
source("functions.R")
```
Work with Shoreline / Coastline shapefiles
```{r eval=FALSE, include=FALSE}
# europe <- rnaturalearth::ne_countries(scale = "medium", returnclass = "sf",
# continent = "Europe")
#
# mediterra <- europe %>%
# filter(admin %in% c("France", "Italy", "Spain")) %>%
# st_crop(xmin = 3, xmax = 11, ymin = 40, ymax = 45)
#
# mediterra %>%
# # st_transform(crs = 2154) %>%
# ggplot() + geom_sf() +
# geom_point(data = grd[!is.na(grd$Bathy) & !is.na(grd$SST1),],
# aes(x = Longitude, y = Latitude), size = 0.2)
#
# df_union_cast <- mediterra$geometry %>%
# st_cast("MULTIPOLYGON")
#
# plot(df_union_cast)
```
Remove cells near the coast boundary
```{r message=FALSE, warning=FALSE}
grd <- grid_oceano_2011[[1]]
mediterranean <- make_bbox(lat = Latitude, lon = Longitude, data = grd)
map <- get_map(location = mediterranean) %>% ggmap()
df_grid <- grd %>% filter(!is.na(SST1), !is.na(Bathy))
map + geom_point(data = df_grid, aes(x = Longitude, y = Latitude), size = 0.1)
```
Visualise colonies
```{r eval=FALSE, include=FALSE}
colony_colors <- c("#FC8D58", "#D72F28", "#1A7838", "#69ADDB")
png("figures/map_sites.png", width=7, height=4.2, units="in", res=200)
mediterranean <- make_bbox(lat = Latitude, lon = Longitude, data = ALL2011)
map <- get_stamenmap(bbox = mediterranean, maptype = "toner-lite", zoom = 7) %>% ggmap()
map + geom_point(data = ALL2011, aes(x = Longitude, y = Latitude, color = Site), size = 0.1) +
scale_color_manual(values = colony_colors) +
xlab("Longitude") + ylab("Latitude") +
labs(title = "Répartition des individus selon le site de leur colonie en 2011")+
theme(legend.position = "bottom",
legend.title = element_blank()) +
guides(colour = guide_legend(override.aes = list(size=3)))
dev.off()
```
```{r eval=FALSE, include=FALSE}
# colony_colors <- c("#FC8D58", "#D72F28", "#1A7838", "#69ADDB")
# png("figures/bar_site.png", width=8, height=3, units="in", res=200)
# ALL2011 %>% group_by(Site) %>%
# summarise(nb_ind = n_distinct(ID)) %>%
# ggplot() + geom_col(aes(x = Site, y = nb_ind), width = 0.5,
# fill = colony_colors) +
# ylab(element_blank()) + xlab(element_blank()) +
# labs(title = "Répartition des individus selon le site de leur colonie en 2011",
# caption = "Nombre total d'individus : 94") +
# theme(plot.title = element_text(size = 16, hjust = 0.5, vjust = 2.7),
# plot.caption = element_text(size = 10, face = "italic", hjust = 1),
# legend.position = "bottom",
# legend.title = element_blank(),
# axis.text.x = element_text(vjust = 2, size = 14, color = colony_colors),
# axis.text.y = element_text(size = 10),
# panel.background = element_rect(fill = "white"),
# panel.grid = element_line(color = 'grey', linetype = 'dotted'),
# panel.grid.major.x = element_blank(),
# axis.ticks.x = element_blank())
# dev.off()
```
```{r eval=FALSE, include=FALSE}
colony_colors <- c("#FC8D58", "#D72F28", "#1A7838", "#69ADDB")
png("figures/bar_site.png", width=8, height=3, units="in", res=200)
ALL %>%
# mutate(ID = paste(ID, Year, sep = '')) %>%
group_by(Year, Site) %>%
summarise(nb_ind = n_distinct(ID)) %>%
ggplot() + aes(x = Site, y = nb_ind, fill = Year) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.8, width = 0.5) +
ylab(element_blank()) + xlab(element_blank()) +
labs(title = "Répartition des individus selon le site de leur colonie",
caption = "Nombre total d'individus par année : 94") +
theme(plot.title = element_text(size = 16, hjust = 0.5, vjust = 2.7),
plot.caption = element_text(size = 10, face = "italic", hjust = 1),
legend.position = "right",
legend.title = element_blank(),
axis.text.x = element_text(vjust = 2, size = 14, color = colony_colors),
axis.text.y = element_text(size = 10),
panel.background = element_rect(fill = "white"),
panel.grid = element_line(color = 'grey', linetype = 'dotted'),
panel.grid.major.x = element_blank(),
axis.ticks.x = element_blank())
dev.off()
```
# Build steps (observed and available) and the 4 models in 2011
```{r eval=FALSE, include=FALSE}
# initialisation
steps <- list()
models <- list()
# env_issa <- list()
```
## Running iSSA for shearwaters (including some covariates NA)
```{r eval=FALSE, include=FALSE}
# running the analysis for 2011 data
start <- Sys.time()
for (site in unique(ALL$Site)){
print(site)
stps <- iSSA_steps(colony = site, n_control = 6,
covariates = c("Bathy", "GBathy", "SST28", "GSST28","Var_SST28",
"SLA1", "VEL7", "GCHLA28"))
# # removing observed + available steps if NA
# na.ind <- unique(which(is.na(stps), arr.ind = TRUE)[,1])
# na.step_id <- stps[na.ind, "step_id_"] %>% unique() %>% pull()
# stps <- stps %>%
# filter(!step_id_%in%na.step_id)
# saving steps dataframe
steps[[site]] <- stps
# saving models
models[[site]] <- stps %>% amt::fit_issf(case_ ~ sl_ + ta_ + log_sl + Bathy +
SST28 + GSST28 + Var_SST28 + SLA1 + VEL7 +
GCHLA28 + strata(step_id_))
}
end <- Sys.time()
print(end-start)
save(models, steps, file = "issa_env_n6.RData")
```
## Running iSSA for 2011 shearwaters (witout oceanographics NA steps)
```{r eval=FALSE, include=FALSE}
# # running the analysis for 2011 data
# start <- Sys.time()
# for (site in unique(ALL$Site)){
# print(site)
# stps <- iSSA_steps(colony = site, year = 2011,
# covariates = c("Bathy", "GBathy", "SST28", "GSST28", "Var_SST28",
# "SLA1", "VEL7", "logCHLA28", "GCHLA28"))
#
# # removing observed + available steps if NA
# na.ind <- unique(which(is.na(stps), arr.ind = TRUE)[,1])
# na.step_id <- stps[na.ind, "step_id_"] %>% unique() %>% pull()
# stps <- stps %>%
# filter(!step_id_%in%na.step_id)
#
# # saving steps dataframe
# steps[[site]] <- stps
#
# # saving models
# models[[site]] <- stps %>% amt::fit_issf(case_ ~ sl_ + ta_ + log_sl + Bathy + GBathy +
# SST28 + GSST28 + Var_SST28 + SLA1 + VEL7 +
# logCHLA28 + GCHLA28 + strata(step_id_))
# }
# end <- Sys.time()
# print(end-start)
#
# env_issa[["clean"]] <- list(steps, models)
```
<!-- ## Running iSSA for 2012 shearwaters -->
```{r eval=FALSE, include=FALSE}
# # running the analysis for 2012 data
# start <- Sys.time()
# for (site in unique(ALL2012$Site)){
# print(site)
# stps <- iSSA_steps(colony = site, year = 2012,
# covariates = c("Bathy", "GBathy", "SST28", "GSST28", "Var_SST28",
# "SLA1", "VEL7", "logCHLA28", "GCHLA28"))
#
# # removing observed + available steps if NA
# na.ind <- unique(which(is.na(stps), arr.ind = TRUE)[,1])
# na.step_id <- stps[na.ind, "step_id_"] %>% unique() %>% pull()
# stps <- stps %>%
# filter(!step_id_%in%na.step_id)
#
# # saving steps dataframe
# steps[[site]] <- stps
#
# # saving models
# models[[site]] <- stps %>% amt::fit_issf(case_ ~ sl_ + ta_ + log_sl + Bathy + GBathy +
# SST28 + GSST28 + Var_SST28 + SLA1 + VEL7 +
# logCHLA28 + GCHLA28 + strata(step_id_))
# }
# end <- Sys.time()
# print(end-start)
#
# env_year[[2012]] <- list(steps, models)
```
## Load models and steps generated before
```{r}
# load("issa_env.RData")
load("issa_env_n6.RData")
```
<!-- compare Lavezzi birds'positions to remaining observed steps -->
<!-- ```{r} -->
<!-- obs_stps_miss_L <- steps_miss$Lavezzi %>% filter(case_ == TRUE) -->
<!-- mediterranean <- make_bbox(lat = y2_, lon = x2_, data = obs_stps_miss_L) -->
<!-- map_steps <- get_map(location = mediterranean) %>% ggmap() -->
<!-- map_steps + geom_point(data = obs_stps_miss_L, aes(x = x2_, y = y2_), size = 0.1) -->
<!-- mediterranean <- make_bbox(lat = Latitude, lon = Longitude, -->
<!-- data = ALL2012[ALL2012$Site=="Lavezzi",]) -->
<!-- map_coord <- get_map(location = mediterranean) %>% ggmap() -->
<!-- map_coord + geom_point(data = ALL2012[ALL2012$Site=="Lavezzi",], -->
<!-- aes(x = Longitude, y = Latitude), size = 0.1) -->
<!-- ``` -->
Correlation analysis for the covariates
```{r}
grd <- grid_oceano_2011[[36]]
covariates_corr <- cor(na.omit(grd[, c("Bathy", "GBathy", "SST28", "GSST28", "Var_SST28",
"SLA1", "VEL7", "logCHLA28", "GCHLA28")]))
res.PCA <- FactoMineR::PCA(na.omit(grd[, c("Bathy", "GBathy", "SST28", "GSST28", "Var_SST28",
"SLA1", "VEL7", "logCHLA28", "GCHLA28")]))
corrplot::corrplot(covariates_corr)
```
```{r}
colony <- "Riou"
print(colony)
modR <- models$Riou$model ; modR
```
```{r echo=FALSE, results='asis'}
stargazer(modR, type = "latex",
title = paste("Résumé du modèle de régression logistique conditionnel pour la colonie", colony))
```
```{r message=FALSE, warning=FALSE}
obs_stps <- steps$Riou %>% filter(case_ == TRUE)
rdm_stps <- steps$Riou %>% filter(case_ == FALSE)
mediterranean <- make_bbox(lat = y2_, lon = x2_, data = steps$Riou)
map <- get_map(location = mediterranean) %>% ggmap()
png(paste0("figures/", colony, "_map.png"), width=4.4, height=4, units="in", res=200)
map + geom_point(data = rdm_stps, aes(x = x2_, y = y2_),
size = 0.1, color = "red", alpha = 0.1) +
geom_point(data = obs_stps, aes(x = x2_, y = y2_), size = 0.1) +
ggtitle(colony)
dev.off()
```
```{r}
colony <- "Lavezzi"
print(colony)
modL <- models$Lavezzi$model ; modL
```
```{r echo=FALSE, results='asis'}
stargazer(modL, type = "latex", title = paste(
"Résumé du modèle de régression logistique conditionnel pour la colonie",
colony), summary = FALSE)
```
There are too many steps removed because missing. Let's investigate why.
```{r message=FALSE, warning=FALSE}
obs_stps <- steps$Lavezzi %>% filter(case_ == TRUE)
rdm_stps <- steps$Lavezzi %>% filter(case_ == FALSE)
mediterranean <- make_bbox(lat = y2_, lon = x2_, data = steps$Lavezzi)
map <- get_map(location = mediterranean) %>% ggmap()
png(paste0("figures/", colony, "_map.png"), width=4.4, height=4, units="in", res=200)
map + geom_point(data = rdm_stps, aes(x = x2_, y = y2_),
size = 0.1, color = "red", alpha = 0.1) +
geom_point(data = obs_stps, aes(x = x2_, y = y2_), size = 0.1) +
ggtitle(colony)
dev.off()
```
Taille des vols
```{r}
steps$Lavezzi %>% as_tibble() %>%
group_by(burst_) %>%
summarise(nb_stps = n_distinct(step_id_)) %>%
filter(nb_stps < 10)
```
```{r}
colony <- "Porquerolles"
print(colony)
modP <- models$Porquerolles$model ; modP
```
```{r echo=FALSE, results='asis'}
stargazer(modP, type = "latex", title = paste(
"Résumé du modèle de régression logistique conditionnel pour la colonie",
colony), summary = FALSE)
```
```{r message=FALSE, warning=FALSE}
obs_stps <- steps$Porquerolles %>% filter(case_ == TRUE)
rdm_stps <- steps$Porquerolles %>% filter(case_ == FALSE)
mediterranean <- make_bbox(lat = y2_, lon = x2_, data = steps$Porquerolles)
map <- get_map(location = mediterranean) %>% ggmap()
png(paste0("figures/", colony, "_map.png"), width=4.4, height=4, units="in", res=200)
map + geom_point(data = rdm_stps, aes(x = x2_, y = y2_),
size = 0.1, color = "red", alpha = 0.1) +
geom_point(data = obs_stps, aes(x = x2_, y = y2_), size = 0.1) +
ggtitle(colony)
dev.off()
```
```{r}
colony <- "Giraglia"
print(colony)
modG <- models$Giraglia$model ; modG
```
```{r echo=FALSE, results='asis'}
stargazer(modG, type = "latex", title = paste(
"Résumé du modèle de régression logistique conditionnel pour la colonie",
colony), summary = FALSE)
```
```{r message=FALSE, warning=FALSE}
obs_stps <- steps$Giraglia %>% filter(case_ == TRUE)
rdm_stps <- steps$Giraglia %>% filter(case_ == FALSE)
mediterranean <- make_bbox(lat = y2_, lon = x2_, data = steps$Giraglia)
map <- get_map(location = mediterranean) %>% ggmap()
png(paste0("figures/", colony, "_map.png"), width=4.4, height=4, units="in", res=200)
map + geom_point(data = rdm_stps, aes(x = x2_, y = y2_),
size = 0.1, color = "red", alpha = 0.1) +
geom_point(data = obs_stps, aes(x = x2_, y = y2_), size = 0.1) +
ggtitle(colony)
dev.off()
```
Taille des vols
```{r}
steps$Lavezzi %>% as_tibble() %>%
group_by(burst_) %>%
summarise(nb_stps = n_distinct(step_id_)) %>%
filter(nb_stps < 10)
```
# Utilization distribution
## Create a grid from the centroid coordinates
```{r eval=FALSE, include=FALSE}
# # Create an sf object from a data frame of more than two columns
# x <- grd[,c("Longitude", "Latitude","Bathy", "GBathy", "SST28", "GSST28", "Var_SST28",
# "SLA1", "VEL7", "logCHLA28", "GCHLA28")]
# poly <- eSDM::pts2poly_centroids(x, 0.0675/2, crs = 2154, agr = "constant")
#
# mediterranean <- make_bbox(lat = Latitude, lon = Longitude, data = grd)
# map <- get_map(location = mediterranean) %>% ggmap()
#
# # medit <- c(left = min(grd$Longitude),
# # bottom = min(grd$Latitude),
# # right = max(grd$Longitude),
# # top = max(grd$Latitude))
# # map <- get_stamenmap(medit, zoom = 7, "terrain-background") %>% ggmap()
#
#
# world <- ne_countries(scale = "medium", returnclass = "sf")
# ggplot(poly) +
# geom_sf(aes(fill = Bathy), color = NA)
```