-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSockeye_2024.r
1643 lines (1459 loc) · 64 KB
/
Sockeye_2024.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
###################################################
# Juvenile sockeye salmon project
###################################################
#Author: Ellen Yasumiishi
#Address: 17109 Point Lena Loop Road, Juneau, AK 99801
# Load packages
library(cowplot)
library(googleway)
library(ggrepel)
library(ggspatial)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(splines2)
library(splines)
library(fields)
library(mapdata)
library(maps)
library(akima)
library(stats)
library(car)
library(ggplot2)
library(grid)
library(gridBase)
library(gridExtra)
library(reshape2)
library(Matrix)
library(INLA)
library(effects)
library(VAST)
library(dplyr)
library(magrittr)
library(viridis)
library(plotrix)
library(ggmap)
library(maps)
library(devtools)
library(units)
#Help files
#?FishStatsUtils::make_settings
#?FishStatsUtils::fit_model
#?VAST::make_data
#?FishStatsUtils::plot_results
gc()
gc(reset=T)
rm(list = ls())
Version = get_latest_version( package="VAST" )
Version
wd<-setwd("C:/Users/ellen.yasumiishi/Work/GitHub/Sockeye-Ecology-and-Evolution")
#Data set to estimate annual indices
#Simply removed missing years
temp2<-read.csv("IYSsockeyeCovariates30.csv")
Year=temp2[,1] #Survey year
Lat =temp2[,2] #Latitude
Lon =temp2[,3] #Longitude
SST =temp2[,4] #Sea temperature at 20 m depth
A0PollockN =temp2[,5]#Catch (numbers) of age-0 pollock
J_Pink =temp2[,6] #Catch (kg) of juvenile pink salmon
Calanus =temp2[,7] #Calanus copepod density
AirT =temp2[,8] #Air temperature not used
HaulDate =temp2[,9] #Date of surface tow
StationID =temp2[,10]#Station identification
AreaSwept =temp2[,11]#Are swept (km^2) of the surface trawl net tow
kg =temp2[,12] #Catch (kg) of juvenile sockeye salmon
Sci =temp2[,13] #Species name
vessel =temp2[,14] #Vessel ID
Julian =temp2[,15] #Julian day of the sample
Constant=temp2[,16] #Constant used for Calanus
Lat_rnd=temp2[,17] #Rounded Latitude
Lon_rnd=temp2[,18] #Rounded Longitude
# Exploratory Plots ========================================================================
do.explore.plot <- TRUE
if(do.explore.plot==TRUE) {
# Catch Distribution by year
g <- ggplot(temp2, aes(kg)) +
theme_linedraw() +
geom_histogram(fill='blue', color='black') +
facet_wrap(~factor(Year), scales='fixed') +
xlab("Weight of Sockeye catch")
g
ggsave(file.path(wd, "Wt Distribution.png"), plot=g, height=8, width=9, units='in')
# Determine proportion of observations with zero catch
sum.ec <- temp2 %>% group_by(Year) %>% summarize(zero=sum(SST==0), nonZero=sum(SST>0),
n=n(),
prop.zero=sum(SST==0)/n())
sum.ec
write.csv(sum.ec, file=file.path(wd, "Summary of Encounter Prob.csv"))
# Effort Distribution
g.eff <- ggplot(temp2, aes(AreaSwept)) +
theme_linedraw() +
geom_histogram(fill='blue', color='black') +
facet_wrap(~factor(Year), scales='free') +
xlab("Area Swept")
g.eff
ggsave(file.path(wd, "Effort Distribution.png"), plot=g.eff, height=8, width=9, units='in')
# Plot Map of Catch Rates
CPU<-log((kg/AreaSwept)+1)
gmap<-ggplot(temp2, aes(x=Lon, y=Lat, color=CPU)) +
theme_linedraw() +
scale_color_viridis() +
geom_point(size=2) +
facet_wrap(~factor(Year))+
#labs(title = "Calanus", x = "Longitude", y = "Latitude", color = "log((#/m^2)+1)") +
labs(title = "Sea temperature (20m depth)", x = "Longitude", y = "Latitude", color = "Celsius") +
labs(title = "Juvenile sockeye salmon", x = "Longitude", y = "Latitude", color = "log((kg/km^2)+1)") +
theme_bw() +
theme(axis.text.x = element_text(size = 12), axis.title.x = element_text(size = 16),
axis.text.y = element_text(size = 12), axis.title.y = element_text(size = 16),
plot.title = element_text(size = 20, face = "bold", color = "black"),
legend.text=element_text(size=12),strip.text.x = element_text(size = 12, face='bold'))
ggsave(file.path(wd, "Map of Wt.png"), plot=gmap, height=8, width=9, units='in')
#Mean annual sea temperatures at 20m
mean.SST <- temp2 %>% group_by(Year) %>% dplyr::summarize(mean_SST=mean(SST))
length(SST)
mean.SST
# Plot Number of Hauls per year
haul.count <- temp2 %>% group_by(Year) %>% summarize(n=n())
g.haul <- ggplot(haul.count, aes(x=Year, y=n, fill=n)) +
theme_bw() +
scale_fill_viridis() +
geom_bar(stat='identity', color='black') +
ylab('Number of Survey Hauls')
g.haul
ggsave(file.path(wd, "Number of Hauls by Year.png"), plot=g.haul, height=4, width=5, units='in')
}
########################################
# FIGURE 3 MAP OF STUDY AREA
########################################
# install.packages("devtools")
devtools::install_github("MattCallahan-NOAA/AKmarineareas", force=TRUE, lib="C:/Program Files/R/R-4.1.3/library")
install.packages("grid", force=TRUE, lib="C:/Program Files/R/R-4.1.3/library")
install.packages("AKmarineareas")
install.packages("remotes")
install.packages("marmap")
install.packages('raster')
remove.packages('marmap')
install.packages('marmap')
library(ggplot2)
library(maps)
library(mapdata)
library(mapproj)
library(grid)
library(AKmarineareas)
library(sf)
library(tidyverse)
library(raster)
library(marmap)
library(dplyr)
library(sp)
library(ggspatial) # Add a scale bar
theme_bw()
#get russia
russia<-rnaturalearth::ne_countries(scale = "medium", returnclass = "sf")%>%
filter(name=="Russia")
#AK coastlines
AK<-AK_basemap()
#bathymetry data
#resolution ranges from 1 (finest) to 10 (most coarse)
depth <- marmap::as.raster(getNOAA.bathy(lon1=-180,lon2=-129,lat1=50,lat2=72, resolution=10, keep=TRUE, antimeridian=FALSE, path=NULL))
getNOAA.bathy(lon1=-180,lon2=-129,lat1=50,lat2=72, resolution=10, keep=TRUE, antimeridian=FALSE, path=NULL)
#convert to contour
depth_c<-rasterToContour(depth, levels=c(-50,-100,-200))%>%
st_as_sf()
grid.newpage();grid.draw(roundrectGrob(gp = gpar(lwd = 0)))
pdf(file="FIGURE 3.pdf", height=11, width=8.5) #This generates the figure as a hi-res (pub quality) tiff file. You can change the dimensions of the figure by changing the height and width arguments. You can change the resolution with the 'res' argument.
p<-ggplot()+
geom_sf(data=russia)+
geom_sf(data=AK)+
geom_sf(data=depth_c, aes(color=level), colour=c("grey35","grey50","grey75"))+
geom_point(data=temp2, aes(Lon_rnd, Lat_rnd, group=NULL), shape=4)+
coord_sf(xlim=c(-178, -150), ylim=c(50,70))+
theme(panel.background = element_rect(colour = 'black',fill = 'white' ),panel.border = element_rect(colour = "black",fill=NA, size=1))+
xlab(expression(paste(Longitude)))+
ylab(expression(paste(Latitude)))+
geom_text(aes(x=-169.3, y=64, label="St. Lawrence Is.",angle=0), size=4)+
geom_text(aes(x=-172.9, y=61, label="St. Matthews Is.",angle=0), size=4)+
geom_text(aes(x=-164.2, y=54.73, label="Unimak Pass.",angle=0), size=4)+
geom_text(aes(x=-167.1, y=60.6, label="Nunivak Is.",angle=0), size=4)+
geom_label(aes(x = -169.7, y =60.1, label = "50 m"), fill = "white", label.size = NA, size = 3) +
geom_label(aes(x = -174.1, y =60.1, label = "100 m"), fill = "white", label.size = NA, size = 3) +
geom_label(aes(x = -178.1, y =60.1, label = "200 m"), fill = "white", label.size = NA, size = 3) +
geom_text(aes(x=-168.2, y=62, label="Inner",angle=0), size=5)+
geom_text(aes(x=-174, y=62, label="Middle",angle=0), size=5)+
geom_text(aes(x=-177.8, y=62, label="Outer",angle=0), size=5)+
geom_text(aes(x=-155, y=66, label="Alaska"), size=7)+
geom_text(aes(x=-176, y=66, label="Russia"), size=7)+
geom_text(aes(x=-175.1, y=55, label="Bering Sea"), size=7)+
geom_text(aes(x=-168.6, y=66.25, label="Bering"), size=5)+
geom_text(aes(x=-169, y=65.9, label="Strait"), size=5)+
geom_text(aes(x=-168, y=70, label="Chukchi Sea"), size=6)+
geom_text(aes(x=-155, y=53, label="Gulf of Alaska"), size=6)+
geom_text(aes(x=-173, y=53, label="Aleutian Islands"), angle=20,size=6)+
geom_text(aes(x=-165, y=51, label="Pacific Ocean"), size=7)+
annotation_scale()+
annotation_north_arrow( height= unit(.75, "cm"), width= unit(.75, "cm"), pad_x = unit(.5, "cm"),
pad_y = unit(1, "cm"))
#Alaska Stream
p+theme(legend.position = "none")+
geom_segment(
aes(x = -150, y = 56.5, xend = -171, yend = 51.2),
arrow = arrow(
length = unit(0.03, "npc"),
type="closed", # Describes arrow head (open or closed),
ends="last"
),
colour = "#999999",
linewidth = 1,
angle = 10 # Anything other than 90 or 0 can look unusual
)+
geom_text(aes(x=-163.9, y=52.6, label="Alaska Stream"),colour="#999999", angle=25.5,size=5)+
#Alaska Coastal Current
theme(legend.position = "none")+
geom_segment(
aes(x = -156, y = 55.7, xend = -165, yend = 53.4),
arrow = arrow(
length = unit(0.03, "npc"),
type="closed", # Describes arrow head (open or closed),
ends="last"
),
colour = "#999999",
linewidth = 1,
angle = 10 # Anything other than 90 or 0 can look unusual
)+
geom_text(aes(x=-159.8, y=54.4, label="Alaska Coastal Current"),colour="#999999", angle=26,size=5)+
#Aleutian North Slope Current
theme(legend.position = "none")+
geom_segment(
aes(x = -178.5, y = 52.4, xend = -167, yend = 54.75),
arrow = arrow(
length = unit(0.03, "npc"),
type="closed", # Describes arrow head (open or closed),
ends="last"
),
colour = "#999999",
linewidth = 1,
angle = 10 # Anything other than 90 or 0 can look unusual
)+
geom_text(aes(x=-173.9, y=53.8, label="Aleutian North Slope Current"),colour="#999999", angle=22,size=5)+
#Bering Slope Current
theme(legend.position = "none")+
geom_segment(
aes(x = -169.5, y = 55, xend = -178, yend = 59.5),
arrow = arrow(
length = unit(0.03, "npc"),
type="closed", # Describes arrow head (open or closed),
ends="last"
),
colour = "#999999",
linewidth = 1,
angle = 10 # Anything other than 90 or 0 can look unusual
)+
geom_text(aes(x=-174.8, y=57, label="Bering Slope Current"),colour="#999999", angle=315,size=5)+
#Anadyr Current
theme(legend.position = "none")+
geom_segment(
aes(x = -178.6, y = 64.9, xend = -173, yend = 63.5),
arrow = arrow(
length = unit(0.03, "npc"),
type="closed", # Describes arrow head (open or closed),
ends="last"
),
colour = "#999999",
linewidth = 1,
angle = 10 # Anything other than 90 or 0 can look unusual
)+
geom_text(aes(x=-176.5, y=63.8, label="Anadyr Current"),colour="#999999", angle=331,size=5)+
#Bering Strait Current
theme(legend.position = "none")+
geom_segment(
aes(x = -169, y = 64.5, xend = -169, yend = 68),
arrow = arrow(
length = unit(0.03, "npc"),
type="closed", # Describes arrow head (open or closed),
ends="last"
),
colour = "#999999",
linewidth = 1,
angle = 10 # Anything other than 90 or 0 can look unusual
)+
geom_text(aes(x=-168.6, y=66.25, label="Bering"), size=5)+
geom_text(aes(x=-169, y=65.9, label="Strait"), size=5)+
#Bering Current
theme(legend.position = "none")+
geom_segment(
aes(x = -163, y = 57.5, xend = -168, yend = 60),
arrow = arrow(
length = unit(0.03, "npc"),
type="closed", # Describes arrow head (open or closed),
ends="last"
),
colour = "#999999",
linewidth = 1,
angle = 10 # Anything other than 90 or 0 can look unusual
)+
#Bering Strait Current
theme(legend.position = "none")+
geom_segment(
aes(x = -171.2, y = 51.6, xend = -172.1, yend = 52.8),
arrow = arrow(
length = unit(0.03, "npc"),
type="closed", # Describes arrow head (open or closed),
ends="last"
),
colour = "#999999",
linewidth = 1,
angle = 10 # Anything other than 90 or 0 can look unusual
)
dev.off()
######################################################
# FIGURE 4 Base juvenile sockeye Spatiotemporal Model
######################################################
#Data:2002-2018: No sampling in 2013, 2015, 2017, so data are averaged for the VAST model to run.
temp<-read.csv("IYSsockeyeCovariates29.csv")
Year=temp[,1] #Smple year
Lat =temp[,2] #Latitude
Lon =temp[,3] #Longitude
SST =temp[,4] #sea temperature at 20m depth at the station
A0PollockN =temp[,5] # number of age-0 pollock caught
J_Pink =temp[,6] # kg of juvenile pink salmon
Calanus =temp[,7] #no./m^2 copepods
AirT =temp[,8] #Air temperature
HaulDate =temp[,9] #haul ID
StationID =temp[,10] #Station ID
AreaSwept =temp[,11] #Area swept
kg =temp[,12] #juvenile sockeye catch kg
Sci =temp[,13] #species
vessel =temp[,14] #vessel
Julian =temp[,15] #Julina day of haul
Constant=temp[,16] # constant 1
settings = make_settings( Version="VAST_v14_0_1",
n_x=500,
Region='User',
purpose="index2",
bias.correct=TRUE,
max_cells = 2000,
ObsModel=c(2,1), # catch, Pollock, pink
fine_scale=TRUE,
treat_nonencounter_as_zero=TRUE,
knot_method='grid',
use_anisotropy = TRUE)
settings$Options = c( settings$Options, "report_additional_variables"=TRUE )
user_region <- readRDS('user_region.rds')
fit_Spatiotemporal = fit_model( "settings"=settings,
"Lat_i"=temp[,'Lat'],
"Lon_i"=temp[,'Lon'],
"observations_LL" = temp[,c('Lat','Lon')],
"t_i"=temp[,'Year'],
"b_i"=(as_units(temp[,'kg'],'kg')),
"a_i"=(as_units(temp[,'AreaSwept'],'km^2')),
"getsd"=TRUE,
"test_fit"=TRUE,
"build_model" = TRUE,
input_grid=user_region ,
getReportCovariance=TRUE
)
check_fit(parameter_estimates, check_gradients = FALSE, quiet = FALSE)
#Check extrapolation grid total area....
colSums(fit_Spatiotemporal$extrapolation_list$a_el)
#Total survey area 344,804 km^2
#SAVE DATA =================================================================================
x=summary(fit_Spatiotemporal$parameter_estimates$SD, select='report')
print(x)
write.csv(x,"Full_sockeye.csv", row.names = TRUE)
plot_results(fit_Spatiotemporal, n_cells=2000 )
## Below shows to you get the model estimate of density, D_gct,
## for each grid (g), category (c; not used here single
## univariate); and year (t); and link it spatially to a lat/lon
## extrapolation point. You can do this for any _gct or _gc
## variable in the Report.
years <- unique(temp$Year)
nyrs <- length(years)
#Year=rep(years, each=nrow(ak_map))
## Remake map list locally for recreating plots
mdl <- make_map_info(Region = settings$Region,
spatial_list = fit_Spatiotemporal$spatial_list,
Extrapolation_List = fit_Spatiotemporal$extrapolation_list)
## quick dirty AK map
ak_map <- subset(map_data("world"), region=='USA' & subregion=='Alaska')
## Have to duplicate it for each year so can facet below
ak_map <- cbind(ak_map[rep(1:nrow(ak_map), times=nyrs),],
Year=rep(years, each=nrow(ak_map)))
gmap <- ggplot(ak_map, aes(x = long, y = lat, group = group)) +
# geom_raster(aes(x=long, y=lat, fill=D))+
# geom_raster(aes(fill = D), interpolate = TRUE)+
geom_polygon(fill="black", colour = "white") +
# scale_colour_gradientn(colors=c('red','orange','yellow','green', 'blue','purple')) +
# scale_colour_gradientn(colors=c('darkblue','blue','lightblue','lightgreen','yellow', 'orange','red')) +
#scale_color_gradientn(colours = rainbow(5))+
# scale_fill_viridis() +
scale_color_viridis_c(option = "D") + #changed form magma to plasma or virdis
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.spacing.x=unit(0, "lines"),
panel.spacing.y=unit(0, "lines"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() ) +
# coord_cartesian(xlim=mdl$Xlim, ylim=mdl$Ylim)
coord_cartesian(xlim=mdl$Xlim, ylim=mdl$Ylim)+
guides(fill=guide_legend(title='Energy Density'))
#Densities at each extrapolation grid location
fit_Spatiotemporal$Report$D_gct
#Densities at each extrapolation grid location
names(fit_Spatiotemporal$Report)[grepl('_gc|_gct', x=names(fit_Spatiotemporal$Report))]
D_gt <- fit_Spatiotemporal$Report$D_gct[,1,] # drop the category
#dimnames(D_gt) <- list(cell=1:nrow(D_gt), year=c(2003:2019))
dimnames(D_gt) <- list(cell=1:nrow(D_gt), year=years)
## tidy way of doing this, reshape2::melt() does
## it cleanly but is deprecated
#?pivot_longer.sf
#?rownames_to_column
#require(tibble)
library(tidyverse)
#?pivot_longer
D_gt <- D_gt %>% as.data.frame() %>%
tibble::rownames_to_column(var = "cell") %>%
#tibble::rownames_to_column(var = "cell") %>%
pivot_longer(-cell, names_to = "Year", values_to='D')
D <- merge(D_gt, mdl$PlotDF, by.x='cell', by.y='x2i')
D3<-lapply(((D)), as.numeric)
D4<-data.frame(D3)
head(D4)
summary(exp(D4$D))
summary((D4$D))
#Subet D values greater than .1% of D to plot
D02 <- D4[which((D$Year == "2002")),names(D4) %in% c("Year","D","Lat","Lon")]
D03 <- D4[which(D$Year == "2003"),names(D4) %in% c("Year","D","Lat","Lon")]
D04 <- D4[which(D$Year == "2004"),names(D4) %in% c("Year","D","Lat","Lon")]
D05 <- D4[which(D$Year == "2005"),names(D4) %in% c("Year","D","Lat","Lon")]
D06 <- D4[which(D$Year == "2006"),names(D4) %in% c("Year","D","Lat","Lon")]
D07 <- D4[which(D$Year == "2007"),names(D4) %in% c("Year","D","Lat","Lon")]
D08 <- D4[which(D$Year == "2008"),names(D4) %in% c("Year","D","Lat","Lon")]
D09 <- D4[which(D$Year == "2009"),names(D4) %in% c("Year","D","Lat","Lon")]
D10 <- D4[which(D$Year == "2010"),names(D4) %in% c("Year","D","Lat","Lon")]
D11 <- D4[which(D$Year == "2011"),names(D4) %in% c("Year","D","Lat","Lon")]
D12 <- D4[which(D$Year == "2012"),names(D4) %in% c("Year","D","Lat","Lon")]
D14 <- D4[which(D$Year == "2014"),names(D4) %in% c("Year","D","Lat","Lon")]
D16 <- D4[which(D$Year == "2016"),names(D4) %in% c("Year","D","Lat","Lon")]
D18 <- D4[which(D$Year == "2018"),names(D4) %in% c("Year","D","Lat","Lon")]
D5<-rbind(D02,D03,D04,D05,D06,D07,D08, D09,D10,D11,D12,D14,D16,D18)
D6<-lapply(D5, as.numeric)
D7<-data.frame(D6)
summary(D7)
write.csv(D7, "SOCKEYE_OUT.csv")
theme_set(theme_bw())
D7$D<=0.001*D #Plot the top 99.9% of the densities.
#########################
# FIGURE 4
#########################
pdf(file="FIGURE 4.pdf", height=11, width=8.5) #This generates the figure as a hi-res (pub quality) tiff file. You can change the dimensions of the figure by changing the height and width arguments. You can change the resolution with the 'res' argument.
g2 <- gmap +
theme_bw()+
#scale_color_gradientn(colours = colorspace::divergingx_hcl(palette="RdGy", n=7, rev=TRUE),oob=scales::squish,limits=c(0,4))+
#geom_polygon(data = temp, aes(x = Lon, y = Lat, group = NULL), fill=NA,color="lightgray" ,size = 0.005) +
geom_point(data=D7 , aes(Lon, Lat, color=(log(D)), group=NULL), #Use log(D) for GRP to covert back from exp(GRP)
#geom_point(data=temp2 , aes(Lon, Lat, color=(D), group=NULL), #Use log(D) for GRP to covert back from exp(GRP)
na.rm=TRUE, size=2, stroke=0, shape=16) +facet_wrap('Year')+
#labs(color=expression(paste("ln(kg/",km^2,")")))+
labs(color=paste('log(kg\U00B7km\u00b2+1)'))+
xlab(expression(paste(Longitude^o,~'W')))+
ylab(expression(paste(Latitude^o,~'N')))+
#coord_map(xlim = c(-173, -155),ylim = c(50, 66.5))+
labs(title="Juvenile sockeye salmon")+
theme(strip.text.x = element_text(size = 12,face="bold")) +
theme(axis.text=element_text(size=12),
axis.title=element_text(size=16,face="bold"),
panel.grid.major=element_line(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = "grey50"))
g2
dev.off()
####################################
# FIGURE 5: DIET PLOT
####################################
library(ggplot2)
library(viridis)
library(hrbrthemes)
library(tidyverse)
dir <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(dir)
#I converted to annual proportion before reading in
diet <- read.csv("SockDietProp.csv")
#Changed prey item names to appear the way I wanted them to, instead of _ or . separators.
diet.name <- diet %>% rename("Age-0 Pollock" = Age0pollock, "Arrow Worms" = Arrowworm, "Calanus spp." = Calanus,
"Large Copepods" = Largecopepods, "Other Crustaceans" = Othercrustaceans, "Small Copepods" = Smallcopepods)
#Lon pivot to make ggplot happy.
diet.Lon <- pivot_longer(diet.name, cols = 2:12, names_to = "PreyItem", values_to = "SCI")
#Quick easy way to remove the gaps on the plot for years with no data.
diet.Lon$Year <- as.factor(diet.Lon$Year)
#Reorder prey items to the order that I want them to appear in. I grouped fish with fish, crustaceans with crustaceans,
#descending order within each group.
pdf(file="FIGURE 5.pdf", height=8, width=11)
p1 <- diet.Lon %>%
mutate(PreyItem = fct_relevel(PreyItem,
"Age-0 Pollock", "Fish", "Euphausiids",
"Amphipods", "Calanus spp.", "Large Copepods", "Small Copepods",
"Other Crustaceans", "Arrow Worms", "Pteropods", "Other")) %>%
ggplot(aes(Year,PreyItem,fill=SCI))+
geom_tile(color= "white",size=0.1) +
scale_fill_viridis(name="%SCI",option ="C")+
theme_minimal(base_size = 8)+
theme(text = element_text(size = 12, colour="black"),axis.text=element_text(colour="black"),axis.title = element_text(colour = "black",size = 16))+
ylab("Prey Item")+
xlab("Year")
dev.off()
pdf(file="FIGURE 5.pdf", height=8, width=11)
p1+theme(axis.text.y=element_text(size=16),axis.text.x=element_text(size=16),axis.title.y = element_text(size=24),axis.title.x = element_text(size=24),legend.title = element_text(size = 24),
legend.text = element_text(size = 16),strip.background = element_rect(colour="white"),axis.ticks=element_blank())
dev.off()
####################################
# FIGURE 6 MODEL VALIDATION FIGURE
####################################
# FULL-SAMPLE =======================================================================
#Log transformed Predicted and observed data for Sockeye model ObsModel=c(2,3) full-sample
#log(Predicted)-exp(logSigmaM)^2/2
#LogSima -0.10784334 for data w/o 2013, 15, 17
#LogSimg w means for 13, 15, 17 -0.2715638
#Logsigma 27 (@,3)
settings = make_settings( Version="VAST_v14_0_1",
n_x=500,
Region='User',
purpose="index2",
bias.correct=TRUE,
max_cells = 2000,
ObsModel=c(2,1), # catch, Pollock, pink
fine_scale=TRUE,
treat_nonencounter_as_zero=TRUE,
knot_method='grid',
use_anisotropy = TRUE)
settings$Options = c( settings$Options, "report_additional_variables"=TRUE )
user_region <- readRDS('user_region.rds')
fit = fit_model( "settings"=settings,
"Lat_i"=temp[,'Lat'],
"Lon_i"=temp[,'Lon'],
"observations_LL" = temp[,c('Lat','Lon')],
"t_i"=temp[,'Year'],
"b_i"=(as_units(temp[,'kg'],'kg')),
"a_i"=(as_units(temp[,'AreaSwept'],'km^2')),
"getsd"=TRUE,
"test_fit"=TRUE,
"build_model" = TRUE,
input_grid=user_region ,
getReportCovariance=TRUE
)
Sockeye_Pred_in<-log(fit$Report$R2_i+1)-exp(-0.23875180)^2/2 #Predicted in-bag-sample log, old -0.06686186
Sockeye_Obs_in<-log(temp[,'kg']+1) #Observed in-bag-sample log
plot(Sockeye_Pred_in,Sockeye_Obs_in) #Plot
summary(lm(Sockeye_Obs_in~Sockeye_Pred_in)) #Linear regresson
#Not log transformed
Sockeye_Pred_in<-fit$Report$R2_i-exp(-0.23875180)^2/2 #Predicted in-bag-sample
Sockeye_Obs_in<-(temp[,'kg']) #Observed in-bag-sample
plot(Sockeye_Pred_in,Sockeye_Obs_in) #Plot
summary(lm(Sockeye_Obs_in~0+Sockeye_Pred_in)) #R2=0.67
#R2=0.8765
#Compare object to fit of out-bag sample
ParHat = fit$ParHat
ParHat
# Generate partitions in data
n_fold = 10
Partition_i = sample( 1:n_fold, size=nrow(temp), replace=TRUE )
prednll_f = rep(NA, n_fold )
# Loop through partitions, refitting each time with a different PredTF_i
for( fI in 1:n_fold ){
PredTF_i = ifelse( Partition_i==fI, TRUE, FALSE )
settings = make_settings( Version="VAST_v14_0_1",
n_x=500,
Region='User',
purpose="index2",
bias.correct=TRUE,
max_cells = 2000,
ObsModel=c(2,1), # catch, Pollock, pink
fine_scale=TRUE,
treat_nonencounter_as_zero=TRUE,
knot_method='grid',
use_anisotropy = TRUE)
# settings$RhoConfig["Beta2"] = 3
# Refit, starting at MLE, without calculating standard errors (to save time)
fit_new = fit_model("settings"=settings,
"Lat_i"=temp[,'Lat'],
"Lon_i"=temp[,'Lon'],
"observations_LL" = temp[,c('Lat','Lon')],
"t_i"=temp[,'Year'],
"b_i"=(as_units(temp[,'kg'],'kg')),
"a_i"=(as_units(temp[,'AreaSwept'],"km^2")),
"getsd"=TRUE,
"test_fit"=TRUE,
"build_model" = TRUE,
input_grid=user_region,
getReportCovariance=TRUE,"PredTF_i"=PredTF_i, "Parameters"=ParHat)
# Save fit to out-of-bag data
prednll_f[fI] = fit_new$Report$pred_jnll
}
# Check fit to all out=of-bag data and use as metric of out-of-bag performance
sum( prednll_f )
#1832
#OUT SAMPLE ===============================================================================
#Not log transformed
Sockeye_Pred_out<-fit_new$Report$R2_i
Sockeye_Obs_out<-(temp[,'kg'])
plot(Sockeye_Pred_out, Sockeye_Obs_out)
summary(lm(Sockeye_Obs_out~Sockeye_Pred_out)) #Not logged 0.737 w fake data.
#log transformed data
#log(Predicted)-exp(logSigmaM)^2/2
Sockeye_Pred_out<-log(fit_new$Report$R2_i+1)-exp(-0.2387518 )^2/2
Sockeye_Obs_out<-log(temp[,'kg']+1)
plot(Sockeye_Pred_out, Sockeye_Obs_out)
summary(lm(Sockeye_Obs_out~Sockeye_Pred_out)) #Not logged 0.56 w fake data.
data<-data.frame(Sockeye_Obs_in,Sockeye_Pred_in,Sockeye_Obs_out,Sockeye_Pred_out)
write.csv(data, file = "data2.csv")
data2<-read.csv("data2.csv") #Not logged in and out samples
Obs=data2[,1]
Sockeye_Obs_in=data2[,2]
Sockeye_Pred_in=data2[,3]
Sockeye_Obs_out=data2[,4]
Sockeye_Pred_out=data2[,5]
m2<-lm( Sockeye_Pred_in~Sockeye_Obs_in)
summary(m2)
m4<-lm( Sockeye_Pred_out~Sockeye_Obs_out)
summary(m4)
data3<-read.csv("data3.csv") #Not logged in and out samples
Sample=data3[,1]
Sockeye_Obs=data3[,2]
Sockeye_Pred=data3[,3]
summary(Sockeye_Pred)
summary(Sockeye_Obs)
pdf(file="FIGURE 6.pdf", height=8.5, width=11)
pg1 <- ggplot(data3, aes(Sockeye_Pred, Sockeye_Obs))+ylab("")+xlab("")+
ggtitle(expression(paste("in-sample ", R^2, "=0.87, p<0.0001 out-sample ", R^2, "=0.74, p<0.0001"))) + geom_point(shape = 1) +
facet_grid(~Sample)+ geom_abline(slope = 1) + facet_wrap(~Sample, nrow = 1) + theme(aspect.ratio = 1) +
xlim(-1,200)+
ylim(-1,200)+
# geom_point( )
geom_smooth(method = "lm", se = T, fullrange = T, colour = "blue", cex = 1)
grid.arrange(pg1,bottom=textGrob(expression(paste("Predicted")),
gp = gpar(col = "black", fontsize = 20)),left=textGrob(expression(paste("Observed")),rot=90,
gp = gpar(col = "black", fontsize = 20)),nrow = 1, ncol=1)
dev.off()
#################
# FIGURE 7 and 8
#################
###############################################
# Generate annual indices for each covariate
###############################################
settings = make_settings( Version="VAST_v14_0_1",
n_x=500,
Region='User',
purpose="index2",
bias.correct=TRUE,
max_cells = 2000,
ObsModel=c(2,1), # catch, Pollock, pink
# ObsModel=c(2,4), # calanus
fine_scale=TRUE,
treat_nonencounter_as_zero=TRUE,
knot_method='grid',
use_anisotropy = TRUE)
settings$Options = c( settings$Options, "report_additional_variables"=TRUE )
user_region <- readRDS('user_region.rds')
fit_SST_TS = fit_model( "settings"=settings,
"Lat_i"=temp[,'Lat'],
"Lon_i"=temp[,'Lon'],
"observations_LL" = temp[,c('Lat','Lon')],
"t_i"=temp[,'Year'],
"b_i"=(as_units(SST,"none")),
"a_i"=temp[,'Constant'],
"getsd"=TRUE,
"test_fit"=TRUE,
"build_model" = TRUE,
input_grid=user_region ,
getReportCovariance=TRUE
)
x=summary(fit_SST_TS$parameter_estimates$SD, select='report')
print(x)
write.csv(x,"SST_TS.csv", row.names = TRUE)
plot_results(fit_SST_TS, n_cells=2000 )
fit_Cal_TS = fit_model( "settings"=settings,
"Lat_i"=temp[,'Lat'],
"Lon_i"=temp[,'Lon'],
"observations_LL" = temp[,c('Lat','Lon')],
"t_i"=temp[,'Year'],
"b_i"=(as_units(log(temp[,'Calanus']+1),"count")),
"a_i"=temp[,'Constant'],
"getsd"=TRUE,
"test_fit"=TRUE,
"build_model" = TRUE,
input_grid=user_region ,
getReportCovariance=TRUE
)
x=summary(fit_Cal_TS$parameter_estimates$SD, select='report')
print(x)
write.csv(x,"Cal_TS.csv", row.names = TRUE)
plot_results(fit_Cal_TS, n_cells=2000 )
fit_Poll_TS = fit_model( "settings"=settings,
"Lat_i"=temp[,'Lat'],
"Lon_i"=temp[,'Lon'],
"observations_LL" = temp[,c('Lat','Lon')],
"t_i"=temp[,'Year'],
"b_i"=(as_units(log(temp[,'A0PollockN']+1),"count")),
"a_i"=(as_units(temp[,'AreaSwept'],'km^2')),
"getsd"=TRUE,
"test_fit"=TRUE,
"build_model" = TRUE,
input_grid=user_region ,
getReportCovariance=TRUE
)
x=summary(fit_Poll_TS$parameter_estimates$SD, select='report')
print(x)
write.csv(x,"Poll_TS.csv", row.names = TRUE)
plot_results(fit_Poll_TS, n_cells=2000 )
fit_Pink_TS = fit_model( "settings"=settings,
"Lat_i"=temp[,'Lat'],
"Lon_i"=temp[,'Lon'],
"observations_LL" = temp[,c('Lat','Lon')],
"t_i"=temp[,'Year'],
"b_i"=(as_units(log(temp[,'J_Pink']+1),"kg")),
"a_i"=(as_units(temp[,'AreaSwept'],'km^2')),
"getsd"=TRUE,
"test_fit"=TRUE,
"build_model" = TRUE,
input_grid=user_region ,
getReportCovariance=TRUE
)
x=summary(fit_Pink_TS$parameter_estimates$SD, select='report')
print(x)
write.csv(x,"Pink_TS.csv", row.names = TRUE)
plot_results(fit_Pink_TS, n_cells=2000 )
########################
# TIME SERIES PLOTS
########################
temp2<-read.csv("TimeseriesPlots.csv")
Year=temp2[,1]
J_Sockeye =temp2[,2]
J_sockeye_SE=temp2[,3]
Calanus =temp2[,4]
Calanus_SE=temp2[,5]
Pollock =temp2[,6]
Pollock_SE=temp2[,7]
J_Pink =temp2[,8]
J_Pink_SE=temp2[,9]
SST =temp2[,10]
SST_SE=temp2[,11]
Northing=temp2[,12]
Northing_SE =temp2[,13]
EAO =temp2[,14]
EAO_SE=temp2[,15]
lnCalanus=temp2[,16]
lnCalanus_SE=temp2[,17]
lnPollock =temp2[,18]
lnPollock_SE=temp2[,19]
lnPink =temp2[,20]
lnPink_SE=temp2[,21]
Easting=temp2[,22]
Easting_SE=temp2[,23]
require(cowplot)
require(gridExtra)
require(ggplot2)
require(reprex)
install.packages("reprex")
geom_ribbon(aes(x=Year,
ymin=Tmin,
ymax=Tmax),
fill='blue',alpha=0.2)
theme_bw()
#################################################################################
# FIGURE 7: Juvenile sockeye salmon annual indices of distribution and abundance
#################################################################################
pdf(file="FIGURE 7.pdf", height=11, width=8.5, paper='special', family ='serif')
p1<-ggplot(temp2, aes(x=Year, y=J_Sockeye)) +
theme_bw()+
geom_line()+
geom_pointrange(aes(ymin=J_Sockeye-J_Sockeye_SE, ymax=J_Sockeye+J_Sockeye_SE))+
labs(title="", y="Juvenile sockeye salmon (kg)", x="", cex=3)+
theme(text=element_text(family="sans"),axis.title = element_text(size = 12,colour = "black"),
axis.text = element_text(size = 12,colour = "black"),
panel.background = element_rect(colour = "black", linewidth=1.5))+
geom_point(size=4) +
geom_segment(aes(x=2001.5,y=0,xend=2005.5,yend=0), color="red",linewidth=2)+
geom_segment(aes(x=2005.5,y=0,xend=2012.5,yend=0), color="blue",linewidth=2)+
geom_segment(aes(x=2012.5,y=0,xend=2018.5,yend=0), color="red",linewidth=2)
p2<-ggplot(temp2, aes(x=Year, y=Northing)) +
theme_bw()+
geom_line()+
geom_pointrange(aes(ymin=Northing-Northing_SE, ymax=Northing+Northing_SE))+
labs(title="", y="Northing (km from Equator)", x="", cex=3)+
theme(text=element_text(family="sans"),axis.title = element_text(size = 12,colour = "black"),
axis.text = element_text(size = 12,colour = "black"),
panel.background = element_rect(colour = "black", linewidth=1.5))+
geom_point(size=4)+
geom_segment(aes(x=2001.5,y=6300,xend=2005.5,yend=6300), color="red",linewidth=2)+
geom_segment(aes(x=2005.5,y=6300,xend=2012.5,yend=6300), color="blue",linewidth=2)+
geom_segment(aes(x=2012.5,y=6300,xend=2018.5,yend=6300), color="red",linewidth=2)
p3<-ggplot(temp2, aes(x=Year, y=EAO)) +
theme_bw()+
geom_line()+
geom_pointrange(aes(ymin=EAO-EAO_SE, ymax=EAO+EAO_SE))+
labs(title="", y="", x="", cex=3)+
ylab(bquote('Area occupied '(km^2)))+
theme(text=element_text(family="sans"),axis.title = element_text(size = 12,colour = "black"),
axis.text = element_text(size = 12,colour = "black"),
panel.background = element_rect(colour = "black", linewidth=1.5))+
geom_point(size=4) +
geom_segment(aes(x=2001.5,y=0,xend=2005.5,yend=0), color="red",linewidth=2)+
geom_segment(aes(x=2005.5,y=0,xend=2012.5,yend=0), color="blue",linewidth=2)+
geom_segment(aes(x=2012.5,y=0,xend=2018.5,yend=0), color="red",linewidth=2)
p4<-ggplot(temp2, aes(x=Year, y=Easting)) +
theme_bw()+
geom_line()+
geom_pointrange(aes(ymin=Easting-Easting_SE, ymax=Easting+Easting_SE))+
labs(title="", y="Easting (km from 180)", x="", cex=3)+
theme(text=element_text(family="sans"),axis.title = element_text(size = 12,colour = "black"),
axis.text = element_text(size = 12,colour = "black"),
panel.background = element_rect(colour = "black", linewidth=1.5))+
geom_point(size=4)+
geom_segment(aes(x=2001.5,y=395,xend=2005.5,yend=395), color="red",linewidth=2)+
geom_segment(aes(x=2005.5,y=395,xend=2012.5,yend=395), color="blue",linewidth=2)+
geom_segment(aes(x=2012.5,y=395,xend=2018.5,yend=395), color="red",linewidth=2)
grid.arrange(p1,p2, p3,p4,bottom="Year",top="",left="",nrow = 2, ncol=2)
dev.off()
##############
# FIGURE 8
##############
pdf(file="FIGURE 8.pdf", height=11, width=8.5, paper='special')
p4<-ggplot(temp2, aes(x=Year, y=SST)) +
theme_bw()+
geom_line()+
geom_pointrange(aes(ymin=SST-SST_SE, ymax=SST+SST_SE))+
labs(title="", y="Temperature (Celsius)", x="", cex=3)+
theme(axis.title = element_text(size = 16,colour = "black"),
axis.text = element_text(size = 16,colour = "black"),
panel.background = element_rect(colour = "black", linewidth=1.5))+
geom_point(size=4)
p5<-ggplot(temp2, aes(x=Year, y=lnCalanus)) +
theme_bw()+
geom_line()+
geom_pointrange(aes(ymin=lnCalanus-lnCalanus_SE, ymax=lnCalanus+lnCalanus_SE))+
ylab(bquote('Calanus (ln '(N ~m^-2+1)))+
# ylab(reprex::bquote('Calanus (ln(#~m^-2+1))'))+
theme(axis.title = element_text(size = 16,colour = "black"),
axis.text = element_text(size = 16,colour = "black"),
panel.background = element_rect(colour = "black", linewidth=1.5))+
geom_point(size=4)
p6<-ggplot(temp2, aes(x=Year, y=lnPollock)) +
theme_bw()+
geom_line()+
geom_pointrange(aes(ymin=lnPollock-lnPollock_SE, ymax=lnPollock+lnPollock_SE))+
labs(title="", y="Age-0 Pollock (ln(#+1))", x="", cex=3)+
theme(axis.title = element_text(size = 16,colour = "black"),
axis.text = element_text(size = 16,colour = "black"),
panel.background = element_rect(colour = "black", linewidth=1.5))+
geom_point(size=4)
p7<-ggplot(temp2, aes(x=Year, y=lnPink)) +
theme_bw()+
geom_line()+
geom_pointrange(aes(ymin=lnPink-lnPink_SE, ymax=lnPink+lnPink_SE))+
labs(title="", y="Juvenile pink salmon (ln(kg+1))", x="", cex=3)+
theme(axis.title = element_text(size = 16,colour = "black"),
axis.text = element_text(size = 16,colour = "black"),
panel.background = element_rect(colour = "black", linewidth=1.5))+
geom_point(size=4)
grid.arrange(p4,p5,p6,p7,bottom="Year",top="",left="",nrow = 2, ncol=2)
dev.off()
##############################################
# FIGURE 9: Relationship among annual indices
##############################################
pdf(file="FIGURE 9.pdf", height=11, width=8.5, paper='special')
p1 <- ggplot(temp2, aes(Temp_20m, J_Sockeye)) +
geom_point()+
xlab(NULL)+
ylab("J. sockeye salmon (kg)")+
ggtitle(bquote(R^2~'=0.36, p=0.03'))+
geom_smooth(method = lm, formula = y ~ poly(x, 2), se = TRUE)+
theme_cowplot(12)+
theme(plot.title = element_text(size = 12, face = "bold"))
p2 <- ggplot(temp5, aes(Calanus/1000000, J_Sockeye)) +
geom_point()+
ylab(NULL)+
xlab(NULL)+
ggtitle(bquote('NS'))+
geom_smooth(method = lm, formula = y ~ poly(x, 2), se = TRUE)+
theme_cowplot(12)
p3 <- ggplot(temp5, aes(lnPollock, J_Sockeye)) +
geom_point()+
ylab(NULL)+
xlab(NULL)+
ggtitle(bquote(R^2~'=0.45, p=0.01'))+
geom_smooth(method = lm, formula = y ~ poly(x, 2), se = TRUE)+
theme_cowplot(12)
p4 <- ggplot(temp5, aes(lnPink, J_Sockeye)) +
geom_point()+
ylab(NULL)+
ggtitle(bquote(R^2~'=0.37, p=0.03'))+
xlab(NULL)+