-
Notifications
You must be signed in to change notification settings - Fork 1
/
BelizeRT_CalcSurvData.Rmd
1352 lines (922 loc) · 60.5 KB
/
BelizeRT_CalcSurvData.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: "Calcification and Survival Data Analyses and Visualization *(Castillo et al In Prep)*"
author: "Colleen B. Bove"
date: "*Last run on `r format(Sys.time(), '%d %B %Y')`*"
output:
html_document:
toc: true
toc_float: true
theme: flatly
---
<br/>
This file contains all data manipulations, analyses, and visualizations used to produce the temperature, calcification, and survival results in Castillo KD, Bove CB, et al. (*In Prep*). All gene expression data and analyses can be found in this repository on the *[RT_Host_Sym_GE.R](https://github.com/seabove7/BelizeRT_Castillo/blob/main/RT_Host_Sym_GE.R)* script and the ITS2 analyses can be found in the *[reciprocal_transplants_ITS2.Rmd](https://github.com/seabove7/BelizeRT_Castillo/blob/main/reciprocal_transplants_ITS2.Rmd)* file.
```{r load packages, message=FALSE, warning=FALSE, include=FALSE}
library(lme4)
library(cowplot)
library(kableExtra)
library(tidyverse)
library(survival)
library(coxme)
library(survival)
library(survminer)
library(patchwork)
library(finalfit)
library(performance)
library(gghalves)
library(xts)
library(FSA)
library(png)
library(car)
## Knit standards
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, options(knitr.kable.NA = ''))
options(warn = -1)
```
```{r set some standards, message=FALSE, warning=FALSE, include=FALSE}
## this function removes rows with any missing values:
completeFun <- function(data, desiredCols) {
completeVec <- complete.cases(data[, desiredCols])
return(data[completeVec, ])
}
## Set some values for running bootstrapping of calcification models:
bootnum = 4000 # set number of iterations (we used 4000) between 999 and 9999
seed = 14 # seed to make results replicatable (our seed was 14)
```
---
## *In situ* temperature {.tabset}
```{r temp dataframe setup, message=FALSE, warning=FALSE}
temp_long <- read.csv("Data/RTE_Temperature_Nov2014_Oct2015.csv") %>%
mutate(date_time = as.POSIXct(Date.Time, format = "%m/%d/%y %H:%M")) %>% # convert date/time to POSIXct class
select(-Date.Time) %>%
pivot_longer(Forereef:Nearshore, names_to = "reef", values_to = "temp") %>%
mutate(reef = fct_relevel(reef, c("Nearshore", "Backreef", "Forereef")))
temp_wide <- read.csv("Data/RTE_Temperature_Nov2014_Oct2015.csv") %>%
mutate(date_time = as.POSIXct(Date.Time, format = "%m/%d/%y %H:%M")) %>% # convert date/time to POSIXct class
select(-Date.Time) %>%
drop_na()
```
```{r all temperature measurements across time}
raw_temp_plot <- ggplot(data = temp_long, aes(x = date_time, y = temp, colour = reef, group = reef)) +
geom_line() +
scale_colour_manual(values = c("#D55E00","#009E73","#0072B2")) +
theme_bw() +
theme(panel.grid = element_blank(), axis.title.x = element_blank(), legend.title = element_blank(), legend.position = c(0.8, 0.2)) +
ylab("Temperature (°C)")
raw_temp_plot
```
### Overall temperatures
Mean +/- SE and distribution across reefs
```{r overall temp plot}
temp_plot <- ggplot(data = temp_long, aes(x = reef, y = temp, fill = reef, colour = reef, group = reef)) +
geom_half_violin(side = "r") +
stat_summary(fun.data = "mean_sdl", fun.args = list(mult = 1), geom = "errorbar", width = 0, size = 1, colour = "gray15") +
stat_summary(fun = "mean", size = 0.5, colour = "gray15") +
scale_fill_manual(values = c("#D55E00","#009E73","#0072B2")) +
scale_colour_manual(values = c("#D55E00","#009E73","#0072B2")) +
theme_bw() +
theme(panel.grid = element_blank(), axis.title.x = element_blank(), legend.title = element_blank(), legend.position = "none") +
guides(fill = guide_legend(override.aes = list(fill = NA)),
color = guide_legend(override.aes = list(linetype = 0, colour = c("#D55E00","#009E73","#0072B2")))) +
ylab("Temperature (°C)")
```
```{r temp summary table}
temp_long %>%
group_by(reef) %>%
summarise(Mean = round(mean(temp, rm.na = TRUE), 1),
SD = round(sd(temp), 2),
Min = round(min(temp), 1),
Max = round(max(temp), 1),
N = n(),
SE = round((SD / sqrt(N)), 10)) %>%
kable() %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%
row_spec(0, bold = TRUE) %>%
kable_classic(full_width = FALSE, html_font = "Times")
```
```{r overall temp stat}
### Checking the linear model for overall temps:
temp_lm <- lm((temp) ~ reef, data = temp_long)
##check_model(temp_lm)
#check_heteroscedasticity(temp_lm) # no good
#check_normality(temp_lm) # no good
## transforming the data (log, sqrt, etc., does not improve the distribution of the residuals)
## Moving forward with Kruskal-Wallis test: non-parametric testing of distributions of samples
temp_kw <- kruskal.test(temp ~ reef, data = temp_long)
temp_pval <- substitute(italic(P[reef])==p, list(p = format(temp_kw[["p.value"]], digits = 2))) # save the p-value for plotting
### Pairwise comparisons of the reef environments using Dunn's Test for Multiple Comparisons with bonferroni p adjustment
temp_dt <- dunnTest(temp ~ reef, data = temp_long, method = "bonferroni")
temp_FN_pval <- substitute(italic(P[FR-NS])==p, list(p = format(temp_dt[["res"]][["P.adj"]][3], digits = 2))) # save the p-value for plotting
temp_NB_pval <- substitute(italic(P[NS-BR])==p, list(p = format(temp_dt[["res"]][["P.adj"]][2], digits = 2))) # save the p-value for plotting
temp_FB_pval <- substitute(italic(P[FR-BR])==p, list(p = format(temp_dt[["res"]][["P.adj"]][1], digits = 2))) # save the p-value for plotting
```
```{r overall temp plot with stats, fig.height=4, fig.width=4}
temp_stat_plot <- temp_plot +
#annotate("text", x = 1, y = 24.8, label = deparse(temp_pval), parse = TRUE, size = 3) +
annotate("text", x = 3.0, y = 33.4, label = deparse(temp_FN_pval), parse = TRUE, size = 3.5) +
annotate("text", x = 3.02, y = 32.7, label = deparse(temp_FB_pval), parse = TRUE, size = 3.5) +
annotate("text", x = 3.0, y = 32.0, label = deparse(temp_NB_pval), parse = TRUE, size = 3.5)
temp_stat_plot
#ggsave("Figures/Overall_temp.pdf", height = 4, width = 4, useDingbats = FALSE) # save plot
```
<br/>
### Mean temperatures
```{r temp df to xts}
## convert the temp data to xts for manipulation with apply.xxx
temp_xts <- xts(temp_wide[,-4], order.by = temp_wide[,4])
```
Means (monthly, weekly, daily)
```{r temp mean plots}
## Monthly Mean
monthly_mean <- data.frame(apply.monthly(temp_xts, colMeans, na.rm = TRUE)) %>% # monthly mean SST
rownames_to_column(var = "date") %>%
pivot_longer(-date, names_to = "reef", values_to = "mean_temp") %>%
mutate(reef = fct_relevel(reef, c("Nearshore", "Backreef", "Forereef")))
month_mean_plot <- ggplot(data = monthly_mean, aes(x = reef, y = mean_temp, fill = reef, colour = reef)) +
labs(title = "Monthly Mean: Nov 2014 - Oct 2015",
subtitle = "Temp collected every 30 min") +
geom_half_violin(side = "r", alpha = 0.3, colour = NA) +
geom_half_dotplot(side = "r", dotsize = 0.7) +
#stat_summary(fun = "mean", size = 0.5, colour = "gray15") +
scale_colour_manual(values = c("#D55E00","#009E73","#0072B2")) +
scale_fill_manual(values = c("#D55E00","#009E73","#0072B2")) +
theme_bw() +
theme(panel.grid = element_blank(), axis.title.x = element_blank(), legend.title = element_blank(), legend.position = "none") +
guides(fill = guide_legend(override.aes = list(fill = NA)),
color = guide_legend(override.aes = list(linetype = 0, colour = c("#D55E00","#009E73","#0072B2")))) +
ylab("Temperature (°C)")
## Weekly Mean
weekly_mean <- data.frame(apply.weekly(temp_xts, colMeans, na.rm = TRUE)) %>% # monthly mean SST
rownames_to_column(var = "date") %>%
pivot_longer(-date, names_to = "reef", values_to = "mean_temp") %>%
mutate(reef = fct_relevel(reef, c("Nearshore", "Backreef", "Forereef")))
week_mean_plot <- ggplot(data = weekly_mean, aes(x = reef, y = mean_temp, fill = reef, colour = reef)) +
labs(title = "Weekly Mean: Nov 2014 - Oct 2015",
subtitle = "Temp collected every 30 min") +
geom_half_violin(side = "r", alpha = 0.3, colour = NA) +
geom_half_dotplot(side = "r", dotsize = 0.7) +
#stat_summary(fun = "mean", size = 0.5, colour = "gray15") +
scale_colour_manual(values = c("#D55E00","#009E73","#0072B2")) +
scale_fill_manual(values = c("#D55E00","#009E73","#0072B2")) +
theme_bw() +
theme(panel.grid = element_blank(), axis.title.x = element_blank(), legend.title = element_blank(), legend.position = "none") +
guides(fill = guide_legend(override.aes = list(fill = NA)),
color = guide_legend(override.aes = list(linetype = 0, colour = c("#D55E00","#009E73","#0072B2")))) +
ylab("Temperature (°C)")
## Daily Mean
daily_mean <- data.frame(apply.daily(temp_xts, colMeans, na.rm = TRUE)) %>% # monthly mean SST
rownames_to_column(var = "date") %>%
pivot_longer(-date, names_to = "reef", values_to = "mean_temp") %>%
mutate(reef = fct_relevel(reef, c("Nearshore", "Backreef", "Forereef")))
day_mean_plot <- ggplot(data = daily_mean, aes(x = reef, y = mean_temp, fill = reef, colour = reef)) +
labs(title = "Daily Mean: Nov 2014 - Oct 2015",
subtitle = "Temp collected every 30 min") +
geom_half_violin(side = "r", alpha = 0.3, colour = NA) +
geom_half_dotplot(side = "r", dotsize = 0.3) +
#stat_summary(fun = "mean", size = 0.5, colour = "gray15") +
scale_colour_manual(values = c("#D55E00","#009E73","#0072B2")) +
scale_fill_manual(values = c("#D55E00","#009E73","#0072B2")) +
theme_bw() +
theme(panel.grid = element_blank(), axis.title.x = element_blank(), legend.title = element_blank(), legend.position = "none") +
guides(fill = guide_legend(override.aes = list(fill = NA)),
color = guide_legend(override.aes = list(linetype = 0, colour = c("#D55E00","#009E73","#0072B2")))) +
ylab("Temperature (°C)")
```
```{r stats of daily mean temps}
### Checking the linear model for daily mean temps:
day_mean_lm <- lm((mean_temp) ~ reef, data = daily_mean)
##check_model(day_mean_lm)
#check_heteroscedasticity(day_mean_lm) # no good
#check_normality(day_mean_lm) # no good
## transforming the data (log, sqrt, etc., improves the heteroscedasticity but not the distribution of the residuals)
## Moving forward with Kruskal-Wallis test: non-parametric testing of distributions of samples
day_mean_kw <- kruskal.test((mean_temp) ~ reef, data = daily_mean)
day_mean_pval <- substitute(italic(P[reef])==p, list(p = format(day_mean_kw[["p.value"]], digits = 2))) # save the p-value for plotting
### Pairwise comparisons of the reef environments using Dunn's Test for Multiple Comparisons with bonferroni p adjustment
day_mean_dt <- dunnTest(mean_temp ~ reef, data = daily_mean, method = "bonferroni")
day_mean_FN_pval <- substitute(italic(P[FR-NS])==p, list(p = format(day_mean_dt[["res"]][["P.adj"]][3], digits = 2))) # save the p-value for plotting
day_mean_NB_pval <- substitute(italic(P[NS-BR])==p, list(p = format(day_mean_dt[["res"]][["P.adj"]][2], digits = 2))) # save the p-value for plotting
day_mean_FB_pval <- substitute(italic(P[FR-BR])==p, list(p = format(day_mean_dt[["res"]][["P.adj"]][1], digits = 2))) # save the p-value for plotting
```
```{r stats of weekly mean temps}
### Checking the linear model for weekly mean temps:
week_mean_lm <- lm((mean_temp) ~ reef, data = weekly_mean)
##check_model(week_mean_lm)
#check_heteroscedasticity(week_mean_lm) # okay!
#check_normality(week_mean_lm) # no good
## transforming the data (log, sqrt, etc., does not improve the distribution of the residuals)
## Moving forward with Kruskal-Wallis test: non-parametric testing of distributions of samples
week_mean_kw <- kruskal.test(mean_temp ~ reef, data = weekly_mean)
week_mean_pval <- substitute(italic(P[reef])==p, list(p = format(week_mean_kw[["p.value"]], digits = 2))) # save the p-value for plotting
### Pairwise comparisons of the reef environments using Dunn's Test for Multiple Comparisons with bonferroni p adjustment
week_mean_dt <- dunnTest(mean_temp ~ reef, data = weekly_mean, method = "bonferroni")
```
```{r stats of monthly mean temps}
### Checking the linear model for monthly mean temps:
month_mean_lm <- lm((mean_temp) ~ reef, data = monthly_mean)
##check_model(month_mean_lm)
#check_heteroscedasticity(month_mean_lm) # okay!
#check_normality(month_mean_lm) # no good
## transforming the data (log, sqrt, etc., does not improve the distribution of the residuals)
## Moving forward with Kruskal-Wallis test: non-parametric testing of distributions of samples
month_mean_kw <- kruskal.test(mean_temp ~ reef, data = monthly_mean)
month_mean_pval <- substitute(italic(P[reef])==p, list(p = format(month_mean_kw[["p.value"]], digits = 2))) # save the p-value for plotting
### Pairwise comparisons of the reef environments using Dunn's Test for Multiple Comparisons with bonferroni p adjustment
month_mean_dt <- dunnTest(mean_temp ~ reef, data = monthly_mean, method = "bonferroni")
```
```{r full mean plots, fig.height=4, fig.width=12, message=FALSE, warning=FALSE}
## Add the stats:
month_mean_stat_plot <- month_mean_plot +
annotate("text", x = 1, y = 26, label = deparse(month_mean_pval), parse = TRUE, size = 3)
week_mean_stat_plot <- week_mean_plot +
annotate("text", x = 1, y = 26, label = deparse(week_mean_pval), parse = TRUE, size = 3)
day_mean_stat_plot <- day_mean_plot +
annotate("text", x = 1, y = 25.5, label = deparse(day_mean_pval), parse = TRUE, size = 3) +
annotate("text", x = 3.75, y = 32, label = deparse(day_mean_FN_pval), parse = TRUE, size = 2.5) +
annotate("text", x = 3.83, y = 31.7, label = deparse(day_mean_FB_pval), parse = TRUE, size = 2.5) +
annotate("text", x = 3.77, y = 31.4, label = deparse(day_mean_NB_pval), parse = TRUE, size = 2.5) +
coord_cartesian(x = c(1, 3.7))
## Combine all mean plots together for visual comparison
ggarrange(month_mean_stat_plot, week_mean_stat_plot, day_mean_stat_plot, ncol = 3)
#ggsave("Figures/Mean_temp.pdf", height = 4, width = 12, useDingbats = FALSE) # save plot
```
<br/>
### Temperature ranges
Ranges (monthly, weekly, daily)
```{r temp range plots}
## a column range function to apply across time ranges below
colRange <- function(x) sapply(x, FUN = function(x) (max(x, na.rm = TRUE) - min(x, na.rm = TRUE)))
## Monthly Range
monthly_range <- data.frame(apply.monthly(temp_xts, colRange)) %>% # monthly mean SST
rownames_to_column(var = "date") %>%
pivot_longer(-date, names_to = "reef", values_to = "temp_range") %>%
mutate(reef = fct_relevel(reef, c("Nearshore", "Backreef", "Forereef")))
month_range_plot <- ggplot(data = monthly_range, aes(x = reef, y = temp_range, fill = reef, colour = reef)) +
labs(title = "Monthly Range: Nov 2014 - Oct 2015",
subtitle = "Temp collected every 30 min") +
geom_half_violin(side = "r", alpha = 0.3, colour = NA) +
geom_half_dotplot(side = "r", dotsize = 0.7) +
#stat_summary(fun = "mean", size = 0.5, colour = "gray15") +
scale_colour_manual(values = c("#D55E00","#009E73","#0072B2")) +
scale_fill_manual(values = c("#D55E00","#009E73","#0072B2")) +
theme_bw() +
theme(panel.grid = element_blank(), axis.title.x = element_blank(), legend.title = element_blank(), legend.position = "none") +
guides(fill = guide_legend(override.aes = list(fill = NA)),
color = guide_legend(override.aes = list(linetype = 0, colour = c("#D55E00","#009E73","#0072B2")))) +
ylab("Temperature (°C)")
## Weekly Range
weekly_range <- data.frame(apply.weekly(temp_xts, colRange)) %>% # monthly mean SST
rownames_to_column(var = "date") %>%
pivot_longer(-date, names_to = "reef", values_to = "temp_range") %>%
mutate(reef = fct_relevel(reef, c("Nearshore", "Backreef", "Forereef")))
week_range_plot <- ggplot(data = weekly_range, aes(x = reef, y = temp_range, fill = reef, colour = reef)) +
labs(title = "Weekly Range: Nov 2014 - Oct 2015",
subtitle = "Temp collected every 30 min") +
geom_half_violin(side = "r", alpha = 0.3, colour = NA) +
geom_half_dotplot(side = "r", dotsize = 0.7, binwidth = 1/5) +
#stat_summary(fun = "mean", size = 0.5, colour = "gray15") +
scale_colour_manual(values = c("#D55E00","#009E73","#0072B2")) +
scale_fill_manual(values = c("#D55E00","#009E73","#0072B2")) +
theme_bw() +
theme(panel.grid = element_blank(), axis.title.x = element_blank(), legend.title = element_blank(), legend.position = "none") +
guides(fill = guide_legend(override.aes = list(fill = NA)),
color = guide_legend(override.aes = list(linetype = 0, colour = c("#D55E00","#009E73","#0072B2")))) +
ylab("Temperature (°C)")
## Daily Range
daily_range <- data.frame(apply.daily(temp_xts, colRange)) %>% # monthly mean SST
rownames_to_column(var = "date") %>%
pivot_longer(-date, names_to = "reef", values_to = "temp_range") %>%
mutate(reef = fct_relevel(reef, c("Nearshore", "Backreef", "Forereef")))
day_range_plot <- ggplot(data = daily_range, aes(x = reef, y = temp_range, fill = reef, colour = reef)) +
labs(title = "Daily Range: Nov 2014 - Oct 2015",
subtitle = "Temp collected every 30 min") +
geom_half_violin(side = "r", alpha = 0.3, colour = NA) +
geom_half_dotplot(side = "r", dotsize = 0.3, binwidth = 1/10) +
#stat_summary(fun = "mean", size = 0.5, colour = "gray15") +
scale_colour_manual(values = c("#D55E00","#009E73","#0072B2")) +
scale_fill_manual(values = c("#D55E00","#009E73","#0072B2")) +
theme_bw() +
theme(panel.grid = element_blank(), axis.title.x = element_blank(), legend.title = element_blank(), legend.position = "none") +
guides(fill = guide_legend(override.aes = list(fill = NA)),
color = guide_legend(override.aes = list(linetype = 0, colour = c("#D55E00","#009E73","#0072B2")))) +
ylab("Temperature (°C)")
```
```{r stats of daily range temps}
### Checking the linear model for daily range temps:
day_range_lm <- lm((temp_range) ~ reef, data = daily_range)
##check_model(day_range_lm)
#check_heteroscedasticity(day_range_lm) # no good
#check_normality(day_range_lm) # no good
## transforming the data (log, sqrt, etc., improves the heteroscedasticity but not the distribution of the residuals)
## Moving forward with Kruskal-Wallis test: non-parametric testing of distributions of samples
day_range_kw <- kruskal.test(temp_range ~ reef, data = daily_range)
day_range_pval <- substitute(italic(P[reef])==p, list(p = format(day_range_kw[["p.value"]], digits = 2))) # save the p-value for plotting
### Pairwise comparisons of the reef environments using Dunn's Test for Multiple Comparisons with bonferroni p adjustment
day_range_dt <- dunnTest(temp_range ~ reef, data = daily_range, method = "bonferroni")
day_range_FN_pval <- substitute(italic(P[FR-NS])==p, list(p = format(day_range_dt[["res"]][["P.adj"]][3], digits = 2))) # save the p-value for plotting
day_range_NB_pval <- substitute(italic(P[NS-BR])==p, list(p = format(day_range_dt[["res"]][["P.adj"]][2], digits = 2))) # save the p-value for plotting
day_range_FB_pval <- substitute(italic(P[FR-BR])==p, list(p = format(day_range_dt[["res"]][["P.adj"]][1], digits = 2))) # save the p-value for plotting
```
```{r stats of weekly range temps}
### Checking the linear model for weekly range temps:
week_range_lm <- lm((temp_range) ~ reef, data = weekly_range)
##check_model(week_range_lm)
#check_heteroscedasticity(week_range_lm) # okay!
#check_normality(week_range_lm) # no good
## transforming the data (log, sqrt, etc., does not improve the distribution of the residuals)
## Moving forward with Kruskal-Wallis test: non-parametric testing of distributions of samples
week_range_kw <- kruskal.test(temp_range ~ reef, data = weekly_range)
week_range_pval <- substitute(italic(P[reef])==p, list(p = format(week_range_kw[["p.value"]], digits = 2))) # save the p-value for plotting
### Pairwise comparisons of the reef environments using Dunn's Test for Multiple Comparisons with bonferroni p adjustment
week_range_dt <- dunnTest(temp_range ~ reef, data = weekly_range, method = "bonferroni")
week_range_FN_pval <- substitute(italic(P[FR-NS])==p, list(p = format(week_range_dt[["res"]][["P.adj"]][3], digits = 2))) # save the p-value for plotting
week_range_NB_pval <- substitute(italic(P[NS-BR])==p, list(p = format(week_range_dt[["res"]][["P.adj"]][2], digits = 2))) # save the p-value for plotting
week_range_FB_pval <- substitute(italic(P[FR-BR])==p, list(p = format(week_range_dt[["res"]][["P.adj"]][1], digits = 2))) # save the p-value for plotting
```
```{r stats of monthly range temps}
### Checking the linear model for monthly range temps:
month_range_lm <- lm((temp_range) ~ reef, data = monthly_range)
##check_model(month_range_lm)
#check_heteroscedasticity(month_range_lm) # okay!
#check_normality(month_range_lm) # okay!
## data are good! But to keep consistent, I am moving forward with the same stat tests as other metrics
## Moving forward with Kruskal-Wallis test: non-parametric testing of distributions of samples
month_range_kw <- kruskal.test(temp_range ~ reef, data = monthly_range)
month_range_pval <- substitute(italic(P[reef])==p, list(p = format(month_range_kw[["p.value"]], digits = 2))) # save the p-value for plotting
### Pairwise comparisons of the reef environments using Dunn's Test for Multiple Comparisons with bonferroni p adjustment
month_range_dt <- dunnTest(temp_range ~ reef, data = monthly_range, method = "bonferroni")
month_range_FN_pval <- substitute(italic(P[FR-NS])==p, list(p = format(month_range_dt[["res"]][["P.adj"]][3], digits = 2))) # save the p-value for plotting
month_range_NB_pval <- substitute(italic(P[NS-BR])==p, list(p = format(month_range_dt[["res"]][["P.adj"]][2], digits = 2))) # save the p-value for plotting
month_range_FB_pval <- substitute(italic(P[FR-BR])==p, list(p = format(month_range_dt[["res"]][["P.adj"]][1], digits = 2))) # save the p-value for plotting
```
```{r full range plots, fig.height=4, fig.width=12, message=FALSE, warning=FALSE}
## Add the stats:
month_range_stat_plot <- month_range_plot +
annotate("text", x = 0.95, y = 1.8, label = deparse(month_range_pval), parse = TRUE, size = 3) +
annotate("text", x = 3.22, y = 5.2, label = deparse(month_range_FN_pval), parse = TRUE, size = 2.5) +
annotate("text", x = 3.28, y = 5.6, label = deparse(month_range_NB_pval), parse = TRUE, size = 2.5) +
annotate("text", x = 3.26, y = 5.4, label = deparse(month_range_FB_pval), parse = TRUE, size = 2.5)
week_range_stat_plot <- week_range_plot +
#annotate("text", x = 0.95, y = 0.1, label = deparse(week_range_pval), parse = TRUE, size = 3) +
annotate("text", x = 3.62, y = 5.0, label = deparse(week_range_FN_pval), parse = TRUE, size = 3.5) +
annotate("text", x = 3.67, y = 6.0, label = deparse(week_range_NB_pval), parse = TRUE, size = 3.5) +
annotate("text", x = 3.62, y = 5.5, label = deparse(week_range_FB_pval), parse = TRUE, size = 3.5) +
coord_cartesian(x = c(1, 3.7))
day_range_stat_plot <- day_range_plot +
annotate("text", x = 0.95, y = 0.1, label = deparse(day_range_pval), parse = TRUE, size = 3) +
annotate("text", x = 3.9, y = 3.2, label = deparse(day_range_FN_pval), parse = TRUE, size = 2.5) +
annotate("text", x = 3.97, y = 3.5, label = deparse(day_range_NB_pval), parse = TRUE, size = 2.5) +
annotate("text", x = 3.93, y = 3.35, label = deparse(day_range_FB_pval), parse = TRUE, size = 2.5) +
coord_cartesian(x = c(1, 4))
## Combine all range plots together for visual comparison
ggarrange(month_range_stat_plot, week_range_stat_plot, day_range_stat_plot, ncol = 3)
#ggsave("Figures/Range_temp.pdf", height = 4, width = 12, useDingbats = FALSE) # save plot
```
<br/>
<br/>
## Coral survival {.tabset}
### Figure 2A
```{r survival dataframe setup, message=FALSE, warning=FALSE}
sur <- read.csv("Data/RT_survival.csv") %>% # Read in survival data (0 = living, 1 = mortality event under 'Status')
mutate(Source = fct_relevel(Source, c("NS", "BR", "FR")), # relevel source to be by distance from land
Transplant = fct_relevel(Transplant, c("NS", "BR", "FR"))) # relevel transplant to be by distance from land
sur$Colony <- as.factor(sur$Colony) # make colony ID a factor
sur.r <- Surv(sur$Days, sur$Status) # Creates your survival dataframe
# This produces the event data for each grouping (this case transplant) (% surviving at each Day and the CI with it)
sur.all <- survfit(sur.r ~ Source + Transplant, data = sur) # Computes an estimate of a survival curve based on transplantation using Kaplan-Meier; this will also be used for plotting
#sur.all # view output to see mortality event and sample size summary stats
#summary(sur.all)
```
<br/>
#### <span style="color: navy;">**Data Visualization**</span>
```{r survival figure, message=FALSE, warning=FALSE, fig.align='center', fig.width = 5.7, fig.height = 5.3}
surv_plot <- ggsurvplot(sur.all, data = sur,
palette = c("#D55E00", "#D55E00", "#D55E00", "#0072B2", "#0072B2", "#0072B2", "#009E73", "#009E73", "#009E73"),
linetype = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
censor = FALSE,
legend.title = "",
legend.labs = c("NS to NS", "NS to BR", "NS to FR", "BR to NS", "BR to BR", "BR to FR", "FR to NS", "FR to BR", "FR to FR"))
surv_plot$plot <- surv_plot$plot + scale_x_continuous(breaks = c(0, 365, 711, 1088, 1445), labels = c("2011", "2012", "2013", "2014", "2015")) +
guides(col = guide_legend(ncol = 3)) +
labs(x = "Time (years)", y = "Fraction surviving") +
theme_bw() +
theme(plot.margin = margin(0.1, 0.7, 0.1, 0.1, "cm"), panel.grid = element_blank(), legend.position = c(0.37, 0.13), text = element_text(size = 14), legend.key.width = unit(2, "line"), legend.background = element_blank()) +
coord_cartesian(y = c(-0.02, 1.05), expand = FALSE)
fig2a <- surv_plot$plot
fig2a
## Save survival plots
# ggsave("Figures/Figure2A_Survival.pdf", width = 5.7, height = 5.5, useDingbats = FALSE) # save as PDF
# ggsave("Figures/Figure2A_Survival.png", width = 5.7, height = 5.5) # save as PNG
```
**Figure 2A.** Fraction of surviving *Siderastrea siderea* colonies through time (2011-2015) after reciprocal transplantation with respect to source location and transplanted location. Source location is represented by line type (FR = dashed; BR = solid; NS = dotted) and transplant location is denoted by color (<span style="color: #D55E00;">orange = NS [nearshore]</span>, <span style="color: #009E73;">green = BR [backreef]</span>, and <span style="color: #0072B2;">blue = FR [forereef]</span>).
<br/>
### Supplementary Tables
#### Supplemental Table 4
**Table S4.** Number or corals surviving per year in each transplant location (BR = backreef, FR = forereef, NS = Nearshore).
```{r}
tableS4 <- read.csv("Data/RT_survival_table.csv")[1:7] # Read in num corals surviving at each timepoint
names(tableS4) <- gsub(x = names(tableS4), pattern = "X", replacement = "") # remove 'X' from in front of years of columns
kable(tableS4) %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%
row_spec(0, bold = TRUE) %>%
kable_classic(full_width = FALSE, html_font = "Times")
```
<br/>
#### Supplemental Table 5
```{r survival model, echo=TRUE, message=FALSE, warning=FALSE}
sur$Source <- fct_relevel(sur$Source, c("FR", "BR", "NS"))
sur$Transplant <- fct_relevel(sur$Transplant, c("FR", "BR", "NS"))
surv.mod <- coxph(sur.r ~ Transplant + Source, data = sur)
```
<br/>
**Table S5.** Statistical assessment of the cox proportional hazards survival model. (A) Type 2 analysis of deviance of the overall effect of transplant and source. (B) Cox proportional hazard model output, with exp(coef) representing the hazard ratio, se(coef) is the standard error, z is the z score, and Pr(>|z|) is the probability that the estimate could be 0.
```{r survival ANOVA}
surv_aov <- Anova(surv.mod)
surv_aov$`Pr(>Chisq)` <- format(surv_aov$`Pr(>Chisq)`, digits = 3)
kable(surv_aov, digits = 3) %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%
column_spec(1, bold = TRUE) %>%
kable_classic(full_width = FALSE, html_font = "Times")
```
```{r survival model table, message=FALSE, warning=FALSE}
surv_mod_df <- data.frame(summary(surv.mod)$coefficients)
colnames(surv_mod_df) <- c("coefficient", "exp(coef)" , "se(coef)", "z", "Pr(>|z|)")
kable(surv_mod_df, digits = 2) %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%
column_spec(1, bold = TRUE) %>%
kable_classic(full_width = FALSE, html_font = "Times")
```
<br/>
###############################
## Calcification rate {.tabset}
<br/>
### Overall Calcification Statistics
```{r calc df setup, message=FALSE, warning=FALSE}
# read in dataframe
RT.calc <- read.csv('Data/RT_calcification.csv') # read in data
# convert dataframe from wide to long
RT.calc <- gather(RT.calc, TP, rate, y23:avg2)
# use function to remove any rows with missues values in the 'rate' column
RT.calc <- completeFun(RT.calc, "rate")
# subset for the average rate of corals that made it to the end
RT.calc.fin <- subset(RT.calc, TP == "avg")
RT.calc.fin$rate <- as.numeric(RT.calc.fin$rate)
# subset for timepoint calcification rates (this dataframe is for the 'timepoint calcification' analysis)
RT.calc.tp <- subset(RT.calc, TP != "avg2")
RT.calc.tp <- subset(RT.calc.tp, TP != "avg")
# make TP and colony factors
RT.calc.tp$colony <- factor(RT.calc.tp$colony)
RT.calc.tp$TP <- factor(RT.calc.tp$TP)
```
#### <span style="color: navy;">**Model selection**</span>
**Overall calcification Akaike information criterion (AIC) model selection** Model selection for assessment of source and transplant location impacts on overall (2012 - 2015) coral calcification rates was performed using AIC. The model with the lowest AIC (in bold) best fit the data and was used for analyses.
```{r calcification AIC model selection, message=FALSE, warning=FALSE}
## AIC model selection
full.int <- lm(rate ~ source * trans, data = RT.calc.fin) # interaction of source with transplant location
# data look normal so I am okay with proceeding with a linear model here
##check_normality(full.int)
##check_model(full.int)
full.add <- lm(rate ~ source + trans, data = RT.calc.fin) # additive model of source with transplant location
tran.only <- lm(rate ~ trans, data = RT.calc.fin) # transplant location only
sourc.only <- lm(rate ~ source, data = RT.calc.fin) # source location only
## AIC table
calc.AIC <- AIC(full.int, full.add, tran.only, sourc.only) # create AIC comparison table
row.names(calc.AIC) <- c("Source * Transplant", "Source + Transplant", "Transplant only", "Source only") # rename the rows to reflect the model
calc.AIC$AIC <- round(calc.AIC$AIC, 2) # round the AIC values to 2 decimal places
kable(calc.AIC) %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%
row_spec(1, bold = TRUE) %>%
kable_classic(full_width = FALSE, html_font = "Times")
```
<br/>
#### <span style="color: navy;">**Modelling Parametric Bootstrap 95% Confidence Intervals**</span>
Model:
```{r calcification model, echo = TRUE}
calc.model <- lm(rate ~ source * trans, data = RT.calc.fin) # selected model
```
```{r calcification bootstrap, eval=FALSE, message=FALSE, warning=FALSE, include=FALSE}
# NOTE: This is the code to run the parametric bootstrapping of the modeled calcification rates. It will NOT run when you knit the code. If you wish to run this portion of the code, either remove 'eval=FALSE' from codechunk. Bootstrapping code may take a long time to run, depending on model and dataframe size.
out <- simulate(calc.model, nsim = bootnum, seed = seed, re.form=NULL)
boots <- apply(out,2,function(x) predict(lm(x ~ source * trans, data = RT.calc.fin), reform = NA))
boots <- cbind(predict(calc.model, re.form=NA), boots)
RT.calc.fin2 <- (cbind(RT.calc.fin, as.data.frame(t(apply(boots, 1, function(x) c(mean(x), quantile(x, c(0.025, 0.975))))))))
colnames(RT.calc.fin2)[6:8] <- c("estimate", "lowerci", "upperci")
write.csv(RT.calc.fin2, "Data/RT_calc_boot.csv")
```
<br/>
**Modeled Mean Calcification Rate and 95% Confidence Interval**
Parametric bootstrapped mean and 95% confidence intervals of calcification rates (mg cm^-2^ day^-1^) in response to transplant treatment (BR = backreef, FR = forereef, NS = Nearshore).
```{r CI table, message=FALSE, warning=FALSE}
RT.calc <- read.csv("Data/RT_calc_boot.csv", header = TRUE) # new dataframe with modeled 95% CI and mean calcification rates
RT.calc$trans <- factor(RT.calc$trans, levels = c("NS", "BR", "FR")) # relevel transplant factors
RT.calc$source <- factor(RT.calc$source, levels = c("NS", "BR", "FR")) # relevel source factors
RT.calc$Treatment <- paste(RT.calc$source, RT.calc$trans, sep = " to ") # make a 'treatment' column
# condensed dataframe of modeled mean and 95% CI per treatment
RT.calc.tab <- RT.calc %>%
group_by(Treatment) %>%
summarize(`Modeled Mean` = mean(estimate, na.rm=TRUE),
`Lower 95% CI` = mean(lowerci, na.rm=TRUE),
`Upper 95% CI` = mean(upperci, na.rm=TRUE))
kable(RT.calc.tab, digits = 2) %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%
column_spec(1, bold = TRUE) %>%
kable_classic(full_width = FALSE, html_font = "Times")
```
### Figure 2B
```{r calcification plot, message=FALSE, warning=FALSE, fig.align='center', fig.height=4, fig.width=5}
fig2b <- ggplot(data = RT.calc, aes(x = trans, y = rate, colour = source)) +
theme_bw() +
theme(panel.grid = element_blank(), legend.key=element_blank(), text = element_text(size = 13)) +
geom_point(size = 2, position = position_jitterdodge(jitter.height = 0, dodge.width = 0.8, seed = 14)) +
geom_linerange(aes(ymin = lowerci, ymax = upperci, colour = source), position = position_dodge(width = 0.7), size = 1.2, alpha = 0.2) +
scale_color_manual("Source \nLocation", values = c("#D55E00", "#009E73", "#0072B2")) +
xlab("Transplant Location") +
ylab(bquote("Net Calcification (mg cm" ^-2~day^-1*")"))
fig2b
```
**Figure 2B.** Calcification rate (mg cm^–2^ day^–1^) averaged over the entire transplant experimental period (2012 – 2015) by transplant location. Source location is represented by color: <span style="color: #D55E00;">orange = NS (nearshore)</span>, <span style="color: #009E73;">green = BR (backreef)</span>, and <span style="color: #0072B2;">blue = FR (forereef)</span>. Colored bars represent modeled 95% confidence intervals with corresponding raw calcification rates per colony denoted by circles of the same color.
<br/>
### Calcification by Year
#### <span style="color: navy;">**Model selection**</span>
**Year calcification Akaike information criterion (AIC) model selection**
Model selection for assessment of source and transplant location impacts on Year coral calcification rates was performed using AIC. The model in bold appropriately fit the data and was used for analyses.
```{r TP calcification AIC model selection, message=FALSE, warning=FALSE}
## AIC model selection
full.int <- lm(rate ~ source * trans * TP, data = RT.calc.tp) # fully interactive model
# data look normal so I am okay with proceeding with a linear model here
##check_normality(full.int)
##check_model(full.int)
full.add <- lm(rate ~ source + trans + TP, data = RT.calc.tp) # fully additive model
add.tp <- lm(rate ~ source * trans + TP, data = RT.calc.tp) # interaction of source with transplant location, add Year
add.trans <- lm(rate ~ source * TP + trans, data = RT.calc.tp) # interaction of source location with Year, add transplant location
add.source <- lm(rate ~ trans * TP + source, data = RT.calc.tp) # interaction of transplant with Year, add source location
tran.only <- lm(rate ~ trans, data = RT.calc.tp) # transplant location only
sourc.only <- lm(rate ~ source, data = RT.calc.tp) # source location only
tp.only <- lm(rate ~ TP, data = RT.calc.tp) # Year only
## AIC table
calc.tp.AIC <- AIC(full.int, full.add, add.tp, add.trans, add.source, tran.only, sourc.only, tp.only) # create AIC comparison table
row.names(calc.tp.AIC) <- c("Source * Transplant * Year", "Source + Transplant + Year", "Source * Transplant + Year", "Source * Year + Transplant", "Transplant * Year + Source", "Transplant only ", "Source only ", "Year only") # rename the rows to reflect the model
calc.tp.AIC$AIC <- round(calc.tp.AIC$AIC, 2) # round the AIC values to 2 decimal places
kable(calc.tp.AIC) %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%
row_spec(1, bold = TRUE) %>%
kable_classic(full_width = FALSE, html_font = "Times")
```
<br/>
#### <span style="color: navy;">**Modelling Parametric Bootstrap 95% Confidence Intervals**</span>
Model:
```{r TP calcification model, echo = TRUE}
tp.model <- lm(rate ~ source * trans * TP, data = RT.calc.tp) # selected model
```
```{r TP calcification bootstrap, eval=FALSE, message=FALSE, warning=FALSE, include=FALSE}
# NOTE: This is the code to run the parametric bootstrapping of the modeled calcification rates. It will NOT run when you knit the code. If you wish to run this portion of the code, either remove 'eval=FALSE' from codechunk. Bootstrapping code may take a long time to run, depending on model and dataframe size.
out <- simulate(tp.model, nsim = bootnum, seed = seed, re.form=NULL)
boots <- apply(out,2,function(x) predict(lm(x ~ source * trans * TP, data = RT.calc.tp), reform = NA))
boots <- cbind(predict(tp.model, re.form=NA), boots)
RT.calc.tp2 <- (cbind(RT.calc.tp, as.data.frame(t(apply(boots, 1, function(x) c(mean(x), quantile(x, c(0.025, 0.975))))))))
colnames(RT.calc.tp2)[6:8] <- c("estimate", "lowerci", "upperci")
write.csv(RT.calc.tp2, "Data/RT_TP_calc_boot.csv")
```
<br/>
**Modeled Mean Calcification Rate and 95% Confidence Interval by Year**
Parametric bootstrapped mean and 95% confidence intervals of calcification rates (mg cm^-2^ day^-1^) in response to transplant treatment (BR = backreef, FR = forereef, NS = Nearshore) over time.
```{r TP CI table, message=FALSE, warning=FALSE}
RT.calc.tp <- read.csv("Data/RT_TP_calc_boot.csv", header = TRUE) # new dataframe with modeled 95% CI and mean calcification rates
RT.calc.tp$trans <- factor(RT.calc.tp$trans, levels = c("NS", "BR", "FR")) # relevel transplant location factors
RT.calc.tp$source <- factor(RT.calc.tp$source, levels = c("NS", "BR", "FR")) # relevel source location factors
RT.calc.tp$TP <- factor(RT.calc.tp$TP) # make Year a factor
levels(RT.calc.tp$TP) <- list("2013" = "y23", "2014" = "y34", "2015" = "y45")
RT.calc.tp$Treatment <- paste(RT.calc.tp$source, RT.calc.tp$trans, sep = " to ") # make a 'treatment' column
RT.calc.tp$Treatment <- paste(RT.calc.tp$Treatment, RT.calc.tp$TP, sep = " in ") # make a 'treatment' column
# condensed dataframe of modeled mean and 95% CI per treatment
RT.calc.TP.tab <- RT.calc.tp %>%
group_by(Treatment) %>%
summarize(`Modeled Mean` = mean(estimate, na.rm=TRUE),
`Lower 95% CI` = mean(lowerci, na.rm=TRUE),
`Upper 95% CI` = mean(upperci, na.rm=TRUE))
kable(RT.calc.TP.tab, digits = 2) %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%
column_spec(1, bold = TRUE) %>%
kable_classic(full_width = FALSE, html_font = "Times")
```
#### <span style="color: navy;">**Data Visualization**</span>
```{r TP calcification plot, message=FALSE, warning=FALSE, fig.align='center', fig.height=4, fig.width=9.5}
calc.tp.plot <- ggplot(data = RT.calc.tp) +
theme_bw() +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_blank(), legend.key=element_blank(), legend.position = "none", strip.background = element_blank(), panel.border = element_rect(colour = "black", fill=NA, size=1)) +
geom_point(aes(x = TP, y = rate, colour = trans), size = 1.8, position = position_jitterdodge(jitter.height = 0, dodge.width = 0.5, seed = 14)) +
geom_linerange(aes(x = TP, ymin = lowerci, ymax = upperci, colour = trans), position = position_dodge(width = 0.5), size = 1.2, alpha = 0.2) +
facet_wrap(~ source) +
scale_color_manual("Transplant Location", values=c("#D55E00", "#009E73", "#0072B2")) +
xlab("Time (years)") +
ylab(bquote("Net Calcification (mg cm" ^-2~day^-1*")"))
calc.tp.plot
```
**Figure S2A |** Calcification rate (mg cm^-2^ day^-1^) at yearly intervals over the entire transplant experimental period (2012 – 2015) by source location. Transplant location is represented by color: <span style="color: #D55E00;">red = NS (nearshore)</span>, <span style="color: #009E73;">green = BR (backreef)</span>, and <span style="color: #0072B2;">blue = FR (forereef)</span>. Colored bars represent modeled 95% confidence intervals with corresponding raw calcification rates per colony denoted by circles of the same color.
```{r subset analysis, eval=FALSE, include=FALSE}
### Exploring the calcification data when subset down to the lowest N across treatments (per reviewer comment)
# setting a seed to have the same values pulled each time (for reproducibility) -- I used 14 in the rest of the code so keeping that here too
set.seed(14)
## created subsampled df with 2 random samples per source/transplant pairing
RT_calc_sub.fin_subset <- RT.calc.fin %>%
group_by(source, trans) %>%
sample_n(., 2)
#### Now running the same analyses with the subset data
## AIC model selection
full.int_sub <- lm(rate ~ source * trans, data = RT_calc_sub.fin_subset) # interaction of source with transplant location
# data look normal so I am okay with proceeding with a linear model here
##check_normality(full.int) # OK: residuals appear as normally distributed (p = 0.653).
##check_model(full.int)
full.add <- lm(rate ~ source + trans, data = RT_calc_sub.fin_subset) # additive model of source with transplant location
tran.only <- lm(rate ~ trans, data = RT_calc_sub.fin_subset) # transplant location only
sourc.only <- lm(rate ~ source, data = RT_calc_sub.fin_subset) # source location only
compare_performance(full.int_sub, full.add, tran.only, sourc.only)
# Comparison of Model Performance Indices
# Name | Model | AIC (weights) | AICc (weights) | BIC (weights) | R2 | R2 (adj.) | RMSE | Sigma
# -------------------------------------------------------------------------------------------------------
# full.int | lm | -1.2 (0.179) | 30.2 (<.001) | 7.7 (0.016) | 0.465 | -0.011 | 0.134 | 0.190 ** best model performance **
# full.add | lm | 0.6 (0.069) | 8.3 (0.009) | 6.0 (0.036) | 0.073 | -0.212 | 0.177 | 0.208
# tran.only | lm | -3.2 (0.469) | -0.1 (0.618) | 0.4 (0.592) | 0.064 | -0.061 | 0.177 | 0.194
# sourc.only | lm | -2.2 (0.283) | 0.9 (0.372) | 1.4 (0.356) | 0.009 | -0.123 | 0.182 | 0.200
## Running model bootstrap
out <- simulate(full.int_sub, nsim = bootnum, seed = seed, re.form=NULL)
boots <- apply(out,2,function(x) predict(lm(x ~ source * trans, data = RT_calc_sub.fin_subset), reform = NA))
boots <- cbind(predict(full.int_sub, re.form=NA), boots)
RT_calc_sub.fin2 <- (cbind(RT_calc_sub.fin_subset, as.data.frame(t(apply(boots, 1, function(x) c(mean(x), quantile(x, c(0.025, 0.975))))))))
colnames(RT_calc_sub.fin2)[6:8] <- c("estimate", "lowerci", "upperci")
RT_calc_sub <- RT_calc_sub.fin2
## Update boot dataframe and make table of mean/CI
RT_calc_sub$trans <- factor(RT_calc_sub$trans, levels = c("NS", "BR", "FR")) # relevel transplant factors
RT_calc_sub$source <- factor(RT_calc_sub$source, levels = c("NS", "BR", "FR")) # relevel source factors
RT_calc_sub$Treatment <- paste(RT_calc_sub$source, RT_calc_sub$trans, sep = " to ") # make a 'treatment' column
# condensed dataframe of modeled mean and 95% CI per treatment
RT_calc_sub.tab <- RT_calc_sub %>%
group_by(Treatment) %>%
summarize(`Modeled Mean` = mean(estimate, na.rm=TRUE),
`Lower 95% CI` = mean(lowerci, na.rm=TRUE),
`Upper 95% CI` = mean(upperci, na.rm=TRUE))
kable(RT_calc_sub.tab, digits = 2) %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%
column_spec(1, bold = TRUE) %>%
kable_classic(full_width = FALSE, html_font = "Times")
## Plot figure of subset calc data
fig2b_sub <- ggplot(data = RT_calc_sub, aes(x = trans, y = rate, colour = source)) +
theme_bw() +
theme(panel.grid = element_blank(), legend.key=element_blank(), text = element_text(size = 13)) +
geom_point(size = 2, position = position_jitterdodge(jitter.height = 0, dodge.width = 0.8, seed = 14)) +
geom_linerange(aes(ymin = lowerci, ymax = upperci, colour = source), position = position_dodge(width = 0.7), size = 1.2, alpha = 0.2) +
scale_color_manual("Source \nLocation", values = c("#D55E00", "#009E73", "#0072B2")) +
xlab("Transplant Location") +
ylab(bquote("Net Calcification (mg cm" ^-2~day^-1*")")) +
ggtitle("Subset Fig 2B")
## Plot original with subset calcification data for comparison
fig2b_edit <- fig2b + ggtitle("Original Fig 2B")
ggarrange(fig2b_edit, fig2b_sub, ncol = 2, labels = "AUTO")
# these patterns look pretty similar so we feel pretty good about the analysis!
# pulling the coefficients
summary(full.int_sub)$coefficients
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.730030746 0.1486962 4.909545862 0.000836565
# sourceFR 0.235472372 0.2102882 1.119760466 0.291807695
# sourceNS 0.157549647 0.2102882 0.749208343 0.472858309
# transFR -0.042910032 0.2102882 -0.204053482 0.842851226
# transNS 0.146580305 0.2102882 0.697044965 0.503380276
# sourceFR:transFR -0.015524793 0.2973924 -0.052203062 0.959507213
# sourceNS:transFR -0.002063829 0.2973924 -0.006939751 0.994614317
# sourceFR:transNS -0.546340891 0.2973924 -1.837104583 0.099367828
# sourceNS:transNS -0.156978868 0.2973924 -0.527851024 0.610367760
```
<br/>
### Supplemental Tables
#### Supplemental Table 1
**Table S1.** Akaike information criterion (AIC) model selection for calcification linear models. Model selection for assessment of source and transplant location impacts on overall (2012 - 2015) and annual coral calcification rates performed using AIC. The model with the lowest AIC (in bold) best fit the data and was used for analyses.
```{r}
# combine both AIC tables
AIC_full <- rbind(calc.AIC, calc.tp.AIC)
kable(AIC_full) %>%
kable_styling(position = "center", font_size = 14, full_width = FALSE) %>%