-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIA_range_shift_analysis_subset2022.Rmd
6605 lines (5486 loc) · 278 KB
/
FIA_range_shift_analysis_subset2022.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: "FIA Analysis Take 3"
author: "Katie Nigro"
date: "`r Sys.Date()`"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Set-up
These are the packages I need:
```{r,message=FALSE,warning=FALSE}
library(readr)
library(tidyr)
library(dplyr)
library(plyr)
library(purrr)
library(DT)
library(ggplot2)
library(ggfortify)
library(ggpubr)
library(leaflet)
library(mapdata)
library(car)
library(ggmap)
library(fitdistrplus)
library(logspline)
library(SIBER)
library(emmeans)
library(MASS)
#library(rjags)
library(MuMIn)
library(stringr)
```
Let's first read in the data.
```{r,warning=FALSE,message=FALSE}
##read in FIA tree & regen data
tree<- read_csv("TREE.csv")
seedling<- read_csv("SEEDLING.csv")
plot<- read_csv("annual_plots2020.csv")
```
```{r}
#subset plots to those with NF_SAMPLING_STATUS_CD = 1 that were inventoried
cond<- read_csv("compiled_data_annual2020/COND.csv")
##read in environmental vars
env_vars_OG <- read_csv("env_vars_FIA_2022.csv") %>%
dplyr::select(-1)
plot_subset <- plot %>%
filter(CN %in% env_vars_OG$PLT_CN) %>%
left_join(cond, by=c("CN"="PLT_CN")) %>%
filter(!(COND_STATUS_CD == 2 & NF_SAMPLING_STATUS_CD ==0)) %>%
filter(!(COND_STATUS_CD == 2 & NF_PLOT_STATUS_CD == 3))
nrow(plot_subset)
length(unique(plot_subset$CN))
plot_subset %>%
group_by(CN) %>%
dplyr::summarise(n_conds = n_distinct(COND_STATUS_CD)) %>%
group_by(n_conds) %>%
dplyr::summarise(n_plots=n()) #all plots only have one condition status (Either forest or nonforest)
```
```{r}
#filter env_vars to just plots in new subset
env_vars <- env_vars_OG %>%
filter(PLT_CN %in% plot_subset$CN)
```
Now I will summarize the number of adults and seedlings of each species in each plot.
```{r,echo=FALSE,warning=FALSE,message=FALSE, results='hide'}
adults <- tree %>%
group_by(SPCD,PLT_CN) %>%
dplyr::summarise(n = n())
head(adults)
seeds <- seedling %>%
group_by(SPCD, PLT_CN) %>%
dplyr::summarise(n = n())
head(seeds)
```
```{r,warning=FALSE, message=FALSE, echo=FALSE}
##read in plots by disturbance type
fire<- read_csv("fire.plots2022.csv")
ID<- read_csv("insect.disease.plots2022.csv")
harvest<- read_csv("harvest.plots2022.csv")
other<- read_csv("other.plots2022.csv")
wind<- read_csv("wind.plots2022.csv")
undis<- read_csv("undisturbed.plots2022.csv")
dist <- bind_rows(fire,ID,harvest,other,wind,undis)%>%
filter(PLT_CN %in% plot_subset$CN)
```
```{r,echo=FALSE,eval=FALSE}
survey_data<- dist %>%
left_join(plot, by=c("PLT_CN" = "CN"))
survey_data %>%
group_by(DESIGNCD) %>%
dplyr::summarise(n=n()) #all plots are design code = 1 = standard plot design
##disturbance types sample size
dist %>%
group_by(Agent) %>%
dplyr::summarise(n=n())
```
Join disturbance data with adult and seedling dataframes. There are NA's in the data because the adults and seeds dataframes have all plots, where as the disturbance dataframe only has plots that met my criteria. So this is ok.
```{r,echo=FALSE}
dist_adults_joined <- left_join(adults, dist, by="PLT_CN")
head(dist_adults_joined)
summary(is.na(dist_adults_joined)) #there are NAs for the plots that are not in my analysis, this is ok
##this table shows the number of seedling plots surveyed each year. We will just use those surveyed from 2009-2018
left_join(seeds, dist, by="PLT_CN") %>% group_by(MEASYEAR) %>% tally() %>% datatable()
dist_seeds_joined <- left_join(seeds, dist, by="PLT_CN") %>%
filter(MEASYEAR > 2008)
head(dist_seeds_joined)
summary(is.na(dist_seeds_joined)) #there are NAs for the plots that are not in my analysis, this is ok
```
##Selecting species for analysis
I will use both the most recent surveys and the previous survey (if applicable) to tally adult presence at a plot. If the adult of a species was present in at least one of the surveys (either alive or dead), that plot will be counted as having adults of that species present.
```{r, echo=FALSE, results='hide'}
### count plots as containing adults if either the most current survey OR the previous survey documented the species ###
#make data frame linking most recent plot CN to previous plot CN
recent.plots <- dist %>%
dplyr::select("PLT_CN")
plot_progression <- left_join(recent.plots, plot, by=c("PLT_CN"="CN")) %>%
dplyr::select(c("PLT_CN","PREV_PLT_CN"))
#make datatable with species recorded in most recent survey (SPCD) and in the previous survey (SPCD.prev)
prev_plot_spp <- plot_progression %>%
filter(!is.na(PREV_PLT_CN)) %>%
dplyr::select("PREV_PLT_CN") %>%
left_join(adults, by=c("PREV_PLT_CN" = "PLT_CN")) %>%
left_join(plot_progression, by="PREV_PLT_CN") %>%
dplyr::rename(SPCD.prev = SPCD, n.prev = n) %>%
right_join(dist_adults_joined, by="PLT_CN") %>%
filter(!is.na(Agent))
length(unique(prev_plot_spp$PREV_PLT_CN))
length(unique(prev_plot_spp$PLT_CN))
#looking at differences in species codes between recent and previous surveys
species.sum <- prev_plot_spp %>%
group_by(PLT_CN, Agent) %>%
dplyr::summarise(spp = toString(unique(SPCD)),spp.prev = toString(unique(SPCD.prev)))
#look at just plots where species differ between recent and previous surveys
species.sum %>%
filter(!spp == spp.prev) %>%
filter(!spp.prev=="NA")
## melt dataframe so that all species are listed under "SPCD", whether identified in most recent survey or previous survey. Clean up the dataframe by removing unneccesary columns. Then remove duplicated rows that were created when recent survey was fully joined with previous survey
species.sum.long<- prev_plot_spp %>%
pivot_longer(c(SPCD.prev, SPCD), names_to="period", values_to="SPCD") %>%
dplyr::select(-one_of(c("n.prev","n","period"))) %>%
distinct()
```
The histogram below displays the year that plots in the analysis were measured. Some plots were measured twice, and we used both surveys to detect adult presence. These plots (~40%) have both the recent survey year (all between 2010-2018) and the previous survey year (all between 2000-2013) displayed in the histogram. Some plots (~60%) were only surveyed once, and their year of measurement is shown in pink (surveyed between 1995-2018). The second histogram displays the difference in years between the first and second survey for all plots that were surveyed twice.
```{r, echo=FALSE}
##look at when recent vs. previous surveys were done
timeline <- left_join(recent.plots, plot, by=c("PLT_CN"="CN")) %>%
dplyr::select(c("PLT_CN","PREV_PLT_CN","MEASYEAR")) %>%
left_join(plot, by=c("PREV_PLT_CN"="CN")) %>%
dplyr::select(c("PLT_CN","PREV_PLT_CN","MEASYEAR.x","MEASYEAR.y")) %>%
dplyr::rename(MEASYEAR.rec = MEASYEAR.x, MEASYEAR.prev = MEASYEAR.y) %>%
dplyr::mutate(year.diff = MEASYEAR.rec - MEASYEAR.prev)
par(mfrow=c(1,1))
#####histogram of 3 plot time periods
hist(timeline[is.na(timeline$PREV_PLT_CN),]$MEASYEAR.rec,col=rgb(1,0,0,0.5),breaks=24, main="Histogram of plot measurement year",xlab="Measurement Year") #plots that didn't have a previous survey were surveyed from 1995 - 2018
hist(timeline[!is.na(timeline$PREV_PLT_CN),]$MEASYEAR.rec,col=rgb(0,0,1,0.3),breaks=9,add=TRUE) #all plots that had a previous survey had their most recent survey between 2010 - 2018
hist(timeline$MEASYEAR.prev, col=rgb(1,1,0,0.2),breaks=14, add=TRUE) #previous surveys were all measured between 2000 - 2013
legend("topright",legend=c("MEASYEAR of plots with only 1 survey","recent MEASYEAR of plots with 2 surveys", "previous MEASYEAR of plots with 2 surveys"),fill=c(rgb(1,0,0,0.5),rgb(0,0,1,0.3),rgb(1,1,0,0.2)))
######
##number of plots in each category
no.prev.surv.plots <- length(timeline[is.na(timeline$PREV_PLT_CN),]$MEASYEAR.rec)
no.prev.surv.plots#plots that didn't have a previous survey
prev.surv.plots<- length(timeline[!is.na(timeline$PREV_PLT_CN),]$MEASYEAR.rec)
prev.surv.plots#plots that had a previous survey
no.prev.surv.plots/(prev.surv.plots+no.prev.surv.plots) #57% of plots in analysis only had one survey
##difference in years between first and second survey
par(mfrow=c(1,1))
hist(timeline$year.diff, main="Histogram of number of years between surveys") #most plots have 10 years between surveys
```
Since some the plots have a most recent survey as early as 1995- 2006, we want to exclude these plots from our seedling sample, as seedlings present in 1995 are likely to be adult trees now and do not really capture the newer cohort of trees that we would expect to have been influenced by recent climate warming. Therefore, we will only look at plots most recently surveyed in or after 2007, which will include the later chunk of plots with only one survey and all the most recent visits of plots with two surveys. Below are tables of sample sizes for each species and disturbance type. The first shows adult sample sizes and the second shows seedling sample sizes.
```{r, echo=FALSE,warning=FALSE,message=FALSE}
#calculate sample size for adults and seedlings
adult_sample_cmbsurv <- species.sum.long %>%
filter(!is.na(SPCD)) %>%
group_by(SPCD, Agent) %>%
dplyr::summarise(n_plots = n_distinct(PLT_CN))
datatable(adult_sample_cmbsurv)
sample_seeds <- dist_seeds_joined %>%
filter(!is.na(Agent)) %>%
filter(MEASYEAR > 2008) %>% #only look at seedling data from plots surveyed in the later chunk of surveys
group_by(SPCD, Agent) %>%
dplyr::summarise(n_plots = n_distinct(PLT_CN))
datatable(sample_seeds, caption = "Seedling Sample Size")
```
Let's set the minimum number of plots at 60 Since there are always less seedling plots than adult plots, we will look at the seedlings first. We will also only look at fire, insect/disease, and undisturbed categories since the other disturbance categories have very low sample sizes.
```{r, echo=FALSE}
#seedling sample
sample_seeds_over60 <- sample_seeds %>%
filter(n_plots >= 60) %>%
filter(!Agent %in% c("harvest","other",'wind')) %>%
group_by(SPCD) %>%
dplyr::summarise(dist_over60 = n_distinct(Agent), disturbance = list(Agent)) %>%
filter(dist_over60 > 1)
#make a dataframe to translate species codes to names
species.names <- data.frame(species.code = c(15,17,19,66,73,93,101,106,108,113,122,133,202,242,746,814), species.name = c("white fir","grand fir","subalpine fir","Rocky Mountain juniper","western larch","Engelmann spruce","whitebark pine","two needle pinyon","lodgepole pine","limber pine","ponderosa pine","singleleaf pinyon","Douglas-fir","western redcedar","trembling aspen","Gambel oak"))
species.names<- species.names %>%
mutate(species.code = as.numeric(species.code))
sample_seeds_over60 %>%
left_join(species.names, by=c("SPCD"="species.code")) %>%
datatable() #picked up western larch and rocky mountain juniper in 2/3 disturbances (insect.disease and none for juniper and fire and none for western larch.)
```
There are 16 species in the FIA database that have 60 or more plots in at least 2 of the 3 disturbance types with seedlings present.
This is a sample size table for each species, age and disturbance
```{r,echo=FALSE}
sample_size <- adult_sample_cmbsurv %>%
filter(SPCD %in% species.names$species.code) %>%
filter(!Agent %in% c('harvest','other','wind')) %>%
dplyr::rename(n.adult = n_plots) %>%
left_join(sample_seeds) %>%
dplyr::rename(n.seed = n_plots) %>%
left_join(species.names, by=c("SPCD"="species.code"))
sample_size_wide<- sample_size %>%
filter(Agent == "fire") %>%
dplyr::rename(n.adult.fire = n.adult, n.seed.fire = n.seed) %>%
dplyr::select(-Agent) %>%
left_join(sample_size %>%
filter(Agent == "insect.disease") %>%
dplyr::rename(n.adult.ID = n.adult, n.seed.ID = n.seed) %>%
dplyr::select(-Agent),
by="species.name") %>%
left_join(sample_size %>%
filter(Agent == "none") %>%
dplyr::rename(n.adult.none = n.adult, n.seed.none = n.seed) %>%
dplyr::select(-Agent),
by="species.name") %>%
dplyr::select(species.name, n.seed.fire, n.adult.fire, n.seed.ID, n.adult.ID, n.seed.none, n.adult.none)
#write.csv(sample_size_wide, "sample_size_wide.csv")
insectonlyspp <- sample_size %>%
filter(n.seed < 60 & Agent == "fire") %>%
pull(species.name)
fireonlyspp <- sample_size %>%
filter(n.seed < 60 & Agent == "insect.disease") %>%
pull(species.name)
```
And the below plots show sample size for each species within each disturbance type graphically.
```{r}
adult.sample <- ggplot(sample_size,aes(x=Agent, y=n.adult, fill=factor(species.name)))+
geom_bar(stat='identity',position='dodge2')+
ggtitle("Number of plots with Adults")+
xlab("")+ylab("# plots")+
ylim(c(0,4000))+
scale_fill_discrete(name = "Species")
seedling.sample <- ggplot(sample_size,aes(x=Agent, y=n.seed, fill=factor(species.name)))+
geom_bar(stat='identity',position='dodge2')+
ggtitle("Number of plots with Seedlings")+
xlab("")+ylab("# plots")+
ylim(c(0,4000))+
scale_fill_discrete(name = "Species")
ggarrange(adult.sample,seedling.sample, common.legend = TRUE)
```
Looking at the seedling and adult sample sizes, we will exclude white fir, grand fir, limber pine, two needle pinyon, and western redcedar in fire plots from the analysis, because these groups have very low numbers of plots with seedlings. We also exclude singleleaf pinyon from the analysis because the plots in this analysis do not seem to capture the full range of this species (it's niche is weirdly cut off in climate space).
```{r}
#look at sample size of seedling only plots
seedlingonly.sample = data.frame()
for(i in 1:nrow(species.names)){
n.seedonly <- dist_seeds_joined %>%
filter(SPCD == species.names$species.code[i]) %>%
filter(! PLT_CN %in% c(species.sum.long %>% filter(SPCD == species.names$species.code[i]) %>% pull(PLT_CN))) %>%
dplyr::group_by(Agent,SPCD) %>%
dplyr::summarise(n.seedonly=n())
seedlingonly.sample = rbind(seedlingonly.sample, n.seedonly)
}
View(sample_size %>%
left_join(seedlingonly.sample))
```
##Measurement years distribution
The histograms below show the distribution of recent (blue) and previous (green) measurement years for the plots where adults and seedlings of each species were found.
```{r, echo=FALSE}
# spp_hist_data <- species.sum.long %>%
# left_join(plot, by=c("PREV_PLT_CN" = "CN")) %>%
# dplyr::select(c(PREV_PLT_CN:SPCD,MEASYEAR.y)) %>%
# dplyr::rename("Prev.MEASYEAR" = "MEASYEAR.y","MEASYEAR"="MEASYEAR.x")
# par(mfrow=c(5,3))
# for(i in 1:length(species.names$species.code)){
# hist(spp_hist_data[spp_hist_data$SPCD==species.names$species.code[i],]$MEASYEAR, col=rgb(0,0,1,0.5), breaks=(max(spp_hist_data[which(spp_hist_data$SPCD==species.names$species.code[i] & !is.na(spp_hist_data$MEASYEAR)),]$MEASYEAR) - min(spp_hist_data[which(spp_hist_data$SPCD==species.names$species.code[i] & !is.na(spp_hist_data$MEASYEAR)),]$MEASYEAR))+1, main=paste("Histogram for",species.names$species.name[i],"adults",sep=" "),xlab="Measurement Year")
# hist(spp_hist_data[spp_hist_data$SPCD==species.names$species.code[i],]$Prev.MEASYEAR, col=rgb(0,1,0,0.3), breaks=(max(spp_hist_data[which(spp_hist_data$SPCD==species.names$species.code[i] & !is.na(spp_hist_data$Prev.MEASYEAR)),]$Prev.MEASYEAR) - min(spp_hist_data[which(spp_hist_data$SPCD==species.names$species.code[i] & !is.na(spp_hist_data$Prev.MEASYEAR)),]$Prev.MEASYEAR))+1, add=TRUE)
# }
```
```{r, echo=FALSE}
#seedling histograms
# par(mfrow=c(5,3))
# for(i in 1:length(species.names$species.code)){
# hist(dist_seeds_joined[dist_seeds_joined$SPCD == species.names$species.code[i],]$MEASYEAR, col=rgb(0,0,1,0.5), breaks=(max(dist_seeds_joined[which(dist_seeds_joined$SPCD==species.names$species.code[i] & !is.na(dist_seeds_joined$MEASYEAR)),]$MEASYEAR) - min(dist_seeds_joined[which(dist_seeds_joined$SPCD==species.names$species.code[i] & !is.na(dist_seeds_joined$MEASYEAR)),]$MEASYEAR))+1, main=paste("Histogram for",species.names$species.name[i],"seedlings",sep=" "),xlab="Measurement Year")
# }
```
```{r, echo=FALSE}
##create function that appends climate data to each species/lifestage/disturbance
#' merge_sp_dist_env
#'
#' @param joined_df dataframe with disturbance and species data joined. Either species.sum.long or dist_seeds_joined
#' @param sp_code code for desired species. Can be found in FIA user guide.
#' @param agent_name name of the desired disturbance. Either fire, insect.disease, harvest, other or none.
#' @param env_vars environmental variables dataframe. Called env_vars here.
#'
#' @return dataframe with environmental variables associated with each plot for a desired species/lifestage/disturbance combination
#' @export
#'
#' @examples
merge_sp_dist_env <- function(joined_df, sp_code, agent_name, env_vars) { sp_dist_env <- joined_df %>%
filter(SPCD == sp_code &
Agent == agent_name) %>%
dplyr::select(SPCD, PLT_CN, Agent, dist_year, source) %>%
left_join(env_vars, by = "PLT_CN")
return(sp_dist_env)
}
#FIA codes for all desired species (n=14) and disturbances (n=3)
sp_codes <- as.character(unique(sample_seeds_over60$SPCD))
dist_names <- c("fire","insect.disease","none")
#put environmental data for each species/disturbance combination in a dataframe within a list for adults
adult_data <- list()
for(i in 1:length(sp_codes)){
for(j in 1:length(dist_names)){
df <- merge_sp_dist_env(species.sum.long, sp_codes[i], dist_names[j], env_vars)
adult_data[[paste(sp_codes[i],"_",dist_names[j],"_cvars",sep="")]]<- df
}
}
#list for seedlings
seedling_data <- list()
for(i in 1:length(sp_codes)){
for(j in 1:length(dist_names)){
df <- merge_sp_dist_env(dist_seeds_joined, sp_codes[i], dist_names[j], env_vars)
seedling_data[[paste(sp_codes[i],"_",dist_names[j],"_cvars",sep="")]]<- df
}
}
```
```{r}
##writing out adult and seedling data with environmental vars
#write.csv(adult_data[[1]],paste(names(adult_data)[1],".csv",sep=""))
```
#Analysis
##Principal Components Analysis
I will first run a principal components analysis (PCA) on a suite of climate variables for all plots included in the analysis (n = 59,453). Climate variables were collected from PRISM and [ClimateWNA] (<https://sites.ualberta.ca/~ahamann/data/climatewna.html>). First I will evaluate which climate variables are highty correlated (\>0.9) and only keep one of each highly correlated pair.
```{r, echo = FALSE,eval=FALSE}
#first look at correlations in climate variables
colnames(env_vars)
```
```{r, echo=FALSE}
env_vars %>%
dplyr::select(-PLT_CN,-X,-RSCD,-INVYR,-ANN_INV,-CYCLE,-td) %>%
cor() %>%
abs()>0.9
##CMD is only super correlated to temperature variables and not precipitation variables, which makes me think it is overall more driven by temperature than precipitation
env_vars_cut <- env_vars %>%
dplyr::select(-PLT_CN,-X,-RSCD,-INVYR,-ANN_INV,-CYCLE,-td, -ppt, -tmean, -tmax, - tmin, -vpdmin, -vpdmax, -mwmt, -ahm, -DD5, -bffp, -effp, -emt, -eref, -tave_wt, -tave_sm, -ppt_sm)
# env_vars_cut_alt <- env_vars %>%
# dplyr::select(-PLT_CN,-X,-td, -ppt, -tmean, -tmax, - tmin, -vpdmin, -vpdmax, -mwmt, -ahm, -DD5, -bffp, -effp, -emt, -eref, -tave_wt, -tave_sm, -ppt_sm)
#
# env_vars_cut_alt %>%
# cor() %>%
# abs()>0.9
#log transform precipitation variables to make relationships linear
# env_vars_cut_alt_trans <- env_vars_cut_alt %>%
# mutate(MSP_log = log(msp),SHM_log = log(shm), PPT_wt_log = log(ppt_wt), PAS_log = log(pas+0.999), DD_0_log = log(dd_0), .keep="unused")
```
```{r,eval=FALSE,echo=FALSE}
#look at linearity of relationships
ncol(env_vars_cut)
#
# pdf(file="C:/Users/Katie/Google Drive/FIA project/FIA_rproject/pre_trans_pairs_2022.pdf")
#
# pairs(env_vars_cut, lower.panel = NULL)
#
# dev.off()
```
```{r, echo=FALSE}
##log-transform precipitation variables to make relationships between climate variables more linear
env_vars_cut_trans <- env_vars_cut %>%
mutate(MSP_log = log(msp),SHM_log = log(shm), PPT_wt_log = log(ppt_wt), PAS_log = log(pas+0.999), DD_0_log = log(dd_0), .keep="unused")
```
```{r,eval=FALSE}
#check linearity of relationships with plots
#these take a while to produce
# pdf(file="C:/Users/Katie/Google Drive/FIA project/FIA_rproject/post_trans_pairs.pdf")
#
# pairs(env_vars_cut_trans, lower.panel=NULL)
#
# dev.off()
```
After removing some variables due to high correlation, some of the precipitation variables were log-transformed to make relationships linear, which is an assumption of PCA. The climate variable codes which were kept and their definitions are as follows:
- CMD: climate moisture deficit
- DD_0: degree days below 0 degrees Celsius
- MCMT: mean temperature of the coldest month (Celsius)
- MSP_log: log-transformed mean summer (May - Sept) precipitation (mm)
- NFFD: number of frost-free degree days
- PAS_log: log-transformed precipitation as snow (mm)
- PPT_wt_log: log-transformed winter (Dec - Feb) precipitation (mm)
- SHM_log: log-transformed summer heat moisture index (mean temperature of the warmest month/ (mean summer precipitation/1000))
After running the PCA on these variables, we see that the first two components capture 88% of the variability in climate data.
```{r, echo = FALSE}
#run the PCA on transformed climate variables
# set.seed(83)
# clim_pca <- prcomp(env_vars_cut_trans,scale.=T) #proceed from correlation matrix, which scales variables--important because our variables have different units
# saveRDS(clim_pca, file = "FIA_pca_2022_subset.RDS")
clim_pca <- readRDS("FIA_pca_2022_subset.RDS")
##climate pca without CMD
#alt_clim_pca <- prcomp(env_vars_cut_alt_trans, scale.=T)
#summary(alt_clim_pca)
#par(mfrow=c(1,1))
#plot(alt_clim_pca,type="l")
##to get eigenvalues, we do the square of the stdev
eigenvals <- (clim_pca$sdev)^2
eigenvals
sum(eigenvals) #the sum of the eigenvalues = the number of variables (8)
eigenvals/sum(eigenvals)#this is the proportion variance explained
summary(clim_pca)
par(mfrow=c(1,1))
plot(clim_pca,type="l")
```
```{r}
#try varimax rotation
library(psych)
set.seed(83)
clim_pca_rotated <- principal(env_vars_cut_trans,cor=TRUE,nfactors=2,rotate="varimax", scores=TRUE)
head(clim_pca_rotated$scores)
l.x = clim_pca_rotated$loadings[,1]
l.y = clim_pca_rotated$loadings[,2]
loadings.rotated = data.frame(rc1 = l.x, rc2 = l.y, names=names(l.x))
ggplot(data=clim_pca_rotated$scores, aes(x=RC1, y=RC2))+
geom_point(col="gray")+
xlab("RC1 (54%)")+
ylab("RC2 (37%)")+
geom_segment(data = loadings.rotated, aes(x=0, y=0, xend=rc1*1.5, yend=rc2*1.5),arrow = arrow(type = "open", length = unit(0.2, "cm")), size=1)+
geom_text(data=loadings.rotated, aes(x=rc1,y=rc2,label=names),col="red",vjust=c(-1,1,1,-2,3,-2,-1,2.5))
```
The variables most strongly correlated with PC1 are climate moisture deficit (CMD), precipitation as snow (PAS), number of frost free degree days (NFFD), degree days below zero (DD_0), and mean temperature of the coldest month (MCMT). The variables most strongly correlated with PC2 are mean summer precipitation (MSP), winter precipitation (PPT_wt), and summer heat moisture index (SHM_log). So, PC1 seems to be a temperature related axis, while PC2 corresponds to precipitation.
```{r, echo=FALSE}
as.data.frame(clim_pca$rotation[,1:2]) %>%
tibble::rownames_to_column() %>%
arrange(desc(abs(PC1)))
as.data.frame(clim_pca$rotation[,1:2]) %>%
tibble::rownames_to_column() %>%
arrange(desc(abs(PC2)))
as.data.frame(clim_pca$rotation[,1:3]) %>%
tibble::rownames_to_column() %>%
arrange(desc(abs(PC3))) #PC3 separates monsoonal areas from dry summer/wet winter areas
```
```{r, echo=FALSE}
#plot pca and climate variable loadings
# png("figures/FIA_pca22_subset_realnames.png", width = 700, height = 400)
#make climate variables real words
loadings.df<- as.data.frame(clim_pca$rotation) %>%
mutate(x=c(-1,-3.4,-3.2,2,-3.2,2.3,3.2,2.5), y=c(-2,-1.5,0,-4,1.9,-1.5,1,2.5), names = c(str_wrap("mean temp coldest month", width=14),str_wrap("# frost free degree days",width=14), str_wrap("climate moisture deficit", width=14),str_wrap("log(mean summer precip)",width=14), str_wrap( "log(summer heat moisture index)",width=14),str_wrap("log(winter precip)",width=14),str_wrap("log(precip as snow)",width=14), str_wrap("log(degree days below 0)",width=14)))
autoplot(clim_pca,x=1,y=2,colour="grey",loadings.colour="red", loadings=T, loadings.label=F, loadings.label.colour="black", scale=0, loadings.label.vjust=c(.5,-.3,-.5,-.3,-.4,-.05,1.4,0), loadings.label.hjust=c(1.1,1,0,-.1,1.1,-.05,0,-.1), loadings.label.size=6, alpha=0.5)+
geom_hline(yintercept=0,linetype="dashed")+geom_vline(xintercept=0,linetype="dashed")+
geom_abline(intercept=0, slope = 1, linetype="dashed")+
geom_abline(intercept=0, slope = -1, linetype="dashed")+
annotate("text",x=-0.4,y=3.2,label="cold/dry",size=6,angle=90)+
annotate(geom="text",x=5.2,y=-0.2,label="cold/wet",size=6)+
annotate(geom="text",x=-0.4,y=-4,label="hot/wet",size=6,angle=90)+
annotate(geom="text",x=-10,y=- 0.2,label="hot/dry",size=6)+
annotate(geom="text",x=3.2,y= 3.6,label="cold",size=6, angle=45)+
annotate(geom="text",x=5,y= -4.5,label="wet",size=6, angle=-45)+
annotate(geom="text",x=-5,y= -4.5,label="hot",size=6, angle=45)+
annotate(geom="text",x=-3.2,y= 3.7,label="dry",size=6, angle=-45)+
annotate(geom="text", x=loadings.df$x,y=loadings.df$y, label=loadings.df$names, size=4.5) +
theme_bw()+
theme(axis.text = element_text(color="black",size=18),text = element_text(size=18, lineheight = 0.1),legend.text = element_text(size=18))
# dev.off()
```
```{r}
env_vars %>%
left_join(plot, by=c("PLT_CN"="CN")) %>%
bind_cols(clim_pca$x) %>%
ggplot(aes(x=PC1,y=PC2,color=LAT, alpha=0.5))+
geom_point()
env_vars %>%
left_join(plot, by=c("PLT_CN"="CN")) %>%
bind_cols(clim_pca$x) %>%
ggplot(aes(x=PC1,y=PC2,color=ELEV))+
geom_point()+
scale_color_viridis_c(option="magma")
```
```{r, echo = FALSE}
#make dataframe with PCA coordinates of all plots
clim_pca_points <- as.data.frame(clim_pca$x)
clim_pca_points <- clim_pca_points %>%
bind_cols(plot = env_vars$PLT_CN)
#left join the positions on the PCA for adults and seedlings in all disturbance types by their plot ID #s
adult_data_pcapts <-
map2( .x = seq_along( along.with = adult_data )
, .y = adult_data
, .f = function( i, j )
{
left_join( x = j,
y = clim_pca_points,
by=c("PLT_CN" = "plot") )
})
names(adult_data_pcapts)<- names(adult_data)
seedling_data_pcapts <-
map2( .x = seq_along( along.with = seedling_data )
, .y = seedling_data
, .f = function( i, j )
{
left_join( x = j,
y = clim_pca_points,
by=c("PLT_CN" = "plot") )
})
names(seedling_data_pcapts) <- names(seedling_data)
```
```{r, echo = FALSE}
################ ALTERNATE FOR VARIMAX ROTATION ################
# #make dataframe with PCA coordinates of all plots
# clim_pca_points <- as.data.frame(clim_pca_rotated$scores)
# clim_pca_points <- clim_pca_points %>%
# bind_cols(plot = env_vars$PLT_CN) %>%
# dplyr::rename(PC1=RC1, PC2=RC2)
#
# #left join the positions on the PCA for adults and seedlings in all disturbance types by their plot ID #s
# adult_data_pcapts <-
# map2( .x = seq_along( along.with = adult_data )
# , .y = adult_data
# , .f = function( i, j )
# {
# left_join( x = j,
# y = clim_pca_points,
# by=c("PLT_CN" = "plot") )
# })
# names(adult_data_pcapts)<- names(adult_data)
#
# seedling_data_pcapts <-
# map2( .x = seq_along( along.with = seedling_data )
# , .y = seedling_data
# , .f = function( i, j )
# {
# left_join( x = j,
# y = clim_pca_points,
# by=c("PLT_CN" = "plot") )
# })
# names(seedling_data_pcapts) <- names(seedling_data)
```
These boxplots display the range of PC1 and PC2 coordinates that plots within each disturbance category occupy. The disturbances are pretty evenly distributed across climate space, with insect/disease plots occupying slightly higher values on PC1 and PC2, which correspond to cooler and wetter climates, and fire plots occupying slightly lower values on PC1 and PC2, corresponding to warmer and drier climates, with undisturbed plots having a median climate even hotter and drier than the other two disturbance categories (but also with the most variation). ANOVAs indicate that these three disturbances are all significantly different from each other in their positions on both PC1 and PC2, likely in part due to the extremely high sample size.
```{r, echo=FALSE}
#color palette
disturbance.colors<- c("#bb5f4c","#8e5db0","#729b57")
#red,blue,gray c("#661100", "#6699CC", "#888888")
#summarize position on PC1 and 2 of plots in each disturbance category.
pca_pts_by_dist <- clim_pca_points %>%
left_join(dist, by=c("plot" = "PLT_CN"))
ggplot(pca_pts_by_dist[which(! pca_pts_by_dist$Agent %in% c("other","harvest","wind")),], aes(x=Agent,y=PC1,fill=Agent))+
geom_boxplot()+
scale_fill_manual(values=disturbance.colors)+
theme_bw()
#significant differnces in disturbances? yes
pc1.agent.mod <- aov(PC1~Agent, data=pca_pts_by_dist %>% filter(!Agent %in% c("other","harvest","wind")))
summary(pc1.agent.mod)
TukeyHSD(pc1.agent.mod)
##PC2
pc2.agent.mod <- aov(PC2~Agent, data=pca_pts_by_dist %>% filter(!Agent %in% c("other","harvest","wind")))
summary(pc2.agent.mod)
TukeyHSD(pc2.agent.mod)
ggplot(pca_pts_by_dist[which(! pca_pts_by_dist$Agent %in% c("other","harvest","wind")),], aes(x=Agent,y=PC2,fill=Agent))+
geom_boxplot()+
scale_fill_manual(values=disturbance.colors)+
theme_bw()
```
##Species specific range shifts
The one species-specific test we can do, is a t-test comparing the mean positions on PC1 and PC2 of seedlings vs. adults in different disturbance types.
###PC1
First, PC1:
```{r, echo=FALSE, results='hide'}
##check for normality PC1
#grab multiplot function script
source("multiplot_fxn.R")
#make density plots to check for normality in each of the species x disturbance x age category
##adult
adultplots = list()
for(i in 1:length(adult_data)){
group <- adult_data_pcapts %>%
pluck(i) %>%
ungroup() %>%
pull(PC1)
adultplots[[i]]<- group
}
plotlist<- list()
for(i in 1:length(adult_data)){
plot<- ggdensity(adultplots[[i]])
plotlist[[i]]<- plot
}
qplotlist<- list()
for(i in 1:length(adult_data)){
plot<- ggqqplot(adultplots[[i]])
qplotlist[[i]]<- plot
}
##seedling
seedlingplots = list()
for(i in 1:length(seedling_data)){
group <- seedling_data_pcapts %>%
pluck(i) %>%
ungroup() %>%
pull(PC1)
seedlingplots[[i]]<- group
}
splotlist<- list()
for(i in 1:length(seedling_data)){
plot<- ggdensity(seedlingplots[[i]])
splotlist[[i]]<- plot
}
sqplotlist<- list()
for(i in 1:length(seedling_data)){
plot<- ggqqplot(seedlingplots[[i]])
sqplotlist[[i]]<- plot
}
vartest.results<-data.frame()
for(i in 1:length(seedling_data)){
vartest<- var.test(seedlingplots[[i]],adultplots[[i]],alternative="two.sided")
vartest.results<-rbind(vartest.results, data.frame(num=i, p=vartest$p.value))
}
vartest.results##not all have equal variance between adult and seedling data
```
```{r, echo=FALSE, results='hide'}
#shapiro test to formally evaluate normality of data. Even though, not all pass this test, I think we can still use t-test because sample size is relatively large
#shapiro test for adults
shapiro.results.a<-data.frame()
for(i in 1:length(adult_data)){
shapiro<- shapiro.test(adultplots[[i]])
shapiro.results.a<-rbind(shapiro.results.a, data.frame(num=i, p=shapiro$p))
}
shapiro.results.a %>%
filter(p<0.05)
#look at those that failed normality test
data.names<- data.frame(name = names(adult_data_pcapts),num=seq(1,length(adult_data_pcapts),1))
#this table shows sample size for adult data that didn't pass normality test. Lowest for adults is 151, I think this is reasonably large to still use a t-test.
shapiro.results.a %>%
filter(p<0.05) %>%
left_join(data.names, by='num') %>%
separate(name,sep="_",into=c("species","agent","var")) %>%
mutate(species = as.numeric(species)) %>%
left_join(sample_size, by=c("species" = "SPCD", "agent"="Agent")) %>%
datatable()
shapiro.results.s<-data.frame()
for(i in 1:length(seedling_data)){
shapiro<- shapiro.test(seedlingplots[[i]])
shapiro.results.s<-rbind(shapiro.results.s, data.frame(num=i, p=shapiro$p))
}
head(shapiro.results.s)
shapiro.results.s %>%
filter(p<0.05)
#this table shows sample size for seedling data that didn't pass normality test. Lowest is 162 so i think this is big enough to be fine.
shapiro.results.s %>%
filter(p<0.05) %>%
left_join(data.names, by='num') %>%
separate(name,sep="_",into=c("species","agent","var")) %>%
mutate(species = as.numeric(species)) %>%
left_join(sample_size, by=c("species" = "SPCD", "agent"="Agent")) %>%
datatable()
```
```{r, echo=FALSE, results="hide"}
#do the t-tests for PC1
pc1.ttest.pvalues <- data.frame()
for(i in 1:length(adult_data_pcapts)){
temp.adult <- adult_data_pcapts %>%
pluck(i) %>%
ungroup() %>%
dplyr::select(PC1)
temp.seed <- seedling_data_pcapts %>%
pluck(i) %>%
ungroup() %>%
dplyr::select(PC1)
temp.test<- t.test(temp.adult, temp.seed, paired=FALSE, var.equal = TRUE, conf.level=0.95)
pc1.ttest.pvalues <- bind_rows(pc1.ttest.pvalues, data.frame(uid = names(adult_data_pcapts)[i], diff.means = temp.test$estimate[2] - temp.test$estimate[1],p.value = temp.test$p.value))
}
#unequal variance t-test for some species
pc1.unequal.var <- vartest.results %>%
filter(p<0.05)
pc1.ttest.pvalues.alt <- data.frame()
for(i in pc1.unequal.var$num){
temp.adult <- adult_data_pcapts %>%
pluck(i) %>%
ungroup() %>%
dplyr::select(PC1)
temp.seed <- seedling_data_pcapts %>%
pluck(i) %>%
ungroup() %>%
dplyr::select(PC1)
temp.test<- t.test(temp.adult, temp.seed, paired=FALSE, var.equal = FALSE, conf.level=0.95)
pc1.ttest.pvalues.alt <- bind_rows(pc1.ttest.pvalues.alt, data.frame(uid = names(adult_data_pcapts)[i], diff.means = temp.test$estimate[2] - temp.test$estimate[1],p.value = temp.test$p.value))
}
pc1.ttest.pvalues.alt
##replace rows with unequal variance results
pc1.ttest.pvalues<- pc1.ttest.pvalues %>%
rows_update(pc1.ttest.pvalues.alt, by="uid")
pc1.ttest.pvalues
#get dataframe in order
pc1.ttest.pvalues
pc1.ttest.pvalues.sep <- pc1.ttest.pvalues %>%
separate(uid, c("species","agent"),sep="_",extra="drop",remove = FALSE) %>%
mutate(species = as.numeric(species)) %>%
left_join(species.names, by = c("species" = "species.code")) %>% mutate(sym.pos = ifelse(p.value<0.05, 0.45, NA)) %>%
filter(!(agent == "fire" & species.name %in% insectonlyspp)) %>%
filter(!(agent == "insect.disease" & species.name %in% fireonlyspp)) %>%
filter(!species.name == "singleleaf pinyon")
```
This plot shows the difference in seedling vs. adult positions on PC1 (seedling mean - adult mean), separated by species and disturbance type. Differences in means greater than zero indicate that seedlings occupy higher values on PC1 (cooler climates) than adults whereas negative differences in means indicate that seedlings occupy lower values on PC1 (warmer climates) than adults. Asterisks indicate that seedling and adult positions on PC1 are significantly different (p\<0.05), as evaluated with a T-test.
```{r,echo=FALSE}
allspp_pc1<- ggplot(pc1.ttest.pvalues.sep, aes(x = agent, y = diff.means, fill = agent))+
geom_col()+
geom_text(data = subset(pc1.ttest.pvalues.sep, !is.na(sym.pos)), aes(x=agent, y=sym.pos), label="*", color="black",size=8)+
scale_fill_manual(values=disturbance.colors)+
geom_hline(yintercept=0,col="black")+
facet_wrap(~species.name,nrow=3)+
ylim(c(-0.2,0.5))+
ggtitle("PC1")+
xlab("")+
ylab("Difference in Means")+
labs(fill = "Disturbance Agent")+
theme_bw()+
theme(axis.text.x = element_text(angle=45, hjust=1), axis.text=element_text(color="black", size=14), legend.text = element_text(color="black", size=14), text=element_text(color="black", size=14), strip.text = element_text(color="black", size=11), legend.position = "none",legend.justification = c(1, -0.3))
# png("allspecies.plot.pc1.png", width=700,height=600)
#
# allspp_pc1
#
# dev.off()
```
###PC2
Now for PC2, where postive differences mean that seedlings occupied higher values on PC2 (wetter climates) than adults, and negative differences mean that seedlings occupied lower values on PC2 (drier climates).
```{r, echo=FALSE, results='hide'}
##check for normality on PC2
##adults
pc2_adultplots = list()
for(i in 1:length(adult_data)){
group <- adult_data_pcapts %>%
pluck(i) %>%
ungroup() %>%
pull(PC2)
pc2_adultplots[[i]]<- group
}
pc2_plotlist<- list()
for(i in 1:length(adult_data)){
plot<- ggdensity(pc2_adultplots[[i]])
pc2_plotlist[[i]]<- plot
}
pc2_qplotlist<- list()
for(i in 1:length(adult_data)){
plot<- ggqqplot(pc2_adultplots[[i]])
pc2_qplotlist[[i]]<- plot
}
##seedlings
pc2_seedlingplots = list()
for(i in 1:length(adult_data)){
group <- seedling_data_pcapts %>%
pluck(i) %>%
ungroup() %>%
pull(PC2)
pc2_seedlingplots[[i]]<- group
}
pc2_splotlist<- list()
for(i in 1:length(adult_data)){
plot<- ggdensity(pc2_seedlingplots[[i]])
pc2_splotlist[[i]]<- plot
}
pc2_sqplotlist<- list()
for(i in 1:length(adult_data)){
plot<- ggqqplot(pc2_seedlingplots[[i]])
pc2_sqplotlist[[i]]<- plot
}
##test for equal variance. p<0.05 indicates unequal variance
pc2_vartest.results<-data.frame()
for(i in 1:length(adult_data)){
vartest<- var.test(pc2_seedlingplots[[i]],pc2_adultplots[[i]],alternative="two.sided")
pc2_vartest.results<-rbind(pc2_vartest.results, data.frame(num=i, p=vartest$p.value))
}
pc2_vartest.results %>%
filter(p<0.05)
```
```{r, eval=FALSE, echo=FALSE}
#plot checks for normality PC2
##adults
# multiplot(plotlist=pc2_plotlist[1:16],cols=4)
# multiplot(plotlist=pc2_qplotlist[1:16],cols=4)
# multiplot(plotlist=pc2_qplotlist[17:33],cols=4)
##seedlings
# multiplot(plotlist=pc2_splotlist,cols=4)
# multiplot(plotlist=pc2_sqplotlist[1:16],cols=4)
# multiplot(plotlist=pc2_sqplotlist[17:33],cols=4)
```
```{r}
#shapiro test to formally evaluate normality of data. Even though, not all pass this test, I think we can still use t-test because sample size is relatively large
#shapiro test for adults
pc2.shapiro.results.a<-data.frame()
for(i in 1:length(adult_data)){
shapiro<- shapiro.test(pc2_adultplots[[i]])
pc2.shapiro.results.a<-rbind(pc2.shapiro.results.a, data.frame(num=i, p=shapiro$p))
}
pc2.shapiro.results.a %>%
filter(p<0.05)
#this table shows sample size for adult data that didn't pass normality test. Lowest for adults is 195, I think this is reasonably large to still use a t-test.
pc2.shapiro.results.a %>%
filter(p<0.05) %>%
left_join(data.names, by='num') %>%
separate(name,sep="_",into=c("species","agent","var")) %>%
mutate(species=as.numeric(species)) %>%
left_join(sample_size, by=c("species"="SPCD","agent"="Agent")) %>%
datatable()
pc2.shapiro.results.s<-data.frame()
for(i in 1:length(pc2_seedlingplots)){
shapiro<- shapiro.test(pc2_seedlingplots[[i]])
pc2.shapiro.results.s<-rbind(pc2.shapiro.results.s, data.frame(num=i, p=shapiro$p))
}
pc2.shapiro.results.s %>%
filter(p<0.05)
#this table shows sample size for seedling data that didn't pass normality test. Lowest is 67. I think this is ok for t-test.
pc2.shapiro.results.s %>%
filter(p<0.05) %>%
left_join(data.names, by='num') %>%
separate(name,sep="_",into=c("species","agent","var")) %>%
mutate(species=as.numeric(species)) %>%
left_join(sample_size, by=c("species"="SPCD","agent"="Agent")) %>% datatable()
```
```{r, echo=FALSE, results='hide'}
#do the t-test with assumption of equal vars PC2
pc2.ttest.pvalues <- data.frame()
for(i in 1:length(adult_data_pcapts)){
temp.adult <- adult_data_pcapts %>%
pluck(i) %>%
ungroup() %>%
dplyr::select(PC2)
temp.seed <- seedling_data_pcapts %>%
pluck(i) %>%
ungroup() %>%