-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostStratificationAssessment.Rmd
2157 lines (1714 loc) · 88.4 KB
/
PostStratificationAssessment.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: "Oyster Survey Effort Reallocation and Restratification"
author: "Robert Isdell"
date: "April 16, 2019"
output:
html_document: default
word_document: default
pdf_document: default
---
```{r set-options, echo=FALSE, cache=FALSE}
options(width = 175)
knitr::opts_chunk$set(warning = FALSE, message = FALSE, echo = FALSE, fig.align = 'center')
```
```{r, echo=FALSE, warning=FALSE}
# Install BIOSurvey2 package
# devtools::install("BIOSurvey2/BIOSurvey2")
# Load in required packages
pacman::p_load(tidyverse, readxl, BIOSurvey2, cluster, fpc, random, knitr)
# Read in data by year
site.info <- read_xlsx("Data/PTData_2006_2018.xlsx",1)
site.info <- site.info[1:199,] #trim off extra from the end
dat.2006 <- read_xlsx("Data/PTData_2006_2018.xlsx",2)
dat.2007 <- read_xlsx("Data/PTData_2006_2018.xlsx",3)
dat.2008 <- read_xlsx("Data/PTData_2006_2018.xlsx",4)
dat.2009 <- read_xlsx("Data/PTData_2006_2018.xlsx",5)
dat.2010 <- read_xlsx("Data/PTData_2006_2018.xlsx",6)
dat.2011 <- read_xlsx("Data/PTData_2006_2018.xlsx",7)
dat.2012 <- read_xlsx("Data/PTData_2006_2018.xlsx",8)
dat.2013 <- read_xlsx("Data/PTData_2006_2018.xlsx",9)
dat.2014 <- read_xlsx("Data/PTData_2006_2018.xlsx",10)
dat.2015 <- read_xlsx("Data/PTData_2006_2018.xlsx",11)
dat.2016 <- read_xlsx("Data/PTData_2006_2018.xlsx",12)
dat.2017 <- read_xlsx("Data/PTData_2006_2018.xlsx",13)
dat.2018 <- read_xlsx("Data/PTData_2006_2018.xlsx",14)
dat.2019 <- read_xlsx("Data/PTData_2006_2019.xlsx",16)
# Add year variable to each dataset
dat.2006$Year <- 2006
dat.2007$Year <- 2007
dat.2008$Year <- 2008
dat.2009$Year <- 2009
dat.2010$Year <- 2010
dat.2011$Year <- 2011
dat.2012$Year <- 2012
dat.2013$Year <- 2013
dat.2014$Year <- 2014
dat.2015$Year <- 2015
dat.2016$Year <- 2016
dat.2017$Year <- 2017
dat.2018$Year <- 2018
# merge all of the datasets into single dataframe
dat.years <- rbind(dat.2006, dat.2007, dat.2008, dat.2009, dat.2010,
dat.2011, dat.2012, dat.2013, dat.2014, dat.2015,
dat.2016, dat.2017, dat.2018)
# Correct and merge various river names for continuity
dat.years$River_name[dat.years$River_name == "Mobjack"] <- "Mobjack Bay"
dat.years$River_name[dat.years$River_name == "Great Wicomico"] <- "Great Wicomico River"
dat.years$River_name[dat.years$River_name == "Piankatank"] <- "Piankatank River"
dat.years$River_name[dat.years$River_name == "Rappahannock"] <- "Rappahannock River"
dat.years$River_name[dat.years$River_name == "Elizabeth River Western Branch"] <- "Elizabeth River"
dat.years$River_name[dat.years$River_name == "Corrotoman River"] <- "Rappahannock River"
dat.years$River_name[dat.years$River_name == "Elizabeth River"] <- "James River"
dat.years$River_name[dat.years$River_name == "Lafayette River"] <- "James River"
dat.years$River_name[dat.years$River_name == "East River"] <- "York River"
dat.years$River_name[dat.years$River_name == "Mobjack Bay"] <- "York River"
dat.years$River_name[dat.years$River_name == "Pocomoke Sound"] <- "Tangier Sound"
dat.years$Station_ID[dat.years$Station_ID == 390] <- 395
dat.years$Station_ID[dat.years$Station_ID == 1131] <- 1128
# Select only relevant fields from site.info
site.acres <- site.info %>%
select(`Station ID`, `Management Subgroups`, Total, n_years)
# Correct the site IDs
site.acres$`Station ID`[site.acres$`Station ID` == "1128 (1131)"] <- 1128
site.acres$`Station ID`[site.acres$`Station ID` == "390 (395)"] <- 395
# Convert Station ID field to numeric
site.acres$`Station ID` <- as.numeric(site.acres$`Station ID`)
# Join the site.acres dataset to the full dataset
dat.full <- left_join(dat.years, site.acres, by = c("Station_ID" = "Station ID"))
## Select stations with 5+ years of data
dat.full <- dat.full %>%
filter(n_years > 4)
```
The following document summarizes our efforts-to-date to restratify and reallocate effort within the annual oyster survey. This work has relied heavily on the R package **BioSurvey2** written by the late Stephen Smith and distributed by SCeMFIS ([http://www.scemfis.org/](http://www.scemfis.org/)).
# Introduction
After the initial meeting with Roger, Missy, Rob, Jim, Andrew, Vernon, John, and myself, we decided to make the following changes, which are reflected in this version of the document:
1) Only use reefs that have at least 5 years of data in the analysis
2) When available, use the predefined management subgroups within a given waterbody
3) If all reefs within a given waterbody do not fall into a predefined management subgroup, use the clustering and restratification method previously demonstrate.
Reefs that are <5 years old should continue to be sampled at their current levels until enough data has been collected to include in the analysis.
I am willing to make all data, code, and analyes publicly available via GitHub. Alternatively, the work could be hosted on WM Scholarworks where it would be assigned a DOI and useage statistics would be tracked.
# James River
The James River did not have predefined management subgroups, and so was stratified using the k-means method.
```{r}
# Subset the James River data
all.james.dat <- dat.full %>%
filter(River_name == "James River" & Station_ID != 16) # Reef 16 now defunct, split into 410 and 411
# Create a vector of unique station IDs within the James
all.james.stations <- unique(all.james.dat$Station_ID)
# Make Station ID the same for both datasets
colnames(site.info)[2] <- "Station_ID"
colnames(site.acres)[1] <- "Station_ID"
# Subset the James River site info
all.james.site.info <- site.acres %>%
filter(Station_ID %in% all.james.stations) %>%
arrange(Station_ID)
all.james.site.info$Station_ID <- as.numeric(all.james.site.info$Station_ID)
# Multiply counts by subsample factor
all.james.dat$Lives <- rowSums(all.james.dat[,16:52]*all.james.dat$Subsample_Factor)
all.james.dat$Markets <- rowSums(all.james.dat[,31:52]*all.james.dat$Subsample_Factor)
all.james.dat$Spat1 <- rowSums(all.james.dat[,55:67]*all.james.dat$Subsample_Factor)
# Select only the variables needed for analysis
all.james.dat <- all.james.dat %>% select(Station_ID, Sample_ID, Lives, Markets, Spat1)
```
## Set up strata and summarize
The following table provides the number of grabs from each reef from 2006-2018 (Observed) and the number of grabs over that same time frame, given the same total effort within the James, that would have best addressed total catch and the variability of the catch relative to the area of the reef.
```{r}
# Specify Strata and total number of possible samples (NH = Reef acreage * years sampled * 4046.86 m^2/acre)
all.james.strata <- data.frame("Strata" = all.james.site.info$Station_ID, NH = all.james.site.info$Total*4046.86)
# Check current allocation using BIOSurvey2::Stratify()
all.james.numbers <- Stratify(all.james.dat, all.james.strata, "Station_ID", species = "Lives")
kable(summary(all.james.numbers, effic = TRUE, nopt = TRUE)$n.opt[,1:3])
```
```{r}
# Store the summary output
all.james.numbers.summary <- summary(all.james.numbers, effic = TRUE, nopt = TRUE)
# Extract the optimal number of samples per strata
all.james.numbers.nopt <- all.james.numbers.summary$n.opt
# Join the optimal sample numbers to the site info
all.james.reallocation <- left_join(all.james.numbers.nopt, site.info, by = c("Strata" = as.character("Station_ID")))
# Specify the column names
names(site.acres) <- c("Station_ID", "Subgroup", "Acreage", "Years")
# Set Station ID to numeric
site.acres$Station_ID <- as.numeric(site.acres$Station_ID)
# Join James counts and site info
all.james.full <- left_join(all.james.dat, site.acres, by = "Station_ID")
# Create a dataset of the mean and variance of Live counts, and reef acreage
all.james.mva <- all.james.full %>%
group_by(Station_ID) %>%
summarise(Mean = mean(Lives), Variance = var(Lives), Acreage = mean(Acreage))
row.names(all.james.mva) <- all.james.mva$Station_ID
# Scale and center the mva to prep for clustering
all.james.mva <- scale(all.james.mva)
```
## Clustering
One way to address the effort reallocation problem is to rexamine the current stratification, and see if we can reduce the number of strata while still getting accurate estimates of the mean and variation of the population within each strata. To do this, we can see if certain reefs cluster together based on similar features. I used the mean and variance of live oysters within each reef, and the acreage of each reef as factors for a K-means clustering analysis.
```{r, fig.align='center', fig.height=5, fig.width=7}
# Identify best number of k-means cluster groups by finding inflection point
# Iterate 100 times to avoid chances of spurious result
all.james.wss <- matrix(nrow = 100, ncol = 15)
for(j in 1:100) {
wss <- (nrow(all.james.mva[,2:4])-1)*sum(apply(all.james.mva[,2:4], 2, var))
for (i in 1:15) all.james.wss[j,i] <- sum(kmeans(all.james.mva[,2:4], centers = i, iter.max = 1000, nstart = 1)$withinss)
}
all.james.wss <- as.data.frame(all.james.wss)
all.james.wss.mean <- colMeans(all.james.wss)
# Plot the reduction in within-group sums-of-squares per cluster
plot(1:15, all.james.wss.mean, type = "b", xlab = "Number of Clusters",
ylab = "Within groups SS")
#################################################################
k <- 4 # number of clusters
#################################################################
```
In the above plot, you can see that the inflection point in the line indicates that based on these features, the 36 reefs within the James could be most confidently grouped into 4 clusters.
```{r, fig.align='center', fig.height=5}
# Check 1000 seeds to identify which provides the best clustering
best.seed <- matrix(nrow = 1000, ncol = 2)
for (i in 1:1000){
set.seed(i)
fit <- kmeans(all.james.mva[,2:4], k, iter.max = 1000, nstart = 1, algorithm = "Hartigan-Wong")
best.seed[i,1] <- i
best.seed[i,2] <- fit$betweenss/fit$totss*100
}
best.seed <- as.data.frame(best.seed)
seed <- best.seed$V1[best.seed$V2 == max(best.seed$V2)][1]
set.seed(seed)
# Cluster the reefs
fit.all.james <- kmeans(all.james.mva[,2:4], k, iter.max = 1000, nstart = 1, algorithm = "Hartigan-Wong")
# Create a dataset of the Station IDs and cluster groups
all.james.cluster.groups <- data.frame("Station_ID" = as.numeric(rownames(all.james.mva)))
all.james.cluster.groups$kmeans <- fit.all.james$cluster
# Generate a cluster plot of the results
clusplot(all.james.mva[,2:4], all.james.cluster.groups$kmeans, color = T, shade = T, labels = 2, lines = 0)
```
This plot shows how well those reefs cluster.
```{r}
# Show what cluster group each reef will be in
a2 <- fit.all.james$cluster
suggested.strata.all.james <- data_frame("Code" = as.numeric(names(a2)), "Cluster" = a2)
site.name <- dat.full %>%
select(Station_ID, Full_station_name) %>%
mutate("Code" = Station_ID) %>%
select(Code, Full_station_name)
site.name <- site.name[!duplicated(site.name),]
suggested.strata.all.james <- left_join(suggested.strata.all.james, site.name, by = "Code") %>%
arrange(Cluster)
kable(suggested.strata.all.james, caption = "Suggested Strata")
suggested.strata.all.james$Subgroup <- paste0(
"JR", suggested.strata.all.james$Cluster)
# set station ID to character
suggested.strata.all.james$Code <- as.character(suggested.strata.all.james$Code)
```
## Add new suggested groups and check allocation
```{r}
# Create new dataset of cluster groups as strata
names(all.james.site.info) <- c("Station_ID", "Subgroup", "NH")
all.james.site.info$NH <- all.james.site.info$NH*4046.86 #convert acres to square meters
all.james.site.info$Station_ID <- as.character(all.james.site.info$Station_ID)
all.james.site.info2 <- data.frame(all.james.site.info)
all.james.domain <- data_frame(NH = all.james.site.info$NH,
"kmeans" = all.james.cluster.groups$kmeans)
#Summarize NH by new strata (aka domains)
all.james.domain.info <- all.james.domain %>%
group_by(kmeans) %>%
summarise(NH = mean(NH))
all.james.domain.info2 <- data.frame(all.james.domain.info)
all.james.dat2 <- left_join(all.james.dat, all.james.cluster.groups)
all.james.dat2 <- arrange(all.james.dat2, Station_ID)
all.james.dat2$Station_ID <- as.character(all.james.dat2$Station_ID)
all.james.dat3 <- data.frame(all.james.dat2)
all.james.site.info2 <- data.frame(all.james.site.info)
# length(all.james.site.info2[, "Station_ID"])
# length(SFA29.Geophysical[, "GEOPHYS_ID"])
```
How well is effort allocated based on current strata?
```{r}
# How well is effort allocated based on current strata?
names(all.james.domain.info2) <- c("Strata","NH")
all.james.numbers2 <- Stratify(all.james.dat3, all.james.domain.info2, "kmeans", species = "Lives")
kable(summary(all.james.numbers2, effic = T, nopt = T)$n.opt)
```
Overall, effort allocation is inefficient within the new strata.
## Estimate the population mean and variance given the new strata and allocation
### Simulate the data
We can resample the data at the proportions suggested by BIOSurvey2, sequentially reduce the effort by 10% across all strata, and then plot the results. We are effectively creating 100 datasets per proportional effort reduction for a total of 1000 datasets. Each one is the result of resampling (with replacement). Then, we can plot the mean of each strata within the datasets for each proportional effort reduction, and get a sense of what the spread in the data would look like. By plotting the 95% CI of the data, a wedge forms as the estimates become less accurate with increasing proportional reduction. Identifying the point where our overall interpretation of the data remains more-or-less the same while maximixing effort reduction should provide support for how much we could reduce effort within a given waterbody.
```{r}
# Create a blank dataframe for the mean, SD, and variance of each cluster group
# sampled (with replacement) 100 times at the optimal level suggested by BIOSurvey2
varest.boot.dat.all.james <- matrix(ncol = 4, nrow = k*100)
colnames(varest.boot.dat.all.james) <- c("kmeans", "MEAN", "SD", "VAR")
varest.boot.dat.all.james <- as.data.frame(varest.boot.dat.all.james)
# Cummulative number of samples for each cluster
n.opt.test.cumm.all.james <- cumsum(summary(all.james.numbers2, effic = T, nopt = T)$n.opt$Optimal[1:k])
# Optimal number of samples per cluster
n.opt.test.all.james <- summary(all.james.numbers2, effic = T, nopt = T)$n.opt$Optimal[1:k]
# Identify the starting position for each cluster in the dataframe
start.pos.all.james <- c(1, n.opt.test.cumm.all.james[1:k-1]+1)
# Blank dataframe of the BIOSurvey2::Stratify() output for each resampled dataset
nopt.boot.dat.all.james <- matrix(ncol = 7, nrow = k*100)
colnames(nopt.boot.dat.all.james) <- c("Strata", "Observed", "Optimal", "Perc.Increase.Var.Opt",
"Compromise", "Perc.Increase.Var.comp", "run")
nopt.boot.dat.all.james <- as.data.frame(nopt.boot.dat.all.james)
# Start and end position for each cluster summary in varest.boot.dat.all.james
j.start.all.james <- seq(1,k*100, by = k)
j.end.all.james <- seq(k, k*100, by = k)
# Estimating the mean and variance of 100 resampled datasets at the original effort level
for(j in 1:100) {
sub.dat1 <- matrix(ncol=6,nrow = n.opt.test.cumm.all.james[k])
colnames(sub.dat1) <- names(all.james.dat3)
sub.dat1 <- as.data.frame(sub.dat1)
for(i in 1:k) {
temp <- all.james.dat3 %>%
filter(kmeans == i)
rand.n <- sample(1:nrow(temp),n.opt.test.all.james[i], replace = T)
temp2 <- temp[rand.n,]
sub.dat1[start.pos.all.james[i]:n.opt.test.cumm.all.james[i],] <- temp2
}
sub.summary <- sub.dat1 %>%
group_by(kmeans) %>%
summarise("MEAN" = mean(Lives), "SD" = sd(Lives), "VAR" = var(Lives))
varest.boot.dat.all.james[j.start.all.james[j]:j.end.all.james[j],] <- sub.summary
}
#kable(head(varest.boot.dat.all.james, 10), caption = "Population mean for each strata with a random resampling with new effort")
```
```{r, warning=FALSE}
# Same as section above, except with sequential 10% reduction in effort from 0% to 90% reduction
all.james.sample <- n.opt.test.all.james
all.james.sample.cumm <- n.opt.test.cumm.all.james
all.james.plot.dat <- matrix(ncol = 3, nrow = k*1000)
colnames(all.james.plot.dat) <- c("ER","KMEAN", "MEAN")
all.james.plot.dat <- as.data.frame(all.james.plot.dat)
all.james.plot.dat$ER <- rep(seq(0, 0.9, 0.1), each = k*100)
all.james.plot.dat$KMEAN <- rep(1:k, 1000)
all.james.plot.dat$KMEAN <- factor(all.james.plot.dat$KMEAN)
h.start <- seq(1, 1+(k*1000 - k*100), by = k*100)
h.end <- seq(k*100, k*1000, by = k*100)
all.james.sub.sample <- all.james.sample/sum(all.james.sample)
all.james.sub.cumm <- round(seq(1, 0.1, by = -0.1)*all.james.sample.cumm[k], 0)
j.start <- seq(1,k*100, by = k)
j.end <- seq(k, k*100, by = k)
for(h in 1:10){
varest.boot.dat <- matrix(ncol = 4, nrow = k*100)
colnames(varest.boot.dat) <- c("kmeans", "MEAN", "SD", "VAR")
varest.boot.dat <- as.data.frame(varest.boot.dat)
sample.cumm <- cumsum(round(all.james.sub.sample*all.james.sub.cumm[h],0))
start.pos <- c(1, sample.cumm[1:k-1]+1)
for(j in 1:100) {
sub.dat1 <- matrix(ncol=6, nrow = all.james.sub.cumm[h])
colnames(sub.dat1) <- names(all.james.dat3)
sub.dat1 <- as.data.frame(sub.dat1)
for(i in 1:k) {
temp <- all.james.dat3 %>%
filter(kmeans == i)
rand.n <- sample(1:nrow(temp),round(all.james.sub.sample[i]*all.james.sub.cumm[h],0), replace = T)
temp2 <- temp[rand.n,]
sub.dat1[start.pos[i]:sample.cumm[i],] <- temp2
}
sub.summary <- sub.dat1 %>%
group_by(kmeans) %>%
summarise("MEAN" = mean(Lives), "SD" = sd(Lives), "VAR" = var(Lives))
varest.boot.dat[j.start[j]:j.end[j],] <- sub.summary
}
all.james.plot.dat$MEAN[h.start[h]:h.end[h]] <- varest.boot.dat$MEAN
}
#kable(head(all.james.plot.dat, 10), caption = "Population mean for each strata with a random resampling with new effort")
```
```{r}
# Create a function to calculate the mean and CI of the mean
mean_ci <- function(x, mult = 1) {
x <- na.omit(x)
SD <- mult * sd(x)
MEAN <- mean(x)
data.frame(y = MEAN, ymin = MEAN - qt(1 - (0.05 / 2), 99) * SD, ymax = MEAN + qt(1 - (0.05 / 2), 99) * SD)
}
# Plot the change in quality of the data with each sequential reduction in effort
ggplot(all.james.plot.dat, aes(y = MEAN, x = ER, colour = KMEAN, fill = KMEAN)) +
geom_smooth(se = FALSE) +
geom_point(size = 2) +
geom_abline(intercept = c(148,163,366,415,30,36, 27, 32),
size = 1, linetype = "dashed", alpha = 0.7) +
stat_summary(fun.data = mean_ci, geom = "ribbon", alpha = 0.5) +
theme_light() +
scale_color_brewer(palette = "Paired") +
scale_fill_brewer(palette = "Paired") +
theme(axis.text = element_text(size = 15, colour = "black")) +
theme(axis.title = element_text(size = 15)) +
theme(legend.text = element_text(size = 15)) +
theme(legend.title = element_text(size = 15)) +
labs(y = expression(paste("Mean Oysters m"^-2 %+-% "CI"))) +
labs(x = "Proportional Effort Reduction")
```
In this plot, you can see that the mean and CI remain relatively constant through a 40% reduction in effort.
```{r}
# Create a function to calculate the coefficient of variation (CV)
cv <- function(x) {
m <- mean(x)
v <- sd(x)
cfvar <- v/m
return(cfvar)
}
# Plot the change in quality of the data using the CV with each sequential reduction in effort
all.james.plot.dat.cv <- all.james.plot.dat %>%
group_by(KMEAN, ER) %>%
summarise(CV = cv(MEAN))
## Create a sequence of the no effort reduction CV for comparison
all.james.plot.dat.cv$PctCV <- all.james.plot.dat.cv$CV / rep(pull(all.james.plot.dat.cv[c(seq(1,((k-1)*10)+1,10)),3]), each = 10) * 100 - 100
ggplot(all.james.plot.dat.cv, aes(y = PctCV, x = ER, color = KMEAN, linetype = KMEAN)) +
geom_smooth(size = 1.2, se = F) +
geom_abline(intercept = 40, slope = 0, size = 1.2, linetype = "dashed") +
theme_light() +
scale_color_brewer(palette = "Paired") +
theme(axis.text = element_text(size = 15, colour = "black")) +
theme(axis.title = element_text(size = 15)) +
theme(legend.text = element_text(size = 15)) +
theme(legend.title = element_text(size = 15)) +
labs(y = "Relative Coefficient of Variation") +
labs(x = "Proportional Effort Reduction")
```
This plot shows how the coefficient of variation (CV) changes with each proportion reduction in effort relative to the starting CV.
```{r}
# Population summary for each cluster
strat.kmeans.summary.all.james <- all.james.dat3 %>%
group_by(kmeans) %>%
summarise(pMEAN = mean(Lives), pSD = sd(Lives), pVAR = var(Lives), nOBS = n()) %>%
mutate(lower.ci.mean = pMEAN - qt(1 - (0.05 / 2), nOBS) * (pSD/sqrt(nOBS)),
upper.ci.mean = pMEAN + qt(1 - (0.05 / 2), nOBS) * (pSD/sqrt(nOBS)))
kable(strat.kmeans.summary.all.james, caption = "Population Stats based on all data")
all.james.ci.summary <- all.james.plot.dat %>%
group_by(ER, KMEAN) %>%
summarise(m.MEAN = mean(MEAN), sd.MEAN = sd(MEAN)) %>%
mutate(lower.ci.mean = m.MEAN - qt(1 - (0.05 / 2), 99) * sd.MEAN,
upper.ci.mean = m.MEAN + qt(1 - (0.05 / 2), 99) * sd.MEAN) %>%
arrange(KMEAN, ER)
kable(all.james.ci.summary, caption = "Mean of the means")
```
Based on the information above, it appears that the total effort in Burwell Bay could be reduced by 40% and still obtain similar population parameters. Each year, total effort (i.e., number of grabs) in each cluster would be as follows:
```{r}
n.reefs <- suggested.strata.all.james %>%
group_by(Cluster) %>%
summarise(N = n())
n.suggested <- round(all.james.sub.sample*all.james.sub.cumm[5]/13,0)
effort <- 1:k
for (i in 1:k) {
effort[i] <- ifelse(n.suggested[i] < n.reefs$N[i], n.reefs$N[i], n.suggested[i])
}
kable(data_frame("Cluster" = 1:k, "Future Effort" = effort,
"Current Effort" = round(summary(all.james.numbers2, effic = TRUE, nopt = TRUE)$n.opt[,2][1:4]/13,0)))
```
This would mean a total of 310 grabs in the James Bay compared to 518 in previous years.
# Lynnhaven River
Lynnhaven remains unchanged from the previous iteration. If you all decide to continue, then there's no need to restratify given how few reefs are down there and that it's a full day either way. However, if you do continue sampling, BIOSurvey2 does recommend reallocating effort.
```{r, echo=FALSE}
lynnhaven.dat <- dat.full %>%
filter(River_name == "Lynnhaven")
lynnhaven.stations <- unique(lynnhaven.dat$Station_ID)
colnames(site.info)[2] <- "Station_ID"
colnames(site.acres)[1] <- "Station_ID"
lynnhaven.site.info <- site.acres %>%
filter(Station_ID %in% lynnhaven.stations) %>%
arrange(Station_ID)
lynnhaven.site.info$Station_ID <- as.numeric(lynnhaven.site.info$Station_ID)
lynnhaven.dat$Lives <- rowSums(lynnhaven.dat[,16:52]*lynnhaven.dat$Subsample_Factor)
lynnhaven.dat$Markets <- rowSums(lynnhaven.dat[,31:52]*lynnhaven.dat$Subsample_Factor)
lynnhaven.dat$Spat1 <- rowSums(lynnhaven.dat[,55:67]*lynnhaven.dat$Subsample_Factor)
lynnhaven.dat <- lynnhaven.dat %>% select(Station_ID, Sample_ID, Lives, Markets, Spat1)
```
## Set up strata and summarize
```{r, echo=FALSE}
lynnhaven.strata <- data_frame("Strata" = lynnhaven.site.info$Station_ID, NH = lynnhaven.site.info$Acreage*4046.86)
lynnhaven.numbers <- Stratify(lynnhaven.dat, lynnhaven.strata, "Station_ID", species = "Lives")
kable(summary(lynnhaven.numbers, effic = TRUE, nopt = TRUE)$n.opt[,1:3])
```
Each year, total effort (i.e., number of grabs) in each reef would be as follows:
```{r, echo=FALSE}
lynnhaven.summary <- summary(lynnhaven.numbers, effic = TRUE, nopt = TRUE)$n.opt[,1:3]
lynnhaven.summary <- mutate(lynnhaven.summary, `Annual Effort` = ifelse(Optimal/13 < 1, 1, ceiling(Optimal/13)))
kable(lynnhaven.summary[1:12,c(1,4)])
```
This would be a total of 78 grabs per year in the Lynnhaven.
# Rappahannock River
The Rappahannock has predefined management subgroups, thereby making any cluster analyses unneccesary. The results shown here demonstrate how the effort should be allocated among management subgroups.
```{r}
Rappahannock.dat <- dat.full %>%
filter(River_name == "Rappahannock River")
Rappahannock.stations <- unique(Rappahannock.dat$Station_ID)
colnames(site.info)[2] <- "Station_ID"
colnames(site.acres)[1] <- "Station_ID"
Rappahannock.site.info <- site.acres %>%
filter(Station_ID %in% Rappahannock.stations) %>%
arrange(Station_ID)
Rappahannock.site.info$Station_ID <- as.numeric(Rappahannock.site.info$Station_ID)
Rappahannock.site.info2 <- Rappahannock.site.info %>%
group_by(Subgroup) %>%
summarise(Acreage2 = sum(Acreage))
Rappahannock.dat$Lives <- rowSums(Rappahannock.dat[,16:52]*Rappahannock.dat$Subsample_Factor)
Rappahannock.dat$Markets <- rowSums(Rappahannock.dat[,31:52]*Rappahannock.dat$Subsample_Factor)
Rappahannock.dat$Spat1 <- rowSums(Rappahannock.dat[,55:67]*Rappahannock.dat$Subsample_Factor)
Rappahannock.dat <- Rappahannock.dat %>% select(Station_ID, Sample_ID, Lives, Markets, Spat1)
Rappahannock.dat <- left_join(Rappahannock.dat, Rappahannock.site.info)
```
## Set up strata and summarize
```{r}
Rappahannock.sites <- site.info %>%
filter(Station_ID %in% Rappahannock.stations) %>%
select(Station_ID, Site, `Management Subgroups`) %>%
arrange(`Management Subgroups`)
kable(Rappahannock.sites, caption = "Rappahannock Reef and Management Subgroup")
```
```{r}
Rappahannock.strata <- data_frame("Strata" = Rappahannock.site.info2$Subgroup, NH = Rappahannock.site.info2$Acreage2*4046.86)
Rappahannock.numbers <- Stratify(Rappahannock.dat, Rappahannock.strata, "Subgroup", species = "Lives")
kable(summary(Rappahannock.numbers, effic = TRUE, nopt = TRUE)$n.opt[,1:3], caption = "Observed vs. Optimal Effort")
```
```{r}
Rappahannock.numbers.summary <- summary(Rappahannock.numbers, effic = TRUE, nopt = TRUE)
Rappahannock.numbers.nopt <- Rappahannock.numbers.summary$n.opt
Rappahannock.reallocation <- left_join(Rappahannock.numbers.nopt, site.info, by = c("Strata" = as.character("Management Subgroups")))
site.acres$Station_ID <- as.numeric(site.acres$Station_ID)
Rappahannock.full <- left_join(Rappahannock.dat, site.acres, by = "Station_ID")
```
## Estimate the population mean and variance given the new strata and allocation
```{r}
k <- length(unique(Rappahannock.dat$Subgroup))
Rappahannock.dat3 <- Rappahannock.dat %>%
select(1:6)
varest.boot.dat.Rappahannock <- matrix(ncol = 4, nrow = k*100)
colnames(varest.boot.dat.Rappahannock) <- c("Subgroup", "MEAN", "SD", "VAR")
varest.boot.dat.Rappahannock <- as.data.frame(varest.boot.dat.Rappahannock)
n.opt.test.cumm.Rappahannock <- cumsum(summary(Rappahannock.numbers, effic = T, nopt = T)$n.opt$Optimal[1:k])
n.opt.test.Rappahannock <- summary(Rappahannock.numbers, effic = T, nopt = T)$n.opt$Optimal[1:k]
start.pos.Rappahannock <- c(1, n.opt.test.cumm.Rappahannock[1:k-1]+1)
nopt.boot.dat.Rappahannock <- matrix(ncol = 7, nrow = k*100)
colnames(nopt.boot.dat.Rappahannock) <- c("Strata", "Observed", "Optimal", "Perc.Increase.Var.Opt",
"Compromise", "Perc.Increase.Var.comp", "run")
nopt.boot.dat.Rappahannock <- as.data.frame(nopt.boot.dat.Rappahannock)
j.start.Rappahannock <- seq(1,k*100, by = k)
j.end.Rappahannock <- seq(k, k*100, by = k)
subgroup.Rappahannock <- sort(unique(Rappahannock.dat3$Subgroup))
for(j in 1:100) {
sub.dat1 <- matrix(ncol=6,nrow = n.opt.test.cumm.Rappahannock[k])
colnames(sub.dat1) <- names(Rappahannock.dat3)
sub.dat1 <- as.data.frame(sub.dat1)
for(i in 1:k) {
temp <- Rappahannock.dat3 %>%
filter(Subgroup == subgroup.Rappahannock[i])
rand.n <- sample(1:nrow(temp),n.opt.test.Rappahannock[i], replace = T)
temp2 <- temp[rand.n,]
sub.dat1[start.pos.Rappahannock[i]:n.opt.test.cumm.Rappahannock[i],] <- temp2
}
sub.summary <- sub.dat1 %>%
group_by(Subgroup) %>%
summarise("MEAN" = mean(Lives), "SD" = sd(Lives), "VAR" = var(Lives))
varest.boot.dat.Rappahannock[j.start.Rappahannock[j]:j.end.Rappahannock[j],] <- sub.summary
}
```
```{r}
Rappahannock.sample <- n.opt.test.Rappahannock
Rappahannock.sample.cumm <- n.opt.test.cumm.Rappahannock
Rappahannock.plot.dat <- matrix(ncol = 3, nrow = k*1000)
colnames(Rappahannock.plot.dat) <- c("ER","Subgroup", "MEAN")
Rappahannock.plot.dat <- as.data.frame(Rappahannock.plot.dat)
Rappahannock.plot.dat$ER <- rep(seq(0, 0.9, 0.1), each = k*100)
Rappahannock.plot.dat$Subgroup <- rep(subgroup.Rappahannock, 1000)
Rappahannock.plot.dat$Subgroup <- factor(Rappahannock.plot.dat$Subgroup)
h.start <- seq(1, 1+(k*1000 - k*100), by = k*100)
h.end <- seq(k*100, k*1000, by = k*100)
Rappahannock.sub.sample <- Rappahannock.sample/sum(Rappahannock.sample)
Rappahannock.sub.cumm <- round(seq(1, 0.1, by = -0.1)*Rappahannock.sample.cumm[k], 0)
j.start <- seq(1,k*100, by = k)
j.end <- seq(k, k*100, by = k)
for(h in 1:10){
varest.boot.dat <- matrix(ncol = 4, nrow = k*100)
colnames(varest.boot.dat) <- c("Subgroup", "MEAN", "SD", "VAR")
varest.boot.dat <- as.data.frame(varest.boot.dat)
sample.cumm <- cumsum(round(Rappahannock.sub.sample*Rappahannock.sub.cumm[h],0))
start.pos <- c(1, sample.cumm[1:k-1]+1)
for(j in 1:100) {
sub.dat1 <- matrix(ncol=6, nrow = Rappahannock.sub.cumm[h])
colnames(sub.dat1) <- names(Rappahannock.dat3)
sub.dat1 <- as.data.frame(sub.dat1)
for(i in 1:k) {
temp <- Rappahannock.dat3 %>%
filter(Subgroup == subgroup.Rappahannock[i])
rand.n <- sample(1:nrow(temp),round(Rappahannock.sub.sample[i]*Rappahannock.sub.cumm[h],0), replace = T)
temp2 <- temp[rand.n,]
sub.dat1[start.pos[i]:sample.cumm[i],] <- temp2
}
sub.summary <- sub.dat1 %>%
group_by(Subgroup) %>%
summarise("MEAN" = mean(Lives), "SD" = sd(Lives), "VAR" = var(Lives))
varest.boot.dat[j.start[j]:j.end[j],] <- sub.summary
}
Rappahannock.plot.dat$MEAN[h.start[h]:h.end[h]] <- varest.boot.dat$MEAN
}
```
```{r}
ggplot(Rappahannock.plot.dat, aes(y = MEAN, x = ER, colour = Subgroup, fill = Subgroup)) +
geom_smooth(se = FALSE) +
geom_point(size = 2) +
stat_summary(fun.data = mean_ci, geom = "ribbon", alpha = 0.5) +
theme_light() +
scale_color_brewer(palette = "Paired") +
scale_fill_brewer(palette = "Paired") +
theme(axis.text = element_text(size = 15, colour = "black")) +
theme(axis.title = element_text(size = 15)) +
theme(legend.text = element_text(size = 15)) +
theme(legend.title = element_text(size = 15)) +
labs(y = expression(paste("Mean Oysters m"^-2 %+-% "CI"))) +
labs(x = "Proportional Effort Reduction")
# Plot the change in quality of the data using the CV with each sequential reduction in effort
Rappahannock.plot.dat.cv <- Rappahannock.plot.dat %>%
group_by(Subgroup, ER) %>%
summarise(CV = cv(MEAN))
## Create a sequence of the no effort reduction CV for comparison
Rappahannock.plot.dat.cv$PctCV <- Rappahannock.plot.dat.cv$CV / rep(pull(Rappahannock.plot.dat.cv[c(seq(1,((k-1)*10)+1,10)),3]), each = 10) * 100 - 100
ggplot(Rappahannock.plot.dat.cv, aes(y = PctCV, x = ER, color = Subgroup, linetype = Subgroup)) +
geom_line(size = 1.2) +
geom_abline(intercept = 40, slope = 0, size = 1.2, linetype = "dashed") +
theme_light() +
scale_color_brewer(palette = "Paired") +
theme(axis.text = element_text(size = 15, colour = "black")) +
theme(axis.title = element_text(size = 15)) +
theme(legend.text = element_text(size = 15)) +
theme(legend.title = element_text(size = 15)) +
labs(y = "Relative Coefficient of Variation") +
labs(x = "Proportional Effort Reduction")
```
The current subgroups are *very* messy in terms of overlapping population estimates. However, it does still appear that a 40% reduction is possible in the Rappahannock.
```{r}
strat.kmeans.summary.Rappahannock <- Rappahannock.dat3 %>%
group_by(Subgroup) %>%
summarise(pMEAN = mean(Lives), pSD = sd(Lives), pVAR = var(Lives), nOBS = n()) %>%
mutate(lower.ci.mean = pMEAN - qt(1 - (0.05 / 2), nOBS) * (pSD/sqrt(nOBS)),
upper.ci.mean = pMEAN + qt(1 - (0.05 / 2), nOBS) * (pSD/sqrt(nOBS)))
kable(strat.kmeans.summary.Rappahannock, caption = "Population Stats based on all data")
Rappahannock.ci.summary <- Rappahannock.plot.dat %>%
group_by(ER, Subgroup) %>%
summarise(m.MEAN = mean(MEAN), sd.MEAN = sd(MEAN)) %>%
mutate(lower.ci.mean = m.MEAN - qt(1 - (0.05 / 2), 99) * sd.MEAN,
upper.ci.mean = m.MEAN + qt(1 - (0.05 / 2), 99) * sd.MEAN)
# kable(Rappahannock.ci.summary, caption = "Mean of the means")
```
Each year, total effort (i.e., number of grabs) in each cluster would be as follows:
```{r}
kable(data_frame("Subgroup" = subgroup.Rappahannock, "Future Effort" = round(Rappahannock.sub.sample*Rappahannock.sub.cumm[5]/13,0),
"Current Effort" = round(summary(Rappahannock.numbers, effic = TRUE, nopt = TRUE)$n.opt[,2][1:13]/13,0)))
```
This would mean a total of 234 grabs in the Rappahannock compared to 393 in previous years.
# Piankatank River
The Piankatank also lacked management subgroups, so clusters were identified.
```{r}
Piankatank.dat <- dat.full %>%
filter(River_name == "Piankatank River")
Piankatank.stations <- unique(Piankatank.dat$Station_ID)
colnames(site.info)[2] <- "Station_ID"
colnames(site.acres)[1] <- "Station_ID"
Piankatank.site.info <- site.acres %>%
filter(Station_ID %in% Piankatank.stations) %>%
arrange(Station_ID)
Piankatank.site.info$Station_ID <- as.numeric(Piankatank.site.info$Station_ID)
Piankatank.dat$Lives <- rowSums(Piankatank.dat[,16:52]*Piankatank.dat$Subsample_Factor)
Piankatank.dat$Markets <- rowSums(Piankatank.dat[,31:52]*Piankatank.dat$Subsample_Factor)
Piankatank.dat$Spat1 <- rowSums(Piankatank.dat[,55:67]*Piankatank.dat$Subsample_Factor)
Piankatank.dat <- Piankatank.dat %>% select(Station_ID, Sample_ID, Lives, Markets, Spat1)
```
## Set up strata and summarize
```{r}
Piankatank.strata <- data_frame("Strata" = Piankatank.site.info$Station_ID, NH = Piankatank.site.info$Acreage*4046.86)
Piankatank.numbers <- Stratify(Piankatank.dat, Piankatank.strata, "Station_ID", species = "Lives")
kable(summary(Piankatank.numbers, effic = TRUE, nopt = TRUE)$n.opt[,1:3], caption = "Observed vs. Optimal Effort")
```
```{r}
Piankatank.numbers.summary <- summary(Piankatank.numbers, effic = TRUE, nopt = TRUE)
Piankatank.numbers.nopt <- Piankatank.numbers.summary$n.opt
Piankatank.reallocation <- left_join(Piankatank.numbers.nopt, site.info, by = c("Strata" = as.character("Station_ID")))
site.acres$Station_ID <- as.numeric(site.acres$Station_ID)
Piankatank.full <- left_join(Piankatank.dat, site.acres, by = "Station_ID")
Piankatank.mv <- Piankatank.full %>%
group_by(Station_ID) %>%
summarise(Mean = mean(Lives), Variance = var(Lives))
Piankatank.mv2 <- Piankatank.dat %>%
group_by(Station_ID) %>%
summarise(Mean = mean(Lives), Variance = var(Lives))
Piankatank.site.info$Station_ID <- as.numeric(Piankatank.site.info$Station_ID)
Piankatank.for.mclust <- left_join(Piankatank.mv2, Piankatank.site.info, by=c("Station_ID" = "Station_ID"))
row.names(Piankatank.for.mclust) <- Piankatank.for.mclust$Station_ID
Piankatank.mva <- Piankatank.full %>%
group_by(Station_ID) %>%
summarise(Mean = mean(Lives), Variance = var(Lives), Acreage = mean(Acreage))
row.names(Piankatank.mva) <- Piankatank.mva$Station_ID
Piankatank.mva <- scale(Piankatank.mva)
```
## Clustering
```{r, fig.align='center', fig.height=5, fig.width=7}
Piankatank.wss <- matrix(nrow = 100, ncol = 10)
for(j in 1:100) {
wss <- (nrow(Piankatank.mva[,2:4])-1)*sum(apply(Piankatank.mva[,2:4], 2, var))
for (i in 1:10) Piankatank.wss[j,i] <- sum(kmeans(Piankatank.mva[,2:4], centers = i, iter.max = 1000, nstart = 1)$withinss)
}
Piankatank.wss <- as.data.frame(Piankatank.wss)
Piankatank.wss.mean <- colMeans(Piankatank.wss)
plot(1:10, Piankatank.wss.mean, type = "b", xlab = "Number of Clusters",
ylab = "Within groups SS")
#################################################################
k <- 3
#################################################################
```
```{r, fig.align='center', fig.height=5}
best.seed <- matrix(nrow = 1000, ncol = 2)
for (i in 1:1000){
set.seed(i)
fit <- kmeans(Piankatank.mva[,2:4], k, iter.max = 1000, nstart = 1, algorithm = "Hartigan-Wong")
best.seed[i,1] <- i
best.seed[i,2] <- fit$betweenss/fit$totss*100
}
best.seed <- as.data.frame(best.seed)
seed <- best.seed$V1[best.seed$V2 == max(best.seed$V2)][1]
set.seed(seed)
fit.Piankatank <- kmeans(Piankatank.mva[,2:4], k, iter.max = 1000, nstart = 1, algorithm = "Hartigan-Wong")
Piankatank.cluster.groups <- data.frame("Station_ID" = as.numeric(rownames(Piankatank.mva)))
Piankatank.cluster.groups$kmeans <- fit.Piankatank$cluster
clusplot(Piankatank.mva[,2:4], Piankatank.cluster.groups$kmeans, color = T, shade = T, labels = 2, lines = 0)
```
This plot shows how well those reefs cluster.
```{r}
a2 <- fit.Piankatank$cluster
suggested.strata.Piankatank <- data_frame("Code" = as.numeric(names(a2)), "Cluster" = a2)
site.name <- dat.full %>%
select(Station_ID, Full_station_name) %>%
mutate("Code" = Station_ID) %>%
select(Code, Full_station_name)
site.name <- site.name[!duplicated(site.name),]
suggested.strata.Piankatank <- left_join(suggested.strata.Piankatank, site.name, by = "Code") %>%
arrange(Cluster)
kable(suggested.strata.Piankatank, caption = "Suggested Strata")
suggested.strata.Piankatank$Subgroup <- paste0(
"P",
suggested.strata.Piankatank$Cluster
)
```
## Add new suggested groups
```{r}
names(Piankatank.site.info) <- c("Station_ID", "Subgroup", "NH")
Piankatank.site.info$NH <- Piankatank.site.info$NH*4046.86 #convert acres to square meters
Piankatank.site.info$Station_ID <- as.character(Piankatank.site.info$Station_ID)
Piankatank.site.info2 <- data.frame(Piankatank.site.info)
Piankatank.domain <- data_frame(NH = Piankatank.site.info$NH,
"kmeans" = Piankatank.cluster.groups$kmeans)
#Summarize NH by new strata (aka domains)
Piankatank.domain.info <- Piankatank.domain %>%
group_by(kmeans) %>%
summarise(NH = mean(NH))
Piankatank.domain.info2 <- data.frame(Piankatank.domain.info)
Piankatank.dat2 <- left_join(Piankatank.dat, Piankatank.cluster.groups)
Piankatank.dat2 <- arrange(Piankatank.dat2, Station_ID)
Piankatank.dat2$Station_ID <- as.character(Piankatank.dat2$Station_ID)
Piankatank.dat3 <- data.frame(Piankatank.dat2)
Piankatank.site.info2 <- data.frame(Piankatank.site.info)
```
## Testing Stratification
How well is effort allocated based on current strata?
```{r}
# How well is effort allocated based on current strata?
names(Piankatank.domain.info2) <- c("Strata","NH")
Piankatank.numbers2 <- Stratify(Piankatank.dat3, Piankatank.domain.info2, "kmeans", species = "Lives")
kable(summary(Piankatank.numbers2, effic = T, nopt = T)$n.opt[,1:3])
```
## Estimate the population mean and variance given the new strata and allocation
```{r}
varest.boot.dat.Piankatank <- matrix(ncol = 4, nrow = k*100)
colnames(varest.boot.dat.Piankatank) <- c("kmeans", "MEAN", "SD", "VAR")
varest.boot.dat.Piankatank <- as.data.frame(varest.boot.dat.Piankatank)
n.opt.test.cumm.Piankatank <- cumsum(summary(Piankatank.numbers2, effic = T, nopt = T)$n.opt$Optimal[1:k])
n.opt.test.Piankatank <- summary(Piankatank.numbers2, effic = T, nopt = T)$n.opt$Optimal[1:k]
start.pos.Piankatank <- c(1, n.opt.test.cumm.Piankatank[1:k-1]+1)
nopt.boot.dat.Piankatank <- matrix(ncol = 7, nrow = k*100)
colnames(nopt.boot.dat.Piankatank) <- c("Strata", "Observed", "Optimal", "Perc.Increase.Var.Opt",
"Compromise", "Perc.Increase.Var.comp", "run")
nopt.boot.dat.Piankatank <- as.data.frame(nopt.boot.dat.Piankatank)
j.start.Piankatank <- seq(1,k*100, by = k)
j.end.Piankatank <- seq(k, k*100, by = k)
for(j in 1:100) {
sub.dat1 <- matrix(ncol=6,nrow = n.opt.test.cumm.Piankatank[k])
colnames(sub.dat1) <- names(Piankatank.dat3)
sub.dat1 <- as.data.frame(sub.dat1)
for(i in 1:k) {
temp <- Piankatank.dat3 %>%
filter(kmeans == i)
rand.n <- sample(1:nrow(temp),n.opt.test.Piankatank[i], replace = T)
temp2 <- temp[rand.n,]
sub.dat1[start.pos.Piankatank[i]:n.opt.test.cumm.Piankatank[i],] <- temp2
}
sub.summary <- sub.dat1 %>%
group_by(kmeans) %>%
summarise("MEAN" = mean(Lives), "SD" = sd(Lives), "VAR" = var(Lives))
varest.boot.dat.Piankatank[j.start.Piankatank[j]:j.end.Piankatank[j],] <- sub.summary
}
#kable(head(varest.boot.dat.Piankatank, 10), caption = "Population mean for each strata with a random resampling with new effort")
```
```{r}
Piankatank.sample <- n.opt.test.Piankatank
Piankatank.sample.cumm <- n.opt.test.cumm.Piankatank
Piankatank.plot.dat <- matrix(ncol = 3, nrow = k*1000)
colnames(Piankatank.plot.dat) <- c("ER","KMEAN", "MEAN")
Piankatank.plot.dat <- as.data.frame(Piankatank.plot.dat)
Piankatank.plot.dat$ER <- rep(seq(0, 0.9, 0.1), each = k*100)
Piankatank.plot.dat$KMEAN <- rep(1:k, 1000)
Piankatank.plot.dat$KMEAN <- factor(Piankatank.plot.dat$KMEAN)
h.start <- seq(1, 1+(k*1000 - k*100), by = k*100)
h.end <- seq(k*100, k*1000, by = k*100)
Piankatank.sub.sample <- Piankatank.sample/sum(Piankatank.sample)
Piankatank.sub.cumm <- round(seq(1, 0.1, by = -0.1)*Piankatank.sample.cumm[k], 0)
j.start <- seq(1,k*100, by = k)
j.end <- seq(k, k*100, by = k)
for(h in 1:10){
varest.boot.dat <- matrix(ncol = 4, nrow = k*100)
colnames(varest.boot.dat) <- c("kmeans", "MEAN", "SD", "VAR")
varest.boot.dat <- as.data.frame(varest.boot.dat)
sample.cumm <- cumsum(round(Piankatank.sub.sample*Piankatank.sub.cumm[h],0))
start.pos <- c(1, sample.cumm[1:k-1]+1)
for(j in 1:100) {
sub.dat1 <- matrix(ncol=6, nrow = Piankatank.sub.cumm[h])
colnames(sub.dat1) <- names(Piankatank.dat3)
sub.dat1 <- as.data.frame(sub.dat1)
for(i in 1:k) {
temp <- Piankatank.dat3 %>%
filter(kmeans == i)
rand.n <- sample(1:nrow(temp),round(Piankatank.sub.sample[i]*Piankatank.sub.cumm[h],0), replace = T)
temp2 <- temp[rand.n,]
sub.dat1[start.pos[i]:sample.cumm[i],] <- temp2
}
sub.summary <- sub.dat1 %>%
group_by(kmeans) %>%
summarise("MEAN" = mean(Lives), "SD" = sd(Lives), "VAR" = var(Lives))
varest.boot.dat[j.start[j]:j.end[j],] <- sub.summary
}
Piankatank.plot.dat$MEAN[h.start[h]:h.end[h]] <- varest.boot.dat$MEAN
}
#kable(head(Piankatank.plot.dat, 10), caption = "Population mean for each strata with a random resampling with new effort")