forked from anitas-giraldo/Power-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.04_King-Wrasse-BRUVs.R
1408 lines (935 loc) · 30.6 KB
/
1.04_King-Wrasse-BRUVs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
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
##### Raw point data into Binary: presence - absence of seagrass ###
# libraries ----
library(tidyr)
library(dplyr)
library(readr)
library(stringr)
library(readr)
library(devtools)
library(sp)
library(raster)
library(rgdal)
#install.packages("remotes")
#remotes::install_github("UWAMEGFisheries/GlobalArchive")
#library(GlobalArchive)
rm(list=ls()) #clear memory
# Study name----
study <- "stereo-BRUVs"
# Set work directory----
w.dir<-dirname(rstudioapi::getActiveDocumentContext()$path) # sets working directory to where this script is saved (DON't MOVE)
setwd(w.dir)
# Set sub directories----
raw.dir = paste(w.dir,"Data/raw",sep="/")
tidy.dir = paste(w.dir,"Data/tidy",sep="/")
# Set File to use ----
f <- "2014-12_Geographe.Bay_stereoBRUVs.king.wrasse.maxn.csv"
# set species name
s <- "king-wrasse"
#### Raw to PA ----
## read data --
# these files already have lat and long
fish <- read.csv(paste(raw.dir, f, sep ='/')) %>%
dplyr::select(sample,maxn,latitude, longitude, depth) %>% # Select columns to keep
glimpse()
str(fish)
head(fish)
# test for NAs --
any(is.na(fish))
#### Match to CMR zones ----
# read GB shapefile --
s.dir <- "G:/My Drive/Anita/Shapefiles"
gb <- readOGR(paste(s.dir, "GeoBay.shp", sep='/'))
plot(gb)
gb
# Make fish data spatial --
#names(fish)
#fishsp <- SpatialPoints(fish[,c(4,3)])
fishsp <- fish
coordinates(fishsp) <- ~longitude+latitude
points(fishsp)
proj4string(fishsp) <- proj4string(gb)
# extract zone from each point --
points.zone <- raster::extract(gb, fishsp, df = T)
head(points.zone)
# check for duplicates --
points.zone$point.ID[duplicated(points.zone$point.ID)]
#points.zone <- points.zone[!duplicated(points.zone$point.ID),] # remove
fsh <- as.data.frame(fishsp)
head(fsh)
# combine habitat and zone dfs --
f.points.zone <- cbind(fsh, points.zone)
head(f.points.zone)
#### Save data with zone info----
write.csv(f.points.zone, paste(tidy.dir, paste(study, paste(s, 'csv', sep='.'), sep='_'), sep='/'))
#### Remove BRUVS w NAs ----
df <- read.csv(paste(tidy.dir, paste(study, paste(s, 'csv', sep='.'), sep='_'), sep='/'))
str(df) # 322 obs
# check NAs in column Zone Name --
names(df)
any(is.na(df[,12]))
# Remove NA's - in the zone column --
#df <- na.omit(df)
df <- df %>% drop_na(ZoneName)
str(df)# 243 ob
# Remove unwanted columns
names(df)
#df <- df[, c(3:5,9:12,15,26)]
df <- df %>%
dplyr::select(sample, maxn, latitude, longitude, depth, ZoneName) %>%
glimpse()
names(df)
head(df)
dfs <- df
coordinates(dfs) <- ~ longitude + latitude
points(dfs)
proj4string(dfs) <- proj4string(gb)
# Save CMR data ----
write.csv(df, paste(tidy.dir, paste(study, paste(s, "CMR", 'csv', sep='.'), sep='_'), sep='/'))
writeOGR(dfs, dsn = "Y:/Power-Analysis/Data/shapefiles", layer = paste(study, s, sep='_'), driver = "ESRI Shapefile", overwrite = T)
##### CLUSTER BRUVS #####
# Method 1 : hierarchical clustering approach----
# https://gis.stackexchange.com/questions/17638/clustering-spatial-data-in-r
library(geosphere)
# convert data to a SpatialPointsDataFrame object
#xy <- SpatialPointsDataFrame(
# matrix(c(df$Longitude,df$Latitude), ncol=2), data.frame(ID=seq(1:length(df$Longitude))),
#proj4string=CRS("+proj=longlat +ellps=GRS80 +no_defs"))
#writeOGR(xy, "Y:/Power-Analysis/Bimodal/Data/spatial", "CMR-points", driver = "ESRI Shapefile")
xyp <- readOGR("Y:/Power-Analysis/Data/shapefiles/stereo-BRUVs_king-wrasse.shp")
plot(xyp)
## Cluster by zone ----
plot(gb)
plot(xyp, add=T)
xyzones <- raster::extract(gb, xyp)
xy.df <- as.data.frame(xyp)
xyzones <- cbind(xy.df, xyzones)
head(xyzones)
coordinates(xyzones) <- ~coords.x1+coords.x2
xyzones
proj4string(xyzones) <- proj4string(gb)
# MUZ clusters ----
muz <- xyzones[xyzones$ZoneName=="Multiple Use Zone",]
muz # 3486 features
# use the distm function to generate a geodesic distance matrix in meters
mdist <- distm(muz)
# cluster all points using a hierarchical clustering approach
hc <- hclust(as.dist(mdist), method="average")
# define the distance threshold, in this case 40 m
d <- 6000
# define clusters based on a tree "height" cutoff "d" and add them to the SpDataFrame
#muz$clust <- cutree(hc, k=15, h=d) use k to define number of clusters
muz$clust <- cutree(hc, h=d)
muz
plot(gb)
plot(muz, col = muz$clust, add=T) # 6 clusters
levels(muz$clust)
# join clusters 2 and 6 --
# check which ones they are
plot(muz, col = muz$clust=="2", add=T)
plot(muz, col = muz$clust=="6", add=T)
# Join them
str(muz)
muz$clust <- as.factor(muz$clust)
levels(muz$clust)[levels(muz$clust)=="6"] <- "2"
plot(muz, col = muz$clust, add=T) # 5 clusters
# SPZ clusters ----
spz <- xyzones[xyzones$ZoneName=="Special Purpose Zone (Mining Exclusion)",]
spz # 4415 features
# use the distm function to generate a geodesic distance matrix in meters
mdist <- distm(spz)
# cluster all points using a hierarchical clustering approach
hc <- hclust(as.dist(mdist), method="average")
# define the distance threshold, in this case 40 m
d <- 8000
# define clusters based on a tree "height" cutoff "d" and add them to the SpDataFrame
#muz$clust <- cutree(hc, k=15, h=d) #use k to define number of clusters
spz$clust <- cutree(hc, h=d) #use k to define number of clusters
spz
str(spz)
spz$clust <- as.factor(spz$clust)
levels(spz$clust) # 8 clusters
plot(gb)
plot(spz, col = spz$clust, add=T) # 8 clusters
# NPZ clusters ----
npz <- xyzones[xyzones$ZoneName=="National Park Zone",]
npz # 340 features
# use the distm function to generate a geodesic distance matrix in meters
mdist <- distm(npz)
# cluster all points using a hierarchical clustering approach
hc <- hclust(as.dist(mdist), method="average")
# define the distance threshold, in this case 40 m
d <- 1000
# define clusters based on a tree "height" cutoff "d" and add them to the SpDataFrame
#muz$clust <- cutree(hc, k=15, h=d) #use k to define number of clusters
npz$clust <- cutree(hc, h=d) #use k to define number of clusters
npz
str(npz)
npz$clust <- as.factor(npz$clust)
levels(npz$clust) # 8 clusters
plot(gb)
plot(npz, col = npz$clust, add=T) # 8 clusters
str(npz)
# join clusters 1,3,5 and 8 = 1 and then 2,4,6,7
plot(npz, col = npz$clust=="1", add=T)#
plot(npz, col = npz$clust=="2", add=T)
plot(npz, col = npz$clust=="3", add=T)#
plot(npz, col = npz$clust=="4", add=T)
plot(npz, col = npz$clust=="5", add=T)#
plot(npz, col = npz$clust=="6", add=T)
plot(npz, col = npz$clust=="7", add=T)
plot(npz, col = npz$clust=="8", add=T)#
str(npz)
npz$clust <- as.factor(npz$clust)
levels(npz$clust)[levels(npz$clust)=="3"] <- "1"
levels(npz$clust)[levels(npz$clust)=="5"] <- "1"
levels(npz$clust)[levels(npz$clust)=="8"] <- "1"
levels(npz$clust)[levels(npz$clust)=="4"] <- "2"
levels(npz$clust)[levels(npz$clust)=="6"] <- "2"
levels(npz$clust)[levels(npz$clust)=="7"] <- "2"
plot(gb)
plot(npz, col = npz$clust, add=T) # 2 clusters
# HPZ clusters ----
hpz <- xyzones[xyzones$ZoneName=="Habitat Protection Zone",]
hpz # 340 features
# use the distm function to generate a geodesic distance matrix in meters
mdist <- distm(hpz)
# cluster all points using a hierarchical clustering approach
hc <- hclust(as.dist(mdist), method="average")
# define the distance threshold, in this case 40 m
d <- 2000
# define clusters based on a tree "height" cutoff "d" and add them to the SpDataFrame
#muz$clust <- cutree(hc, k=15, h=d) #use k to define number of clusters
hpz$clust <- cutree(hc, h=d) #use k to define number of clusters
hpz
str(hpz)
hpz$clust <- as.factor(hpz$clust)
levels(hpz$clust) # 8 clusters
plot(gb)
plot(hpz, col = hpz$clust, add=T) # 2 clusters
str(hpz)
# join clusters 1 and 4 = 1 and then 2,3, and 5 = 2
plot(hpz, col = hpz$clust=="1", add=T)#
plot(hpz, col = hpz$clust=="2", add=T)
plot(hpz, col = hpz$clust=="3", add=T)
plot(hpz, col = hpz$clust=="4", add=T)#
plot(hpz, col = hpz$clust=="5", add=T)
str(hpz)
hpz$clust <- as.factor(hpz$clust)
levels(hpz$clust)[levels(hpz$clust)=="4"] <- "1"
levels(hpz$clust)[levels(hpz$clust)=="3"] <- "2"
levels(hpz$clust)[levels(hpz$clust)=="5"] <- "2"
plot(gb)
plot(hpz, col = hpz$clust, add=T) # 2 clusters
### rename clusters for unique IDs -----
levels(muz$clust)
levels(muz$clust)[levels(muz$clust)=="1"] <- "9"
levels(muz$clust)[levels(muz$clust)=="2"] <- "10"
levels(muz$clust)[levels(muz$clust)=="3"] <- "11"
levels(muz$clust)[levels(muz$clust)=="4"] <- "12"
levels(muz$clust)[levels(muz$clust)=="5"] <- "13"
levels(npz$clust)
levels(npz$clust)[levels(npz$clust)=="1"] <- "14"
levels(npz$clust)[levels(npz$clust)=="2"] <- "15"
levels(hpz$clust)
levels(hpz$clust)[levels(hpz$clust)=="1"] <- "16"
levels(hpz$clust)[levels(hpz$clust)=="2"] <- "17"
## Join all clustered points ----
clusteredpoints <- union(spz,muz)
clusteredpoints <- union(clusteredpoints,npz)
clusteredpoints <- union(clusteredpoints,hpz)
clusteredpoints$Seagrss # check you still have sg data
plot(gb)
plot(clusteredpoints, col = clusteredpoints$clust, pch=20, add=T)
### Shave shapefile and df ----
writeOGR(clusteredpoints, "Y:/Power-Analysis/Data/shapefiles", "BRUV_clusteredpoints", driver = "ESRI Shapefile", overwrite=T)
#clusteredpoints <- readOGR("Y:/Power-Analysis/Data/shapefiles/BRUV_clusteredpoints.shp")
clusteredp.df <- as.data.frame(clusteredpoints)
write.csv(clusteredp.df, paste(tidy.dir, "BRUV_clusteredpoints.csv", sep ='/'))
### ---- ###
# Method 2: MBH ----
############ I DID NOT END UP USING THIS METHOD --------------------
# Using clusters planned for GB transects as centres of clusters --
# but first I need to reduce the amount of clusters, because they are too many
library(pdist)
library(fields)
library(rgeos)
# read GB poly in utm ----
gbu <- readOGR('G:/My Drive/Anita/Shapefiles/GeoBay_CMR_UTM.shp')
gbu
crsp <- "+proj=utm +zone=50 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
## High res - MUZ ----
hmuz <- readOGR("G:/My Drive/meg_projects/Project_Parks_Geo_fish and habitat/Sampling design/GB_Design/Multib_Lidar_Design/coords_MUZ/coords_MUZ_2020-05-18.shp")
plot(gb)
plot(hmuz, add=T)
hmuzp <- spTransform(hmuz, crsp)
hmuz.mat <- gWithinDistance(hmuzp, dist = 3000, byid = TRUE)
diag(hmuz.mat) <- NA
hmuz.mat
# extract the upper triangular part of matrix and use the column sums as a criterion to remove the points:
hmuz.mat[lower.tri(hmuz.mat, diag=TRUE)] <- NA
hmuz.mat
colSums(hmuz.mat, na.rm=TRUE) == 0
v1 <- colSums(hmuz.mat, na.rm=TRUE) == 0
hmuz[v1, ] # 5 features left
# plot --
plot(gbu)
plot(hmuzp[v1, ], pch=20, col="red", add=T)
plot(gb)
plot(hmuz[v1,], add=T)
# Save cluster centroids --
hmuz.cluters <- hmuz[v1, ]
hmuz.clus.df <- as.data.frame(hmuz.cluters)
## High res - SPZ----
transects <- readOGR("G:/My Drive/meg_projects/Project_Parks_Geo_fish and habitat/Sampling design/GB_Design/Multib_Lidar_Design/coords_SPZ/coords_spz_2020-05-18.shp")
plot(gb)
plot(transects, add=T)
transectsp <- spTransform(transects, crsp)
transects.mat <- gWithinDistance(transectsp, dist = 3000, byid = TRUE)
diag(transects.mat) <- NA
transects.mat
# extract the upper triangular part of matrix and use the column sums as a criterion to remove the points:
transects.mat[lower.tri(transects.mat, diag=TRUE)] <- NA
transects.mat
colSums(transects.mat, na.rm=TRUE) == 0
v1 <- colSums(transects.mat, na.rm=TRUE) == 0
transects[v1, ] # 5 features left
# plot --
plot(gbu)
plot(transectsp[v1, ], pch=20, col="red", add=T)
plot(gb)
plot(transects[v1,], add=T)
# Save cluster centroids --
hspz.cluters <- transects[v1, ]
hspz.clus.df <- as.data.frame(hspz.cluters)
## High res - NPZ and HPZ ----
#transects <- readOGR("G:/My Drive/meg_projects/Project_Parks_Geo_fish and habitat/Sampling design/GB_Design/Multib_Lidar_Design/coords_SPZ/coords_spz_2020-05-18.shp")
t <- read.csv("G:/My Drive/meg_projects/Project_Parks_Geo_fish and habitat/Sampling design/GB_Design/Multib_Lidar_Design/coordinates_hires_NPZ_HPZ.csv")
str(t)
transectsp <- t
coordinates(transectsp) <- ~ start_x + start_y
proj4string(transectsp) <- proj4string(gbu)
#plot(gb)
#plot(transects, add=T)
#transectsp <- spTransform(transects, crsp)
transects.mat <- gWithinDistance(transectsp, dist = 2000, byid = TRUE)
diag(transects.mat) <- NA
transects.mat
# extract the upper triangular part of matrix and use the column sums as a criterion to remove the points:
transects.mat[lower.tri(transects.mat, diag=TRUE)] <- NA
transects.mat
colSums(transects.mat, na.rm=TRUE) == 0
v1 <- colSums(transects.mat, na.rm=TRUE) == 0
transects[v1, ] # 5 features left
# plot --
plot(gbu)
plot(transectsp[v1, ], pch=20, col="red", add=T)
#plot(gb)
#plot(transects[v1,], add=T)
# Save cluster centroids --
hspz.cluters <- transectsp[v1, ]
gb
crsgb <- "+proj=longlat +ellps=GRS80 +no_defs"
hnpz.hpz.cluters2 <- spTransform(hspz.cluters, crsgb)
hnpz.hpz.clus.df <- as.data.frame(hnpz.hpz.cluters2)
## Low res - SPZ----
transects <- readOGR("G:/My Drive/meg_projects/Project_Parks_Geo_fish and habitat/Sampling design/GB_Design/Bathy250m_design/rstudio-export (1)/Design4_notClustTransects_2020-05-17.shp")
plot(gbu)
plot(transects, add=T)
transectsp <- transects
#transectsp <- spTransform(transects, crsp)
transects.mat <- gWithinDistance(transectsp, dist = 4000, byid = TRUE)
diag(transects.mat) <- NA
transects.mat
# extract the upper triangular part of matrix and use the column sums as a criterion to remove the points:
transects.mat[lower.tri(transects.mat, diag=TRUE)] <- NA
transects.mat
colSums(transects.mat, na.rm=TRUE) == 0
v1 <- colSums(transects.mat, na.rm=TRUE) == 0
transects[v1, ] # 5 features left
# plot --
plot(gbu)
plot(transectsp[v1, ], pch=20, col="red", add=T)
#plot(gb)
#plot(transects[v1,], add=T)
# Save cluster centroids --
transects2 <- transects[v1, ]
transects3 <- spTransform(transects2, crsgb)
plot(gb)
plot(transects3, add=T)
bz.cluters <- transects3
bz.clus.df <- as.data.frame(bz.cluters)
#### Join all the cluster centroids ----
head(bz.clus.df)
bz <- bz.clus.df[, c(10,11)]
bz$cluster <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
bz$bathy.res <- "Low"
names(bz) <- c("long", "lat", "cluster", "bathy.res")
head(hmuz.clus.df)
hmuz <- hmuz.clus.df[,c(7,8)]
hmuz$cluster <- c(1,2,3,4,5)
hmuz$bathy.res <- "High"
names(hmuz) <- c("long", "lat", "cluster", "bathy.res")
head(hspz.clus.df)
hspz <- hspz.clus.df[,c(7,8)]
hspz$cluster <- c(1,2,3,4,5)
hspz$bathy.res <- "High"
names(hspz) <- c("long", "lat", "cluster", "bathy.res")
head(hnpz.hpz.clus.df)
hnhpz <- hnpz.hpz.clus.df[,c(12,13)]
hnhpz$cluster <- c(1,2,3,4)
hnhpz$bathy.res <- "High"
names(hnhpz) <- c("long", "lat", "cluster", "bathy.res")
# joing all clusters
gbclusters <- rbind(bz, hmuz, hspz, hnhpz)
gbclusters
c <- gbclusters
coordinates(c) <- ~long+lat
plot(gb)
plot(c, add=T)
proj4string(c) <- proj4string(gb)
c2 <- spTransform(c, crsp)
## some around the HPZ deep zone are really close.. run another buffer
c.mat <- gWithinDistance(c2, dist = 2000, byid = TRUE)
diag(c.mat) <- NA
c.mat
# extract the upper triangular part of matrix and use the column sums as a criterion to remove the points:
c.mat[lower.tri(c.mat, diag=TRUE)] <- NA
c.mat
colSums(c.mat, na.rm=TRUE) == 0
v1 <- colSums(c.mat, na.rm=TRUE) == 0
c2[v1, ] # 5 features left
# plot --
plot(gbu)
plot(c2[v1, ], pch=20, col="red", add=T)
plot(gb)
plot(c[v1, ], pch=20, col="red", add=T)
gbclust <- c[v1, ]
writeOGR(gbclust, "Y:/Power-Analysis/Bimodal/Data/spatial", "BRUV_MBHclusters", driver = "ESRI Shapefile")
gbclust.df <- as.data.frame(gbclust)
write.csv(gbclust.df, paste(tidy.dir, "BRUV_MBHclusters.csv", sep ='/'))
gbclust.df <- read.csv(paste(tidy.dir, "BRUV_MBHclusters.csv", sep ='/'))
####
gbclust.df$clusterID <- c(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)
df <- gbclust.df
coordinates(df) <- ~ long + lat
proj4string(df) <- proj4string(gb)
gbclustp <- spTransform(df, crsp)
#cids <- gbclustp$clusterID
## Create 2000m buffer ----
buff <- gBuffer(gbclustp, byid=T, width = 2000)
plot(gbu)
plot(buff, add=T)
writeOGR(buff, "Y:/Power-Analysis/Bimodal/Data/spatial", "BRUV_MBHclusters_2000mbuffer", driver = "ESRI Shapefile")
buff <- readOGR("Y:/Power-Analysis/Bimodal/Data/spatial/BRUV_MBHclusters_2000mbuffer.shp")
## extract points within buffers ----
xyp <- spTransform(xy, crsp)
buff2 <- spTransform(buff, proj4string(gb))
clustpoints <- raster::extract(buff2, xy, df=T)
head(clustpoints)
toremove <- clustpoints$point.ID[duplicated(clustpoints$point.ID)] # check for replicates
# remove reps
clustpoints <- clustpoints[-toremove,]
head(clustpoints)
# bind to df
test <- cbind(clustpoints, as.data.frame(xy))
# remove nas from culsterIDs
# Remove NA's - in the zone column --
#df <- na.omit(df)
head(test)
test <- test %>% drop_na(clusterID)
str(test)# 3342 obs
test$clusterID <- as.factor(test$clusterID)
head(test)
levels(test$clusterID)
plot(gb)
points(xy)
#points(test$coords.x1, test$coords.x2, col = "red", cex=1)
points(test$coords.x1, test$coords.x2, col = test$clusterID, cex=1)
plot(buff2, add=T)
## Create buffer according to zone ----
plot(gbu)
plot(gbclustp, add=T)
gbclustp.df <- as.data.frame(gbclustp)
gbclustp2 <- raster::extract(gbu, gbclustp, sp = T)
gbclustp2 <- cbind(gbclustp.df, gbclustp2)
clustgb <- gbclustp2
coordinates(clustgb) <- ~long + lat
plot(gbu)
plot(clustgb, col = clustgb$ZoneName, add=T)
# run another buffer for MUZ and SPZ so they include more BRUVs --
# 4000 m buffer --
muzone <- clustgb[clustgb$ZoneName==("Multiple Use Zone"), ]
spzone <- clustgb[clustgb$ZoneName==("Special Purpose Zone (Mining Exclusion)"), ]
plot(muzone)
plot(spzone)
bigzones <- union(muzone, spzone)
plot(bigzones)
m.mat <- gWithinDistance(bigzones, dist = 5000, byid = TRUE)
diag(m.mat) <- NA
m.mat
# extract the upper triangular part of matrix and use the column sums as a criterion to remove the points:
m.mat[lower.tri(m.mat, diag=TRUE)] <- NA
m.mat
colSums(m.mat, na.rm=TRUE) == 0
v1 <- colSums(m.mat, na.rm=TRUE) == 0
bigzones[v1, ] # 16 features left
# plot --
plot(gbu)
plot(bigzones[v1, ], pch=20, col="red", add=T)
#plot(gb)
#plot(muzone[v1, ], pch=20, col="red", add=T)
bigzones.c <- bigzones[v1, ]
buff.bigz <- gBuffer(bigzones.c, byid=T, width = 4000)
plot(gbu)
plot(buff.bigz, add=T)
plot(xyp, add=T, col = "blue", pch = 20, cex = 1)
writeOGR(buff.bigz, "Y:/Power-Analysis/Bimodal/Data/spatial", "BRUV_MBHclusters_4000mbuffer", driver = "ESRI Shapefile")
buff.bigz <- readOGR("Y:/Power-Analysis/Bimodal/Data/spatial/BRUV_MBHclusters_4000mbuffer.shp")
### --- ### --- ### ---
## Method 1 : Get data ready for epower ----
### NPZ ####
c.points <- readOGR("Y:/Power-Analysis/Data/shapefiles/BRUV_clusteredpoints.shp")
df <- read.csv(paste(tidy.dir, "BRUV_clusteredpoints.csv", sep ='/'))
# Remove unwanted columns
names(df)
head(df)
df <- df %>% dplyr::select(sample, maxn, ZoneName, clust) %>%
glimpse()
names(df)
head(df)
### split the data by zone name--
dfz <- split(df, df$ZoneNam)
str(dfz)
# remove special purpose zone
dfn <- dfz[-4]
str(dfn)
# new listo into data frame
dfn <- do.call(rbind.data.frame, dfn)
dfn
str(dfn)
names(dfn)
head(dfn)
summary(dfn)
dfn <- droplevels(dfn)
levels(dfn$ZoneNam)
#### NOT GOING TO SUBSAMPLE BRUV DATA --
dfn1 <- dfn
names(dfn1)
head(dfn1)
str(dfn1) #126 obs
## BRUVS as samples ----
# T1 --
f1 <- dfn1
f1$Time <- "T1"
# T2 --
f2 <- dfn1
f2$Time <- "T2"
# T3 --
f3 <- dfn1
f3$Time <- "T3"
# join
fall <- rbind(f1, f2, f3)
head(fall)
names(fall) <- c("sample", "maxn", "ZoneName", "Cluster", "Time")
fall$Period <- "Before"
# Make control impact column
levels(fall$ZoneName)
fall$CvI <- ifelse(fall$ZoneName=="National Park Zone", "Impact", "Control")
head(fall)
#### Save data for epower ----
write.csv(fall, paste(tidy.dir, paste(study, s, "NPZ-All-epower.csv", sep='-'), sep='/'))
## Mean maxn by cluster ----
# T1 --
f1 <- aggregate(maxn ~ clust + ZoneName, data = dfn1, sum)
f1
names(f1) <- c("Cluster", "ZoneName", "maxn_sum")
no.scored1 <- aggregate(sample ~ clust + ZoneName, data = dfn1, length)
no.scored1
names(no.scored1) <- c("Cluster", "ZoneName", "no.samples")
fm <- aggregate(maxn ~ clust +ZoneName, data = dfn1, mean)
fm
names(fm) <- c("Cluster", "ZoneName", "maxn_mean")
df1 <- cbind(f1, maxn_sum= no.scored1[,3], maxn_mean=fm[,3])
df1
#names(df1) <- c("Transect", "ZoneName", "Seagrass", "no.scored")
df1$Time <- "T1"
df1
# T2 --
df2 <- df1
df2$Time <- "T2"
df2
# T3 --
df3 <- df1
df3$Time <- "T3"
df3
## joint these together --
dfall <- rbind(df1, df2, df3)
dfall
# Make Period Column
dfall$Period <- "Before"
names(dfall)
# Make control impact column
levels(dfall$ZoneName)
dfall$CvI <- ifelse(dfall$ZoneName=="National Park Zone", "Impact", "Control")
head(dfall)
#### Save mean data for epower ----
write.csv(dfall, paste(tidy.dir, paste(study, s, "NPZ-epower.csv", sep='_'), sep='/'))
### HPZ ####
c.points <- readOGR("Y:/Power-Analysis/Data/shapefiles/BRUV_clusteredpoints.shp")
df <- read.csv(paste(tidy.dir, "BRUV_clusteredpoints.csv", sep ='/'))
# Remove unwanted columns
names(df)
head(df)
df <- df %>% dplyr::select(sample, maxn, ZoneName, clust) %>%
glimpse()
names(df)
head(df)
### split the data by zone name--
dfz <- split(df, df$ZoneNam)
str(dfz)
# remove special purpose zone
dfn <- dfz[-4]
str(dfn)
# new listo into data frame
dfn <- do.call(rbind.data.frame, dfn)
dfn
str(dfn)
names(dfn)
head(dfn)
summary(dfn)
dfn <- droplevels(dfn)
levels(dfn$ZoneNam)
#### NOT GOING TO SUBSAMPLE BRUV DATA --
## BRUVS as samples ----
dfn1 <- dfn
names(dfn1)
# T1 --
f1 <- dfn1
f1$Time <- "T1"
# T2 --
f2 <- dfn1
f2$Time <- "T2"
# T3 --
f3 <- dfn1
f3$Time <- "T3"
# join
fall <- rbind(f1, f2, f3)
head(fall)
names(fall) <- c("sample", "maxn", "ZoneName", "Cluster", "Time")
fall$Period <- "Before"
# Make control impact column
levels(fall$ZoneName)
fall$CvI <- ifelse(fall$ZoneName=="Habitat Protection Zone", "Impact", "Control")
head(fall)
#### Save mean data for epower ----
write.csv(fall, paste(tidy.dir, paste(study, s, "HPZ-All-epower.csv", sep='-'), sep='/'))
## Mean maxn by cluster ----
# T1 --
head(dfn1)
f1 <- aggregate(maxn ~ clust + ZoneName, data = dfn1, sum)
f1
names(f1) <- c("Cluster", "ZoneName", "maxn_sum")
no.scored1 <- aggregate(sample ~ clust + ZoneName, data = dfn1, length)
no.scored1
names(no.scored1) <- c("Cluster", "ZoneName", "no.samples")
fm <- aggregate(maxn ~ clust +ZoneName, data = dfn1, mean)
fm
names(fm) <- c("Cluster", "ZoneName", "maxn_mean")
df1 <- cbind(f1, no.samples= no.scored1[,3], maxn_mean=fm[,3])
df1
#names(df1) <- c("Transect", "ZoneName", "Seagrass", "no.scored")
df1$Time <- "T1"
df1
# T2 --
#dfn2 <- dfn
#head(dfn1)
df2 <- df1
df2$Time <- "T2"
df2
# T3 --
df3 <- df2
df3$Time <- "T3"
## joint these together --
dfall <- rbind(df1, df2, df3)
dfall
# Make Period Column
dfall$Period <- "Before"
names(dfall)
# Make control impact column
levels(dfall$ZoneName)
dfall$CvI <- ifelse(dfall$ZoneName=="Habitat Protection Zone", "Impact", "Control")
head(dfall)
#### Save mean data for epower ----
write.csv(dfall, paste(tidy.dir, paste(study, s, "HPZ-epower.csv", sep='_'), sep='/'))
### MUZ ####