-
Notifications
You must be signed in to change notification settings - Fork 0
/
SW_PR_analysis_markdown_20190630.Rmd
1173 lines (805 loc) · 55.3 KB
/
SW_PR_analysis_markdown_20190630.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: "Resilience assessment of NCRMP2024 sites near Guanica"
author: "David Gibbs"
date: "6/11/19"
output:
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Libraries
```{r necessary libraries}
# library(devtools)
# install_github("vqv/ggbiplot")
library(ggbiplot)
library(xlsx)
library(psych)
library(PerformanceAnalytics)
library(reshape2)
library(stats)
library(psy)
library(BiodiversityR)
library(Metrics)
```
Loads data into R
```{r load data}
#Loads the survey data, calculated as indicators
setwd("C:/Users/dagib/Documents/EPA ORISE EARCG/Coral projects/Adaptation Design Tool/Analyses/Inputs")
all_data <- read.xlsx("NCRMP_2014_sites_SW_PR_20190630.xlsx", sheetIndex=2, header=TRUE)
all_data <- all_data[order(all_data$Station), c(1:9)]
#Converts the values in the indicator spreadheet from factors to numeric
type <- sapply(all_data[,c(2:9)], is.factor)
all_data[type] <- lapply(all_data[type], function(x) as.numeric(as.character(x)))
# Removes rows that have NAs for indicator values
complete_records <- na.omit(all_data)
#Loads the indicator weight spreadsheet
indic_weights <- read.xlsx("NCRMP_2014_sites_SW_PR_20190630.xlsx", sheetIndex = 3, header = TRUE)
indic_weights <- indic_weights[c(1:10),c(3, 5:11)]
#Converts the values in the weighting spreadheet from factors to numeric
weights <- sapply(indic_weights[,-c(1:2)], is.factor)
indic_weights[weights] <- lapply(indic_weights[weights], function(x) as.numeric(as.character(x)))
#Loads the station property data
sta_props <- read.xlsx("NCRMP_PR2014_station_properties.xlsx", sheetIndex = 1, header = TRUE)
#Reorders the station property file by station number
sta_props <- sta_props[order(sta_props$survey_index),]
#Joins the site attributes to the indicators
complete_records_attribs <- merge(x = complete_records, y = sta_props, by.x = "Station", by.y = "survey_index")
#The sites that have coral demographic surveys
demog_sites_attribs <- complete_records_attribs[which(complete_records_attribs$demo=="1"),]
demog_sites <- demog_sites_attribs[,c(1:9)]
#Just the sites with demographic surveys that actually had full-sized coral colonies.
#This removes the sites that did not have any full-sized coral colonies.
demog_sites_complete_records <- demog_sites_attribs[complete.cases(demog_sites_attribs),]
#Converts the values in the indicator spreadheet from factors to numeric
demog_sites_complete_records[,3] <- as.numeric(as.character(demog_sites_complete_records[,3]))
#Keeps just the demographic survey indicators at the demographic sites
#(coral diversity, coral disease, coral thermal tolerance)
demog_sites_demog_fields <- demog_sites_complete_records[,c(1:3, 9)]
#All sites without demographic data.
#Removes the columns that are only for sites with demographic data.
all_sites_non_demog_fields <- complete_records_attribs[,c(1, 4:8)]
#Loads the fishing pressure data
# setwd("C:/Users/dagib/Documents/EPA ORISE EARCG/Coral projects/Resilience_assessment/GIS/Stressors/Fishing_pressure")
fishing <- read.csv("NCRMP2014_stations_with_Shivlani_fishing_pressure_sptl_join_20180409.csv", header = TRUE)
#Loads land-based sources of pollution (LBSP) data
# setwd("C:/Users/dagib/Documents/EPA ORISE EARCG/Coral projects/Resilience_assessment/GIS/Stressors/LBSP_coastal_dispersion")
LBSP <- read.xlsx("LBSP_at_NCRMP_sites_90ft_depth_using_7_nearest_OpenNSPECT_pour_points_CCAP2010_20180412.xls", sheetIndex = 10, header = TRUE)
#loads annual bleaching onset year (scenario rcp8.5) estimates for each site
# setwd("C:/Users/dagib/Documents/EPA ORISE EARCG/Coral projects/Resilience_assessment/GIS/UNEP van Hooindonk 2016 bleaching projections")
annual_bleaching_rcp85 <- read.csv("annual_bleaching_onset_year_rcp85_20180424.csv", header = TRUE)
```
Performs principal components analysis as shown at
https://www.r-bloggers.com/computing-and-visualizing-pca-in-r/
using the prcomp method
```{r performs PCA}
#PCA for sites with demographic data using demog and non-demog (i.e. all) indicators
demog_sites_pca <- prcomp(demog_sites_complete_records[,c(2:9)], center=TRUE, scale.=TRUE)
print(demog_sites_pca)
summary(demog_sites_pca)
plot(demog_sites_pca, type = "lines")
#Matrix of each site with its PCA scores
demog_sites_pca_scores <- demog_sites_pca$x
demog_sites_pca_scores <- cbind(demog_sites_complete_records[,1], demog_sites_pca_scores)
# #PCA for all sites using non-demographic indicators only
# all_sites_non_demog_fields_pca <- prcomp(all_sites_non_demog_fields[,c(2:5)], center=TRUE, scale.=TRUE)
# print(all_sites_non_demog_fields_pca)
# summary(all_sites_non_demog_fields_pca)
# plot(all_sites_non_demog_fields_pca, type = "lines")
# #Matrix of each site with its PCA scores
# all_sites_non_demog_fields_pca_scores <- all_sites_non_demog_fields_pca$x
# all_sites_non_demog_fields_pca_scores <- cbind(all_sites_non_demog_fields[,1], all_sites_non_demog_fields_pca_scores)
#
# #PCA for demog sites using demographic indicators only at demographic sites
# demog_sites_demog_fields_pca <- prcomp(demog_sites_demog_fields[,c(2:4)], center=TRUE, scale.=TRUE)
# print(demog_sites_demog_fields_pca)
# summary(demog_sites_demog_fields_pca)
# plot(demog_sites_demog_fields_pca, type = "lines")
# #Matrix of each site with its PCA scores
# demog_sites_demog_fields_pca_scores <- demog_sites_demog_fields_pca$x
# demog_sites_demog_fields_pca_scores <- cbind(demog_sites_demog_fields[,1], demog_sites_demog_fields_pca_scores)
```
Creates PCA ordination plots
```{r PCA ordination plots}
#PCA ordination plots for sites with demographic data using demog and non-demog (i.e. all) indicators
demog_sites_pca_plot <- ggbiplot(demog_sites_pca, obs.scale = 1, var.scale = 1)
demog_sites_pca_plot <- demog_sites_pca_plot + scale_color_discrete(name = '')
demog_sites_pca_plot <- demog_sites_pca_plot + theme(legend.direction = 'horizontal',
legend.position = 'top')
demog_sites_pca_plot <- demog_sites_pca_plot + ggtitle("PCA for sites with demographic data")
print(demog_sites_pca_plot)
# #PCA ordination plots for demographic indicators at demographic sites
# demog_pca_plot <- ggbiplot(demog_sites_demog_fields_pca, obs.scale = 1, var.scale = 1)
# demog_pca_plot <- demog_pca_plot + scale_color_discrete(name = '')
# demog_pca_plot <- demog_pca_plot + theme(legend.direction = 'horizontal',
# legend.position = 'top')
# demog_pca_plot <- demog_pca_plot + ggtitle("PCA for demographic indicators at sites with demographic data")
# print(demog_pca_plot)
#
# #PCA ordination plots for all sites using non-demographic indicators only
# non_demog_pca_plot <- ggbiplot(all_sites_non_demog_fields_pca, obs.scale = 1, var.scale = 1)
# non_demog_pca_plot <- non_demog_pca_plot + scale_color_discrete(name = '')
# non_demog_pca_plot <- non_demog_pca_plot + theme(legend.direction = 'horizontal',
# legend.position = 'top')
# non_demog_pca_plot <- non_demog_pca_plot + ggtitle("PCA for non-demographic indicators at all sites")
# print(non_demog_pca_plot)
```
Correlations between all pairs of indicators
```{r Correlations between indicators}
#Sites with coral demographic data that have all indicator values (i.e. excludes 8 sites that did not have any demogrphic data)
chart.Correlation(demog_sites_complete_records[,c(2:9)], histogram=TRUE, method=c("spearman"), pch=".")
demog_indic_cor <- cor(demog_sites_complete_records[,c(2:9)], use="pairwise.complete.obs", method=c("spearman"))
# pairs.panels(demog_sites_complete_records[,c(2:9)], method="spearman", lm=TRUE, ellipses=FALSE)
# #Sites with demographic data but just using the indicators that don't come from the demographic surveys
# chart.Correlation(all_sites_non_demog_fields[,c(2:6)], histogram=TRUE, method=c("spearman"), pch=".")
# # pairs.panels(all_sites_non_demog_fields[,c(2:6)], method="spearman", lm=TRUE, ellipses=FALSE)
write.table(demog_indic_cor, file = paste("demog_indic_Pearson_", Sys.Date(), ".csv", sep=""), row.names = FALSE)
```
Factor analysis between all indicators
Sources include:
http://www.statpower.net/Content/312/R%20Stuff/Exploratory%20Factor%20Analysis%20with%20R.pdf
https://www.promptcloud.com/blog/exploratory-factor-analysis-in-r/
http://www.di.fc.ul.pt/~jpn/r/factoranalysis/factoranalysis.html#introduction
```{r Factor analysis between all indicators}
#Conducts the factor analysis using varimax rotation
demog_fa <- factanal(demog_sites_complete_records[,c(2:9)], factors=4, rotation="varimax")
demog_fa
#Scree plot of factors' importance to show how many factors to use
scree.plot(demog_fa$correlation)
#Plots the loadings of the indicators on two of the factors
demog_fa_load <- demog_fa$loadings[,c(1:2)]
plot(demog_fa_load, type="n")
text(demog_fa_load, labels=names(demog_sites_complete_records[,c(2:9)]), cex=0.7)
```
Rescales indicators
```{r Rescale indicators}
#Divides each indicator by the largest value for that indicator across all sites with demographic data
demog_sites_rescaled <- t(t(demog_sites_complete_records[,c(2:9)])/apply(demog_sites_complete_records[,c(2:9)], 2, function(x) max(na.omit(x))))
#Binds the rescaled indicators to the raw indicators
demog_sites_rescaled <- cbind(demog_sites_complete_records[,c(1:9)], demog_sites_rescaled)
#Renames the columns to distinguish the raw indicators from the rescaled indicators
colnames(demog_sites_rescaled)[1] <- colnames(demog_sites_attribs)[1]
colnames(demog_sites_rescaled)[c(2:9)] <- paste(colnames(demog_sites_rescaled)[c(2:9)], "_raw", sep="")
colnames(demog_sites_rescaled)[c(10:17)] <- paste(colnames(demog_sites_rescaled)[c(10:17)], "_rescaled", sep="")
# #Turns unweighted indicators into long form for box plot. Also reorders the variables for box plot.
# demog_sites_rescaled_long <- melt(demog_sites_rescaled[,c(10:17)])
# demog_sites_rescaled_long$variable <- factor(demog_sites_rescaled_long$variable, levels=c("SimpsonDiv_rescaled", "FractNotDiseased_rescaled","ThermalTol_rescaled", "HardCoralCover_rescaled", "AlgaeCover_rescaled", "Rugosity_rescaled", "HerbivoreBiomass_rescaled", "TempVar_rescaled"))
#
# #creates boxplot of each rescaled indicator (1 is maximum value)
# p <- ggplot(data=demog_sites_rescaled_long, aes(x=variable, y=value)) + geom_boxplot(aes(fill=variable))
# p <- p + theme(legend.position="none")
# p <- p + theme(axis.text.x = element_text(angle = 65, vjust = 0.6))
# p <- p + labs(x = "Indicator", y = "Rescaled value")
# p <- p + scale_fill_manual(values=c("#999555", "#999555", "#999555", "#999555", "#999555", "#999555", "#999555", "#999555"))
# p
```
Functions to calculate composite resilience scores, ranks, and rank quartiles at sites with demographic data: 1) using all indicators, and 2) without the temperature variation indicator
```{r Functions to calculate unweighted resilience scores}
#Calculates the composite resilience score by averaging the rescaled indicators
resilScore <- function(rescaled_indics) {
#Creates empty fields for the resilience scores
rescaled_indics$resil_all_indic <- NA
rescaled_indics$resil_no_temp_var <- NA
#Calculates resilience scores with all indicators and without the temperature variation indicator.
resil_all_indic_demog <- rowMeans(rescaled_indics[which(rescaled_indics$demo == 1), c(10:17)], na.rm = TRUE)
resil_no_temp_var_demog <- rowMeans(rescaled_indics[which(rescaled_indics$demo == 1), c(10:15,17)], na.rm = TRUE)
#Rescales the resilience scores to the highest resilience score
resil_all_indic_demog <- resil_all_indic_demog/max(resil_all_indic_demog)
resil_no_temp_var_demog <- resil_no_temp_var_demog/max(resil_no_temp_var_demog)
#Attaches the resilience scores (all indicators and without temperature variation) to the rescaled indicator table
rescaled_indics[which(rescaled_indics$demo == 1), ]$resil_all_indic <- resil_all_indic_demog
rescaled_indics[which(rescaled_indics$demo == 1), ]$resil_no_temp_var <- resil_no_temp_var_demog
return(rescaled_indics)
}
#Calculates the resilience score rank (1 is highest resilience) for each site
resilRank <- function(rescaled_indics) {
rescaled_indics <- as.data.frame(rescaled_indics)
#Creates empty columns for the resilience ranks
rescaled_indics$resil_rank_all_indic <- NA
rescaled_indics$resil_rank_no_temp_var <- NA
#Resilience ranks for demographic sites using all indicators
rescaled_indics[which(rescaled_indics$demo == 1),]$resil_rank_all_indic <- rank(-rescaled_indics[which(rescaled_indics$demo == 1),]$resil_all_indic, na.last = "keep", ties.method = "first")
#Resilience ranks for demographic sites using all indicators except temperature variation
rescaled_indics[which(rescaled_indics$demo == 1),]$resil_rank_no_temp_var <- rank(-rescaled_indics[which(rescaled_indics$demo == 1),]$resil_no_temp_var, na.last = "keep", ties.method = "first")
return(rescaled_indics)
}
#Calculates the quartile of resilience that each site is in (1 is most resilient quartile, 4 is least resilient quartile
resilQuartile <- function(rescaled_indics) {
#Creates empty columns for the resilience rank quartiles
rescaled_indics$resil_quart_all_indic <- NA
rescaled_indics$resil_quart_no_temp_var <- NA
#Resilience rank quartiles for demographic sites using all indicators
rescaled_indics[which(rescaled_indics$demo == 1),]$resil_quart_all_indic <-
cut(rescaled_indics[which(rescaled_indics$demo == 1),]$resil_rank_all_indic, breaks=quantile(rescaled_indics[which(rescaled_indics$demo == 1),]$resil_rank_all_indic, probs=seq(0, 1, 0.25)), include.lowest=TRUE, labels = 1:4)
# #Resilience rank quartiless for demographic sites using all indicators except temperature variation
rescaled_indics[which(rescaled_indics$demo == 1),]$resil_quart_no_temp_var <-
cut(rescaled_indics[which(rescaled_indics$demo == 1),]$resil_rank_no_temp_var, breaks=quantile(rescaled_indics[which(rescaled_indics$demo == 1),]$resil_rank_no_temp_var, probs=seq(0, 1, 0.25)), include.lowest=TRUE, labels = 1:4)
return(rescaled_indics)
}
```
Calculates resilience scores, ranks, and quartiles for unweighted indicators
```{r Calculates resilience scores, ranks, and quartiles for unweighted indicators}
demog_sites_rescaled <- as.data.frame(demog_sites_rescaled)
#Attaches the site attributes to the indicators and resilience scores
demog_sites_rescaled_attribs <- merge(x = demog_sites_rescaled, y = sta_props, by.x = "Station", by.y = "survey_index")
#Resilience scores based on rescaled indicators
demog_sites_rescaled_scores <- resilScore(demog_sites_rescaled_attribs)
#Resilience ranks based on the composite resilience scores
demog_sites_rescaled_ranks <- resilRank(demog_sites_rescaled_scores)
#Resilience quartiles based on the resilience ranks (1 is most resilient quartile, 4 is least resilient quartile)
demog_sites_rescaled_quartiles <- resilQuartile(demog_sites_rescaled_ranks)
#Adds a field with the weighting system for the indicators. This one is unweighted.
demog_sites_rescaled_quartiles["weighting_sys"] <- 0
#Deletes a redundant field
demog_sites_rescaled_quartiles <- demog_sites_rescaled_quartiles[,-c(21)]
#Adds four fields for comparing the unweighted and weighted ranks and quartiles (two for ranks/quartiles with all indicators and two for ranks/quartiles without temperature variation). Does not apply for the unweighted data, of course.
demog_sites_rescaled_quartiles["quartile_change_all_indic"] <- "N/A"
demog_sites_rescaled_quartiles["diff_unweighted_weighted_all_indic"] <- "N/A"
demog_sites_rescaled_quartiles["quartile_change_no_temp_var"] <- "N/A"
demog_sites_rescaled_quartiles["diff_unweighted_weighted_no_temp_var"] <- "N/A"
#Saves the unweighted data in its own dataframe
data_demog_sites_unweighted <- demog_sites_rescaled_quartiles
write.table(data_demog_sites_unweighted, file = paste("demog_sites_unweighted_", Sys.Date(), ".csv", sep=""), row.names = FALSE)
```
Calculates resilience scores for each site using different weighting systems
```{r Calculates resilience scores using different weighting systems}
#Creates a new dataframe for weighted data
data_demog_sites_weighted <- demog_sites_rescaled_quartiles
for(i in 2:nrow(indic_weights)) {
#Creates an empty weighting table
weighted <- NA
#Iterates through every station
for(j in 1:nrow(data_demog_sites_unweighted)) {
#Multiplies the rescaled values by the indicator weights
weighted_new <- data_demog_sites_unweighted[j,c(10:17)]*indic_weights[i,c(1:8)]
weighted <- rbind(weighted, weighted_new)
}
#Removes the empty first row of the weighted table
weighted <- weighted[-c(1),]
#Adds the station property columns to the weighted values and rearranges columns so they match the unweighted table
weighted <- cbind(data_demog_sites_weighted[c(1:nrow(data_demog_sites_unweighted)),c(1:9)], weighted)
weighted <- cbind(weighted, data_demog_sites_weighted[c(1:nrow(data_demog_sites_unweighted)),c(18:ncol(data_demog_sites_weighted))])
#Resilience scores based on all rescaled indicators available at a site
weighted <- resilScore(weighted)
#Resilience ranks (separate for sites with and without demographic indicators (111 and 119, respectively))
weighted <- resilRank(weighted)
#Resilience quartiles (separate for sites with and without demographic indicators (111 and 119, respectively))
weighted <- resilQuartile(weighted)
#Adds a field with the weighting system for the indicators
weighted["weighting_sys"] <- i-1
#Adds four fields for comparing the unweighted and weighted ranks and quartiles (two for ranks/quartiles with all indicators and two for ranks/quartiles without temperature variation). Does not apply for the unweighted data, of course.
weighted["quartile_change_all_indic"] <- NA
weighted["diff_unweighted_weighted_all_indic"] <- NA
weighted["quartile_change_no_temp_var"] <- NA
weighted["diff_unweighted_weighted_no_temp_var"] <- NA
#Adds and populates columns for storing changes in resilience rank and quartile between the unweighted and weighted systems
weighted$quartile_change_all_indic <- paste(data_demog_sites_unweighted$resil_quart_all_indic, " to ", weighted$resil_quart_all_indic)
weighted$diff_unweighted_weighted_all_indic <- data_demog_sites_unweighted$resil_rank_all_indic - weighted$resil_rank_all_indic
weighted$quartile_change_no_temp_var <- paste(data_demog_sites_unweighted$resil_quart_no_temp_var, " to ", weighted$resil_quart_no_temp_var)
weighted$diff_unweighted_weighted_no_temp_var <- data_demog_sites_unweighted$resil_rank_no_temp_var - weighted$resil_rank_no_temp_var
#Gives the weighted table the same column names as the unweighted table
colnames(weighted) <- colnames(data_demog_sites_weighted)
#Adds the weighted table to the end of the previous table
data_demog_sites_weighted <- rbind(data_demog_sites_weighted, weighted)
}
write.table(data_demog_sites_weighted, file = paste("resil_with_weighting_", Sys.Date(), ".csv", sep=""), row.names = FALSE)
```
Plots comparing ranks of unweighted resilience ranks and some of the weighted resilience ranks against each other
Also calculates the root mean square error of ranks under the weighting systems against the ranks under the unweighted system
```{r Plots comparing ranks of unweighted resilience ranks and some of the weighted resilience ranks against each other. Also, calculated RMSE of weighted vs. unweighted.}
#Function to plot the resilience score ranks. Run separately on sites with and without demographic indicators. This is just for resilience ranks calculated without the temperature variation indicator.
resilRankCompare <- function(sensit_table, demog) {
#Table of unweighted ranks
unweighted_table <- sensit_table[which(sensit_table$demo==demog & sensit_table$weighting_sys == 0),]$resil_rank_no_temp_var
rank_table <- data.frame(unweighted_table)
colnames(rank_table) <- "Unweighted"
#Iterates through the chosen weighting systems
for (i in c(3,7,9)){
#For each weighting system, a dataframe with the ranks
weighted_table <- sensit_table[which(sensit_table$demo==demog & sensit_table$weighting_sys == i),]$resil_rank_no_temp_var
weighted_table <- data.frame(weighted_table)
colnames(weighted_table) <- paste("Weighting_", i, sep="")
#Attaches the table with weighted ranks
rank_table <- cbind(rank_table, weighted_table)
}
#Creates the plot
g_total<- ggplot(rank_table, aes(x = Unweighted, y = Weighted_7, color = Weighting)) +
geom_point(aes(y = rank_table[,2], col = gsub("_", " ", colnames(rank_table)[2])), shape=19, size=3) +
geom_point(aes(y = rank_table[,3], col = gsub("_", " ", colnames(rank_table)[3])), shape=15, size=3) +
geom_point(aes(y = rank_table[,4], col = gsub("_", " ", colnames(rank_table)[4])), shape=18, size=3) +
scale_color_grey(start=0.65, end=0.05)
g_total <- g_total + labs(x = "Unweighted rank", y = "Weighted rank", color = "Weighting system")
g_total <- g_total + xlim(0, nrow(rank_table)+10)
g_total <- g_total + ylim(0, nrow(rank_table)+10)
# g_total <- g_total + ggtitle("Weighted vs. unweighted resilience score ranks")
# g_total <- g_total + labs(subtitle = "For select weighting systems")
g_total <- g_total + theme(plot.title = element_text(hjust = 0.5))
g_total <- g_total + theme(plot.subtitle=element_text(hjust = 0.5))
g_total <- g_total + scale_x_continuous(breaks=c(seq(0,nrow(rank_table)+10,25)))
g_total <- g_total + scale_y_continuous(breaks=c(seq(0,nrow(rank_table)+10,25)))
g_total <- g_total + theme_bw()
g_total <- g_total + guides(color = guide_legend(override.aes = list(shape = c(19,15,18))))
g_total <- g_total + theme(legend.position = c(0.9,0.2))
# #Different subtitles depending on whether demographic sites or non-demographic sites are being plotted
# if (demog==0) {
# g_total<- g_total+ labs(subtitle = "Only sites without demographic indicators")
# }
# if (demog==1) {
# g_total<- g_total+ labs(subtitle = "Only sites with demographic data (no temperature variation indicator)")
# }
return(g_total)
}
# p1 <- resilRankCompare(all_data_rescaled_attribs, 0)
# p1
p2 <- resilRankCompare(data_demog_sites_weighted, 1)
p2
ggsave("indic_sensit_analysis.jpg", plot = p2, device = "jpeg", width=10, height=8, dpi=300)
####Calculates root mean square error (RMSE)for each comparison of weighted and unweighted indicator rankings
#Creates an empty dataframe
rmse_weights <- data.frame(matrix(NA, nrow=9, ncol=1))
colnames(rmse_weights) <- "RMSE"
#Unweighted ranks
unweighted_ranks <- data_demog_sites_weighted[which(data_demog_sites_weighted$weighting_sys == 0),]$resil_rank_no_temp_var
#Calculates RMSE for each weighted ranking system
for (i in 1:max(data_demog_sites_weighted$weighting_sys)){
weighted_ranks <- data_demog_sites_weighted[which(data_demog_sites_weighted$weighting_sys == i),]$resil_rank_no_temp_var
rmse_weights[i,1] <- rmse(unweighted_ranks, weighted_ranks)
}
print(rmse_weights)
```
Plots comparing the difference between weighted resilience ranks against unweighted ranks
```{r Plots comparing the difference between weighted resilience ranks against unweighted ranks}
#Function to plot the resilience score ranks. Run separately on sites with and without demographic indicators. This is just for resilience ranks calculated without the temperature variation indicator.
resilRankDiff <- function(sensit_table, demog) {
#Table of unweighted ranks
unweighted_table <- sensit_table[which(sensit_table$demo==demog & sensit_table$weighting_sys == 0),]$resil_rank_no_temp_var
rank_table <- data.frame(unweighted_table)
colnames(rank_table) <- "Unweighted"
#Iterates through the chosen weighting systems
for (i in c(3,7,9)){
#For each weighting system, a dataframe with the rank differences
weighted_table <- sensit_table[which(sensit_table$demo==demog & sensit_table$weighting_sys == i),]$diff_unweighted_weighted_no_temp_var
weighted_table <- data.frame(weighted_table)
colnames(weighted_table) <- paste("Weighting_", i, sep="")
#Attaches the table with weighted ranks
rank_table <- cbind(rank_table, weighted_table)
}
#Converts the rank differences into numbers
rank_table[,c(2:4)] <- lapply(rank_table[,c(2:4)], function(x) as.numeric(as.character(x)))
#Creates the plot
g_diff<- ggplot(rank_table, aes(x = Unweighted, y = Weighted_7, color = Weighting)) +
geom_point(aes(y = rank_table[,2], col = colnames(rank_table)[2])) +
geom_point(aes(y = rank_table[,3], col = colnames(rank_table)[3])) +
geom_point(aes(y = rank_table[,4], col = colnames(rank_table)[4]))
g_diff<- g_diff+ labs(x = "Unweighted rank", y = "Difference from unweighted rank")
g_diff<- g_diff+ ggtitle("Difference between weighted and unweighted ranks")
g_diff<- g_diff+ theme(plot.title = element_text(hjust = 0.5))
g_diff<- g_diff+ theme(plot.subtitle = element_text(hjust = 0.5))
g_diff <- g_diff + theme_bw()
#Different subtitles depending on whether demographic sites or non-demographic sites are being plotted
if (demog==0) {
g_diff<- g_diff+ labs(subtitle = "Only sites without demographic indicators")
}
if (demog==1) {
g_diff<- g_diff+ labs(subtitle = "Only sites with demographic data (no temperature variation indicator)")
}
return(g_diff)
}
# p3 <- resilRankCompare(all_data_rescaled_attribs, 0)
# p3
p4 <- resilRankDiff(data_demog_sites_weighted, 1)
p4
```
Matrix of how many sites change quartiles using different weighting systems. Only for quartiles assigned without the temperature variation indicator.
```{r Table of how many sites change quartiles using different weighting systems}
#Makes the quartile change field a factor
data_demog_sites_weighted$quartile_change_no_temp_var <- as.factor(data_demog_sites_weighted$quartile_change_no_temp_var)
#dataframe for quartile changes that is one column for each weighting scheme and one row for each quartile change found in the data
quartile_change <- data.frame(matrix(0, nrow = nlevels(data_demog_sites_weighted$quartile_change_no_temp_var), ncol = max(data_demog_sites_weighted$weighting_sys)))
#Assigns row and column names
row.names(quartile_change) <- c(levels(data_demog_sites_weighted$quartile_change_no_temp_var))
colnames(quartile_change) <- paste("Weighting_", c(1:max(data_demog_sites_weighted$weighting_sys)), sep="")
#Deletes the empty first row
quartile_change <- quartile_change[-c(nrow(quartile_change)),]
#Iteratres through all the weighting schemes (i.e. columns of the quartile_change dataframe)
for (i in 1:max(data_demog_sites_weighted$weighting_sys)) {
#Pulls out just the appropriate weighting
weighted <- data_demog_sites_weighted[which(data_demog_sites_weighted$weighting_sys == i),]
#Iterates through every row (i.e. station) of the weighting to pull out its quartile transition
for (j in 1:nrow(weighted)) {
#The actual transition for the row (station) of the weighting system that is being added to the transition matrix
change <- weighted$quartile_change_no_temp_var[j]
#Increments the value in the transition matrix by 1 for each transition found
value <- as.integer(subset(quartile_change, rownames(quartile_change) %in% change)[i]) + 1
quartile_change[which(rownames(quartile_change) %in% change),i] <- value
}
}
#Sums the number of transitions for each weighting system. It should be 230.
quartile_change["Sum",] <- colSums(quartile_change)
quartile_change <- quartile_change/nrow(data_demog_sites_unweighted)
print(quartile_change)
write.table(quartile_change, file = paste("sensit_analysis_quartile_change_", Sys.Date(), ".csv", sep=""), row.names = TRUE)
```
Merges LBSP and fishing stressor data with indicator data and makes boxplots of rescaled stressors and indicators
```{r Merges LBSP and fishing stressor data with indicator data and makes boxplots}
#Calculates the average of the rescaled nitrogen and sediment values for each site
LBSP$StrdLBSP <- rowMeans(LBSP[,c("StrdNtrgen", "StrdSedmnt")])
#Replaces the non-existent LBSP values with NAs
LBSP[LBSP$StrdNtrgen == -99999, ]$StrdNtrgen <- NA
LBSP[LBSP$StrdSedmnt == -99999, ]$StrdSedmnt <- NA
LBSP[LBSP$StrdLBSP == -99999, ]$StrdLBSP <- NA
#Only the necessary columns from the fishing data, and changes field names
fishing_no_attribs <- fishing[,c(4, 32:36)]
colnames(fishing_no_attribs)[c(2:6)] <- paste(colnames(fishing_no_attribs)[c(2:6)], "_fishing", sep="")
#New dataset that will have indicators and stressors
demog_sites_with_stressors <- data_demog_sites_weighted
#Merges the fishing data to the indicator data
demog_sites_with_stressors <- merge(x = demog_sites_with_stressors, y = fishing_no_attribs, by.x = "Station", by.y = "SurveyIndx")
#Merges the LBSP data to the indicator data
demog_sites_with_stressors <- merge(x = demog_sites_with_stressors, y = LBSP[,-c(1)], by.x = "Station", by.y = "StationID")
#Rescales fishing values to a highest value of 1 for the sites in the assessment
demog_sites_with_stressors$all_total_fishing_rescaled <- demog_sites_with_stressors$all_total_fishing/max(demog_sites_with_stressors$all_total_fishing, na.rm=TRUE)
#Rescales the LBSP values to a highest value of 1 for the sites in the assessment
demog_sites_with_stressors$StrdNtrgen <- demog_sites_with_stressors$StrdNtrgen/max(demog_sites_with_stressors$StrdNtrgen, na.rm=TRUE)
demog_sites_with_stressors$StrdSedmnt <- demog_sites_with_stressors$StrdSedmnt/max(demog_sites_with_stressors$StrdSedmnt, na.rm=TRUE)
demog_sites_with_stressors$StrdLBSP <- demog_sites_with_stressors$StrdLBSP/max(demog_sites_with_stressors$StrdLBSP, na.rm=TRUE)
#Since the above merges changed the order of the rows, reorders the indicator/stressor data by the weighting system, then by the station ID
demog_sites_with_stressors <- demog_sites_with_stressors[order(demog_sites_with_stressors$weighting_sys, demog_sites_with_stressors$Station),]
#Turns unweighted indicators and stressors into long form for box plot. Also reorders the variables for box plot.
demog_sites_rescaled_long <- melt(demog_sites_with_stressors[which(demog_sites_with_stressors$weighting_sys == 0),c(10:17, 41,42, 64:67)])
demog_sites_rescaled_long$variable <- factor(demog_sites_rescaled_long$variable, levels=c("SimpsonDiv_rescaled", "FractNotDiseased_rescaled","ThermalTol_rescaled", "HardCoralCover_rescaled", "AlgaeCover_rescaled", "Rugosity_rescaled", "HerbivoreBiomass_rescaled", "TempVar_rescaled", "resil_all_indic", "resil_no_temp_var", "StrdNtrgen", "StrdSedmnt", "StrdLBSP", "all_total_fishing_rescaled"))
#creates boxplot of each rescaled indicator (1 is maximum value)
p <- ggplot(data=demog_sites_rescaled_long, aes(x=variable, y=value)) + geom_boxplot(aes(fill=variable))
p <- p + theme_bw()
p <- p + theme(text = element_text(size=14),
axis.text.x = element_text(angle = 0, vjust = 0.6))
p <- p + labs(x = "Metric \n (indicator, stressor, or resilience score)", y = "Rescaled value")
p <- p + scale_fill_manual(values=c("#999555", "#999555", "#999555", "#999555", "#999555", "#999555", "#999555", "#999555", "blue", "blue", "#999999", "#999999", "#999999", "#999999"))
p <- p + scale_x_discrete(labels=c("Simpson \n diversity ", "Colonies \n not \n diseased","Coral \n thermal \n tolerance", "Hard \n coral \n cover", "Macroalgae \n cover", "Rugosity", "Herbivore \n biomass", "Temperature \n variation", "Resilience- \n all indicators", "Resilience- \n without \n temperature \n variation", "Nitrogen \n level", "Sediment \n level", "N + sed \n average", "Fishing \n pressure"))
p <- p + scale_y_continuous(breaks=c(seq(0,1,0.2)))
p <- p + theme(legend.position="none")
#Minimum, mean, and maximum values of raw indicators, where appropriate
simpson_min <- min(data_demog_sites_unweighted$SimpsonDiv_raw)
simpson_mean <- mean(data_demog_sites_unweighted$SimpsonDiv_raw)
simpson_max <- max(data_demog_sites_unweighted$SimpsonDiv_raw)
disease_min <- min(data_demog_sites_unweighted$FractNotDiseased_raw)
disease_mean <- mean(data_demog_sites_unweighted$FractNotDiseased_raw)
disease_max <- max(data_demog_sites_unweighted$FractNotDiseased_raw)
thermalTol_min <- min(data_demog_sites_unweighted$ThermalTol_raw)
thermalTol_mean <- mean(data_demog_sites_unweighted$ThermalTol_raw)
thermalTol_max <- max(data_demog_sites_unweighted$ThermalTol_raw)
coralCover_min <- min(data_demog_sites_unweighted$HardCoralCover_raw)
coralCover_mean <- mean(data_demog_sites_unweighted$HardCoralCover_raw)
coralCover_max <- max(data_demog_sites_unweighted$HardCoralCover_raw)
algaeCover_min <- min(data_demog_sites_unweighted$AlgaeCover_raw)
algaeCover_mean <- mean(data_demog_sites_unweighted$AlgaeCover_raw)
algaeCover_max <- max(data_demog_sites_unweighted$AlgaeCover_raw)
rugosity_min <- min(data_demog_sites_unweighted$Rugosity_raw)
rugosity_mean <- mean(data_demog_sites_unweighted$Rugosity_raw)
rugosity_max <- max(data_demog_sites_unweighted$Rugosity_raw)
herbiv_min <- min(data_demog_sites_unweighted$HerbivoreBiomass_raw)
herbiv_mean <- mean(data_demog_sites_unweighted$HerbivoreBiomass_raw)
herbiv_max <- max(data_demog_sites_unweighted$HerbivoreBiomass_raw)
tempVar_min <- min(data_demog_sites_unweighted$TempVar_raw)
tempVar_mean <- mean(data_demog_sites_unweighted$TempVar_raw)
tempVar_max <- max(data_demog_sites_unweighted$TempVar_raw)
#Adds minimum, mean and maximum values of raw indicators, where applicable
p <- p + annotate("text", x=0.6, y=1.1, label="Min", size = 4)
p <- p + annotate("text", x=0.6, y=1.15, label="Mean", size = 4)
p <- p + annotate("text", x=0.6, y=1.2, label="Max", size = 4)
p <- p + annotate("text", x=1, y=1.1, label=round(simpson_min, 2), size = 4)
p <- p + annotate("text", x=1, y=1.15, label=round(simpson_mean, 2), size = 4)
p <- p + annotate("text", x=1, y=1.2, label=round(simpson_max, 2), size = 4)
p <- p + annotate("text", x=2, y=1.1, label=paste(round(disease_min*100, 0), "%", sep=""), size = 4)
p <- p + annotate("text", x=2, y=1.15, label=paste(round(disease_mean*100, 0), "%", sep=""), size = 4)
p <- p + annotate("text", x=2, y=1.2, label=paste(round(disease_max*100, 0), "%", sep=""), size = 4)
p <- p + annotate("text", x=3, y=1.1, label=round(thermalTol_min, 2), size = 4)
p <- p + annotate("text", x=3, y=1.15, label=round(thermalTol_mean, 2), size = 4)
p <- p + annotate("text", x=3, y=1.2, label=round(thermalTol_max, 2), size = 4)
p <- p + annotate("text", x=4, y=1.1, label=paste(round(coralCover_min, 0), "%", sep=""), size = 4)
p <- p + annotate("text", x=4, y=1.15, label=paste(round(coralCover_mean, 0), "%", sep=""), size = 4)
p <- p + annotate("text", x=4, y=1.2, label=paste(round(coralCover_max, 0), "%", sep=""), size = 4)
p <- p + annotate("text", x=5, y=1.1, label=paste(round(algaeCover_min, 0), "%", sep=""), size = 4)
p <- p + annotate("text", x=5, y=1.15, label=paste(round(algaeCover_mean, 0), "%", sep=""), size = 4)
p <- p + annotate("text", x=5, y=1.2, label=paste(round(algaeCover_max, 0), "%", sep=""), size = 4)
p <- p + annotate("text", x=6, y=1.1, label=round(rugosity_min, 2), size = 4)
p <- p + annotate("text", x=6, y=1.15, label=round(rugosity_mean, 2), size = 4)
p <- p + annotate("text", x=6, y=1.2, label=round(rugosity_max, 2), size = 4)
p <- p + annotate("text", x=7, y=1.08, label=paste(round(herbiv_min, 2), "\n g/100 m^2"), size = 4)
p <- p + annotate("text", x=7, y=1.15, label=round(herbiv_mean, 0), size = 4)
p <- p + annotate("text", x=7, y=1.2, label=round(herbiv_max, 0), size = 4)
p <- p + annotate("text", x=8, y=1.1, label=paste(round(tempVar_min, 2), "C"), size = 4)
p <- p + annotate("text", x=8, y=1.15, label=paste(round(tempVar_mean, 2), "C"), size = 4)
p <- p + annotate("text", x=8, y=1.2, label=paste(round(tempVar_max, 2), "C"), size = 4)
#Adds annotation saying that summary stats for raw values aren't applicable
for (i in 9:14) {
p <- p + annotate("text", x=i, y=1.15, label="N/A", size=4)
}
p
ggsave("indic_resil_stressor_box_plot.jpg", plot = p, device = "jpeg", width= 15, height=7, dpi=300)
```
Functions for management queries
```{r Functions for management queries}
#Identifies sites that are conducive to coral restoration
#Criteria: 1) hard coral cover is lower than the average across all sites, and 2) site resilience is in the upper two quartiles (excluding the temperature variation indicator)
restorn_axn <- function(indics_stressors, i, cutoff_quartile) {
#Average hard coral cover across all sites
coral_cov_avg <- mean(indics_stressors[,c("HardCoralCover_rescaled")], na.rm=TRUE)
#Determines whether each site fits the restoration criteria
for (j in 1:nrow(indics_stressors)) {
#Applies the two criteria to each site
if(indics_stressors[j,"HardCoralCover_rescaled"] < coral_cov_avg && indics_stressors[j,"resil_quart_no_temp_var"] <= cutoff_quartile){
#If the site meets both critera, it is labeled "TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "restorn_axn"] <- "RESTORE"
} else {
#If the site does not meet both criteria, it is labeled "NOT TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "restorn_axn"] <- "NOT TARGET"
}
}
return(demog_sites_with_queries)
}
#Identifies sites that are conducive to LBSP reduction
#Criteria: 1) LBSP is higher than the average across all sites, and 2) site resilience is in the upper two quartiles (excluding the temperature variation indicator)
LBSP_axn <- function(indics_stressors, i, cutoff_quartile) {
#Average LBSP values (sediment and nitrogen combined) across all sites that had LBSP values
LBSP_avg <- mean(indics_stressors[,c("StrdLBSP")], na.rm=TRUE)
#Determines whether each site fits the LBSP management criteria
for (j in 1:nrow(indics_stressors)) {
#Marks the site as not having LBSP data
if(is.na(indics_stressors[j, "StrdLBSP"])) {
demog_sites_with_queries[i*nrow(indics_stressors) + j, "LBSP_axn"] <- "NO LBSP DATA"
}
#Applies the management criteria to each site
else if(indics_stressors[j,"StrdLBSP"] > LBSP_avg && indics_stressors[j,"resil_quart_no_temp_var"] <= cutoff_quartile) {
#If the site meets both critera, it is labeled "TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "LBSP_axn"] <- "LBSP"
} else {
#If the site does not meet both criteria, it is labeled "NOT TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "LBSP_axn"] <- "NOT TARGET"
}
}
return(demog_sites_with_queries)
}
#Identifies sites that are conducive to bleaching management
#Criteria: 1) Bleaching resistance is lower than the average across all sites, and 2) herbivore biomass is lower than average across all sites
bleaching_axn <- function(indics_stressors, i) {
#Average herbivore biomass across all sites
HerbivoreBiomass_avg <- mean(indics_stressors[,c("HerbivoreBiomass_rescaled")], na.rm=TRUE)
#Average coral thermal tolerance across all sites
ThermalTol_avg <- mean(indics_stressors[,c("ThermalTol_rescaled")], na.rm=TRUE)
#Determines whether each site fits the bleaching management criteria
for (j in 1:nrow(indics_stressors)) {
#Marks the site as not having coral demographic data
if(is.na(indics_stressors[j, "ThermalTol_rescaled"])) {
demog_sites_with_queries[i*nrow(indics_stressors) + j, "bleaching_axn"] <- "NO DEMOG DATA/NO CORAL COLONIES"
}
#Applies the management criteria to each site
else if(indics_stressors[j,"HerbivoreBiomass_rescaled"] < HerbivoreBiomass_avg && indics_stressors[j,"ThermalTol_rescaled"] < ThermalTol_avg) {
#If the site meets both critera, it is labeled "TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "bleaching_axn"] <- "BLEACHING"
} else {
#If the site does not meet both criteria, it is labeled "NOT TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "bleaching_axn"] <- "NOT TARGET"
}
}
return(demog_sites_with_queries)
}
#Identifies sites that are conducive to tourism outreach
#Criteria: 1) Coral diversity is higher than average, 2) algae cover is lower than average, and 3) herbivore biomass is higher than average
tourism_axn <- function(indics_stressors, i) {
#Average coral diversity across all sites
SimpsonDiv_avg <- mean(indics_stressors[,c("SimpsonDiv_rescaled")], na.rm=TRUE)
#Average algae cover across all sites
AlgaeCover_avg <- mean(indics_stressors[,c("AlgaeCover_rescaled")], na.rm=TRUE)
#Average herbivore biomass across all sites
HerbivoreBiomass_avg <- mean(indics_stressors[,c("HerbivoreBiomass_rescaled")], na.rm=TRUE)
#Determines whether each site fits the tourism outreach criteria
for (j in 1:nrow(indics_stressors)) {
#Marks the site as not having the necessary data
if(is.na(indics_stressors[j, "SimpsonDiv_rescaled"])) {
demog_sites_with_queries[i*nrow(indics_stressors) + j, "tourism_axn"] <- "NO DEMOG DATA/NO CORAL COLONIES"
}
#Applies the management criteria to each site
else if(indics_stressors[j,"SimpsonDiv_rescaled"] > SimpsonDiv_avg && indics_stressors[j,"AlgaeCover_rescaled"] < AlgaeCover_avg & indics_stressors[j,"HerbivoreBiomass_rescaled"] > HerbivoreBiomass_avg) {
#If the site meets all critera, it is labeled "TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "tourism_axn"] <- "TOURISM"
} else {
#If the site does not meet both criteria, it is labeled "NOT TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "tourism_axn"] <- "NOT TARGET"
}
}
return(demog_sites_with_queries)
}
#Identifies sites that are conducive to fishery management and enforcement
#Criteria: 1) Herbivore biomass is lower than the average across all sites, and 2) fishing pressure is higher than average across all sites
fishing_axn <- function(indics_stressors, i) {
#Average herbivore biomass across all sites
HerbivoreBiomass_avg <- mean(indics_stressors[,c("HerbivoreBiomass_rescaled")], na.rm=TRUE)
#Average fishing pressure across all sites
fishing_avg <- mean(indics_stressors[,c("all_total_fishing_rescaled")], na.rm=TRUE)
#Determines whether each site fits the fishing management criteria
for (j in 1:nrow(indics_stressors)) {
#Applies the two criteria to each site
if(indics_stressors[j,"HerbivoreBiomass_rescaled"] < HerbivoreBiomass_avg && indics_stressors[j,"all_total_fishing_rescaled"] > fishing_avg){
#If the site meets both critera, it is labeled "TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "fishing_axn"] <- "FISHING"
} else {
#If the site does not meet both criteria, it is labeled "NOT TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "fishing_axn"] <- "NOT TARGET"
}
}
return(demog_sites_with_queries)
}
#Identifies sites that are conducive to disease monitoring
#Criteria: 1) Coral cover is higher than average across all sites, and 2) disease prevalence is higher than the average across all sites
disease_axn <- function(indics_stressors, i) {
#Average coral cover across all sites
CoralCover_avg <- mean(indics_stressors[,c("HardCoralCover_rescaled")], na.rm=TRUE)
#Average fraction of colonies without disease across all sites
NotDiseased_avg <- mean(indics_stressors[,c("FractNotDiseased_rescaled")], na.rm=TRUE)
#Determines whether each site fits the disease management criteria
for (j in 1:nrow(indics_stressors)) {
#Marks the site as not having the necessary data
if(is.na(indics_stressors[j, "FractNotDiseased_rescaled"])) {
demog_sites_with_queries[i*nrow(indics_stressors) + j, "disease_axn"] <- "NO DEMOG DATA/NO CORAL COLONIES"
}
#Applies the two criteria to each site
else if(indics_stressors[j,"HardCoralCover_rescaled"] > CoralCover_avg && indics_stressors[j,"FractNotDiseased_rescaled"] < NotDiseased_avg){
#If the site meets both critera, it is labeled "TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "disease_axn"] <- "DISEASE"
} else {
#If the site does not meet both criteria, it is labeled "NOT TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "disease_axn"] <- "NOT TARGET"
}
}
return(demog_sites_with_queries)
}
#Identifies sites that are conducive to coral protection
#Criteria: 1) LBSP is moderate, 2) fishing is moderate, and 3) resilience is in the upper two quartiles (excluding the temperature variation indicator)
protexn_axn <- function(indics_stressors, i, cutoff_quartile) {
#Average and standard deviation of LBSP values (sediment and nitrogen combined) across all sites that had LBSP values
LBSP_avg <- mean(indics_stressors[,c("StrdLBSP")], na.rm=TRUE)
LBSP_stdev <- sd(indics_stressors[,c("StrdLBSP")], na.rm=TRUE)
#Average and standard deviation of fishing pressure across all sites
fishing_avg <- mean(indics_stressors[,c("all_total_fishing_rescaled")], na.rm=TRUE)
fishing_stdev <- sd(indics_stressors[,c("all_total_fishing_rescaled")], na.rm=TRUE)
#Determines whether each site fits the protection criteria
for (j in 1:nrow(indics_stressors)) {
#Marks the site as not having LBSP data
if(is.na(indics_stressors[j, "StrdLBSP"])) {
demog_sites_with_queries[i*nrow(indics_stressors) + j, "protexn_axn"] <- "NO LBSP DATA"
}
#Applies the management criteria to each site
else if(indics_stressors[j,"StrdLBSP"] <= LBSP_avg + LBSP_stdev && indics_stressors[j,"StrdLBSP"] >= LBSP_avg - LBSP_stdev && indics_stressors[j,"all_total_fishing_rescaled"] <= fishing_avg + fishing_stdev && indics_stressors[j,"all_total_fishing_rescaled"] >= fishing_avg - fishing_stdev && indics_stressors[j,"resil_quart_no_temp_var"] <= cutoff_quartile) {
#If the site meets both critera, it is labeled "TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "protexn_axn"] <- "PROTECT"
} else {
#If the site does not meet both criteria, it is labeled "NOT TARGET"
demog_sites_with_queries[i*nrow(indics_stressors) + j, "protexn_axn"] <- "NOT TARGET"
}
}
return(demog_sites_with_queries)
}
```
Runs management queries using indicators, resilience scores, and fishing/LBSP stressor values
```{r Runs management queries using indicators, resilience scores, and fish/LBSP stressor values}
#Dataframe that stores the management query results
demog_sites_with_queries <- demog_sites_with_stressors
#The number of higher resilience quartiles which are being used for querying management