-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupplementary_material_2.Rmd
1522 lines (932 loc) · 44.7 KB
/
Supplementary_material_2.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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Luypaert et al. (2021) - Supplementary Material 3"
author: '"Thomas Luypaert - Norwegian University of Life Sciences"'
date: "9/23/2021"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Introduction
This RMarkdown file contains the code which accompanies the supplementary material S3 provided in Luypaert et al. (2021).
## Loading the required packages & downloading the raw data
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE}
# Loading packages
library(readr)
library(data.table)
library(knitr)
library(hilldiv)
library(ggplot2)
library(viridis)
library(dataone)
library(iNEXT)
library(devtools)
library(dataone)
library(dplyr)
library(soundscapeR)
library(ggpubr)
library(ggforce)
library(scales)
library(patchwork)
library(codyn)
library(ggpmisc)
library(purrr)
library(magrittr)
```
```{r}
# Downloading data from the KNB repository
cn <- CNode()
mn <- getMNode(cn, "urn:node:KNB")
queryParamList <- list(q="id:doi*10.5063/F1MS3R6W", fl="id,title")
result <- query(mn, solrQuery=queryParamList, as="data.frame")
packagePid <- result[1,1]
cn <- CNode()
mn <- getMNode(cn, "urn:node:KNB")
bagitFileName <- getPackage(mn, id=packagePid)
bagitFileName <- gsub("\\\\", "/", as.character(bagitFileName))
to_unzip <- unzip(zipfile = bagitFileName, list = TRUE)
unzip(zipfile = bagitFileName,
files = to_unzip[c(3, 5, 7, 8),1],
exdir = paste0(gsub(sub(".*/", "", bagitFileName), "", bagitFileName), "extracted"))
list.dirs.depth.n <- function(p, n) {
res <- list.dirs(p, recursive = FALSE)
if (n > 1) {
add <- list.dirs.depth.n(res, n-1)
c(res, add)
} else {
res
}
}
locations <- list.files(list.dirs.depth.n(p=paste0(gsub(sub(".*/", "", bagitFileName), "", bagitFileName), "extracted"), n=2), full.names = TRUE)
unzip(zipfile = bagitFileName,
files = to_unzip[4,1],
exdir = paste0(gsub(sub(".*/", "", bagitFileName), "", bagitFileName), "extracted"))
locations <- list.files(list.dirs.depth.n(p=paste0(gsub(sub(".*/", "", bagitFileName), "", bagitFileName), "extracted"), n=2), full.names = TRUE)
unzip(zipfile = locations[1],
exdir = paste0(gsub(sub(".*/", "", bagitFileName), "", bagitFileName), "extracted", "/data/",gsub("\\.zip", "", basename(locations[1]))))
locations <- list.files(list.dirs.depth.n(p=paste0(gsub(sub(".*/", "", bagitFileName), "", bagitFileName), "extracted"), n=2), full.names = TRUE)
```
## 0. Compiling the taxonomic richness data
### 0.1. Anuran data
The anuran data was derived from the aural and visual identification of anuran vocalisations by a trained expert (Gabriel Masseli) using the RFCx ARBIMON Visualizer Tool (ENTER LINK). All species identities were cross-verified by second observer (Igor Kaefer) to ensure accuracy. The data can be downloaded from the ARBIMON Platform directly using aforementioned link. The compiled data is also available on the KNB repository (see below).
#### Load the data into R
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
frog_man <- read_csv(locations[4], show_col_types = FALSE)
```
#### Remove rows containing 'None' for the species
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
frog_man <- frog_man[!frog_man$species=="None",]
```
#### Remove all the riparian sites from the data
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
frog_man <- frog_man[-grep("_Rip", frog_man$plot), ]
```
#### Subset to contain only the plots used in the soundscape study
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
frog_man$plot <- gsub("-", "_", frog_man$plot)
frog_man$plot <- gsub("Waba", "WABA", frog_man$plot)
wanted <- c("Abusado",
"Adeus_A",
"Adeus_B",
"Aline",
"Andre",
"Arrepiado",
"Bacaba_B",
"Beco_do_Catitu_A",
"Beco_do_Catitu_B",
"Beco_do_Catitu_D",
"Beco_do_Catitu_E",
"Cafundo",
"CF_Grid_CampTrail_A",
"CF_Grid_NS3_1200",
"CF_Loreno_A",
"CF_Loreno_B",
"CF_WABA_B",
"CF_WABA_C",
"Cipoal_A",
"Cipoal_B",
"Cipoal_C",
"Coata",
"Formiga",
"Furo_de_Santa_Luzia_B",
"Furo_de_Santa_Luzia_C",
"Fuzaca_B",
"Fuzaca_C",
"Fuzaca_D",
"Garrafa",
"Gaviao_real_A",
"Gaviao_real_B",
"Gaviao_real_C",
"Gaviao_real_D",
"Jabuti_A",
"Jabuti_B",
"Jabuti_C",
"Jiquitaia",
"Joaninha",
"Martelo_B",
"Martelo_C",
"Mascote_A1",
"Mascote_A2",
"Mascote_B1",
"Mascote_B2",
"Moita_A",
"Moita_B",
"Palhal",
"Panema",
"Pe_Torto",
"Piquia",
"Pontal_B",
"Pontal_C",
"Porto_Seguro_B",
"Porto_Seguro_C",
"Porto_Seguro_D",
"Relogio_B",
"Sapupara_A",
"Sapupara_B",
"Torem",
"Tristeza_A",
"Tristeza_B",
"Tristeza_C",
"Tucumari_A",
"Tucumari_B",
"Tucumari_C")
frog_man <- frog_man[with(frog_man, plot %in% wanted ),]
```
#### Count the number of unique frog species per site
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
frog_df_richness <- aggregate(data = frog_man,
species ~ site,
function(x) length(unique(x)))
```
#### Create a site by species list
```{r}
colnames(frog_df_richness) <- c("site", "anuran_richness")
frog_df_richness$site <- toupper(frog_df_richness$site)
knitr::kable(frog_df_richness, caption = "Anuran taxonomic species richness per island", align = "l")
```
### 0.2. Avian data
The avian data was derived from the pattern matching algorithm available on the RFCx ARBIMON Platform: <https://arbimon.rfcx.org/project/balbina>, combined with visual and aural verification by a trained expert (Marconi Campos-Cerqueira). The data can be downloaded from the ARBIMON Platform directly using aforementioned link. The compiled data is also available on the KNB repository (see below).
#### Load the data into R
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
bird_files <- list.files(path = locations[3], full.names = TRUE)
bird_list <- vector("list", length = length(bird_files))
for (i in 1:length(bird_list)){
bird_list[[i]] <- read_csv(bird_files[i], show_col_types = FALSE)
}
names(bird_list) <- gsub(".csv", "", basename(bird_files))
```
#### Modify the site names to match the sites in the soundscape study
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
for (i in 1:length(bird_list)){
bird_list[[i]]$site <- gsub('\\.', '_', bird_list[[i]]$site)
bird_list[[i]]$site <- gsub('-', '_', bird_list[[i]]$site)
}
```
#### Filter the data by sites in the soundscape study
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
for (i in 1:length(bird_list)){
bird_list[[i]] <- bird_list[[i]][with(bird_list[[i]], site %in% wanted ),]
}
```
#### Filter to retain only the verified records
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
for (i in 1:length(bird_list)){
bird_list[[i]] <- bird_list[[i]][bird_list[[i]]$validated=="present",]
}
```
#### Add a species column to the data and rbind
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
for (i in 1:length(bird_list)){
bird_list[[i]]$species <- names(bird_list)[i]
}
bird_df <- rbindlist(bird_list)
```
#### Make the species names uniform across call types
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
bird_df$species <- gsub("1|2|_call|_duet|_nocturnal", "", bird_df$species)
bird_df$species[bird_df$species=="Glyphorynchus_spirurus"] <- "Glyphorynchus_spirurus"
bird_df$species[bird_df$species=="Glyphorhynchus_spirurus"] <- "Glyphorynchus_spirurus"
```
#### Make the plot names uniform per island
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
island_name <- bird_df$site
island_name <- gsub(pattern = "_A$", "", island_name)
island_name <- gsub(pattern = "_A.$", "", island_name)
island_name <- gsub(pattern = "_B$", "", island_name)
island_name <- gsub(pattern = "_B.$", "", island_name)
island_name <- gsub(pattern = "_C$", "", island_name)
island_name <- gsub(pattern = "_D$", "", island_name)
island_name <- gsub(pattern = "_E$", "", island_name)
island_name <- gsub(pattern = "_CampTrail$", "", island_name)
island_name <- gsub(pattern = "_NS3_1200$", "", island_name)
bird_df$site <- island_name
```
#### Count the number of unique species per island
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
bird_df_richness <- aggregate(data = bird_df,
species ~ site,
function(x) length(unique(x)))
```
#### Create a site by species list
```{r}
colnames(bird_df_richness) <- c("site", "avian_richness")
bird_df_richness$site <- toupper(bird_df_richness$site)
knitr::kable(bird_df_richness, caption = "Avian taxonomic species richness per island", align = "l")
```
### 0.3. Monkey data
The monkey taxonomic richness data was compiled from a previous study in the area which collected taxonomic data for terresrial vertebrates (Benchimol & Peres 2015). For more information, consult the main manuscript of Luypaert et al. (2021) or head on over to the original paper: <https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0129818#sec014>
#### Load the data into R
```{r}
monkey_df_richness <- read_csv(locations[6], show_col_types = FALSE)
colnames(monkey_df_richness) <- c("site", "primate_richness")
monkey_df_richness$site <- gsub("-", "_", monkey_df_richness$site)
monkey_df_richness$site <- toupper(monkey_df_richness$site)
knitr::kable(monkey_df_richness, caption = "Primate taxonomic richness per island")
```
### 0.4. Compiling the taxonomic richness data into a single metric
```{r}
total_richness <- merge(
merge(frog_df_richness, bird_df_richness, by="site"),
monkey_df_richness, by="site")
total_richness$total_tax <- total_richness$anuran_richness + total_richness$avian_richness + total_richness$primate_richness
knitr::kable(total_richness, caption = "Total taxnomic richness of soniferous species per island")
```
## 2. Assessing the effect of threshold choice on the functional soundscape richness – taxonomic richness relationship
Choosing the binarization threshold which is used to obtain detection / non-detection values for each OSU in the 24-hour functional trait space constitutes an import step in our workflow. The choice of this threshold value will depend on the sound transmission characteristics of the habitat under investigation, and the amount and type of background noise in the environment. The ideal threshold value will remove the influence of low-amplitude and transient or short-duration noise on the functional soundscape diversity metrics described in this study, thus increasing the sensitivity to the biophonic taxonomic diversity. Several thresholding approaches exist to achieve this objective. Here, we investigated various approaches and how they influence the observed relationship between the functional soundscape richness and the taxonomic richness of soniferous species. The preferred thresholding method is the one which increases the sensitivity of the proposed metrics to the diversity of soniferous species.
### 2.1. Loading the chronologically concatenated CVR-files from before
Since we already computed the CVR-indices and concatenated then chronologically in the RMarkdown of the case study, to save time, here we will simply load these data into R.
```{r, echo = FALSE, results='hide', error=FALSE, warning=FALSE, message=FALSE}
load(locations[5])
merged_csv_list_1_5 <- merged_csv_list_256
```
### 2.1. Applying a constant threshold value across all sites
Since we already loaded the chronologically concatenated CVR-index dataframes per site, we will start here at the binarisation step. First, we'll try a range of threshold values between 0.01-0.5, the threshold value being equal for all sites in the study.
#### Binarisation of CVR-index data using a range of thresholds
```{r}
thresh_values_1 <- seq(0.01, 0.5, 0.01)
thresh_list_1 <- vector("list", length = length(thresh_values_1))
for (i in 1:length(thresh_list_1)){
thresh_list_1[[i]] <- merged_csv_list_1_5
}
names(thresh_list_1) <- thresh_values_1
for (i in 1:length(thresh_list_1)){
for (j in 1:length(thresh_list_1[[i]])){
thresh_list_1[[i]][[j]] <- binarize_df(merged_soundscape = thresh_list_1[[i]][[j]], method = "custom", value = thresh_values_1[i])
}
names(thresh_list_1[[i]]) <- names(merged_csv_list_1_5)
}
```
#### Separating the OSUs into 24-hour samples of acoustic trait space + subsetting to 14,300 Hz
```{r}
sampling_duration_list_threshold_1 <- vector("list", length(thresh_list_1))
for (i in 1:length(sampling_duration_list_threshold_1)){
sampling_duration_list_threshold_1[[i]] <- vector("list", length(thresh_list_1[[i]]))
}
duration_start <- seq(1, 2593, 288)
duration_end <- seq(288, 2880, 288)
for (i in 1:length(sampling_duration_list_threshold_1)){
for (j in 1:length(sampling_duration_list_threshold_1[[i]])){
sampling_duration_list_threshold_1[[i]][[j]] <- vector("list", length = length(duration_end))
for (k in 1:length(duration_start)){
sampling_duration_list_threshold_1[[i]][[j]][[k]] <- thresh_list_1[[i]][[j]]
}
names(sampling_duration_list_threshold_1[[i]][[j]]) <- seq(1, length(duration_start), 1)
}
names(sampling_duration_list_threshold_1[[i]]) <- names(merged_csv_list_1_5)
}
names(sampling_duration_list_threshold_1) <- thresh_values_1
for (i in 1:length(sampling_duration_list_threshold_1)){
for (j in 1:length(sampling_duration_list_threshold_1[[i]])){
for (k in 1:length(sampling_duration_list_threshold_1[[i]][[j]])){
if(ncol(sampling_duration_list_threshold_1[[i]][[j]][[k]]@merged_df) >= duration_end[k]){
sampling_duration_list_threshold_1[[i]][[j]][[k]]@merged_df <- sampling_duration_list_threshold_1[[i]][[j]][[k]]@merged_df[46:128,duration_start[k]:duration_end[k]]
sampling_duration_list_threshold_1[[i]][[j]][[k]]@binarized_df <- sampling_duration_list_threshold_1[[i]][[j]][[k]]@binarized_df[46:128,duration_start[k]:duration_end[k]]
}
else{
sampling_duration_list_threshold_1[[i]][[j]][[k]] <- as.list(c(NA))
}
}
}
}
for (i in 1:length(sampling_duration_list_threshold_1)){
for (j in 1:length(sampling_duration_list_threshold_1[[i]])){
if (length(which(sapply(sampling_duration_list_threshold_1[[i]], function(x) is.list(x))))==0){
sampling_duration_list_threshold_1[[i]][[j]] <- sampling_duration_list_threshold_1[[i]][[j]]
}
else{
sampling_duration_list_threshold_1[[i]][[j]] <- sampling_duration_list_threshold_1[[i]][[j]][-which(sapply(sampling_duration_list_threshold_1[[i]][[j]], function(x) is.list(x)))]
}
}
}
names(sampling_duration_list_threshold_1) <- thresh_values_1
```
#### Converting the data into an OSU-by-sample incidence matrix
```{r}
inc_mat_threshold_1 <- vector("list", length(sampling_duration_list_threshold_1))
for (i in 1:length(sampling_duration_list_threshold_1)){
inc_mat_threshold_1[[i]] <- vector("list", length(sampling_duration_list_threshold_1[[i]]))
}
for (i in 1:length(sampling_duration_list_threshold_1)){
for (j in 1:length(sampling_duration_list_threshold_1[[i]])){
inc_mat_threshold_1[[i]][[j]] <- vector("list", length(sampling_duration_list_threshold_1[[i]][[j]]))
}
}
for (i in 1:length(inc_mat_threshold_1)){
for (j in 1:length(inc_mat_threshold_1[[i]])){
for (k in 1:length(inc_mat_threshold_1[[i]][[j]])){
inc_mat_threshold_1[[i]][[j]][[k]] <- unlist(sampling_duration_list_threshold_1[[i]][[j]][[k]]@binarized_df)
sampling_duration_list_threshold_1[[i]][[j]][[k]] <- NA
}
inc_mat_threshold_1[[i]][[j]] <- as.data.frame(t(data.frame(do.call(rbind, inc_mat_threshold_1[[i]][[j]]))))
rownames(inc_mat_threshold_1[[i]][[j]]) <- paste0("OSU", seq(1, nrow(inc_mat_threshold_1[[i]][[j]]), 1))
inc_mat_threshold_1[[i]][[j]] <- as.matrix(inc_mat_threshold_1[[i]][[j]])
sampling_duration_list_threshold_1[[i]][[j]] <- NA
}
}
sampling_duration_list_threshold_1 <- NA
names(inc_mat_threshold_1) <- thresh_values_1
for (i in 1:length(inc_mat_threshold_1)){
names(inc_mat_threshold_1[[i]]) <- wanted
}
```
#### Combine OSU incidence matrices per island
```{r, echo=FALSE, results='hide', warning=FALSE, message=FALSE, error=FALSE}
list_name <- names(inc_mat_threshold_1[[1]])
list_name <- gsub(pattern = "_A$", "", list_name)
list_name <- gsub(pattern = "_A.$", "", list_name)
list_name <- gsub(pattern = "_B$", "", list_name)
list_name <- gsub(pattern = "_B.$", "", list_name)
list_name <- gsub(pattern = "_C$", "", list_name)
list_name <- gsub(pattern = "_D$", "", list_name)
list_name <- gsub(pattern = "_E$", "", list_name)
list_name <- gsub(pattern = "_CampTrail$", "", list_name)
list_name <- gsub(pattern = "_NS3_1200$", "", list_name)
for (i in 1:length(inc_mat_threshold_1)){
names(inc_mat_threshold_1[[i]]) <- list_name
}
for (i in 1:length(inc_mat_threshold_1)){
inc_mat_threshold_1[[i]] <- split(inc_mat_threshold_1[[i]], names(inc_mat_threshold_1[[i]])) %>% map(cbind.data.frame)
}
for (i in 1:length(inc_mat_threshold_1)){
for (j in 1:length(inc_mat_threshold_1[[i]])){
inc_mat_threshold_1[[i]][[j]] <- as.matrix(inc_mat_threshold_1[[i]][[j]])
}
}
```
#### Rarefaction 8 sampling days and calculate the functional soundscape diversity metrics
```{r, echo = FALSE, results='hide', error=FALSE}
rarefied_sounddiv_threshold_1 <- inc_mat_threshold_1
for (i in 1:length(rarefied_sounddiv_threshold_1)){
for (j in 1:length(rarefied_sounddiv_threshold_1[[i]])){
rarefied_sounddiv_threshold_1[[i]][[j]] <- NA
}
}
for (i in 1:length(inc_mat_threshold_1)){
for (j in 1:length(inc_mat_threshold_1[[i]])){
rarefied_sounddiv_threshold_1[[i]][[j]] <- estimateD(x = inc_mat_threshold_1[[i]][[j]],
datatype = "incidence_raw",
base = "size",
level = 8
)
rarefied_sounddiv_threshold_1[[i]][[j]] <- distinct(rarefied_sounddiv_threshold_1[[i]][[j]][,2:ncol(rarefied_sounddiv_threshold_1[[i]][[j]])])
rarefied_sounddiv_threshold_1[[i]][[j]]$site <- names(rarefied_sounddiv_threshold_1[[i]])[j]
}
inc_mat_threshold_1[[i]] <- NA
rarefied_sounddiv_threshold_1[[i]] <- rbindlist(rarefied_sounddiv_threshold_1[[i]])
}
rarefied_sounddiv_threshold_1_q0 <- rarefied_sounddiv_threshold_1
for (i in 1:length(rarefied_sounddiv_threshold_1_q0)){
rarefied_sounddiv_threshold_1_q0[[i]] <- subset(rarefied_sounddiv_threshold_1_q0[[i]],
rarefied_sounddiv_threshold_1_q0[[i]]$order==0)
}
names(rarefied_sounddiv_threshold_1_q0) <- thresh_values_1
for (i in 1:length(rarefied_sounddiv_threshold_1_q0)){
rarefied_sounddiv_threshold_1_q0[[i]]$threshold <- thresh_values_1[i]
}
rarefied_sounddiv_threshold_1_q0_total <- rbindlist(rarefied_sounddiv_threshold_1_q0)
```
#### Assessing which constant threshold value yield the most normal distribution
```{r}
for (i in 1:length(rarefied_sounddiv_threshold_1_q0)){
rarefied_sounddiv_threshold_1_q0[[i]] <- rarefied_sounddiv_threshold_1_q0[[i]][,-c(1,2,3)]
}
# Calculate normality of data
normal_df <- vector("list", length(rarefied_sounddiv_threshold_1_q0))
for (i in 1:length(normal_df)){
normal_df[[i]] <- as.data.frame(t(as.data.frame(
c(shapiro.test(rarefied_sounddiv_threshold_1_q0[[i]]$qD)$statistic,
shapiro.test(rarefied_sounddiv_threshold_1_q0[[i]]$qD)$p.value)))
)
normal_df[[i]]$threshold <- names(rarefied_sounddiv_threshold_1_q0)[i]
colnames(normal_df[[i]]) <- c("w", "p", "threshold")
}
normal_df <- rbindlist(normal_df)
# Check which values are insignificant
print(normal_df[which(normal_df$p < 0.05),])
# Make plots
shap_p <-
ggplot(normal_df, aes(as.numeric(threshold), as.numeric(p))) +
annotate(geom = "rect",
ymin=-Inf, ymax=Inf,
xmin=-Inf, xmax=0.03,
fill = "red",
alpha = 0.2) +
annotate(geom = "rect",
ymin=-Inf, ymax=Inf,
xmin=0.03, xmax=0.34,
fill = "palegreen",
alpha = 0.2) +
annotate(geom = "rect",
ymin=-Inf, ymax=Inf,
xmin=0.34, xmax=Inf,
fill = "red",
alpha = 0.2) +
geom_vline(aes(xintercept= as.numeric(normal_df[which(normal_df$p == max(normal_df$p)),]$threshold)),
linetype="dashed",
color="darkred",
size=1) +
geom_line(size=1) +
geom_point(shape=21,
fill="white",
stroke=1.5,
size=2,
color="black") +
theme_classic() +
xlab("") +
ylab("Shapiro-Wilk p-value \n")+
theme(axis.text.x = element_text(size=12),
axis.text.y = element_text(size=12),
axis.title.x = element_text(size=14),
axis.title.y = element_text(size=14)) +
scale_y_continuous(
labels = scales::number_format(accuracy = 0.01,
decimal.mark = '.'),
limits=c(0, 1))+
annotate("text",
x = 0.5,
y = 0.98,
label = "A.",
size=9,
fontface=2)
shap_W <-
ggplot(normal_df, aes(as.numeric(threshold), as.numeric(w))) +
annotate(geom = "rect",
ymin=-Inf, ymax=Inf,
xmin=-Inf, xmax=0.03,
fill = "red",
alpha = 0.2) +
annotate(geom = "rect",
ymin=-Inf, ymax=Inf,
xmin=0.03, xmax=0.34,
fill = "palegreen",
alpha = 0.2) +
annotate(geom = "rect",
ymin=-Inf, ymax=Inf,
xmin=0.34, xmax=Inf,
fill = "red",
alpha = 0.2) +
geom_vline(aes(xintercept=as.numeric(normal_df[which(normal_df$p == max(normal_df$p)),]$threshold)),
linetype="dashed",
color="darkred",
size=1) +
geom_line(size=1) +
geom_point(shape=21,
fill="white",
stroke=1.5,
size=2,
color="black") +
theme_classic() +
xlab("\n Binarization threshold value") +
ylab("Shapiro-Wilk W \n") +
theme(axis.text.x = element_text(size=12),
axis.text.y = element_text(size=12),
axis.title.x = element_text(size=14),
axis.title.y = element_text(size=14),
panel.spacing = unit(2, "lines"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
strip.text = element_text(size = 14, face="bold")) +
annotate("text",
x = 0.5,
y = 0.98,
label = "B.",
size=9,
fontface=2) +
scale_y_continuous(
labels = scales::number_format(accuracy = 0.01,
decimal.mark = '.'),
limits = c(0, 1.0))
combined_normal <- shap_p/shap_W
```
```{r correlation_plot, fig.width=15, fig.height=15, dpi=600}
plot(combined_normal)
```
#### Assessing which constant threshold value yields the highest correlation with the taxonomic diversity of sonif species
```{r}
# Merge together with metadata
rarefied_sounddiv_threshold_1_q0_total$site <- toupper(rarefied_sounddiv_threshold_1_q0_total$site)
rarefied_sounddiv_threshold_1_q0_total <- merge(x=rarefied_sounddiv_threshold_1_q0_total,
y = total_richness,
by="site")
# Make a new correlation dataframe
cor_list <- vector("list", length(unique(rarefied_sounddiv_threshold_1_q0_total$threshold)))
for (i in 1:length(cor_list)){
cor_list[[i]] <- as.data.frame(
t(as.data.frame(
c(
cor.test(x = subset(rarefied_sounddiv_threshold_1_q0_total,
rarefied_sounddiv_threshold_1_q0_total$threshold==unique(rarefied_sounddiv_threshold_1_q0_total$threshold)[i])$qD,
y = subset(rarefied_sounddiv_threshold_1_q0_total,
rarefied_sounddiv_threshold_1_q0_total$threshold==unique(rarefied_sounddiv_threshold_1_q0_total$threshold)[i])$total_tax)$p.value,
cor.test(x = subset(rarefied_sounddiv_threshold_1_q0_total,
rarefied_sounddiv_threshold_1_q0_total$threshold==unique(rarefied_sounddiv_threshold_1_q0_total$threshold)[i])$qD,
y = subset(rarefied_sounddiv_threshold_1_q0_total,
rarefied_sounddiv_threshold_1_q0_total$threshold==unique(rarefied_sounddiv_threshold_1_q0_total$threshold)[i])$total_tax)$estimate
))))
cor_list[[i]]$threshold <- unique(rarefied_sounddiv_threshold_1_q0_total$threshold)[i]
colnames(cor_list[[i]]) <- c("p", "R2", "threshold")
}
cor_list <- rbindlist(cor_list)
# Check which values are above 0.05
cor_list[which(cor_list$p > 0.05),]
# Check the minimum p-value and maximum R^2 value
cor_list[which(cor_list$p == min(cor_list$p)),]
cor_list[which(cor_list$R2 == max(cor_list$R2)),]
# Make correlation plots
correlation_r2 <-
ggplot(cor_list, aes(threshold, R2))+
annotate(geom = "rect",
ymin=-Inf, ymax=Inf ,
xmin=-Inf, xmax=0.31,
fill = "palegreen",
alpha = 0.2) +
annotate(geom = "rect",
ymin=-Inf , ymax=Inf,
xmin=0.31, xmax=Inf,
fill = "red",
alpha = 0.2) +
geom_vline(aes(xintercept=as.numeric(cor_list[which(cor_list$p == min(cor_list$p)),]$threshold)),
linetype="dashed",
color="darkred",
size=1) +
geom_line(size=1) +
geom_point(shape=21,
size=2,
stroke=1,
fill="white",
color="black") +
theme_classic() +
xlab("") +
ylab("Pearson correlation coefficient \n") +
theme(axis.text.x = element_text(size=12),
axis.text.y = element_text(size=12),
axis.title.x = element_text(size=14),
axis.title.y = element_text(size=14),
panel.spacing = unit(2, "lines"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
strip.text = element_text(size = 14, face="bold")) +
annotate("text",
x = 0.5,
y = 0.84,
label = "A.",
size=9,
fontface=2) +
scale_y_continuous(
labels = scales::number_format(accuracy = 0.01,
decimal.mark = '.'))
correlation_p <-
ggplot(cor_list, aes(threshold, p)) +
annotate(geom = "rect",
ymin=-Inf, ymax=Inf ,
xmin=-Inf, xmax=0.31,
fill = "palegreen",
alpha = 0.2) +
annotate(geom = "rect",
ymin=-Inf , ymax=Inf,
xmin=0.31, xmax=Inf,
fill = "red",
alpha = 0.2) +
geom_vline(aes(xintercept=as.numeric(cor_list[which(cor_list$p == min(cor_list$p)),]$threshold)),
linetype="dashed",
color="darkred",
size=1) +
geom_line(size=1) +
geom_point(shape=21,
size=2,
stroke=1,
fill="white",
color="black") +
theme_classic() +
xlab("\n Binarization threshold value") +
ylab("Pearson correlation p-value \n ") +
theme(axis.text.x = element_text(size=12),
axis.text.y = element_text(size=12),
axis.title.x = element_text(size=14),
axis.title.y = element_text(size=14),
panel.spacing = unit(2, "lines"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),