forked from vjjan91/eBirdOccupancy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
10_visualize-occu-predictor-effects.Rmd
445 lines (380 loc) · 11.6 KB
/
10_visualize-occu-predictor-effects.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
445
---
editor_options:
chunk_output_type: console
---
# Visualizing Occupancy Predictor Effects
In this section, we will visualize the magnitude and direction of species-specific probability of occupancy.
## Prepare libraries
```{r load_libs_results01}
# to load data
library(readxl)
# to handle data
library(dplyr)
library(readr)
library(forcats)
library(tidyr)
library(purrr)
library(stringr)
# library(data.table)
# to wrangle models
source("R/fun_model_estimate_collection.r")
source("R/fun_make_resp_data.r")
# nice tables
library(knitr)
library(kableExtra)
# plotting
library(ggplot2)
library(patchwork)
source("R/fun_plot_interaction.r")
```
## Load species list
```{r }
# list of species
# Removing species after running a chi-square goodness of fit test
species <- read_csv("data/species_list.csv") %>%
filter(!scientific_name %in% c(
"Treron affinis", "Prinia hodgsonii", "Pellorneum ruficeps",
"Hypothymis azurea","Dendrocitta leucogastra","Chalcophaps indica",
"Rubigula gularis", "Muscicapa dauurica", "Geokichla citrina",
"Chrysocolaptes guttacristatus","Terpsiphone paradisi","Orthotomus sutorius",
"Oriolus kundoo", "Dicrurus aeneus", "Cyornis tickelliae",
"Copsychus fulicatus", "Oriolus xanthornus", "Alcippe poioicephala",
"Ficedula nigrorufa","Dendrocitta vagabunda", "Dicrurus paradiseus",
"Ocyceros griseus", "Psilopogon viridis", "Psittacula cyanocephala"))
list_of_species <- as.character(species$scientific_name)
```
## Show AIC weight importance
To get cumulative AIC weights, we first obtained a measure of relative importance of climatic and landscape predictors by calculating cumulative variable importance scores. These scores were calculated by obtaining the sum of model weights (AIC weights) across all models (including the top models) for each predictor across all species. We then calculated the mean cumulative variable importance score and a standard deviation for each predictor [@burnham2002a].
### Read in AIC weight data
```{r}
# which files to read
file_names <- c("data/results/lc-clim-imp.xlsx")
# read in sheets by species
model_imp <- map(file_names, function(f) {
md_list <- map(list_of_species, function(sn) {
# some sheets are not found
tryCatch(
{
readxl::read_excel(f, sheet = sn) %>%
`colnames<-`(c("predictor", "AIC_weight")) %>%
filter(str_detect(predictor, "psi")) %>%
mutate(
predictor = stringr::str_extract(predictor,
pattern = stringr::regex("\\((.*?)\\)")
),
predictor = stringr::str_replace_all(predictor, "[//(//)]", ""),
predictor = stringr::str_remove(predictor, "\\.y")
)
},
error = function(e) {
message(as.character(e))
}
)
})
names(md_list) <- list_of_species
return(md_list)
})
```
### Prepare cumulative AIC weight data
```{r}
# bind rows
model_imp <- map(model_imp, bind_rows) %>%
bind_rows()
# convert to numeric
model_imp$AIC_weight <- as.numeric(model_imp$AIC_weight)
# Let's get a summary of cumulative variable importance
model_imp <- group_by(model_imp, predictor) %>%
summarise(
mean_AIC = mean(AIC_weight),
sd_AIC = sd(AIC_weight),
min_AIC = min(AIC_weight),
max_AIC = max(AIC_weight),
med_AIC = median(AIC_weight)
)
# write to file
write_csv(model_imp, "data/results/cumulative_AIC_weights.csv")
```
Read data back in.
```{r}
# read data and make factor
model_imp <- read_csv("data/results/cumulative_AIC_weights.csv")
model_imp$predictor <- as_factor(model_imp$predictor)
```
```{r}
# make nice names
predictor_name <- tibble(
predictor = levels(model_imp$predictor),
pred_name = c(
"Precipitation seasonality",
"Temperature seasonality",
"% Evergreen Forest", "% Deciduous Forest",
"% Mixed/Degraded Forest", "% Agriculture/Settlements",
"% Grassland", "% Plantations", "% Water Bodies"
)
)
# rename predictor
model_imp <- left_join(model_imp, predictor_name)
```
Prepare figure for cumulative AIC weight. Figure code is hidden in versions rendered as HTML and PDF.
```{r echo=FALSE}
fig_aic <-
ggplot(model_imp) +
geom_pointrange(
aes(
x = reorder(predictor, mean_AIC),
y = mean_AIC,
ymin = mean_AIC - sd_AIC,
ymax = mean_AIC + sd_AIC
)
) +
geom_text(aes(
x = predictor,
y = 0.5,
label = pred_name
),
size = 3,
angle = 0,
hjust = 0.5,
vjust = 2
) +
# scale_y_continuous(breaks = seq(45, 75, 10))+
scale_x_discrete(labels = NULL) +
# scale_color_brewer(palette = "RdBu", values = c(0.5, 1))+
coord_flip(
ylim = c(0, 1)
# ylim = c(45, 75)
) +
theme_test() +
theme(legend.position = "none") +
labs(
x = "Predictor",
y = "Cumulative AIC weight"
)
ggsave(fig_aic,
filename = "figs/fig_aic_weight.png",
device = png(),
dpi = 300,
width = 79, height = 120, units = "mm"
)
```
## Prepare model coefficient data
For each species, we examined those models which had ΔAICc < 4, as these top models were considered to explain a large proportion of the association between the species-specific probability of occupancy and environmental drivers [@burnham2011; @elsen2017]. Using these restricted model sets for each species; we created a model-averaged coefficient estimate for each predictor and assessed its direction and significance [@MuMIn]. We considered a predictor to be significantly associated with occupancy if the range of the 95% confidence interval around the model-averaged coefficient did not contain zero.
```{r read_model_estimates}
file_read <- c("data/results/lc-clim-modelEst.xlsx")
# read data as list column
model_est <- map(list_of_species, function(sn) {
tryCatch(
{
readxl::read_excel(file_read, sheet = sn) %>%
rename(predictor = "...1") %>%
filter(str_detect(predictor, "psi")) %>%
mutate(
predictor = stringr::str_extract(predictor,
pattern = stringr::regex("\\((.*?)\\)")
),
predictor = stringr::str_replace_all(predictor, "[//(//)]", ""),
predictor = stringr::str_remove(predictor, "\\.y")
)
},
error = function(e) {
message(as.character(e))
}
)
})
# assign names
names(model_est) <- list_of_species
# prepare model data
model_data <- tibble(
scientific_name = list_of_species
)
# remove null data
model_est <- keep(model_est, .p = function(x) !is.null(x))
# rename model data components and separate predictors
names <- c(
"predictor", "coefficient", "se", "ci_lower",
"ci_higher", "z_value", "p_value"
)
# get data for plotting:
model_est <- map(model_est, function(df) {
colnames(df) <- names
# df <- separate_interaction_terms(df)
# df <- make_response_data(df)
return(df)
})
# add names and scales
model_est <- imap(model_est, function(.x, .y) {
mutate(.x, scientific_name = .y)
})
# remove modulators
model_est <- bind_rows(model_est) %>%
dplyr::select(-matches("modulator"))
# join data to species name
model_data <- model_data %>%
left_join(model_est)
# Keep only those predictors whose p-values are significant:
model_data <- model_data %>%
filter(p_value < 0.05) %>%
filter(predictor != "Int")
```
Export predictor effects.
```{r}
# get predictor effect data
data_predictor_effect <- distinct(
model_data,
scientific_name,
se,
predictor, coefficient
)
# write to file
write_csv(data_predictor_effect, "data/results/data_predictor_effect.csv")
```
Export model data.
```{r}
model_data_to_file <- model_data %>%
dplyr::select(
predictor,
scientific_name
)
# remove .y
model_data_to_file <- model_data_to_file %>%
mutate(predictor = str_remove(predictor, "\\.y"))
write_csv(
model_data_to_file,
"data/results/data_occupancy_predictors.csv"
)
```
Read in data after clearing R session.
```{r}
# first merge species trait data with significant predictor
species_trait <- read.csv("data/species-trait-dat.csv")
sig_predictor <- read.csv("data/results/data_predictor_effect.csv")
merged_species_traits <- inner_join(sig_predictor, species_trait,
by = c("scientific_name" = "scientific_name")
)
write_csv(
merged_species_traits,
"data/results/results-predictors-species-traits.csv"
)
# read from file
model_data <- read_csv("data/results/results-predictors-species-traits.csv")
```
Fix predictor name.
```{r}
# remove .y from predictors
model_data <- model_data %>%
mutate_at(.vars = c("predictor"), .funs = function(x) {
stringr::str_remove(x, ".y")
})
```
## Get predictor effects
```{r}
# is the coeff positive? how many positive per scale per predictor per axis of split?
# now splitting by habitat --- forest or open country
data_predictor <- mutate(model_data,
direction = coefficient > 0
) %>%
filter(predictor != "Int", predictor != "Ibio4^2" &
predictor != "Ibio15^2") %>%
rename(habitat = "Habitat.type") %>%
count(
predictor,
habitat,
direction
) %>%
mutate(mag = n * (if_else(direction, 1, -1)))
# wrangle data to get nice bars
data_predictor <- data_predictor %>%
dplyr::select(-n) %>%
drop_na(direction) %>%
mutate(direction = ifelse(direction, "positive", "negative")) %>%
pivot_wider(values_from = "mag", names_from = "direction") %>%
mutate_at(
vars(positive, negative),
~ if_else(is.na(.), 0, .)
)
data_predictor_long <- data_predictor %>%
pivot_longer(
cols = c("negative", "positive"),
names_to = "effect",
values_to = "magnitude"
)
# write
write_csv(
data_predictor_long,
"data/results/data_predictor_direction_nSpecies.csv"
)
```
Prepare data to determine the direction (positive or negative) of the effect of each predictor. How many species are affected in either direction?
```{r}
# join with predictor names and relative AIC
data_predictor_long <- left_join(data_predictor_long, model_imp)
```
Prepare figure of the number of species affected in each direction. Figure code is hidden in versions rendered as HTML and PDF.
```{r echo=FALSE}
# habitat labels
labels <- c(
"Generalist" = "Generalist Birds",
"Forest" = "Forest Birds"
)
fig_predictor <-
ggplot(model_imp) +
geom_hline(
yintercept = 0,
lwd = 0.2,
col = "grey"
) +
geom_col(
data = data_predictor_long,
aes(
x = reorder(predictor, mean_AIC),
y = magnitude,
fill = effect
),
# position = position_dodge(width = 1),
width = 0.3
) +
geom_text(aes(
x = predictor,
y = 0,
label = pred_name
),
angle = 0,
vjust = 2,
size = 3
) +
geom_text(
data = tibble(
x = c(5),
y = c(-30, 30),
label = c("Negative effect", "Positive effect")
),
aes(
x, y,
label = label
),
angle = 90
) +
scale_fill_discrete_diverging(
palette = "Berlin",
l1 = 50,
rev = T
) +
scale_x_discrete(labels = NULL) +
scale_y_continuous(
labels = abs,
limits = c(-30, 30)
) +
# uncomment below to split by habitat
facet_grid(~habitat, labeller = labeller(habitat = labels)) +
coord_flip() +
theme_test() +
theme(legend.position = "none") +
labs(x = "Environmental Covariate", y = "# Species")
ggsave(fig_predictor,
filename = "figs/fig_04.png",
dpi = 300,
width = 100, height = 120, units = "mm"
)
```
![**Environmental predictors and species-specific associations**
The direction of association between species-specific probability of occupancy and climatic and landscape predictors is shown here (as a function of habitat preference). Blue colors show the number of species that are positively associated with a climatic/landscape predictor while red colors show the number of species that are negatively associated with a climatic/landscape predictor (see Table 1 for the number of forest/generalist species that show positive/negative association with each of the predictors).](figs/fig_04.png)