-
Notifications
You must be signed in to change notification settings - Fork 0
/
COHORTCDFS.Rmd
1423 lines (1248 loc) · 55.5 KB
/
COHORTCDFS.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: "cohortcdfs GITHUB VERSION"
author: "tara"
date: "7/16/2020"
output:
pdf_document: default
html_document: default
theme: readable
---
```{r setup, echo=FALSE, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning=FALSE, cache=TRUE)
```
## R Markdown
This script takes elements from "taraCDFSBAYYEAR.Rmd" and "taraCDFS2.Rmd"
**objectives**
+ calculate multiple peaks for shinnecock and mattituck cohorts wrt environmental variables.
could randomize cdfs between years and bays to see if they overlap between years for example. for the settlement example.- Not sure what this means.
**Analyses**
**Total sampled area**
I did some rough area calculations in google earth based on previous sample maps.
Sample maps can be found in the powerpoint "Sampling maps w stations for first SK grant 2016"
Shinnecock: 1.61 KM
Moriches: 1.49 KM
Jamaica Bay: 2.24 KM
Napeague Harbor: 1.98 KM
Cold Spring Pond: 0.52 KM
```{r, echo=FALSE, warning=FALSE}
setwd("/Users//tdolan/Documents//R-Github//WFFieldSurveyPaper")
library("tidyr")
library("ggplot2")
library("plyr")
library("purrr")
library("dplyr")
```
**My Data**
```{r}
#somedata3<-read.csv("somedata3_Mar-4-2020.csv", na.strings="", header=TRUE) %>% filter(!is.na(Temp))
somedata3 <-read.csv("skcompiled4gams2.csv", na.strings="", header=TRUE)
```
##**Calculate CDFS for available vs. occupied habitat**##
Separately for each bay, but with data grouped across years.
Split data by bays : **Change this chunk each time** other chunks remain the same.
```{r, include=TRUE, echo=TRUE}
SomeData <-somedata3 %>% dplyr::select(-minl, -maxl, -sdl, -distswept, -Tow, -Towindex) %>% mutate(Pres =ifelse(cpT > 0,1,0))%>%
unite(BayYear,Bay,Year, remove=FALSE)%>% base::split(.$Bay)
Bay <-SomeData$`Shinnecock` # Just change out the bay you are looking at.
total.area <- 1.61*1000 #for Shinnecock
# <- 1.49*1000 #for Moriches
# <- 2.24*1000 #for Jamaica
# <- 1.98*1000 #for Napeague
# <- 0.52*1000 #for Cold Spring Pond
```
### **Cohort CDFS** ###
starred weeks only.
```{r}
SomeData1617 <-somedata3 %>% dplyr::select(-minl, -maxl, -sdl, -distswept, -Towindex, -cpue, -efoff, -Bottom.type, -NumKept) %>% mutate(Pres =ifelse(cpT > 0,1,0))%>%
unite(BayYear,Bay,Year, remove=FALSE) %>% filter(Bay =="Shinnecock")%>% filter(Year %in% c("2016","2017"))
allsco <-read.csv("allscoJune30.csv", na.strings="", header=TRUE)
allsco <-left_join(allsco, SomeData1617, by=c("Bay","Date","Year")) %>% dplyr::select(-X.y, -NumCaught, -rcpT, -X.x, -X.1, -avl)%>%mutate(Date=as.Date(Date))
splitco <-allsco%>%
split(list(.$Year,.$co))
shi16_e <-plyr::ddply(splitco$`2016.1`, Week~Date~Station, summarize, coCatch =sum(value),Date=max(Date), Temp=max(Temp), SAL=max(SAL), DO=max(DO.mg.L), Depth=max(AvDepth)) %>%mutate(Week=as.numeric(as.character(Week)), co="1") %>%
mutate(starred=ifelse(Week %in% c(5,6,7,8,9),"star","nostar"),Date=as.Date(Date))
shi16_l <-plyr::ddply(splitco$`2016.2`, Week~Date~Station, summarize, coCatch =sum(value),Date=max(Date),Temp=max(Temp), SAL=max(SAL), DO=max(DO.mg.L), Depth=max(AvDepth)) %>%mutate(Week=as.numeric(as.character(Week)), co="2")%>%
mutate(starred=ifelse(Week %in% c(5,6,7,8,9),"star","nostar"),Date=as.Date(Date))
shi17_e <-plyr::ddply(splitco$`2017.1`, Week~Date~Station, summarize, coCatch =sum(value),Date=max(Date),Temp=max(Temp), SAL=max(SAL), DO=max(DO.mg.L), Depth=max(AvDepth)) %>%mutate(Week=as.numeric(as.character(Week)), co="1")%>%
mutate(starred=ifelse(Week %in% c(1,2,3,4,5,6),"star","nostar"),Date=as.Date(Date))
shi17_l <-plyr::ddply(splitco$`2017.2`,Week~Date~Station, summarize, coCatch =sum(value),Date=max(Date),Temp=max(Temp), SAL=max(SAL), DO=max(DO.mg.L), Depth=max(AvDepth)) %>%mutate(Week=as.numeric(as.character(Week)), co="2")%>%
mutate(starred=ifelse(Week %in% c(1,2,3,4,5,6),"star","nostar"),Date=as.Date(Date))
shin16co <-bind_rows(shi16_e, shi16_l) %>%filter(starred=="star") %>% mutate(Pres=ifelse(coCatch > 0,1,0))
shin17co <-bind_rows(shi17_e, shi17_l) %>%filter(starred=="star")%>% mutate(Pres=ifelse(coCatch >0,1,0))
```
***Compare cohorts, make separate CDFS for each**
Shinnecock 2016
```{r}
co16_1Temp <- filter(shi16_e, !is.na(Temp) & starred=="star") %>% arrange(Temp)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
co16_2Temp <- filter(shi16_l, !is.na(Temp) & starred=="star") %>% arrange(Temp)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
#AVAILABLE HABITAT: should be for both cohorts!!!
shin16coTemp <-filter(shin16co, !is.na(Temp) & starred=="star")%>% arrange(Temp)
SurveyCDF = cumsum(shin16coTemp$Temp)
SUMTemp<-sum(shin16coTemp$Temp)
shin16coTemp <- mutate(shin16coTemp, SurveyCDFFinal=SurveyCDF/SUMTemp) #this is the AVAILABLE HABITAT over ALL YEARS in Shinnecock
#OCCUPIED HABITAT: cohort 1
Temp <- co16_1Temp$Temp
yh=sum(co16_1Temp$coCatch)
ybar_strata=mean(co16_1Temp$coCatch)
#create and plot CDF for occupied habitat
co16_1Temp <-mutate(co16_1Temp, OccupiedTemp =(coCatch/ybar_strata)*as.numeric(Pres)*Temp)%>%
mutate(OccupiedCDF =cumsum(OccupiedTemp))
SUMOccTemp <-sum(co16_1Temp$OccupiedTemp)
co16_1Temp <-mutate(co16_1Temp, OccCDF_final = OccupiedCDF/SUMOccTemp)
#OCCUPIED HABITAT: cohort 2
Temp <- co16_2Temp$Temp
yh=sum(co16_2Temp$coCatch)
ybar_strata=mean(co16_2Temp$coCatch)
#create and plot CDF for occupied habitat
co16_2Temp <-mutate(co16_2Temp, OccupiedTemp =(coCatch/ybar_strata)*as.numeric(Pres)*Temp)%>%
mutate(OccupiedCDF =cumsum(OccupiedTemp))
SUMOccTemp <-sum(co16_2Temp$OccupiedTemp)
co16_2Temp <-mutate(co16_2Temp, OccCDF_final = OccupiedCDF/SUMOccTemp)
#plot occupied, available, both cohorts
a <- ggplot(shin16coTemp, aes(x=Temp, y=SurveyCDFFinal))
a+geom_line(linetype="dotted")+
geom_line(aes(x=Temp, y=OccCDF_final), color="#a6bddb", data=co16_1Temp)+
geom_line(aes(x=Temp, y=OccCDF_final), color="#1c9099", data=co16_2Temp)+
ylab("CDF")+xlab("Temperature (C)")+ggtitle("Shinnecock 2016")+
theme_classic()
#ggsave("cohortcdfs16_OccvAVTemp.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
#dev.off()
```
**Bootstrap test for significant differences between cohorts: Shinnecock 2016**
```{r}
#TEST STATISTIC: DISTANCE BETWEEN COHORTS
Dist_test<-abs(co16_1Temp$OccCDF_final-co16_2Temp$OccCDF_final)
Test.statistic<-max(Dist_test)
Test.statistic
#BOOTSTRAPPING TEST STATISTIC AND CONFIDENCE INTERVALS
###Bootstrapping occupied CPUE to create new CDFS
DistBoot<-abs(co16_1Temp$OccCDF_final-co16_2Temp$OccCDF_final)#if not defined prior to running loop,
TS.Boot<-max(DistBoot)
#defining variables
catch1 <-co16_1Temp$coCatch #total catch in that TOW
catch2 <-co16_2Temp$coCatch #total catch in that TOW
ybar1 <-mean(catch1)
ybar2 <-mean(catch2)
Pres1 <-co16_1Temp$Pres #Presence absence for that TOW
Pres2 <-co16_2Temp$Pres
Temp1 <-co16_1Temp$Temp
Temp2 <-co16_2Temp$Temp
# THE BOOT LOOP
set.seed(1234)
results<-c()
boot.store1 <-vector("list",length(10000))
boot.store2 <-vector("list",length(10000))
for(i in 1:10000) {
#Cohort 1
BootSampler1<-sample(catch1, length(catch1),replace=FALSE) #catch cohort 1
TempBoot1<-(BootSampler1/ybar1)*(as.numeric(Pres1))*Temp1
CDFBoot1<-cumsum(TempBoot1)
SUMBoot1<-sum(TempBoot1)
CDFFinalBoot1<-CDFBoot1/SUMBoot1
boot.store1[[i]]<-CDFFinalBoot1
#Cohort 2
BootSampler2<-sample(catch2, length(catch2),replace=FALSE) #catch cohort 1
TempBoot2<-(BootSampler2/ybar2)*(as.numeric(Pres2))*Temp2
CDFBoot2<-cumsum(TempBoot2)
SUMBoot2<-sum(TempBoot2)
CDFFinalBoot2<-CDFBoot2/SUMBoot2
boot.store2[[i]]<-CDFFinalBoot2
#Test statistic
DistBoot<-abs(CDFFinalBoot1-CDFFinalBoot2) #absolute value from bootstrapped and observed
results[i]<-max(DistBoot) #store them here
}
summary(results)
#SIGNIFICANCE TEST RESULT
sum(abs(results>abs(Test.statistic))/10000) ## See how many of the values in 'result'are more extreme than the observed value
#EXTRACT BOOTS FOR CI
#Cohort1
b.store1 <- transpose(boot.store1)
lcis1 <-c()
ucis1 <-c()
meanboot1<-c()
for(i in 1:length(b.store1)) {
bvec1 <-unlist(b.store1[i])
lcis1[i]<-quantile(bvec1,0.025)
ucis1[i]<-quantile(bvec1,0.975)
meanboot1[i]<-mean(bvec1)
}
b.store2 <- transpose(boot.store2)
lcis2 <-c()
ucis2 <-c()
meanboot2<-c()
for(i in 1:length(b.store2)) {
bvec2 <-unlist(b.store2[i])
lcis2[i]<-quantile(bvec2,0.025)
ucis2[i]<-quantile(bvec2,0.975)
meanboot2[i]<-mean(bvec2)
}
#Change the colors:
#old colors: Cohort1: #a6bddb, #1c9099, Cohort2: #1c9099
#New colors: cohort1"#0072B2","#D55E00",
#plot occupied, available, both cohorts
a <- ggplot()
a+geom_line(aes(x=Temp, y=OccCDF_final), color="#0072B2", size = 0.7, data=co16_1Temp)+ #cohort 1
geom_line(aes(x=Temp, y=SurveyCDFFinal),linetype="dotted", size = 0.7, data=shin16coTemp)+ #available habitat.
#geom_line(aes(x=Temp,y=meanboot1),linetype=3, color="#748499")+ # bootstrapped mean
#geom_ribbon(aes(x=Temp,ymin=lcis1,ymax=ucis1),fill="#0072B2",alpha=0.2,data=co16_1Temp)+ #bootstrapped CI
geom_line(aes(x=Temp, y=OccCDF_final), color="#D55E00", size = 0.7, data=co16_2Temp)+ #cohort 2.
#geom_ribbon(aes(x=Temp,ymin=lcis2,ymax=ucis2),fill="#D55E00",alpha=0.2,data=co16_2Temp)+ #bootstrapped CI
#geom_line(aes(x=Temp,y=meanboot2),linetype=3, color="#1c9099")+ # bootstrapped mean
ylab("CDF")+xlab("Temperature (C)")+ggtitle("Shinnecock 2016")+
#theme_classic()
theme(plot.title=element_text(size=16),axis.text = element_text(size = 16),axis.title = element_text(size = 16),
panel.background = element_rect(fill = 'white', colour = 'black'),
panel.grid.major = element_line(colour = "white"))+guides(fill = FALSE, colour = FALSE)
#,plot.margin=margin(0.5,1,0.5,0.5,"cm")
ggsave("cohortcdfs16_OccvAVTempCInew.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
dev.off()
#save for later
```
Bob's addition: Are either cohort significantly different from available habitat?
```{r, eval=FALSE,include=FALSE,echo=FALSE}
#TEST STATISTIC: DISTANCE BETWEEN COHORTS
Dist_test<-abs(co16_1Temp$OccCDF_final-shin16coTemp$SurveyCDFFinal)
Test.statistic<-max(Dist_test)
Test.statistic
#BOOTSTRAPPING TEST STATISTIC AND CONFIDENCE INTERVALS
###Bootstrapping occupied CPUE to create new CDFS
DistBoot<-abs(co16_1Temp$OccCDF_final-shin16coTemp$SurveyCDFFinal)#if not defined prior to running loop,
TS.Boot<-max(DistBoot)
#defining variables
catch1 <-co16_1Temp$coCatch #total catch in that TOW
ybar1 <-mean(catch1)
Pres1 <-co16_1Temp$Pres #Presence absence for that TOW
TempOcc <-co16_1Temp$Temp
TempAv <-shin16coTemp$Temp
# THE BOOT LOOP
#set.seed(1234)
results<-c()
boot.store1 <-vector("list",length(10000))
boot.store2 <-vector("list",length(10000))
for(i in 1:100000) {
#Cohort 1
BootSampler1<-sample(catch1, length(catch1),replace=FALSE) #catch cohort 1
TempBoot1<-(BootSampler1/ybar1)*(as.numeric(Pres1))*TempOcc
CDFBoot1<-cumsum(TempBoot1)
SUMBoot1<-sum(TempBoot1)
CDFFinalBoot1<-CDFBoot1/SUMBoot1
boot.store1[[i]]<-CDFFinalBoot1
#Available
BootSampler2<-sample(TempAv, length(TempAv),replace=FALSE) #available habitat.
CDFBoot2<-cumsum(BootSampler2)
SUMBoot2<-sum(BootSampler2)
CDFFinalBoot2<-CDFBoot2/SUMBoot2
boot.store2[[i]]<-CDFFinalBoot2
#Test statistic
DistBoot<-abs(CDFFinalBoot1-CDFFinalBoot2) #absolute value from bootstrapped and observed
results[i]<-max(DistBoot) #store them here
}
summary(results)
#SIGNIFICANCE TEST RESULT
#what proportion of results have a greater max difference than observed difference? 0 have greater... that means they are statistically the same.
quantile(results, c(0.025,0.975)) # if something is outside the 95% CI it is SIGNIFICANTLY DIFFERENT, not signficantly the same.
#what proportion of results have a lesser max difference than observed difference? 0 have lesser... i don't really understand this test.
sum(results>abs(Test.statistic))/10000 ## See how many of the values in 'result'are more extreme than the observed value
```
Bobs occ vs available per cohort. Cohort 2.
```{r,eval=FALSE,include=FALSE,echo=FALSE}
#TEST STATISTIC: DISTANCE BETWEEN COHORTS
Dist_test<-abs(co16_2Temp$OccCDF_final-shin16coTemp$SurveyCDFFinal)
Test.statistic<-max(Dist_test)
Test.statistic
#BOOTSTRAPPING TEST STATISTIC AND CONFIDENCE INTERVALS
###Bootstrapping occupied CPUE to create new CDFS
DistBoot<-abs(co16_2Temp$OccCDF_final-shin16coTemp$SurveyCDFFinal)#if not defined prior to running loop,
TS.Boot<-max(DistBoot)
#defining variables
catch2 <-co16_2Temp$coCatch #total catch in that TOW
ybar2 <-mean(catch2)
Pres2 <-co16_2Temp$Pres #Presence absence for that TOW
TempOcc <-co16_2Temp$Temp
TempAv <-shin16coTemp$Temp
# THE BOOT LOOP
set.seed(1234)
results<-c()
boot.store2 <-vector("list",length(10000))
boot.storea <-vector("list",length(10000))
for(i in 1:10000) {
#Cohort 2
BootSampler2<-sample(catch2, length(catch2),replace=FALSE) #catch cohort 1
TempBoot2<-(BootSampler2/ybar1)*(as.numeric(Pres2))*Temp2
CDFBoot2<-cumsum(TempBoot2)
SUMBoot2<-sum(TempBoot2)
CDFFinalBoot2<-CDFBoot2/SUMBoot2
boot.store2[[i]]<-CDFFinalBoot2
#Available
BootSamplera<-sample(TempAv, length(TempAv),replace=FALSE) #available habitat.
CDFBoota<-cumsum(BootSamplera)
SUMBoota<-sum(BootSamplera)
CDFFinalBoota<-CDFBoot2/SUMBoota
boot.storea[[i]]<-CDFFinalBoota
#Test statistic
DistBoot<-abs(CDFFinalBoot1-CDFFinalBoota) #absolute value from bootstrapped and observed
results[i]<-max(DistBoot) #store them here
}
summary(results)
quantile(results, c(0.025,0.975))
#SIGNIFICANCE TEST RESULT
sum(abs(results>abs(Test.statistic))/10000) ## See how many of the values in 'result'are more extreme than the observed value
```
Shinnecock 2017
```{r}
co17_1Temp <- filter(shi17_e, !is.na(Temp) & starred=="star") %>% arrange(Temp)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
co17_2Temp <- filter(shi17_l, !is.na(Temp) & starred=="star") %>% arrange(Temp)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
#AVAILABLE HABITAT: should be for both cohorts!!!
shin17coTemp <-filter(shin17co, !is.na(Temp)& starred=="star")%>% arrange(Temp)
SurveyCDF = cumsum(shin17coTemp$Temp)
SUMTemp<-sum(shin17coTemp$Temp)
shin17coTemp <- mutate(shin17coTemp, SurveyCDFFinal=SurveyCDF/SUMTemp) #this is the AVAILABLE HABITAT over ALL YEARS in Shinnecock
#OCCUPIED HABITAT: cohort 1
Temp <- co17_1Temp$Temp
yh=sum(co17_1Temp$coCatch)
ybar_strata=mean(co17_1Temp$coCatch)
#create and plot CDF for occupied habitat
co17_1Temp <-mutate(co17_1Temp, OccupiedTemp =(coCatch/ybar_strata)*as.numeric(Pres)*Temp)%>%
mutate(OccupiedCDF =cumsum(OccupiedTemp))
SUMOccTemp <-sum(co17_1Temp$OccupiedTemp)
co17_1Temp <-mutate(co17_1Temp, OccCDF_final = OccupiedCDF/SUMOccTemp)
#OCCUPIED HABITAT: cohort 2
Temp <- co17_2Temp$Temp
yh=sum(co17_2Temp$coCatch)
ybar_strata=mean(co17_2Temp$coCatch)
#create and plot CDF for occupied habitat
co17_2Temp <-mutate(co17_2Temp, OccupiedTemp =(coCatch/ybar_strata)*as.numeric(Pres)*Temp)%>%
mutate(OccupiedCDF =cumsum(OccupiedTemp))
SUMOccTemp <-sum(co17_2Temp$OccupiedTemp)
co17_2Temp <-mutate(co17_2Temp, OccCDF_final = OccupiedCDF/SUMOccTemp)
#plot occupied, available, both cohorts
a <- ggplot(shin17coTemp, aes(x=Temp, y=SurveyCDFFinal))
a+geom_line(linetype="dotted")+
geom_line(aes(x=Temp, y=OccCDF_final), color="#a6bddb", data=co17_1Temp)+
geom_line(aes(x=Temp, y=OccCDF_final), color="#1c9099", data=co17_2Temp)+
ylab("CDF")+xlab("Temperature (C)")+ggtitle("Shinnecock 2017")+
theme_classic()
#ggsave("cohortcdfs17_OccvAVTemp.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
#dev.off()
```
**Bootstrap test for significant differences between cohorts: Shinnecock 2017**
```{r}
#TEST STATISTIC: DISTANCE BETWEEN COHORTS
Dist_test<-abs(co17_1Temp$OccCDF_final-co17_2Temp$OccCDF_final)
Test.statistic<-max(Dist_test)
Test.statistic
#BOOTSTRAPPING TEST STATISTIC AND CONFIDENCE INTERVALS
###Bootstrapping occupied CPUE to create new CDFS
DistBoot<-abs(co17_1Temp$OccCDF_final-co17_2Temp$OccCDF_final)#if not defined prior to running loop,
TS.Boot<-max(DistBoot)
#defining variables
catch1 <-co17_1Temp$coCatch #total catch in that TOW
catch2 <-co17_2Temp$coCatch #total catch in that TOW
ybar1 <-mean(catch1)
ybar2 <-mean(catch2)
Pres1 <-co17_1Temp$Pres #Presence absence for that TOW
Pres2 <-co17_2Temp$Pres
Temp1 <-co17_1Temp$Temp
Temp2 <-co17_2Temp$Temp
# THE BOOT LOOP
set.seed(1234)
results<-c()
boot.store1 <-vector("list",length(10000))
boot.store2 <-vector("list",length(10000))
for(i in 1:10000) {
#Cohort 1
BootSampler1<-sample(catch1, length(catch1),replace=FALSE) #catch cohort 1
TempBoot1<-(BootSampler1/ybar1)*(as.numeric(Pres1))*Temp1
CDFBoot1<-cumsum(TempBoot1)
SUMBoot1<-sum(TempBoot1)
CDFFinalBoot1<-CDFBoot1/SUMBoot1
boot.store1[[i]]<-CDFFinalBoot1
#Cohort 2
BootSampler2<-sample(catch2, length(catch2),replace=FALSE) #catch cohort 1
TempBoot2<-(BootSampler2/ybar2)*(as.numeric(Pres2))*Temp2
CDFBoot2<-cumsum(TempBoot2)
SUMBoot2<-sum(TempBoot2)
CDFFinalBoot2<-CDFBoot2/SUMBoot2
boot.store2[[i]]<-CDFFinalBoot2
#Test statistic
DistBoot<-abs(CDFFinalBoot1-CDFFinalBoot2) #absolute value from bootstrapped and observed
results[i]<-max(DistBoot) #store them here
}
summary(results)
#SIGNIFICANCE TEST RESULT
sum(abs(results>abs(Test.statistic))/10000) ## See how many of the values in 'result'are more extreme than the observed value
#EXTRACT BOOTS FOR CI
#Cohort1
b.store1 <- transpose(boot.store1)
lcis1 <-c()
ucis1 <-c()
meanboot1<-c()
for(i in 1:length(b.store1)) {
bvec1 <-unlist(b.store1[i])
lcis1[i]<-quantile(bvec1,0.025)
ucis1[i]<-quantile(bvec1,0.975)
meanboot1[i]<-mean(bvec1)
}
b.store2 <- transpose(boot.store2)
lcis2 <-c()
ucis2 <-c()
meanboot2<-c()
for(i in 1:length(b.store2)) {
bvec2 <-unlist(b.store2[i])
lcis2[i]<-quantile(bvec2,0.025)
ucis2[i]<-quantile(bvec2,0.975)
meanboot2[i]<-mean(bvec2)
}
#New colors: cohort1"#0072B2","#D55E00",
#plot occupied, available, both cohorts
a <- ggplot()
a+geom_line(aes(x=Temp, y=OccCDF_final), color="#0072B2", size = 0.7, data=co17_1Temp)+
geom_line(aes(x=Temp, y=SurveyCDFFinal),linetype="dotted", size = 0.7, data=shin17coTemp)+
#geom_line(aes(x=Temp,y=meanboot1),linetype=3, color="#748499")+ # bootstrapped mean
#geom_ribbon(aes(x=Temp,ymin=lcis1,ymax=ucis1),fill="#0072B2",alpha=0.2)+ #bootstrapped CI
geom_line(aes(x=Temp, y=OccCDF_final), color="#D55E00", data=co17_2Temp)+
#geom_ribbon(aes(x=Temp,ymin=lcis2,ymax=ucis2),fill="#D55E00",alpha=0.2)+ #bootstrapped CI
#geom_line(aes(x=Temp,y=meanboot2),linetype=3, color="#1c9099")+ # bootstrapped mean
ylab("CDF")+xlab("Temperature (C)")+ggtitle("Shinnecock 2017")+
theme(plot.title=element_text(size=16),axis.text = element_text(size = 16),axis.title = element_text(size = 16),
panel.background = element_rect(fill = 'white', colour = 'black'),
panel.grid.major = element_line(colour = "white"))+guides(fill = FALSE, colour = FALSE)
ggsave("cohortcdfs17_OccvAVTempCInew.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
dev.off()
```
**Percentiles**
```{r, warning=FALSE, echo=TRUE}
#Calculating Quantiles
#FUNCTION TO EXTRACT PERCENTILES OF OCCUPIED HABITAT
percentile.func <-function(df){
df <- mutate(df, BTcolumn=round(Temp,digits=1),OccCDFcolumn=round(OccCDF_final,digits=2))
OccPer.5 <-(df[df$OccCDFcolumn=='0.05','BTcolumn']) #not interpolated version
OccPer.50 <-(df[df$OccCDFcolumn=='0.5','BTcolumn'])
OccPer.95 <-(df[df$OccCDFcolumn=='0.95','BTcolumn'])
not.interpolated <-list(OccPer.5,OccPer.50,OccPer.95)
#occ.ni <- rapply(not.interpolated, mean)
#interpolated version
TabSurveyApprox<-approx(df$BTcolumn,df$OccCDFcolumn, n=20000)
TabSurveyApproxBT<-round(TabSurveyApprox$x,digits=3)
TabSurveyApproxSurveyOccCDF<-round(TabSurveyApprox$y,digits=3)
OccTabSurveyAp<-data.frame(cbind(TabSurveyApproxBT,TabSurveyApproxSurveyOccCDF))
OccPer.5.ap <-(OccTabSurveyAp[OccTabSurveyAp$TabSurveyApproxSurveyOccCDF=='0.05','TabSurveyApproxBT']) #(INTERPOLATED)
OccPer.50.ap <-(OccTabSurveyAp[OccTabSurveyAp$TabSurveyApproxSurveyOccCDF=='0.5','TabSurveyApproxBT'])
OccPer.95.ap <-(OccTabSurveyAp[OccTabSurveyAp$TabSurveyApproxSurveyOccCDF=='0.95','TabSurveyApproxBT'])
interpolated <-list(OccPer.5.ap,OccPer.50.ap,OccPer.95.ap)
occ.i <-rapply(interpolated, mean)
#out <-list(not.interpolated, occ.i)
occ.i
}
```
**Apply percentile function**
```{r}
allcotemp <-bind_rows(co16_1Temp,co16_2Temp,co17_1Temp,co17_2Temp)
allcotemp <-separate(allcotemp, Date,c("Year","m","d"), sep="-", remove=FALSE)
split.percent <-allcotemp %>% split(list(.$Year,.$co))%>% map_dfr(percentile.func) #map it
split.percent
```
**DISSOLVED OXYGEN COHORTS**
Shinnecock 2016
```{r}
co16_1DO <- filter(shi16_e, !is.na(DO) & DO > 0 & DO < 20 & starred=="star") %>% arrange(DO)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
co16_2DO <- filter(shi16_l, !is.na(DO) & DO > 0 & DO < 20 & starred=="star") %>% arrange(DO)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
#AVAILABLE HABITAT: should be for both cohorts!!!
shin16coDO <-filter(shin16co, !is.na(DO)& DO > 0 & DO < 20 & starred=="star")%>% arrange(DO)
SurveyCDF = cumsum(shin16coDO$DO)
SUMDO<-sum(shin16coDO$DO)
shin16coDO <- mutate(shin16coDO, SurveyCDFFinal=SurveyCDF/SUMDO) #this is the AVAILABLE HABITAT over ALL YEARS in Shinnecock
#OCCUPIED HABITAT: cohort 1
DO <- co16_1DO$DO
yh=sum(co16_1DO$coCatch)
ybar_strata=mean(co16_1DO$coCatch)
#create and plot CDF for occupied habitat
co16_1DO <-mutate(co16_1DO, OccupiedDO =(coCatch/ybar_strata)*as.numeric(Pres)*DO)%>%
mutate(OccupiedCDF =cumsum(OccupiedDO))
SUMOccDO <-sum(co16_1DO$OccupiedDO)
co16_1DO <-mutate(co16_1DO, OccCDF_final = OccupiedCDF/SUMOccDO)
#OCCUPIED HABITAT: cohort 2
DO <- co16_2DO$DO
yh=sum(co16_2DO$coCatch)
ybar_strata=mean(co16_2DO$coCatch)
#create and plot CDF for occupied habitat
co16_2DO <-mutate(co16_2DO, OccupiedDO =(coCatch/ybar_strata)*as.numeric(Pres)*DO)%>%
mutate(OccupiedCDF =cumsum(OccupiedDO))
SUMOccDO <-sum(co16_2DO$OccupiedDO)
co16_2DO <-mutate(co16_2DO, OccCDF_final = OccupiedCDF/SUMOccDO)
#plot occupied, available, both cohorts
a <- ggplot(shin16coDO, aes(x=DO, y=SurveyCDFFinal))
a+geom_line(linetype="dotted")+
geom_line(aes(x=DO, y=OccCDF_final), color="#a6bddb", data=co16_1DO)+
geom_line(aes(x=DO, y=OccCDF_final), color="#1c9099", data=co16_2DO)+
ylab("CDF")+xlab("Dissolved Oxygen (mg/L)")+ggtitle("Shinnecock 2016")+
theme_classic()
#ggsave("cohortcdfs16_OccvAVDO.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
#dev.off()
```
**Bootstrap test for significant differences between cohorts: Shinnecock 2016**
```{r}
#TEST STATISTIC: DISTANCE BETWEEN COHORTS
Dist_test<-abs(co16_1DO$OccCDF_final-co16_2DO$OccCDF_final)
Test.statistic<-max(Dist_test)
Test.statistic
#BOOTSTRAPPING TEST STATISTIC AND CONFIDENCE INTERVALS
###Bootstrapping occupied CPUE to create new CDFS
DistBoot<-abs(co16_1DO$OccCDF_final-co16_2DO$OccCDF_final)#if not defined prior to running loop,
TS.Boot<-max(DistBoot)
#defining variables
catch1 <-co16_1DO$coCatch #total catch in that TOW
catch2 <-co16_2DO$coCatch #total catch in that TOW
ybar1 <-mean(catch1)
ybar2 <-mean(catch2)
Pres1 <-co16_1DO$Pres #Presence absence for that TOW
Pres2 <-co16_2DO$Pres
DO1 <-co16_1DO$DO
DO2 <-co16_2DO$DO
# THE BOOT LOOP
set.seed(1234)
results<-c()
boot.store1 <-vector("list",length(10000))
boot.store2 <-vector("list",length(10000))
for(i in 1:10000) {
#Cohort 1
BootSampler1<-sample(catch1, length(catch1),replace=FALSE) #catch cohort 1
DOBoot1<-(BootSampler1/ybar1)*(as.numeric(Pres1))*DO1
CDFBoot1<-cumsum(DOBoot1)
SUMBoot1<-sum(DOBoot1)
CDFFinalBoot1<-CDFBoot1/SUMBoot1
boot.store1[[i]]<-CDFFinalBoot1
#Cohort 2
BootSampler2<-sample(catch2, length(catch2),replace=FALSE) #catch cohort 1
DOBoot2<-(BootSampler2/ybar2)*(as.numeric(Pres2))*DO2
CDFBoot2<-cumsum(DOBoot2)
SUMBoot2<-sum(DOBoot2)
CDFFinalBoot2<-CDFBoot2/SUMBoot2
boot.store2[[i]]<-CDFFinalBoot2
#Test statistic
DistBoot<-abs(CDFFinalBoot1-CDFFinalBoot2) #absolute value from bootstrapped and observed
results[i]<-max(DistBoot) #store them here
}
summary(results)
#SIGNIFICANCE TEST RESULT
sum(abs(results>abs(Test.statistic))/10000) ## See how many of the values in 'result'are more extreme than the observed value
#EXTRACT BOOTS FOR CI
#Cohort1
b.store1 <- transpose(boot.store1)
lcis1 <-c()
ucis1 <-c()
meanboot1<-c()
for(i in 1:length(b.store1)) {
bvec1 <-unlist(b.store1[i])
lcis1[i]<-quantile(bvec1,0.025)
ucis1[i]<-quantile(bvec1,0.975)
meanboot1[i]<-mean(bvec1)
}
b.store2 <- transpose(boot.store2)
lcis2 <-c()
ucis2 <-c()
meanboot2<-c()
for(i in 1:length(b.store2)) {
bvec2 <-unlist(b.store2[i])
lcis2[i]<-quantile(bvec2,0.025)
ucis2[i]<-quantile(bvec2,0.975)
meanboot2[i]<-mean(bvec2)
}
#New colors: cohort1"#0072B2","#D55E00",
#plot occupied, available, both cohorts
a <- ggplot()
a+geom_line(aes(x=DO, y=OccCDF_final), color="#0072B2",size = 0.7, data=co16_1DO)+
geom_line(aes(x=DO, y=SurveyCDFFinal),linetype="dotted",size = 0.7, data=shin16coDO)+
#geom_line(aes(x=DO,y=meanboot1),linetype=3, color="#748499")+ # bootstrapped mean
#geom_ribbon(aes(x=DO,ymin=lcis1,ymax=ucis1),fill="#0072B2",alpha=0.2)+ #bootstrapped CI
geom_line(aes(x=DO, y=OccCDF_final), color="#D55E00",size = 0.7, data=co16_2DO)+
#geom_ribbon(aes(x=DO,ymin=lcis2,ymax=ucis2),fill="#D55E00",alpha=0.2)+ #bootstrapped CI
#geom_line(aes(x=DO,y=meanboot2),linetype=3, color="#1c9099")+ # bootstrapped mean
ylab("CDF")+xlab("Dissolved Oxygen (mg/L)")+ggtitle("Shinnecock 2016")+
theme(plot.title=element_text(size=16),axis.text = element_text(size = 16),axis.title = element_text(size = 16),
panel.background = element_rect(fill = 'white', colour = 'black'),
panel.grid.major = element_line(colour = "white"))+guides(fill = FALSE, colour = FALSE)
ggsave("cohortcdfs16_OccvAVDOCInew.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
dev.off()
#save for later
```
Shinnecock 2017
```{r}
co17_1DO <- filter(shi17_e, !is.na(DO) & starred=="star") %>% arrange(DO)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
co17_2DO <- filter(shi17_l, !is.na(DO) & starred=="star") %>% arrange(DO)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
#AVAILABLE HABITAT: should be for both cohorts!!!
shin17coDO <-filter(shin17co, !is.na(DO)& starred=="star")%>% arrange(DO)
SurveyCDF = cumsum(shin17coDO$DO)
SUMDO<-sum(shin17coDO$DO)
shin17coDO <- mutate(shin17coDO, SurveyCDFFinal=SurveyCDF/SUMDO) #this is the AVAILABLE HABITAT over ALL YEARS in Shinnecock
#OCCUPIED HABITAT: cohort 1
DO <- co17_1DO$DO
yh=sum(co17_1DO$coCatch)
ybar_strata=mean(co17_1DO$coCatch)
#create and plot CDF for occupied habitat
co17_1DO <-mutate(co17_1DO, OccupiedDO =(coCatch/ybar_strata)*as.numeric(Pres)*DO)%>%
mutate(OccupiedCDF =cumsum(OccupiedDO))
SUMOccDO <-sum(co17_1DO$OccupiedDO)
co17_1DO <-mutate(co17_1DO, OccCDF_final = OccupiedCDF/SUMOccDO)
#OCCUPIED HABITAT: cohort 2
DO <- co17_2DO$DO
yh=sum(co17_2DO$coCatch)
ybar_strata=mean(co17_2DO$coCatch)
#create and plot CDF for occupied habitat
co17_2DO <-mutate(co17_2DO, OccupiedDO =(coCatch/ybar_strata)*as.numeric(Pres)*DO)%>%
mutate(OccupiedCDF =cumsum(OccupiedDO))
SUMOccDO <-sum(co17_2DO$OccupiedDO)
co17_2DO <-mutate(co17_2DO, OccCDF_final = OccupiedCDF/SUMOccDO)
#plot occupied, available, both cohorts
a <- ggplot(shin17coDO, aes(x=DO, y=SurveyCDFFinal))
a+geom_line(linetype="dotted")+
geom_line(aes(x=DO, y=OccCDF_final), color="#a6bddb", data=co17_1DO)+
geom_line(aes(x=DO, y=OccCDF_final), color="#1c9099", data=co17_2DO)+
ylab("CDF")+xlab("Dissolved Oxygen (mg/L)")+ggtitle("Shinnecock 2017")+
theme_classic()
#ggsave("cohortcdfs17_OccvAVDO.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
#dev.off()
```
**Bootstrap test for significant differences between cohorts: Shinnecock 2017**
```{r}
#TEST STATISTIC: DISTANCE BETWEEN COHORTS
Dist_test<-abs(co17_1DO$OccCDF_final-co17_2DO$OccCDF_final)
Test.statistic<-max(Dist_test)
Test.statistic
#BOOTSTRAPPING TEST STATISTIC AND CONFIDENCE INTERVALS
###Bootstrapping occupied CPUE to create new CDFS
DistBoot<-abs(co17_1DO$OccCDF_final-co17_2DO$OccCDF_final)#if not defined prior to running loop,
TS.Boot<-max(DistBoot)
#defining variables
catch1 <-co17_1DO$coCatch #total catch in that TOW
catch2 <-co17_2DO$coCatch #total catch in that TOW
ybar1 <-mean(catch1)
ybar2 <-mean(catch2)
Pres1 <-co17_1DO$Pres #Presence absence for that TOW
Pres2 <-co17_2DO$Pres
DO1 <-co17_1DO$DO
DO2 <-co17_2DO$DO
# THE BOOT LOOP
set.seed(1234)
results<-c()
boot.store1 <-vector("list",length(10000))
boot.store2 <-vector("list",length(10000))
for(i in 1:10000) {
#Cohort 1
BootSampler1<-sample(catch1, length(catch1),replace=FALSE) #catch cohort 1
DOBoot1<-(BootSampler1/ybar1)*(as.numeric(Pres1))*DO1
CDFBoot1<-cumsum(DOBoot1)
SUMBoot1<-sum(DOBoot1)
CDFFinalBoot1<-CDFBoot1/SUMBoot1
boot.store1[[i]]<-CDFFinalBoot1
#Cohort 2
BootSampler2<-sample(catch2, length(catch2),replace=FALSE) #catch cohort 1
DOBoot2<-(BootSampler2/ybar2)*(as.numeric(Pres2))*DO2
CDFBoot2<-cumsum(DOBoot2)
SUMBoot2<-sum(DOBoot2)
CDFFinalBoot2<-CDFBoot2/SUMBoot2
boot.store2[[i]]<-CDFFinalBoot2
#Test statistic
DistBoot<-abs(CDFFinalBoot1-CDFFinalBoot2) #absolute value from bootstrapped and observed
results[i]<-max(DistBoot) #store them here
}
summary(results)
#SIGNIFICANCE TEST RESULT
sum(abs(results>abs(Test.statistic))/10000) ## See how many of the values in 'result'are more extreme than the observed value
#EXTRACT BOOTS FOR CI
#Cohort1
b.store1 <- transpose(boot.store1)
lcis1 <-c()
ucis1 <-c()
meanboot1<-c()
for(i in 1:length(b.store1)) {
bvec1 <-unlist(b.store1[i])
lcis1[i]<-quantile(bvec1,0.025)
ucis1[i]<-quantile(bvec1,0.975)
meanboot1[i]<-mean(bvec1)
}
b.store2 <- transpose(boot.store2)
lcis2 <-c()
ucis2 <-c()
meanboot2<-c()
for(i in 1:length(b.store2)) {
bvec2 <-unlist(b.store2[i])
lcis2[i]<-quantile(bvec2,0.025)
ucis2[i]<-quantile(bvec2,0.975)
meanboot2[i]<-mean(bvec2)
}
#New colors: cohort1"#0072B2","#D55E00",
#plot occupied, available, both cohorts
a <- ggplot()
a+geom_line(aes(x=DO, y=OccCDF_final), color="#0072B2",size = 0.7, data=co17_1DO)+
geom_line(aes(x=DO, y=SurveyCDFFinal),linetype="dotted",size = 0.7, data=shin17coDO)+
#geom_line(aes(x=DO,y=meanboot1),linetype=3, color="#748499")+ # bootstrapped mean
#geom_ribbon(aes(x=DO,ymin=lcis1,ymax=ucis1),fill="#0072B2",alpha=0.2)+ #bootstrapped CI
geom_line(aes(x=DO, y=OccCDF_final), color="#D55E00",size = 0.7, data=co17_2DO)+
#geom_ribbon(aes(x=DO,ymin=lcis2,ymax=ucis2),fill="#D55E00",alpha=0.2)+ #bootstrapped CI
#geom_line(aes(x=DO,y=meanboot2),linetype=3, color="#1c9099")+ # bootstrapped mean
ylab("CDF")+xlab("Dissolved Oxygen (mg/L)")+ggtitle("Shinnecock 2017")+
theme(plot.title=element_text(size=16),axis.text = element_text(size = 16),axis.title = element_text(size = 16),
panel.background = element_rect(fill = 'white', colour = 'black'),
panel.grid.major = element_line(colour = "white"))+guides(fill = FALSE, colour = FALSE)
ggsave("cohortcdfs17_OccvAVDOCInew.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
dev.off()
```
**Percentiles**
```{r, warning=FALSE, echo=TRUE}
#Calculating Quantiles
#FUNCTION TO EXTRACT PERCENTILES OF OCCUPIED HABITAT
percentile.func <-function(df){
df <- mutate(df, BTcolumn=round(DO,digits=1),OccCDFcolumn=round(OccCDF_final,digits=2))
OccPer.5 <-(df[df$OccCDFcolumn=='0.05','BTcolumn']) #not interpolated version
OccPer.50 <-(df[df$OccCDFcolumn=='0.5','BTcolumn'])
OccPer.95 <-(df[df$OccCDFcolumn=='0.95','BTcolumn'])
not.interpolated <-list(OccPer.5,OccPer.50,OccPer.95)
#occ.ni <- rapply(not.interpolated, mean)
#interpolated version
TabSurveyApprox<-approx(df$BTcolumn,df$OccCDFcolumn, n=20000)
TabSurveyApproxBT<-round(TabSurveyApprox$x,digits=3)
TabSurveyApproxSurveyOccCDF<-round(TabSurveyApprox$y,digits=3)
OccTabSurveyAp<-data.frame(cbind(TabSurveyApproxBT,TabSurveyApproxSurveyOccCDF))
OccPer.5.ap <-(OccTabSurveyAp[OccTabSurveyAp$TabSurveyApproxSurveyOccCDF=='0.05','TabSurveyApproxBT']) #(INTERPOLATED)
OccPer.50.ap <-(OccTabSurveyAp[OccTabSurveyAp$TabSurveyApproxSurveyOccCDF=='0.5','TabSurveyApproxBT'])
OccPer.95.ap <-(OccTabSurveyAp[OccTabSurveyAp$TabSurveyApproxSurveyOccCDF=='0.95','TabSurveyApproxBT'])
interpolated <-list(OccPer.5.ap,OccPer.50.ap,OccPer.95.ap)
occ.i <-rapply(interpolated, mean)
#out <-list(not.interpolated, occ.i)
occ.i
}
```
**Apply percentile function**
```{r}
allcoDO <-bind_rows(co16_1DO,co16_2DO,co17_1DO,co17_2DO)
allcoDO <-separate(allcoDO, Date,c("Year","m","d"), sep="-", remove=FALSE)
split.percent <-allcoDO %>% split(list(.$Year,.$co))%>% map_dfr(percentile.func) #map it
split.percent
```
**SALINITY**
***Compare cohorts, make separate CDFS for each**
Shinnecock 2016
```{r}
co16_1SAL <- filter(shi16_e, !is.na(SAL) & SAL > 0 & SAL < 40 & starred=="star") %>% arrange(SAL)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
co16_2SAL <- filter(shi16_l, !is.na(SAL) & SAL > 0 & SAL < 40 & starred=="star") %>% arrange(SAL)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
#AVAILABLE HABITAT: should be for both cohorts!!!
shin16coSAL <-filter(shin16co, !is.na(SAL) & SAL > 0 & SAL < 40 & starred=="star")%>% arrange(SAL)
SurveyCDF = cumsum(shin16coSAL$SAL)
SUMSAL<-sum(shin16coSAL$SAL)
shin16coSAL <- mutate(shin16coSAL, SurveyCDFFinal=SurveyCDF/SUMSAL) #this is the AVAILABLE HABITAT over ALL YEARS in Shinnecock
#OCCUPIED HABITAT: cohort 1
SAL <- co16_1SAL$SAL
yh=sum(co16_1SAL$coCatch)
ybar_strata=mean(co16_1SAL$coCatch)
#create and plot CDF for occupied habitat
co16_1SAL <-mutate(co16_1SAL, OccupiedSAL =(coCatch/ybar_strata)*as.numeric(Pres)*SAL)%>%
mutate(OccupiedCDF =cumsum(OccupiedSAL))
SUMOccSAL <-sum(co16_1SAL$OccupiedSAL)
co16_1SAL <-mutate(co16_1SAL, OccCDF_final = OccupiedCDF/SUMOccSAL)
#OCCUPIED HABITAT: cohort 2
SAL <- co16_2SAL$SAL
yh=sum(co16_2SAL$coCatch)
ybar_strata=mean(co16_2SAL$coCatch)
#create and plot CDF for occupied habitat
co16_2SAL <-mutate(co16_2SAL, OccupiedSAL =(coCatch/ybar_strata)*as.numeric(Pres)*SAL)%>%
mutate(OccupiedCDF =cumsum(OccupiedSAL))
SUMOccSAL <-sum(co16_2SAL$OccupiedSAL)
co16_2SAL <-mutate(co16_2SAL, OccCDF_final = OccupiedCDF/SUMOccSAL)
#plot occupied, available, both cohorts
a <- ggplot(shin16coSAL, aes(x=SAL, y=SurveyCDFFinal))
a+geom_line(linetype="dotted")+
geom_line(aes(x=SAL, y=OccCDF_final), color="#a6bddb", data=co16_1SAL)+
geom_line(aes(x=SAL, y=OccCDF_final), color="#1c9099", data=co16_2SAL)+
ylab("CDF")+xlab("Salinity (ppt)")+ggtitle("Shinnecock 2016")+
theme_classic()
#ggsave("cohortcdfs16_OccvAVSAL.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
#dev.off()
```
**Bootstrap test for significant differences between cohorts: Shinnecock 2016**
```{r}
#TEST STATISTIC: DISTANCE BETWEEN COHORTS
Dist_test<-abs(co16_1SAL$OccCDF_final-co16_2SAL$OccCDF_final)
Test.statistic<-max(Dist_test)
Test.statistic
#BOOTSTRAPPING TEST STATISTIC AND CONFIDENCE INTERVALS
###Bootstrapping occupied CPUE to create new CDFS
DistBoot<-abs(co16_1SAL$OccCDF_final-co16_2SAL$OccCDF_final)#if not defined prior to running loop,
TS.Boot<-max(DistBoot)
#defining variables
catch1 <-co16_1SAL$coCatch #total catch in that TOW
catch2 <-co16_2SAL$coCatch #total catch in that TOW
ybar1 <-mean(catch1)
ybar2 <-mean(catch2)
Pres1 <-co16_1SAL$Pres #Presence absence for that TOW
Pres2 <-co16_2SAL$Pres
SAL1 <-co16_1SAL$SAL
SAL2 <-co16_2SAL$SAL
# THE BOOT LOOP
set.seed(1234)
results<-c()
boot.store1 <-vector("list",length(10000))
boot.store2 <-vector("list",length(10000))
for(i in 1:10000) {
#Cohort 1
BootSampler1<-sample(catch1, length(catch1),replace=FALSE) #catch cohort 1
SALBoot1<-(BootSampler1/ybar1)*(as.numeric(Pres1))*SAL1
CDFBoot1<-cumsum(SALBoot1)
SUMBoot1<-sum(SALBoot1)
CDFFinalBoot1<-CDFBoot1/SUMBoot1
boot.store1[[i]]<-CDFFinalBoot1
#Cohort 2
BootSampler2<-sample(catch2, length(catch2),replace=FALSE) #catch cohort 1
SALBoot2<-(BootSampler2/ybar2)*(as.numeric(Pres2))*SAL2
CDFBoot2<-cumsum(SALBoot2)
SUMBoot2<-sum(SALBoot2)
CDFFinalBoot2<-CDFBoot2/SUMBoot2
boot.store2[[i]]<-CDFFinalBoot2
#Test statistic
DistBoot<-abs(CDFFinalBoot1-CDFFinalBoot2) #absolute value from bootstrapped and observed
results[i]<-max(DistBoot) #store them here
}
summary(results)
#SIGNIFICANCE TEST RESULT
sum(abs(results>abs(Test.statistic))/10000) ## See how many of the values in 'result'are more extreme than the observed value
#EXTRACT BOOTS FOR CI
#Cohort1
b.store1 <- transpose(boot.store1)
lcis1 <-c()
ucis1 <-c()
meanboot1<-c()
for(i in 1:length(b.store1)) {
bvec1 <-unlist(b.store1[i])
lcis1[i]<-quantile(bvec1,0.025)
ucis1[i]<-quantile(bvec1,0.975)
meanboot1[i]<-mean(bvec1)
}
b.store2 <- transpose(boot.store2)
lcis2 <-c()
ucis2 <-c()
meanboot2<-c()
for(i in 1:length(b.store2)) {
bvec2 <-unlist(b.store2[i])
lcis2[i]<-quantile(bvec2,0.025)
ucis2[i]<-quantile(bvec2,0.975)
meanboot2[i]<-mean(bvec2)
}
#New colors: cohort1"#0072B2","#D55E00",
#plot occupied, available, both cohorts
a <- ggplot()
a+geom_line(aes(x=SAL, y=OccCDF_final), color="#0072B2",size = 0.7, data=co16_1SAL)+
geom_line(aes(x=SAL, y=SurveyCDFFinal),linetype="dotted",size = 0.7, data=shin16coSAL)+
#geom_line(aes(x=SAL,y=meanboot1),linetype=3, color="#748499")+ # bootstrapped mean
#geom_ribbon(aes(x=SAL,ymin=lcis1,ymax=ucis1),fill="#0072B2",alpha=0.2)+ #bootstrapped CI
geom_line(aes(x=SAL, y=OccCDF_final), color="#D55E00",size = 0.7, data=co16_2SAL)+
#geom_ribbon(aes(x=SAL,ymin=lcis2,ymax=ucis2),fill="#D55E00",alpha=0.2)+ #bootstrapped CI
#geom_line(aes(x=SAL,y=meanboot2),linetype=3, color="#1c9099")+ # bootstrapped mean
ylab("CDF")+xlab("Salinity (ppt)")+ggtitle("Shinnecock 2016")+
theme(plot.title=element_text(size=16),axis.text = element_text(size = 16),axis.title = element_text(size = 16),
panel.background = element_rect(fill = 'white', colour = 'black'),
panel.grid.major = element_line(colour = "white"))+guides(fill = FALSE, colour = FALSE)
ggsave("cohortcdfs16_OccvAVSALCInew.png", path="/Users/tdolan/documents/WF SK PROJ/Survey data/Field Survey Paper/final figures")
dev.off()
#save for later
```
Shinnecock 2017
```{r}
co17_1SAL <- filter(shi17_e, !is.na(SAL) & SAL > 0 & SAL < 40 & starred=="star") %>% arrange(SAL)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
co17_2SAL <- filter(shi17_l, !is.na(SAL) & SAL > 0 & SAL < 40 & starred=="star") %>% arrange(SAL)%>% mutate(Pres=ifelse(coCatch > 0,1,0))
#AVAILABLE HABITAT: should be for both cohorts!!!
shin17coSAL <-filter(shin17co, !is.na(SAL)& SAL > 0 & SAL < 40 & starred=="star")%>% arrange(SAL)
SurveyCDF = cumsum(shin17coSAL$SAL)
SUMSAL<-sum(shin17coSAL$SAL)
shin17coSAL <- mutate(shin17coSAL, SurveyCDFFinal=SurveyCDF/SUMSAL) #this is the AVAILABLE HABITAT over ALL YEARS in Shinnecock
#OCCUPIED HABITAT: cohort 1
SAL <- co17_1SAL$SAL
yh=sum(co17_1SAL$coCatch)
ybar_strata=mean(co17_1SAL$coCatch)
#create and plot CDF for occupied habitat
co17_1SAL <-mutate(co17_1SAL, OccupiedSAL =(coCatch/ybar_strata)*as.numeric(Pres)*SAL)%>%
mutate(OccupiedCDF =cumsum(OccupiedSAL))
SUMOccSAL <-sum(co17_1SAL$OccupiedSAL)
co17_1SAL <-mutate(co17_1SAL, OccCDF_final = OccupiedCDF/SUMOccSAL)
#OCCUPIED HABITAT: cohort 2
SAL <- co17_2SAL$SAL
yh=sum(co17_2SAL$coCatch)
ybar_strata=mean(co17_2SAL$coCatch)