-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_synchrony_anova.R
683 lines (596 loc) · 26.6 KB
/
03_synchrony_anova.R
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
## ------------------------------------------ ##
# Synchrony ANOVAs
## ------------------------------------------ ##
# Written by: Nick J Lyon
# PURPOSE
## Using permutation analysis of variance (perANOVA)
## analyze effects of trait levels (separately) on synchrony (i.e., correlation)
# PRE-REQUISITES
## The following script(s) must be run--in order--for this script to work as intended
## 1. Run `synchrony_stats_prep.R`
## ------------------------------------------ ##
# Housekeeping ----
## ------------------------------------------ ##
# Load libraries
# install.packages("librarian")
librarian::shelf(tidyverse, RRPP)
# Clear environment
rm(list = ls())
# Create needed local folder(s)
dir.create(path = file.path("stats_results"), showWarnings = F)
# Read in needed file
sync_df <- read.csv(file = file.path("tidy_data", "synchrony_data.csv")) %>%
# Make a species pair column quickly
dplyr::mutate(Species_Pair = paste(Species1, Species2, sep = "__"),
.before = Species1)
# Glimpse it
dplyr::glimpse(sync_df)
# Set permutation number
iter_num <- 10000
## ------------------------------------------ ##
# Trait Level ANOVAs - Fit ----
## ------------------------------------------ ##
# Make an empty list for anova tables and one for pairwise
aov_list <- list()
pairs_list <- list()
# For each site (plus "all" sites together)
for(site in c("All", unique(sync_df$lter))){
# for(site in "All"){
# Starting message
message("Processing begun for site: ", site)
# Subset to data to relevant site
if(site == "All"){ sub_df <- sync_df } else { sub_df <- dplyr::filter(sync_df, lter == site) }
# Pollinator_code_values
if(length(unique(sub_df$Pollinator_code_values)) != 1){
## Fit anova
poll_fit <- RRPP::lm.rrpp(r.spearman ~ Pollinator_code_values,
iter = iter_num, data = sub_df)
## Strip AOV table
poll_aov <- as.data.frame(anova(poll_fit)$table) %>%
dplyr::mutate(lter = site,
model = "pollinator values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
poll_pairs <- summary(RRPP::pairwise(fit = poll_fit, groups = sub_df$Pollinator_code_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "pollinator values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_poll")]] <- poll_aov
pairs_list[[paste0(site, "_poll")]] <- poll_pairs }
# Seed_development_values
if(length(unique(sub_df$Seed_development_values)) != 1){
## Fit anova
seed_dev_fit <- RRPP::lm.rrpp(r.spearman ~ Seed_development_values,
iter = iter_num, data = sub_df)
## Strip AOV table
seed_dev_aov <- as.data.frame(anova(seed_dev_fit)$table) %>%
dplyr::mutate(lter = site,
model = "seed development values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
seed_dev_pairs <- summary(RRPP::pairwise(fit = seed_dev_fit,
groups = sub_df$Seed_development_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "seed development values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_seed_dev")]] <- seed_dev_aov
pairs_list[[paste0(site, "_seed_dev")]] <- seed_dev_pairs }
# Mycorrhiza_values
if(length(unique(sub_df$Mycorrhiza_values)) != 1){
## Fit anova
myco_fit <- RRPP::lm.rrpp(r.spearman ~ Mycorrhiza_values,
iter = iter_num, data = sub_df)
## Strip AOV table
myco_aov <- as.data.frame(anova(myco_fit)$table) %>%
dplyr::mutate(lter = site,
model = "mycorrhiza values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
myco_pairs <- summary(RRPP::pairwise(fit = myco_fit, groups = sub_df$Mycorrhiza_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "mycorrhiza values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_myco")]] <- myco_aov
pairs_list[[paste0(site, "_myco")]] <- myco_pairs }
# Deciduous_Evergreen_values
if(length(unique(sub_df$Deciduous_Evergreen_values)) != 1){
## Fit anova
decid_fit <- RRPP::lm.rrpp(r.spearman ~ Deciduous_Evergreen_values,
iter = iter_num, data = sub_df)
## Strip AOV table
decid_aov <- as.data.frame(anova(decid_fit)$table) %>%
dplyr::mutate(lter = site,
model = "deciduous evergreen values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
decid_pairs <- summary(RRPP::pairwise(fit = decid_fit,
groups = sub_df$Deciduous_Evergreen_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "deciduous evergreen values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_decid")]] <- decid_aov
pairs_list[[paste0(site, "_decid")]] <- decid_pairs }
# Dispersal_syndrome_values
if(length(unique(sub_df$Dispersal_syndrome_values)) != 1){
## Fit anova
disp_fit <- RRPP::lm.rrpp(r.spearman ~ Dispersal_syndrome_values,
iter = iter_num, data = sub_df)
## Strip AOV table
disp_aov <- as.data.frame(anova(disp_fit)$table) %>%
dplyr::mutate(lter = site,
model = "dispersal syndrome values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
disp_pairs <- summary(RRPP::pairwise(fit = disp_fit,
groups = sub_df$Dispersal_syndrome_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "dispersal syndrome values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_disp")]] <- disp_aov
pairs_list[[paste0(site, "_disp")]] <- disp_pairs }
# Sexual_system_values
if(length(unique(sub_df$Sexual_system_values)) != 1){
## Fit anova
sex_fit <- RRPP::lm.rrpp(r.spearman ~ Sexual_system_values,
iter = iter_num, data = sub_df)
## Strip AOV table
sex_aov <- as.data.frame(anova(sex_fit)$table) %>%
dplyr::mutate(lter = site,
model = "sexual system values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
sex_pairs <- summary(RRPP::pairwise(fit = sex_fit,
groups = sub_df$Sexual_system_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "sexual system values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_sex")]] <- sex_aov
pairs_list[[paste0(site, "_sex")]] <- sex_pairs }
# Shade_tolerance_values
if(length(unique(sub_df$Shade_tolerance_values)) != 1){
## Fit anova
shade_fit <- RRPP::lm.rrpp(r.spearman ~ Shade_tolerance_values,
iter = iter_num, data = sub_df)
## Strip AOV table
shade_aov <- as.data.frame(anova(shade_fit)$table) %>%
dplyr::mutate(lter = site,
model = "shade tolerance values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
shade_pairs <- summary(RRPP::pairwise(fit = shade_fit, groups = sub_df$Shade_tolerance_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "shade tolerance values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_shade")]] <- shade_aov
pairs_list[[paste0(site, "_shade")]] <- shade_pairs }
# Growth_form_values
if(length(unique(sub_df$Growth_form_values)) != 1){
## Fit anova
growth_fit <- RRPP::lm.rrpp(r.spearman ~ Growth_form_values,
iter = iter_num, data = sub_df)
## Strip AOV table
growth_aov <- as.data.frame(anova(growth_fit)$table) %>%
dplyr::mutate(lter = site,
model = "growth form values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
growth_pairs <- summary(RRPP::pairwise(fit = growth_fit, groups = sub_df$Growth_form_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "growth form values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_growth")]] <- growth_aov
pairs_list[[paste0(site, "_growth")]] <- growth_pairs }
# Fleshy_fruit_values
if(length(unique(sub_df$Fleshy_fruit_values)) != 1){
## Fit anova
flesh_fit <- RRPP::lm.rrpp(r.spearman ~ Fleshy_fruit_values,
iter = iter_num, data = sub_df)
## Strip AOV table
flesh_aov <- as.data.frame(anova(flesh_fit)$table) %>%
dplyr::mutate(lter = site,
model = "fleshy fruit values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
flesh_pairs <- summary(RRPP::pairwise(fit = flesh_fit, groups = sub_df$Fleshy_fruit_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "fleshy fruit values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_flesh")]] <- flesh_aov
pairs_list[[paste0(site, "_flesh")]] <- flesh_pairs }
# Seed_bank_values
if(length(unique(sub_df$Seed_bank_values)) != 1){
## Fit anova
seed_bank_fit <- RRPP::lm.rrpp(r.spearman ~ Seed_bank_values,
iter = iter_num, data = sub_df)
## Strip AOV table
seed_bank_aov <- as.data.frame(anova(seed_bank_fit)$table) %>%
dplyr::mutate(lter = site,
model = "seed bank values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
seed_bank_pairs <- summary(RRPP::pairwise(fit = seed_bank_fit, groups = sub_df$Seed_bank_values))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "seed bank values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_seed_bank")]] <- seed_bank_aov
pairs_list[[paste0(site, "_seed_bank")]] <- seed_bank_pairs }
# Seed_mass_similarity
## Fit anova
seed_mass_fit <- RRPP::lm.rrpp(r.spearman ~ Seed_mass_similarity,
iter = iter_num, data = sub_df)
## Strip AOV table
seed_mass_aov <- as.data.frame(anova(seed_mass_fit)$table) %>%
dplyr::mutate(lter = site,
model = "seed mass similarity",
term = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_seed_mass")]] <- seed_mass_aov
# Phylogenetic_similarity
## Fit anova
phylo_fit <- RRPP::lm.rrpp(r.spearman ~ Phylogenetic_similarity,
iter = iter_num, data = sub_df)
## Strip AOV table
phylo_aov <- as.data.frame(anova(phylo_fit)$table) %>%
dplyr::mutate(lter = site,
model = "phylogenetic similarity",
term = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
aov_list[[paste0(site, "_phylo")]] <- phylo_aov
# Ending message
message("Finished with site: ", site) }
## ------------------------------------------ ##
# Trait Level ANOVAs - Export ----
## ------------------------------------------ ##
# Unlist and examine the ANOVA tables
aov_df <- aov_list %>%
purrr::list_rbind() %>%
# Rename problematic columns
dplyr::rename(P = `Pr(>F)`) %>%
# Arrange by sites and model term
dplyr::arrange(lter, model)
# Glimpse
dplyr::glimpse(aov_df)
# Do the same for pairwise comparisons
pairs_df <- pairs_list %>%
purrr::list_rbind() %>%
# Fix some column names
dplyr::rename(UCL_95perc = `UCL (95%)`,
P = `Pr > d`) %>%
# Arrange by sites and model term and pair
dplyr::arrange(lter, model, pairs)
# Examine
dplyr::glimpse(pairs_df)
# Assemble file names for both
(aov_name <- paste0("ANOVA_trait_aov_tables_", Sys.Date(), "_", iter_num, "perm.csv"))
(pairs_name <- paste0("ANOVA_trait_pairwise_comps_", Sys.Date(), "_", iter_num, "perm.csv"))
# Export both locally
write.csv(x = aov_df, file = file.path("stats_results", aov_name), row.names = F, na = '')
write.csv(x = pairs_df, file = file.path("stats_results", pairs_name), row.names = F, na = '')
# Clean up environment
rm(list = setdiff(ls(), c("sync_df", "iter_num")))
## ------------------------------------------ ##
# Trait Status ANOVAs - Fit ----
## ------------------------------------------ ##
# Make an empty list for anova tables for trait status
stat_list <- list()
stat_pair_list <- list()
# For each site (plus "all" sites together)
for(site in c("All", unique(sync_df$lter))){
# for(site in "All"){
# Starting message
message("Processing begun for site: ", site)
# Subset to data to relevant site
if(site == "All"){ sub_df <- sync_df } else { sub_df <- dplyr::filter(sync_df, lter == site) }
# Pollinator_code_shared
if(length(unique(sub_df$Pollinator_code_shared)) != 1){
## Fit anova
poll_fit <- RRPP::lm.rrpp(r.spearman ~ Pollinator_code_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
poll_aov <- as.data.frame(anova(poll_fit)$table) %>%
dplyr::mutate(lter = site,
model = "pollinator values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
poll_pairs <- summary(RRPP::pairwise(fit = poll_fit, groups = sub_df$Pollinator_code_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "pollinator values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_poll")]] <- poll_aov
stat_pair_list[[paste0(site, "_poll")]] <- poll_pairs }
# Seed_development_shared
if(length(unique(sub_df$Seed_development_shared)) != 1){
## Fit anova
seed_dev_fit <- RRPP::lm.rrpp(r.spearman ~ Seed_development_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
seed_dev_aov <- as.data.frame(anova(seed_dev_fit)$table) %>%
dplyr::mutate(lter = site,
model = "seed development values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
seed_dev_pairs <- summary(RRPP::pairwise(fit = seed_dev_fit,
groups = sub_df$Seed_development_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "seed development values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_seed_dev")]] <- seed_dev_aov
stat_pair_list[[paste0(site, "_seed_dev")]] <- seed_dev_pairs }
# Mycorrhiza_shared
if(length(unique(sub_df$Mycorrhiza_shared)) != 1){
## Fit anova
myco_fit <- RRPP::lm.rrpp(r.spearman ~ Mycorrhiza_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
myco_aov <- as.data.frame(anova(myco_fit)$table) %>%
dplyr::mutate(lter = site,
model = "mycorrhiza values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
myco_pairs <- summary(RRPP::pairwise(fit = myco_fit, groups = sub_df$Mycorrhiza_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "mycorrhiza values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_myco")]] <- myco_aov
stat_pair_list[[paste0(site, "_myco")]] <- myco_pairs }
# Deciduous_Evergreen_shared
if(length(unique(sub_df$Deciduous_Evergreen_shared)) != 1){
## Fit anova
decid_fit <- RRPP::lm.rrpp(r.spearman ~ Deciduous_Evergreen_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
decid_aov <- as.data.frame(anova(decid_fit)$table) %>%
dplyr::mutate(lter = site,
model = "deciduous evergreen values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
decid_pairs <- summary(RRPP::pairwise(fit = decid_fit,
groups = sub_df$Deciduous_Evergreen_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "deciduous evergreen values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_decid")]] <- decid_aov
stat_pair_list[[paste0(site, "_decid")]] <- decid_pairs }
# Dispersal_syndrome_shared
if(length(unique(sub_df$Dispersal_syndrome_shared)) != 1){
## Fit anova
disp_fit <- RRPP::lm.rrpp(r.spearman ~ Dispersal_syndrome_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
disp_aov <- as.data.frame(anova(disp_fit)$table) %>%
dplyr::mutate(lter = site,
model = "dispersal syndrome values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
disp_pairs <- summary(RRPP::pairwise(fit = disp_fit,
groups = sub_df$Dispersal_syndrome_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "dispersal syndrome values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_disp")]] <- disp_aov
stat_pair_list[[paste0(site, "_disp")]] <- disp_pairs }
# Sexual_system_shared
if(length(unique(sub_df$Sexual_system_shared)) != 1){
## Fit anova
sex_fit <- RRPP::lm.rrpp(r.spearman ~ Sexual_system_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
sex_aov <- as.data.frame(anova(sex_fit)$table) %>%
dplyr::mutate(lter = site,
model = "sexual system values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
sex_pairs <- summary(RRPP::pairwise(fit = sex_fit,
groups = sub_df$Sexual_system_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "sexual system values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_sex")]] <- sex_aov
stat_pair_list[[paste0(site, "_sex")]] <- sex_pairs }
# Shade_tolerance_shared
if(length(unique(sub_df$Shade_tolerance_shared)) != 1){
## Fit anova
shade_fit <- RRPP::lm.rrpp(r.spearman ~ Shade_tolerance_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
shade_aov <- as.data.frame(anova(shade_fit)$table) %>%
dplyr::mutate(lter = site,
model = "shade tolerance values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
shade_pairs <- summary(RRPP::pairwise(fit = shade_fit, groups = sub_df$Shade_tolerance_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "shade tolerance values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_shade")]] <- shade_aov
stat_pair_list[[paste0(site, "_shade")]] <- shade_pairs }
# Growth_form_shared
if(length(unique(sub_df$Growth_form_shared)) != 1){
## Fit anova
growth_fit <- RRPP::lm.rrpp(r.spearman ~ Growth_form_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
growth_aov <- as.data.frame(anova(growth_fit)$table) %>%
dplyr::mutate(lter = site,
model = "growth form values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
growth_pairs <- summary(RRPP::pairwise(fit = growth_fit, groups = sub_df$Growth_form_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "growth form values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_growth")]] <- growth_aov
stat_pair_list[[paste0(site, "_growth")]] <- growth_pairs }
# Fleshy_fruit_shared
if(length(unique(sub_df$Fleshy_fruit_shared)) != 1){
## Fit anova
flesh_fit <- RRPP::lm.rrpp(r.spearman ~ Fleshy_fruit_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
flesh_aov <- as.data.frame(anova(flesh_fit)$table) %>%
dplyr::mutate(lter = site,
model = "fleshy fruit values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
flesh_pairs <- summary(RRPP::pairwise(fit = flesh_fit, groups = sub_df$Fleshy_fruit_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "fleshy fruit values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_flesh")]] <- flesh_aov
stat_pair_list[[paste0(site, "_flesh")]] <- flesh_pairs }
# Seed_bank_shared
if(length(unique(sub_df$Seed_bank_shared)) != 1){
## Fit anova
seed_bank_fit <- RRPP::lm.rrpp(r.spearman ~ Seed_bank_shared,
iter = iter_num, data = sub_df)
## Strip AOV table
seed_bank_aov <- as.data.frame(anova(seed_bank_fit)$table) %>%
dplyr::mutate(lter = site,
model = "seed bank values",
term = row.names(.),
.before = dplyr::everything())
## Strip pairwise comparisons out as well
seed_bank_pairs <- summary(RRPP::pairwise(fit = seed_bank_fit, groups = sub_df$Seed_bank_shared))$summary.table %>%
as.data.frame() %>%
dplyr::mutate(lter = site,
model = "seed bank values",
pairs = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_seed_bank")]] <- seed_bank_aov
stat_pair_list[[paste0(site, "_seed_bank")]] <- seed_bank_pairs }
# Seed_mass_similarity
## Fit anova
seed_mass_fit <- RRPP::lm.rrpp(r.spearman ~ Seed_mass_similarity,
iter = iter_num, data = sub_df)
## Strip AOV table
seed_mass_aov <- as.data.frame(anova(seed_mass_fit)$table) %>%
dplyr::mutate(lter = site,
model = "seed mass similarity",
term = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_seed_mass")]] <- seed_mass_aov
# Phylogenetic_similarity
## Fit anova
phylo_fit <- RRPP::lm.rrpp(r.spearman ~ Phylogenetic_similarity,
iter = iter_num, data = sub_df)
## Strip AOV table
phylo_aov <- as.data.frame(anova(phylo_fit)$table) %>%
dplyr::mutate(lter = site,
model = "phylogenetic similarity",
term = row.names(.),
.before = dplyr::everything())
## Add to their respective lists
stat_list[[paste0(site, "_phylo")]] <- phylo_aov
# Ending message
message("Finished with site: ", site) }
## ------------------------------------------ ##
# Trait Status ANOVAs - Export ----
## ------------------------------------------ ##
# Unlist and examine the ANOVA tables
stat_df <- stat_list %>%
purrr::list_rbind() %>%
# Rename problematic columns
dplyr::rename(P = `Pr(>F)`) %>%
# Arrange by sites and model term
dplyr::arrange(lter, model)
# Glimpse
dplyr::glimpse(stat_df)
# Do the same for pairwise comparisons
stat_pair_df <- stat_pair_list %>%
purrr::list_rbind() %>%
# Fix some column names
dplyr::rename(UCL_95perc = `UCL (95%)`,
P = `Pr > d`) %>%
# Arrange by sites and model term and pair
dplyr::arrange(lter, model, pairs)
# Examine
dplyr::glimpse(stat_pair_df)
# Assemble file names for both
(stat_name <- paste0("ANOVA_trait_status_aov_tables_", Sys.Date(), "_", iter_num, "perm.csv"))
(stat_pair_name <- paste0("ANOVA_trait_status_pairwise_comps_", Sys.Date(), "_", iter_num, "perm.csv"))
# Export both locally
write.csv(x = stat_df, file = file.path("stats_results", stat_name), row.names = F, na = '')
write.csv(x = stat_pair_df, file = file.path("stats_results", stat_pair_name), row.names = F, na = '')
# Clean up environment
rm(list = setdiff(ls(), c("sync_df", "iter_num")))
# End ----