-
Notifications
You must be signed in to change notification settings - Fork 4
/
11_predict-probOfOccupancy.Rmd
744 lines (636 loc) · 14.6 KB
/
11_predict-probOfOccupancy.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
---
editor_options:
chunk_output_type: console
---
# Predicting species-specific occupancy as a function of significant predictors
This script plots species-specific probabilities of occupancy as a function of significant environmental predictors and maps occupancy across the study area for a given list of species and significant predictors.
## Prepare libraries
```{r load_libs_results02}
# to handle data
library(dplyr)
library(readr)
library(tidyr)
library(purrr)
library(stringr)
library(glue)
# library(data.table)
# plotting
library(ggplot2)
library(patchwork)
```
## Read data
```{r}
# read coefficient effect data
data <- read_csv("results/10_data-predictor-effect.csv")
# check for a predictor column
assertthat::assert_that(
all(c("predictor", "coefficient", "se") %in% colnames(data)),
msg = "make_response_data: data must have columns called 'predictor',
'coefficient', and 'se'"
)
```
## Prepare predictor data
```{r}
# preparep predictors - now look only for any digits
predictors <- c("bio\\d+", glue("lc_0{seq(9)}"))
# prepare predictor search strings and scaling power
preds <- glue("({predictors})")
preds <- str_flatten(preds, collapse = "|")
# some way of identifying square terms
power <- (str_extract(data$predictor, "Ibio"))
power[!is.na(power)] = 2
power[is.na(power)] <- 1
power = as.numeric(power)
# assign predictor name and power
data <-
mutate(
data,
predictor = str_extract(predictor, preds),
power = power
)
```
## Get predictor responses
```{r}
# make predictor sequences
data <- mutate(
data,
pred_val = map(
predictor,
function(x) {
seq(0, 1, 0.05)
}
),
#handle squared terms
pred_val_pow = purrr::map2(
pred_val, power,
function(x, y) {
x^y
}
))
# get coefficient and error times terms
data_resp <- mutate(
data,
response = map2(
pred_val_pow, coefficient,
function(x, y) {
x * y
}
),
resp_var = map2(
pred_val_pow, se,
function(x, y) {
x * y
}
)
)
```
## Get probability of occupancy
```{r}
# unnest and get responses
data_resp <- unnest(
data_resp,
cols = c("response", "resp_var", "pred_val")
)
# get responses for quadratic terms
data_resp <-
group_by(
data_resp,
scientific_name, predictor, pred_val
) %>%
dplyr::select(-power, -coefficient, -se) %>%
summarise(
across(
.cols = c("response", "resp_var"),
.fns = sum
),
.groups = "keep"
)
# get probability of occupancy
data_resp <- ungroup(
data_resp
) %>%
mutate(
p_occu = 1 / (1 + exp(-response)),
p_occu_low = 1 / (1 + exp(-(response - resp_var))),
p_occu_high = 1 / (1 + exp(-(response + resp_var)))
)
```
## Add scaling for predictors
```{r}
# scale predictors
scale15 <- c(30, 50) # range of precipitation
scale4 <- c(0, 1) # range of temperatures
# scale bio4a and bio15a by actual values
data_resp <- mutate(
data_resp,
pred_val = case_when(
predictor == "bio4" ~ scales::rescale(pred_val, to = scale4),
predictor == "bio15" ~ scales::rescale(pred_val, to = scale15),
T ~ pred_val
)
)
# make long
data_poccu <- dplyr::select(
data_resp,
-response, -resp_var
)
```
```{r}
# select species
soi <- c("Irena puella","Leptocoma minima", "Merops leschenaulti","Myophonus horsfieldii")
which_predictors <- c("bio4")
```
### Figure: Occupancy ~ predictors
```{r}
data_fig <- data_poccu %>%
filter(
scientific_name %in% soi,
predictor %in% which_predictors
) %>%
mutate(
cat = case_when(
scientific_name %in% c("Irena puella","Leptocoma minima") ~ "forest",
T ~ "general"
)
)
# split data
data_fig <- nest(
data_fig,
-cat
)
```
```{r}
# make plots
make_occu_fig <- function(df, this_fill) {
ggplot(
df
) +
geom_ribbon(
aes(
pred_val,
ymin = p_occu_low,
ymax = p_occu_high
),
fill = this_fill,
alpha = 0.5
) +
geom_line(
aes(
pred_val, p_occu
),
size = 1
) +
facet_grid(
~scientific_name
) +
theme_test(
base_family = "Arial"
) +
theme(
strip.text = element_text(
face = "italic"
)
) +
labs(
x = "Temperature seasonality",
y = "Probability of occupancy"
)
}
fig_occu <- map2(data_fig$data, "grey", make_occu_fig)
fig_occu <-
wrap_plots(
fig_occu[c(1, 2)],
ncol = 1, nrow = 2
) &
theme(
plot.tag = element_text(
face = "bold"
)
)
# save figure
ggsave(
fig_occu,
filename = "figs/fig_05.png",
width = 5, height = 5.5
)
```
![**Probability of occupancy as a function of temperature seasonality.**
Predicted probability of occupancy curves as a function of temperature seasonality for four forest species are shown here. Temperature seasonality is negatively associated with the probability of occupancy of several forest species including the asian fairy-bluebird _Irena puella_, the crimson-backed sunbird _Leptocoma minima_, the chestnut-headed bee-eater _Merops leschenaulti_ and the Malabar whistling-thrush _Myophonus horsfieldii_.](figs/fig_05.png)
### Figures: Occupancy ~ predictors for all species
```{r}
data_fig <- nest(
data_poccu,
-scientific_name, -predictor
)
pred_names <- c(
"bio4" = "Temp. seasonality",
"bio15" = "Precip. seasonality",
"lc_01" = "Evergreen",
"lc_02" = "Deciduous",
"lc_03" = "Mixed/degraded",
"lc_04" = "Agri./Settl.",
"lc_05" = "Grassland",
"lc_07" = "Plantation",
"lc_09" = "Water"
)
pred_names <- tibble(
name = pred_names,
predictor = names(pred_names)
)
data_fig <- left_join(
data_fig,
pred_names
)
data_fig <- mutate(
data_fig,
plots = map(
data, function(df) {
ggplot(df) +
geom_ribbon(
aes(
pred_val,
ymin = p_occu_low,
ymax = p_occu_high
),
fill = "grey",
alpha = 0.5
) +
geom_line(
aes(
pred_val, p_occu
)
) +
coord_cartesian(
ylim = c(0, 1)
) +
theme_test(
base_family = "Arial"
) +
labs(
x = "Predictor",
y = "p(Occupancy)"
)
}
)
)
# add names
data_fig <- mutate(
data_fig,
plots = map2(
plots, name,
function(p, name) {
p <- p + labs(
x = name
)
}
)
)
# summarise as patchwork
data_fig <- group_by(
data_fig,
scientific_name
) %>%
summarise(
plots = list(
wrap_plots(
plots,
ncol = 5
)
)
)
# add title as sp
data_fig <- mutate(
data_fig,
plots = map2(
plots, scientific_name,
function(p, name) {
p <- p & plot_annotation(
title = name
)
}
)
)
```
```{r}
# save images
cairo_pdf(
filename = "figs/fig_occupancy_predictors.pdf",
onefile = TRUE, width = 10, height = 2
)
data_fig$plots
dev.off()
```
## Mapping species occupancy
### Read in raster layers
```{r}
library(terra)
library(sf)
```
```{r}
# read saved rasters
lscape = rast("data/spatial/landscape_resamp01_km.tif")
# isolate temperature and rainfall
bio4 = lscape[[4]]
bio15 = lscape[[5]] # rain
# careful while loading this raster, large size
landcover <- rast("data/landUseClassification/landcover_roy_2015_reclassified.tif")
lc_1km <- rast("data/landUseClassification/lc_01000m.tif")
```
### Split landcover into proportions per 1km
```{r}
# separate the fine-scale landcover raster into presence-absence of each class
lc_split <- segregate(landcover)
# resample to 1km
# bilinear resampling uses the mean function.
# mean of N 0s and 1s is the proportion of 1s, ie, proportion of each landcover
lc_split <- terra::resample(
lc_split,
lc_1km,
method = "bilinear"
)
# rename rasters
names(lc_split) <- pred_names$name[-c(1, 2)]
# save raster of landcover proportion
terra::writeRaster(
lc_split,
filename = "data/spatial/raster_landcover_proportion_1km.tif",
overwrite=TRUE
)
rm(landcover)
gc()
```
```{r eval=FALSE}
# plot proportion of landcover classes
png(width = 1200 * 2, height = 1200 * 2, filename = "figs/fig_landcover_proportion_1km.png", res = 300)
plot(
lc_split,
col = colorspace::sequential_hcl(20, palette = "Viridis"),
range = c(0, 1)
)
dev.off()
```
### Prepare climatic layers
```{r}
# load landcover split
lc_split = terra::rast("data/spatial/raster_landcover_proportion_1km.tif")
```
### Mask by study area
```{r}
# mask by hills
# run only if required (makes more sense to map to a larger area)
hills = st_read("data/spatial/hillsShapefile/Nil_Ana_Pal.shp") %>%
st_transform(32643)
#bio_1 = terra::mask(
# bio_1,
# vect(hills)
#)
#bio_12 = terra::mask(
# bio_12,
# vect(hills)
#)
```
```{r}
# get ranges
range4 <- terra::minmax(bio4)[, 1]
range15 <- terra::minmax(bio15)[, 1]
# rescale
bio4 <- (bio4 - min(range4)) / (diff(range4))
bio15 <- (bio15 - min(range15)) / (diff(range15))
# project to UTM
climate <- c(bio4, bio15)
names(climate) = c(
"Temp. seasonality",
"Precip. seasonality"
)
climate <- terra::project(
x = climate, y = lc_1km
)
# make squared terms
climate2 <- climate * climate
# names
names(climate2) = glue("{names(climate)} 2")
# add to landcover proportions and plot
landscape <- c(climate, lc_split)
```
### Plot full bounds of landscape variables
```{r eval=FALSE}
# plot proportion of landcover classes
png(
width = 1200 * 2, height = 1200 * 2, filename = "figs/fig_landscape_1km.png",
res = 300
)
plot(
landscape,
col = colorspace::sequential_hcl(20, palette = "agSunset", rev = T),
range = c(0, 1)
)
dev.off()
```
```{r}
# add squared terms
landscape <- c(
climate, climate2, lc_split
)
#landscape = terra::mask(
# landscape,
# vect(hills)
#)
```
### Prepare soi predictors
Prepare the soi predictor coefficients as a vector of the same length as the number of raster layers.
These will be multiplied with each layer to give the effect of each layer.
```{r}
# get soi coefs
sp_coefs <- filter(
data
) %>%
dplyr::select(
-pred_val, -pred_val_pow
)
# add missing landcover classes
sp_preds <- crossing(
scientific_name = soi,
predictor = pred_names$predictor,
power = c(1, 2)
)
# remove squared terms for landcover
sp_preds <- filter(
sp_preds,
!(str_detect(predictor, "lc") & power == 2)
)
# correct square LC terms
sp_coefs = mutate(
sp_coefs,
power = if_else(
str_detect(predictor, "lc"),
1,
power
)
)
sp_coefs <- full_join(
sp_coefs,
sp_preds
)
# make wide --- this should give no warnings
sp_coefs <-
pivot_wider(
sp_coefs,
id_cols = c("scientific_name"),
names_from = c("predictor", "power"),
values_from = "coefficient"
)
# get into order
sp_coefs <- dplyr::select(
sp_coefs,
scientific_name,
c(
"bio4_1", "bio15_1",
"bio4_2", "bio15_2"
),
matches("lc")
)
# get vectors of coefficients
sp_coefs <- nest(
sp_coefs,
-scientific_name
)
```
### Prepare species occupancy for SOI
Here, we shall simply multiply each landscape layer with the corresponding predictor coefficient. Where these are not available, we shall simply multiply the corresponding layer with NA. The resulting layers will be summed together to get a single response layer, which will then be inverse logit transformed to get the probability of occupancy.
```{r}
# multiply coefficients with layers
soi_occu <- map(
sp_coefs[sp_coefs$scientific_name %in% soi, ]$data,
.f = function(df) {
response <- unlist(slice(df, 1), use.names = F) * landscape
response <- sum(response, na.rm = TRUE) # remove NA layers, i.e., non-sig preds
# now transform for probability occupancy
response <- 1 / (1 + exp(-response))
}
)
# assign names
names(soi_occu) <- soi
# make single stack
soi_occu <- reduce(soi_occu, c)
names(soi_occu) <- c("Irena puella","Leptocoma minima","Merops leschenaulti","Myophonus horsfieldii")
```
```{r}
# use stars for plotting with ggplot
library(stars)
library(colorspace)
soi_occu <- st_as_stars(soi_occu)
fig_occu_map <- ggplot() +
geom_stars(
data = soi_occu
) +
scale_fill_binned_sequential(
palette = "Purple-Yellow",
name = "Probability of Occupancy",
rev = T,
limits = c(0, 1),
breaks = seq(0, 1, 0.1),
na.value = "grey99",
show.limits = T
) +
facet_wrap(
~band,
labeller = labeller(
band = function(x) str_replace(x, "\\.", " ")
)
) +
coord_sf(
crs = 32643,
expand = FALSE
) +
theme_test() +
theme(
# legend.position = "rg",
axis.title = element_blank(),
axis.text = element_blank(),
legend.key.height = unit(10, "mm"),
legend.key.width = unit(1, "mm"),
strip.text = element_text(
face = "italic"
),
legend.title = element_text(
vjust = 1
)
)
# save figure
ggsave(
fig_occu_map,
filename = "figs/fig_06.png",
width = 6, height = 6
)
```
![**Predicted area of occurrence**
Predicted area of occurrence for four forest species are shown here. The probability of occupancy of the asian fairy-bluebird _Irena puella_, the crimson-backed sunbird _Leptocoma minima_ and the chestnut-headed bee-eater _Merops leschenaulti_ is higher across the western slopes and at mid-elevations across our study area. The Malabar whistling-thrush _Myophonus horsfieldii_ has a higher probability of occupancy across mid-elevations throughout the study area examined.](figs/fig_06.png)
### Prepare species occupancy for all species
```{r}
# multiply coefficients with layers
sp_occu <- map(
sp_coefs$data,
.f = function(df) {
response <- unlist(slice(df, 1), use.names = F) * landscape
response <- sum(response, na.rm = TRUE) # remove NA layers, i.e., non-sig preds
# now transform for probability occupancy
response <- 1 / (1 + exp(-response))
}
)
# make single stack
sp_occu <- reduce(sp_occu, c)
# assign names
names(sp_occu) <- sp_coefs$scientific_name
```
```{r}
# use stars for plotting with ggplot
library(stars)
library(colorspace)
sp_occu <- st_as_stars(sp_occu)
fig_occu_map_all <-
ggplot() +
geom_stars(
data = sp_occu
) +
scale_fill_binned_sequential(
palette = "Purple-Yellow",
name = "p(Occu.)",
rev = T,
limits = c(0, 1),
na.value = "grey99",
breaks = seq(0, 1, 0.1), show.limits = T
) +
facet_wrap(
~band,
labeller = labeller(
band = function(x) str_replace(x, "\\.", " ")
)
) +
coord_sf(
crs = 32643,
expand = FALSE
) +
theme_test(
base_size = 8
) +
theme(
# legend.position = "rg",
axis.title = element_blank(),
axis.text = element_blank(),
legend.key.height = unit(10, "mm"),
legend.key.width = unit(1, "mm"),
strip.text = element_text(
face = "italic"
),
strip.background = element_blank(),
legend.title = element_text(
vjust = 1
)
)
# save figure
ggsave(
fig_occu_map_all,
filename = "figs/fig_occupancy_maps.png",
width = 16, height = 16
)
```