-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kspp_model_runs_glmm.Rmd
1193 lines (917 loc) · 47.8 KB
/
Kspp_model_runs_glmm.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: 'Kogia spp. habitat models'
output:
html_document:
toc: true
toc_depth: 4
toc_float: true
theme: spacelab
fig_caption: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r load packages, include = TRUE, message = FALSE, warning = FALSE}
library(mgcv)
library(MASS)
library(rgdal)
library(raster)
library(ggplot2)
library(rgeos)
library(mapview)
library(leaflet)
library(psych)
library(broom)
library(plotrix)
library(magrittr)
library(colorRamps)
library(lubridate)
library(HabitatProject)
library(lmtest)
library(glmmTMB)
library(bbmle)
source('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/HabitatProject/R/multiplot.R')
options(stringsAsFactors = FALSE)
# load some preferences
load('E:/NASData/ModelData/Kspp/setup_info_Kspp.Rdata')
load('E:/NASData/ModelData/Kspp/KsppMergedData.Rdata')
outDir <- file.path('E:/NASData/ModelData',SP,'/')
```
<br>
# I. Exploratory analysis
## Data Inputs
The visual data goes back to 1992, but as shown in the figure below, many predictor variables are only available starting in 2003, therefore earlier visual data is currently excluded from further analyses.
Note: Future work could use monthly climatologies (averages) so that older sightings data could be used. Some dynamic drivers like eddy and front locations would not be able to be considered using that approach.
```{r Missing data}
covarList<-names(mergedSegments[c(2,5:length(mergedSegments))])
percFilled <- plot.missingdata(mergedSegments,covarList,paste0(outDir,'AcousticAndVisual_',SP))
percFilled <- plot.missingdata(AcOnlySegments,covarList,paste0(outDir,'AcousticOnly_',SP))
percFilled <- plot.missingdata(VisOnlySegments,covarList,paste0(outDir,'VisualOnly_',SP))
```
![](E:/NASData/ModelData/Kspp/AcousticOnly_Kspp_missingData.png)
<br>
![](E:/NASData/ModelData/Kspp/VisualOnly_Kspp_missingData.png)
### Testing and Training Sets
The data are split into training and testing sets. In this case, data from 2009 and 2013 are used for testing. Only observations post-2003 are used for modeling due to covariate limitations.
```{r test train split}
# If you decide from the missing data plots that you want to restrict years going forward:
yearListIdx = as.numeric(format(mergedSegments$date,"%Y"))
yearListIdx_AcOnly = as.numeric(format(AcOnlySegments$date,"%Y"))
yearListIdx_VisOnly = as.numeric(format(VisOnlySegments$date,"%Y"))
keepDates.train <- which(yearListIdx != 2009 & yearListIdx >= 2003 & yearListIdx <= 2012)
keepDates.test <- which(yearListIdx == 2009 | yearListIdx == 2013)
keepDates_AcOnly.train <- which(yearListIdx_AcOnly != 2009 & yearListIdx_AcOnly >= 2003 & yearListIdx_AcOnly <= 2012)
keepDates_AcOnly.test <- which(yearListIdx_AcOnly == 2009 | yearListIdx_AcOnly == 2013)
keepDatesVisOnly.train <- which(yearListIdx_VisOnly != 2009 & yearListIdx_VisOnly >= 2003)
keepDatesVisOnly.test <- which(yearListIdx_VisOnly == 2009)
mergedTrain.set<- mergedSegments[keepDates.train,]
Train_AcOnly.set <- AcOnlySegments[keepDates_AcOnly.train,]
Train_VisOnly.set<- VisOnlySegments[keepDatesVisOnly.train,]
mergedTest.set<- mergedSegments[keepDates.test,]
Test_AcOnly.set<- AcOnlySegments[keepDates_AcOnly.test,]
Test_VisOnly.set<- VisOnlySegments[keepDatesVisOnly.test,]
```
<br>
### Preliminary Mapping
The visual data selected for modeling are displayed on the map below. Data from 2009 were held back for testing. Blue markers indicate HARP locations.
```{r map inputs, warning = FALSE}
#Get test visual sightings
sightingsTrain <- Train_VisOnly.set[Train_VisOnly.set$Density>0,c('lat','long','date')]
sightingsTest <- Test_VisOnly.set[Test_VisOnly.set$Density>0,c('lat','long','date')]
HARPsites <- unique(Train_AcOnly.set[c('lat','long')])
pal <-colorFactor(palette = "RdYlGn",
domain = c(2003,2004,2009,2012,2014))
map1 <- leaflet() %>% setView(lng = -89.4, lat = 27.0, zoom = 6)%>%
addProviderTiles(providers$Esri.OceanBasemap) %>%
addCircleMarkers(data = sightingsTrain, lng = ~ long, lat = ~ lat,color = ~pal(year(date)),
stroke = TRUE, fillOpacity = 0.8, group = 'Training Set',radius = 4)%>%
addCircleMarkers(data = sightingsTest, lng = ~ long, lat = ~ lat,color = ~pal(year(date)),
stroke = TRUE, fillOpacity = 0.8, group = 'Test Set',radius = 4)%>%
addMarkers(data = HARPsites, lng = ~ long, lat = ~ lat) %>%
addLegend(pal = pal,values = c(2003,2004,2009,2012,2014),title = 'Year')
map1
```
<br>
The time series below show the acoustic data used for modeling. Data from 2011 and 2012 used for training, and 2013 data is held back for testing. These density magnitudes are very preliminary,and models only used presence absence data.
```{r plot timeseries, message = FALSE}
plot.timeseries(siteList,outDir,AcOnlySegments)
```
![](E:/NASData/ModelData/Kspp/Kspp_Timeseries_Site_MC.png)
![](E:/NASData/ModelData/Kspp/Kspp_Timeseries_Site_GC.png)
![](E:/NASData/ModelData/Kspp/Kspp_Timeseries_Site_DT.png)
<br>
## Examine Covariates
### Identify Outliers
Replace extreme outliers (bad data) with NaNs.
```{r remove outliers, message = FALSE, results = 'hide'}
# Identify and clear problematic outliers
outlierList <-which(mergedSegments$CHL< -10)
mergedSegments$CHL[outlierList] <- NaN
outlierList <-which(mergedSegments$FrontDist_Cayula>800000)
mergedSegments$FrontDist_Cayula[outlierList] <- NaN
outlierList <-which(mergedSegments$Density>10000)
mergedSegments$Density[outlierList] <- NaN
outlierList <-which(AcOnlySegments$CHL< -10)
AcOnlySegments$CHL[outlierList] <- NaN
outlierList <-which(AcOnlySegments$FrontDist_Cayula>800000)
AcOnlySegments$FrontDist_Cayula[outlierList] <- NaN
outlierList <-which(AcOnlySegments$Density>10000)
AcOnlySegments$Density[outlierList] <- NaN
outlierList <-which(VisOnlySegments$CHL< -10)
VisOnlySegments$CHL[outlierList] <- NaN
outlierList <-which(VisOnlySegments$FrontDist_Cayula>800000)
VisOnlySegments$FrontDist_Cayula[outlierList] <- NaN
outlierList <-which(VisOnlySegments$Density>10000)
VisOnlySegments$Density[outlierList] <- NaN
# getRid of rows with NaN because glmmTMD doesnt seem to handle them
mergedSegments_NoNa <-which(rowSums(is.na(mergedSegments))>0)
AcOnlySegments_NoNa <-which(rowSums(is.na(AcOnlySegments))>0)
VisOnlySegments_NoNa <-which(rowSums(is.na(VisOnlySegments))>0)
mergedSegments<-VisOnlySegments[mergedSegments_NoNa,]
AcOnlySegments<-VisOnlySegments[AcOnlySegments_NoNa,]
VisOnlySegments<-VisOnlySegments[VisOnlySegments_NoNa,]
```
<br>
### Check Covariate Distributions
Covariates have different distributions across the observations.
Here are the distributions of covariates from the acoustic observations:
```{r dot plots, eval = TRUE, message = FALSE, results = 'hide'}
plot.cleveland(mergedTrain.set,covarList,FALSE,paste0(outDir,'AcousticAndVisual_',SP))
plot.cleveland(Train_AcOnly.set,covarList,FALSE,paste0(outDir,'AcousticOnly_',SP))
plot.cleveland(Train_VisOnly.set,covarList,FALSE,paste0(outDir,'VisualOnly_',SP))
```
![](E:/NASData/ModelData/Kspp/AcousticOnly_Kspp_clevelandDots_noTransform.png)
<br>
<br>
Here are the distributions of covariates from the visual observations:
![](E:/NASData/ModelData/Kspp/VisualOnly_Kspp_clevelandDots_noTransform.png)
<br>
Some of these covariates are more or less interrelated. HYCOM estimates of salinity at the surface (HYCOM_SALIN_0) and at 100m (HYCOM_SALIN_100) are very similar, so I selected surface salinity for simplicity. HYCOM current and upwelling estimates at 100m were also removed (HYCOM_MAG_100,HYCOM_UPVEL_100). HYCOM current direction is site specific in the acoustic data, so it was excluded for now.
Remaining correlations are examined in the figure below. Numbers closer to 1 above the diagonal in the figure below represent correlation coefficients. Highly correlated covariates should not be used together in the same model. Day of year was excluded to avoid artifacts associated with the temporal differences of the datasets (Acoustic data are year-round, and visual data are collected in spring/summer).
```{r correlation plots, eval = TRUE, message = FALSE, warning = FALSE, results = 'hide'}
covarList2 <- c("SST","SSH","CHL","HYCOM_MLD",
"HYCOM_SALIN_0","HYCOM_DIR_0",
"HYCOM_MAG_0",
"HYCOM_UPVEL_50","FrontDist_Cayula",
"EddyDist","Neg_EddyDist","Pos_EddyDist",
"DayOfYear","fac1","fac2")
# restrict covariates again to limited set
mergedTrain.set2<- mergedTrain.set[,covarList2]
mergedTest.set2<- mergedTest.set[,covarList2]
Train_AcOnly.set2<- Train_AcOnly.set[,covarList2]
Test_AcOnly.set2<- Test_AcOnly.set[,covarList2]
Train_VisOnly.set2<- Train_VisOnly.set[,covarList2]
Test_VisOnly.set2<- Test_VisOnly.set[,covarList2]
# without transform
png(paste(outDir,SP,'_correlations_noTransform.png',sep=''), width = 2000, height = 1600)
pairs.panels(mergedTrain.set2[,1:(length(covarList2)-2)], ellipses=FALSE, method = "spearman",cex.cor=.75)
dev.off()
png(paste(outDir,SP,'_correlations_noTransform_AcOnly.png',sep=''), width = 2000, height = 1600)
pairs.panels(Train_AcOnly.set2[,1:(length(covarList2)-2)], ellipses=FALSE, method = "spearman",cex.cor=.75)
dev.off()
png(paste(outDir,SP,'_correlations_noTransform_visOnly.png',sep=''), width = 2000, height = 1600)
pairs.panels(Train_VisOnly.set2[,1:(length(covarList2)-2)], ellipses=FALSE, method = "spearman",cex.cor=.75)
dev.off()
```
![](E:/NASData/ModelData/Kspp/Kspp_correlations_noTransform.png)
<br>
### Transform Predictor Variables
Some variables, including chlorophyll, current magnitude, mixed layer depth and distance to fronts are highly skewed and can be log-transformed.
```{r transform covars, message = FALSE, results = 'hide'}
# covarList2 <- c("SST","SSH","CHL","HYCOM_MLD",
# "HYCOM_SALIN_0","HYCOM_DIR_0",
# "HYCOM_MAG_0",
# "HYCOM_UPVEL_50","FrontDist_Cayula",
# "EddyDist","Neg_EddyDist", "PosEddyDist",
# "DayOfYear","fac1","fac2")
transformList <- c("none","none","log10","log10",
"none","none",
"log10",
"none","log10",
"none","none","none",
"none","none","none")
transformedCovars.train <-
transform.covars(mergedTrain.set2,covarList2,transformList)
transformedCovars.test <-
transform.covars(mergedTest.set2,covarList2,transformList)
transformedCovars_AcOnly.train <-
transform.covars(Train_AcOnly.set2,covarList2,transformList)
transformedCovars_AcOnly.test <-
transform.covars(Test_AcOnly.set2,covarList2,transformList)
transformedCovars_VisOnly.train <-
transform.covars(Train_VisOnly.set2,covarList2,transformList)
transformedCovars_VisOnly.test <-
transform.covars(Test_VisOnly.set2,covarList2,transformList)
# Generate correlation plots with transform
png(paste(outDir,SP,'_correlations_withTransform.png',sep=''), width = 2000, height = 1600)
pairs.panels(transformedCovars.train[,1:(length(covarList2)-2)], ellipses=FALSE, method = "spearman",cex.cor=.75)
dev.off()
png(paste(outDir,SP,'_correlations_withTransform_AcOnly.png',sep=''), width = 2000, height = 1600)
pairs.panels(transformedCovars_AcOnly.train[,1:(length(covarList2)-2)], ellipses=FALSE, method = "spearman",cex.cor=.75)
dev.off()
png(paste(outDir,SP,'_correlations_withTransform_visOnly.png',sep=''), width = 2000, height = 1600)
pairs.panels(transformedCovars_VisOnly.train[,1:(length(covarList2)-2)], ellipses=FALSE, method = "spearman",cex.cor=.75)
dev.off()
```
<br>
Plotting the transformed variables:
```{r transformed dot plots, eval = TRUE, message = FALSE, results = 'hide'}
plotCols = colnames(transformedCovars.train)[1:(length(covarList2)-2)]
plot.cleveland(transformedCovars.train,
plotCols,TRUE,paste0(outDir,'AcousticAndVisual_',SP))
plot.cleveland(transformedCovars_AcOnly.train,
plotCols,TRUE,paste0(outDir,'AcousticOnly_',SP))
plot.cleveland(transformedCovars_VisOnly.train,
plotCols,TRUE,paste0(outDir,'VisualOnly_',SP))
```
<br>
Below, the two sets of covariates have been combined and transformed.
![](E:/NASData/ModelData/Kspp/AcousticAndVisual_Kspp_clevelandDots_withTransform.png)
<br>
### Check Predictors
To get an idea of the basic predictive power of these covariates, we can look at presence/absence relative to each variable. This also provides an opportunity to look at the range of values observed for each covariate in the visual and acoustic datasets. In the plots below dotted lines indicate the distribution of each covariate when animals were present, and solid lines indicate the distribution when animals were absent. Note that these plots do not account for effort.
```{r presence absence histograms, eval = TRUE, message = FALSE, results = 'hide'}
plot.covarDensity(transformedCovars.train[,1:(length(transformedCovars.train)-2)],
colnames(transformedCovars.train[,1:(length(transformedCovars.train)-2)]),
mergedTrain.set$Density,paste0(outDir,'Both_',SP))
plot.covarDensity(transformedCovars_AcOnly.train[,1:(length(transformedCovars_AcOnly.train)-2)],
colnames(transformedCovars_AcOnly.train[1:(length(transformedCovars_AcOnly.train)-2)]),
Train_AcOnly.set$Density,paste0(outDir,'AcousticOnly_',SP))
plot.covarDensity(transformedCovars_VisOnly.train[,1:(length(transformedCovars_VisOnly.train)-2)],
colnames(transformedCovars_VisOnly.train[,1:(length(transformedCovars_VisOnly.train)-2)]),
Train_VisOnly.set$Density,paste0(outDir,'VisualOnly_',SP))
```
<br>
Acoustic kernel densities:
![](E:/NASData/ModelData/Kspp/AcousticOnly_Kspp_density_pres_abs.png)
<br>
Visual kernel densities:
![](E:/NASData/ModelData/Kspp/VisualOnly_Kspp_density_pres_abs.png)
<br>
### Estimate Relative Weights
<br>
Currently, weights are based on the following logic:
* Acoustic data represent presence in 1 day bins,
* Acoustic data have a maximum detection range of distance of 5km (for dolphins and beaked whales), for a total monitoring area of $(5 km)^{2} \pi$,
* Visual data represent presence during ~10 km transect segments traveling at ~10 knots or ~18.5km/hr. Therefore each visual datapoint represents ~0.54 hours of effort.
* Visual estimated strip width (ESW) varies depending on the conditions. ESW for each sighting is multiplied by trasenct segment length (usually ~10km) and doubled to estimate total area swept.
<br>
\[\text{Acoustic to Visual ratio} = \frac{24 hrs \cdot (5 km)^{2} \pi}{(\text{Segment length}/18.52 km/hr) \cdot \text{ESW} \cdot \text{Segment length} \cdot 2} = 5.58 \approx 6 : 1
\]
<br>
The best visual detection probability (model shown below) incorporated sea state and visibility, with a half-normal distribution.
![](E:/NASData/ModelData/Kspp/KsppsightwTrunc_GU.png)
<br>
```{r Calculate weights}
cat(paste0('Mean visual data point weight: ',
round(mean(Train_VisOnly.set$Weight)),
'\nAcoustic data point weight: ',
round(mean(Train_AcOnly.set$Weight))))
```
<br>
# II. Model Fitting
Models were fit using gamm from the mgcv package in R.
```{r load starting data}
weeklyOccurrenceFile = paste0(outDir,'ALLSITES_weeklyPOccurrence_Kspp_jahStart.csv')
pOccur <- read.csv(weeklyOccurrenceFile, header = TRUE,na.strings=c('',' ','NA','NaN'))
```
<br>
Initialize model parameters:
```{r model setup}
# Run & evaluate models
kVal <- 5 # choose a low value to avoid overfitting
plot(rowSums(is.na(transformedCovars_AcOnly.train)))
yAcOnly_TF <- as.logical(Train_AcOnly.set$Density >0)
yVisOnly_TF <- as.logical(Train_VisOnly.set$Density >0)
y_TF <- as.logical(mergedTrain.set$Density>0)
yAcOnly <- Train_AcOnly.set$Density
yVisOnly <- Train_VisOnly.set$Density
y <- mergedTrain.set$Density
# initialize empty structure for model storage
glmm_full_AcOnly_TF<-NULL
glmm_full_VisOnly_TF<-NULL
glmm_full_TF<-NULL
fac3 <- array(data = NA, dim = c(length(transformedCovars.train$fac1),1))
fac4 <- array(data = NA, dim = c(length(transformedCovars.train$fac1),1))
for (iFac in 1:length(transformedCovars.train$fac1)) {
if (!is.na(transformedCovars.train$fac1[iFac])){
if (transformedCovars.train$fac1[iFac]>5) {
fac3[iFac,1] <- 1
fac4[iFac,1] <- 1
} else {
fac3[iFac,1] <- 0
fac4[iFac,1] <- transformedCovars.train$fac1[iFac]+1
}
}
}
transformedCovars.train$fac3<-fac3
transformedCovars.train$fac4<-fac4
```
```{r}
fac3 <- array(data = NA, dim = c(length(transformedCovars.test$fac1),1))
fac4 <- array(data = NA, dim = c(length(transformedCovars.test$fac1),1))
for (iFac in 1:length(transformedCovars.test$fac1)) {
if (!is.na(transformedCovars.test$fac1[iFac])){
if (transformedCovars.test$fac1[iFac]>5) {
fac3[iFac,1] <- 1
fac4[iFac,1] <- 0
} else {
fac3[iFac,1] <- 0
fac4[iFac,1] <- transformedCovars.test$fac1[iFac]
}
}
}
transformedCovars.test$fac3<-fac3
transformedCovars.test$fac4<-fac4
transformedCovars_AcOnly.train$times<-factor(1:length(transformedCovars_AcOnly.train$fac1))
```
<br>
## Run Full Models
Run full binomial GLMMs on Acoustic only, Visual only, and joint Acoustic/Visual datasets.
```{r AC model 1, eval = FALSE}
glmm_full_AcOnly_ed <- glmmTMB(yAcOnly ~ SST+SSH+log10_CHL+log10_HYCOM_MLD+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+EddyDist+ (1|fac1),
data = transformedCovars_AcOnly.train,ziformula=~fac1,family=poisson)
summary(glmm_full_AcOnly_ed)
glmm_full_AcOnly_ed_pruned <- glmmTMB(yAcOnly ~ SST+SSH+log10_HYCOM_MLD+EddyDist+ (1|fac1),
data = transformedCovars_AcOnly.train,ziformula=~fac1,family=poisson)
summary(glmm_full_AcOnly_ed_pruned)
glmm_full_AcOnly_ped <- glmmTMB(yAcOnly ~ SST+SSH+log10_CHL+log10_HYCOM_MLD+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+Pos_EddyDist+ (1|fac1),
data = transformedCovars_AcOnly.train,ziformula=~fac1,family=poisson)
summary(glmm_full_AcOnly_ped)
glmm_full_AcOnly_ped_pruned <- glmmTMB(yAcOnly ~ SST+SSH+log10_HYCOM_MLD+ (1|fac1),
data = transformedCovars_AcOnly.train,ziformula=~fac1,family=poisson)
summary(glmm_full_AcOnly_ped_pruned)
glmm_full_AcOnly_ned <- glmmTMB(yAcOnly ~ SST+SSH+log10_CHL+log10_HYCOM_MLD+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+Neg_EddyDist+ (1|fac1),
data = transformedCovars_AcOnly.train,ziformula=~fac1,family=poisson)
summary(glmm_full_AcOnly_ned)
glmm_full_AcOnly_ied <- glmmTMB(yAcOnly ~ SST+SSH*EddyDist+log10_CHL+log10_HYCOM_MLD+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+ (1|fac1),
data = transformedCovars_AcOnly.train,ziformula=~fac1,family=poisson)
summary(glmm_full_AcOnly_ied)
glmm_full_AcOnly_ied_pruned <- glmmTMB(yAcOnly ~ SST+SSH*EddyDist+log10_HYCOM_MLD+(1|fac1),
data = transformedCovars_AcOnly.train,ziformula=~fac1,family=poisson)
summary(glmm_full_AcOnly_ied_pruned)
AICtab(glmm_full_AcOnly_ed_pruned,glmm_full_AcOnly_ped_pruned,glmm_full_AcOnly_ied_pruned)
bestACOnly_glmm <- glmm_full_AcOnly_ied_pruned
```
```{r other models 1, eval = FALSE, include = TRUE}
yVisOnly_norm <-round((yVisOnly/max(yVisOnly)*10))
glmm_full_VisOnly_ed <- glmmTMB(yVisOnly_norm ~SST+SSH+log10_HYCOM_MLD+log10_CHL+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+EddyDist,
data = transformedCovars_VisOnly.train,ziformula=~1,family=poisson)
summary(glmm_full_VisOnly_ed)
glmm_full_VisOnly_ed_pruned <- glmmTMB(yVisOnly_norm ~SST+log10_HYCOM_MLD+log10_CHL+HYCOM_SALIN_0
+log10_FrontDist_Cayula+EddyDist,
data = transformedCovars_VisOnly.train,ziformula=~1,family=poisson)
summary(glmm_full_VisOnly_ed_pruned)
glmm_full_VisOnly_ned <- glmmTMB(yVisOnly_norm ~ SST+SSH+log10_HYCOM_MLD+log10_CHL+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+Neg_EddyDist,
data = transformedCovars_VisOnly.train,ziformula=~1,family=poisson)
summary(glmm_full_VisOnly_ned)
glmm_full_VisOnly_ned_pruned <- glmmTMB(yVisOnly_norm ~ SST+log10_CHL+HYCOM_SALIN_0
+HYCOM_DIR_0+Neg_EddyDist,
data = transformedCovars_VisOnly.train,ziformula=~1,family=poisson)
glmm_full_VisOnly_ped <- glmmTMB(yVisOnly_norm ~ SST+SSH+log10_HYCOM_MLD+log10_CHL+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+Pos_EddyDist,
data = transformedCovars_VisOnly.train,ziformula=~1,family=poisson)
summary(glmm_full_VisOnly_ped)
glmm_full_VisOnly_ped_pruned <- glmmTMB(yVisOnly_norm ~ SST++log10_HYCOM_MLD+log10_CHL+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_FrontDist_Cayula+Pos_EddyDist,
data = transformedCovars_VisOnly.train,ziformula=~1,family=poisson)
glmm_full_VisOnly_ied <- glmmTMB(yVisOnly_norm ~ SST+log10_HYCOM_MLD+log10_CHL+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+SSH*EddyDist,
data = transformedCovars_VisOnly.train,ziformula=~1,family=poisson)
summary(glmm_full_VisOnly_ied)
glmm_full_VisOnly_ied_pruned <- glmmTMB(yVisOnly_norm ~ SST+log10_HYCOM_MLD+log10_CHL+HYCOM_SALIN_0
+log10_FrontDist_Cayula+SSH*EddyDist,
data = transformedCovars_VisOnly.train,ziformula=~1,family=poisson)
AICtab(glmm_full_VisOnly_ed_pruned,glmm_full_VisOnly_ned_pruned,glmm_full_VisOnly_ped_pruned,glmm_full_VisOnly_ied_pruned)
bestVisOnly_glmm <- glmm_full_VisOnly_ed_pruned
glmm_full_ed <- glmmTMB(y ~ SST+SSH+log10_HYCOM_MLD+log10_CHL+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+EddyDist+ (1|fac4),
data = transformedCovars.train,ziformula=~fac4,family=poisson)
summary(glmm_full_ed)
glmm_full_ed_pruned <- glmmTMB(y ~ SST+SSH+log10_HYCOM_MLD+EddyDist+ (1|fac4),
data = transformedCovars.train,ziformula=~fac4,family=poisson)
summary(glmm_full_ed_pruned)
glmm_full_ped <- glmmTMB(y ~ SST+SSH+log10_CHL+log10_HYCOM_MLD+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+Pos_EddyDist+ (1|fac4),
data = transformedCovars.train,ziformula=~fac4,family=poisson)
summary(glmm_full_ped)
glmm_full_ped_pruned <- glmmTMB(y ~ SST+SSH+log10_HYCOM_MLD+ (1|fac4),
data = transformedCovars.train,ziformula=~fac4,family=poisson)
summary(glmm_full_ped_pruned)
glmm_full_ned <- glmmTMB(y ~ SST+SSH+log10_CHL+log10_HYCOM_MLD+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+Neg_EddyDist+ (1|fac4),
data = transformedCovars.train,ziformula=~fac4,family=poisson)
summary(glmm_full_ned)
glmm_full_ied <- glmmTMB(y ~ SST+SSH*EddyDist+log10_CHL+log10_HYCOM_MLD+HYCOM_SALIN_0
+HYCOM_DIR_0+log10_HYCOM_MAG_0+HYCOM_UPVEL_50+log10_FrontDist_Cayula+ (1|fac4),
data = transformedCovars.train,ziformula=~fac4,family=poisson)
summary(glmm_full_ied)
glmm_full_ied_pruned <- glmmTMB(y ~ SST+SSH*EddyDist+log10_HYCOM_MLD+(1|fac4),
data = transformedCovars.train,ziformula=~fac4,family=poisson)
summary(glmm_full_ied_pruned)
AICtab(glmm_full_ed_pruned,glmm_full_ped_pruned,glmm_full_ied_pruned)
best_full_glmm <-glmm_full_ied_pruned
```
<br>
Done trying different model configurations for now, time to compare the results.
<br>
```{r AC best model summary}
summary(bestACOnly_glmm)
plot(residuals(bestACOnly_glmm))
save(bestACOnly_glmm,file = paste(outDir,SP,'_best_AcOnly_glmm.Rdata',sep=''))
```
<br>
<br>
## Best Visual Only Model
```{r Vis best model summary}
summary(bestVisOnly_glmm)
plot(residuals(bestVisOnly_glmm))
save(bestVisOnly_glmm,file = paste(outDir,SP,'_best_VisOnly_glmm.Rdata',sep=''))
```
<br>
## Best Joint Visual & Acoustic Model
```{r best joint model summary}
summary(best_full_glmm)
plot(residuals(best_full_glmm))
save(best_full_glmm,file = paste(outDir,SP,'_best_joint_glmm.Rdata',sep=''))
```
<br>
# IV. Predict on Test Data
## Temporal prediction
<br>
### Acoustic Only Model
Predict on acoustic test data, and compare with actual observations for 2013. We are comparing the predicted probability of encountering animals with the actual weekly rate of occurrence of animals at a site.
CAVEAT: Encounter probability from the data is estimated as fraction of days per week during which Risso's were detected. This results in a pretty coarse estimate.
Alternatively we could compare to daily densities or something, but then the y-axis scales are completely different and it may be an apples to oranges comparison. An earlier version used densities, and seemed to suggest that lagging variables might be appropriate...
```{r Ac Temporal Predict}
# Predict on acoustic test data, using acoustic only model for comparison...
compAcSet_MC <- which((Test_AcOnly.set$fac2)==5)
compAcSet_GC <- which((Test_AcOnly.set$fac2)==10)
compAcSet_DT <- which((Test_AcOnly.set$fac2)==15 |
(Test_AcOnly.set$fac2)==16)
# Predict in time
dateTicks = as.POSIXct(c('2013-01-01 GMT','2013-04-01 GMT',
'2013-07-01 GMT','2013-10-01 GMT',
'2014-01-01 GMT'))
dateLabels = c('Jan. 2013','Apr. 2013','Jul. 2013','Oct 2013','Jan. 2014')
predAcOnly_MC <- predict(bestACOnly_glmm,
newdata= transformedCovars_AcOnly.test[compAcSet_MC,],
zitype = 'response')
predAcOnly_GC <- predict(bestACOnly_glmm,
newdata= transformedCovars_AcOnly.test[compAcSet_GC,],
zitype = 'response')
predAcOnly_DT <- predict(bestACOnly_glmm,
newdata= transformedCovars_AcOnly.test[compAcSet_DT,],
zitype = 'response')
occurIdx <- which(as.POSIXct(pOccur[,1])>='2013-01-01' & as.POSIXct(pOccur[,1])<'2014-01-01')
AcOnly_predictionSet <- data.frame(unique(Test_AcOnly.set$date))
colnames(AcOnly_predictionSet) <-"date"
AcOnly_predictionSet$MC <- NA
MC_times <- Test_AcOnly.set$date[compAcSet_MC]
m1 <- match(MC_times,AcOnly_predictionSet$date)
AcOnly_predictionSet$MC[m1] <-predAcOnly_MC
AcOnly_predictionSet$GC <- NA
GC_times <- Test_AcOnly.set$date[compAcSet_GC]
m2 <- match(GC_times,AcOnly_predictionSet$date)
AcOnly_predictionSet$GC[m2] <-predAcOnly_GC
AcOnly_predictionSet$DT <- NA
DT_times <- Test_AcOnly.set$date[compAcSet_DT]
m3 <- match(DT_times,AcOnly_predictionSet$date)
AcOnly_predictionSet$DT[m3] <-predAcOnly_DT
AcOnly_predictionSet$Legend <- "Predictions"
pOccur$Legend <- "Observations"
yTest <-Train_AcOnly.set$Density[compAcSet_MC]
MSE.nn.test <- sum((yTest - predAcOnly_MC)^2)/length(yTest)
```
```{r,fig.height = 10, fig.width = 7, message = FALSE, warning = FALSE}
# par(mfrow = c(5,1),oma=c(3,0,5,0)),colour="#000099",colour="#CC0000"
p1_Ac <- ggplot() +
geom_line(data = pOccur[occurIdx,],
aes(x = as.POSIXct(dateStr), y = MC, color = Legend))+
geom_line(data = AcOnly_predictionSet,
aes(x=date, y=MC, color = Legend)) +
labs(y = "", x = "")+
scale_color_manual("",values= c("gray48","#009999"))+
theme(legend.position = c(0.9, 0.7))
p2_Ac <- ggplot() +
geom_line(data = pOccur[occurIdx,],
aes(x =as.POSIXct(dateStr), y = GC, color = Legend))+
geom_line(data = AcOnly_predictionSet,
aes(x=date, y=GC, color = Legend))+
scale_color_manual("",values= c("gray48","#009999"))+
labs(y = "", x = "")+
theme(legend.position="none")
p3_Ac <- ggplot() +
geom_line(data = pOccur[occurIdx,],
aes(x =as.POSIXct(dateStr), y = DT, color = Legend))+
geom_line(data = AcOnly_predictionSet,
aes(x=date, y=DT, color = Legend)) +
scale_color_manual("",values= c("gray48","#009999"))+
labs(y = "Encounter probability", x = "")+
theme(legend.position="none")
multiplot(p1_Ac, p2_Ac, p3_Ac, cols=1)
```
<br>
<br>
### Visual Only Model
```{r Vis Temporal Predict}
# Confusing, but acoustic only predictors are passed in to since
# we're predicting on the acoustic timeseries.
predVisOnly_MC <- predict.gam(VisOnly_gamm_pruned_TF_best$gam,
transformedCovars_AcOnly.test[compAcSet_MC,],
type = 'response',na.action = na.pass)
predVisOnly_GC <- predict.gam(VisOnly_gamm_pruned_TF_best$gam,
transformedCovars_AcOnly.test[compAcSet_GC,],
type = 'response',na.action = na.pass)
predVisOnly_DT <- predict.gam(VisOnly_gamm_pruned_TF_best$gam,
transformedCovars_AcOnly.test[compAcSet_DT,],
type = 'response',na.action = na.pass)
VisOnly_predictionSet <- data.frame(unique(Test_AcOnly.set$date))
colnames(VisOnly_predictionSet) <-"date"
VisOnly_predictionSet$MC <- NA
m1 <- match(MC_times,VisOnly_predictionSet$date)
VisOnly_predictionSet$MC[m1] <-predVisOnly_MC
VisOnly_predictionSet$GC <- NA
m2 <- match(GC_times,VisOnly_predictionSet$date)
VisOnly_predictionSet$GC[m2] <-predVisOnly_GC
VisOnly_predictionSet$DT <- NA
m3 <- match(DT_times,VisOnly_predictionSet$date)
VisOnly_predictionSet$DT[m3] <-predVisOnly_DT
VisOnly_predictionSet$Legend <- "Predictions"
```
```{r,fig.height = 10, fig.width = 7, message = FALSE, warning = FALSE}
# par(mfrow = c(5,1),oma=c(3,0,5,0)),colour="#000099",colour="#CC0000"
p1_Vis <- ggplot() +
geom_line(data = pOccur[occurIdx,],
aes(x = as.POSIXct(dateStr), y = MC, color = Legend))+
geom_line(data = VisOnly_predictionSet,
aes(x=date, y=MC, color = Legend)) +
labs(y = "", x = "")+
scale_color_manual(values= c("gray48","#009999"))+
theme(legend.position = c(0.9, 0.7))
p2_Vis <- ggplot() +
geom_line(data = pOccur[occurIdx,],
aes(x =as.POSIXct(dateStr), y = GC, color = Legend))+
geom_line(data = VisOnly_predictionSet,
aes(x=date, y=GC, color = Legend)) +
scale_color_manual(values= c("gray48","#009999"))+
labs(y = "", x = "")+
theme(legend.position="none")
p3_Vis <- ggplot() +
geom_line(data = pOccur[occurIdx,],
aes(x =as.POSIXct(dateStr), y = DT, color = Legend))+
geom_line(data = VisOnly_predictionSet,
aes(x=date, y=DT, color = Legend)) +
scale_color_manual(values= c("gray48","#009999"))+
labs(y = "Encounter probability", x = "")+
theme(legend.position="none")
multiplot(p1_Vis, p2_Vis, p3_Vis, cols=1)
```
<br>
<br>
### Joint Model
```{r Combo Temporal Predict,fig.height = 10, fig.width = 7}
# Confusing, but acoustic only predictors are passed in to since we're predicting on the acoustic timeseries.
pred_MC <- predict.gam(gamm_pruned_TF_best$gam,
transformedCovars_AcOnly.test[compAcSet_MC,],
type = 'response',na.action = na.pass)
pred_GC <- predict.gam(gamm_pruned_TF_best$gam,
transformedCovars_AcOnly.test[compAcSet_GC,],
type = 'response',na.action = na.pass)
pred_DT <- predict.gam(gamm_pruned_TF_best$gam,
transformedCovars_AcOnly.test[compAcSet_DT,],
type = 'response',na.action = na.pass)
predict(glmm_full_TF$v01_2_3_2, newdata= transformedCovars_AcOnly.test[compAcSet_MC,], zitype = "response")
Both_predictionSet <- data.frame(unique(Test_AcOnly.set$date))
colnames(Both_predictionSet) <-"date"
Both_predictionSet$MC <- NA
m1 <- match(MC_times,Both_predictionSet$date)
Both_predictionSet$MC[m1] <-pred_MC
Both_predictionSet$GC <- NA
m2 <- match(GC_times,Both_predictionSet$date)
Both_predictionSet$GC[m2] <-pred_GC
Both_predictionSet$DT <- NA
m3 <- match(DT_times,Both_predictionSet$date)
Both_predictionSet$DT[m3] <-pred_DT
Both_predictionSet$Legend <- "Predictions"
```
```{r,fig.height = 10, fig.width = 7, message = FALSE, warning = FALSE}
# par(mfrow = c(5,1),oma=c(3,0,5,0)),colour="#000099",colour="#CC0000"
p1 <- ggplot() +
geom_line(data = pOccur[occurIdx,],
aes(x = as.POSIXct(dateStr), y = MC, color = Legend))+
geom_line(data = Both_predictionSet,
aes(x=date, y=MC, color = Legend)) +
labs(y = "", x = "")+
scale_color_manual(values= c("gray48","#009999"))+
theme(legend.position = c(0.9, 0.7))
p2 <- ggplot() +
geom_line(data = pOccur[occurIdx,],
aes(x =as.POSIXct(dateStr), y = GC, color = Legend))+
geom_line(data = Both_predictionSet,
aes(x=date, y=GC, color = Legend)) +
scale_color_manual(values= c("gray48","#009999"))+
labs(y = "", x = "")+
theme(legend.position="none")
p3 <- ggplot() +
geom_line(data = pOccur[occurIdx,],
aes(x =as.POSIXct(dateStr), y = DT, color = Legend))+
geom_line(data = Both_predictionSet,
aes(x=date, y=DT, color = Legend)) +
scale_color_manual(values= c("gray48","#009999"))+
labs(y = "Encounter probability", x = "")+
theme(legend.position="none")
multiplot(p1, p2, p3, cols=1)
```
<br>
## Spatial Prediction
```{r Load rasters, eval = FALSE, include = FALSE}
#Load in rasters for prediction periods (2009 winter and summer)
predTemplate <-raster('E:/NASData/AcoustoVisualDE/Prediction_template/Predictiontemplate.tif')
log10_CHL_jan_2009 <- log10(resample(raster('E:/NASData/AcoustoVisualDE/CHL/CHL_jan2009.tif'),
predTemplate))
log10_CHL_july_2009 <- log10(resample(raster('E:/NASData/AcoustoVisualDE/CHL/CHL_july2009.tif'),
predTemplate))
SST_jan_2009 <- resample(raster('E:/NASData/AcoustoVisualDE/SST/SST_jan_2009.tif'),
predTemplate)
SST_july_2009 <- resample(raster('E:/NASData/AcoustoVisualDE/SST/SST_july_2009.tif'),
predTemplate)
SSH_jan_2009 <- resample(raster('E:/NASData/AcoustoVisualDE/SSH/SSH_jan2009.tif'),
predTemplate)
SSH_july_2009 <- resample(raster('E:/NASData/AcoustoVisualDE/SSH/SSH_july2009.tif'),
predTemplate)
log10_Cayula_jan_2009 <-
resample(raster('E:/NASData/AcoustoVisualDE/Cayula/log10_CayulaFront_jan2009.tif'),
predTemplate)
log10_Cayula_july_2009 <-
resample(raster('E:/NASData/AcoustoVisualDE/Cayula/log10_CayulaFront_july2009.tif'),
predTemplate)
SAL_jan_2009 <- resample(raster('E:/NASData/AcoustoVisualDE/Salinity/sal0_jan2009.tif'),
predTemplate)
SAL_july_2009 <- resample(raster('E:/NASData/AcoustoVisualDE/Salinity/sal0_july2009.tif'),
predTemplate)
Eddy_jan_2009 <- resample(raster('E:/NASData/Eddy/JPL_ManualEddyDist/Climatologies/Projected/eddyDist_Jan_2009_proj.tif'),
predTemplate)
Eddy_july_2009 <- resample(raster('E:/NASData/Eddy/JPL_ManualEddyDist/Climatologies/Projected/eddyDist_July_2009_proj.tif'),
predTemplate)
Neg_Eddy_jan_2009 <-
resample(raster('E:/NASData/AcoustoVisualDE/EddyDist/DistNegMSLA_eddy_polarities_2009015.img'),
predTemplate)
Neg_Eddy_july_2009 <-
resample(raster('E:/NASData/AcoustoVisualDE/EddyDist/Neg_EddyDist_july2009.tif'),
predTemplate)
log10_HYCOM_MAG_0_jan2009 <-
resample(raster('E:/NASData/AcoustoVisualDE/HYCOM_Mag/log10_mag_jan2009.tif'),
predTemplate)
log10_HYCOM_MAG_0_july2009 <-
resample(raster('E:/NASData/AcoustoVisualDE/HYCOM_Mag/log10_mag_july2009.tif'),
predTemplate)
log10_HYCOM_MLD_jan2009 <-
resample(raster('E:/NASData/AcoustoVisualDE/MLD/log10_mld_jan2009.tif'),
predTemplate)
log10_HYCOM_MLD_july2009 <-
resample(raster('E:/NASData/AcoustoVisualDE/MLD/log10_mld_july2009.tif'),
predTemplate)
```
```{r brick rasters, eval = FALSE, include = FALSE}
jan2009_rasters <- brick(log10_CHL_jan_2009,SST_jan_2009,SSH_jan_2009,log10_Cayula_jan_2009,
SAL_jan_2009,Eddy_jan_2009,Neg_Eddy_jan_2009,
log10_HYCOM_MAG_0_jan2009,log10_HYCOM_MLD_jan2009)
july2009_rasters <- brick(log10_CHL_july_2009,SST_july_2009,SSH_july_2009,log10_Cayula_july_2009,
SAL_july_2009,Eddy_july_2009,Neg_Eddy_july_2009,
log10_HYCOM_MAG_0_july2009,log10_HYCOM_MLD_july2009)
names(jan2009_rasters)<-c('log10_CHL','SST','SSH','log10_FrontDist_Cayula'
,'HYCOM_SALIN_0','EddyDist','Neg_EddyDist',
'log10_HYCOM_MAG_0','log10_HYCOM_MLD')
names(july2009_rasters)<-c('log10_CHL','SST','SSH','log10_FrontDist_Cayula',
'HYCOM_SALIN_0','EddyDist','Neg_EddyDist'
,'log10_HYCOM_MAG_0','log10_HYCOM_MLD')
save(jan2009_rasters,july2009_rasters,
file = 'E:/NASData/AcoustoVisualDE/AcoustoVisualDE/2009_prediction_rasters.Rdata')
```
```{r load raster bricks, echo = FALSE}
load('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/2009_prediction_rasters.Rdata')
```
<br>
### Acoustic Only
Evaluate the models for summer (July 2009) and winter(January 2009) across the entire Gulf of Mexico (US EEZ beyond the 200m contour).
```{r Ac Spatial Predict}
dt1 <- as.data.frame(jan2009_rasters,xy=TRUE)
jan2009_rasters$fac1<-jan2009_rasters$SST
jan2009_rasters$fac1<-1
july2009_rasters$fac1<-july2009_rasters$SST
july2009_rasters$fac1<-1
jan2009_AcOnly_prediction <- raster::predict(jan2009_rasters,
bestACOnly_glmm,
type = 'response',na.action = na.pass)
july2009_AcOnly_prediction <- raster::predict(july2009_rasters,
bestACOnly_glmm,
type = 'response',na.action = na.pass)
```
<br>
### Visual Only
```{r Vis Spatial Predict}
dt1 <- as.data.frame(jan2009_rasters,xy=TRUE)
jan2009_VisOnly_prediction <- raster::predict(jan2009_rasters,bestVisOnly_glmm,
type = 'response',na.action = na.pass)
july2009_VisOnly_prediction <- raster::predict(july2009_rasters,bestVisOnly_glmm,
type = 'response',na.action = na.pass)
```
<br>
### Joint Visual and Acoustic
```{r Combo Spatial Predict, message=FALSE}
dt1 <- as.data.frame(jan2009_rasters,xy=TRUE)
jan2009_rasters$fac4<-jan2009_rasters$SST
jan2009_rasters$fac4<-0
july2009_rasters$fac4<-july2009_rasters$SST
july2009_rasters$fac4<-0
jan2009_prediction <- raster::predict(jan2009_rasters,best_full_glmm,
type = 'response',na.action = na.pass)
july2009_prediction <- raster::predict(july2009_rasters,best_full_glmm,
type = 'response',na.action = na.pass)
```
<br>
Wrangle projections for mapping:
```{r wrangle projections, message = FALSE, warning = FALSE, results = 'hide'}
predict_template <- readOGR('E:/NASData/AcoustoVisualDE/Prediction_template/prediction_polygon.shp')
map_proj <- sp::CRS('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
predict_template_proj <- spTransform(predict_template, CRSobj = map_proj)
# Acoustic
jan2009_AcOnly_prediction_proj <- projectRaster(jan2009_AcOnly_prediction, crs=map_proj)
july2009_AcOnly_prediction_proj <- projectRaster(july2009_AcOnly_prediction, crs=map_proj)
jan2009_AcOnly_prediction_crop <- mask(jan2009_AcOnly_prediction_proj,predict_template_proj)
july2009_AcOnly_prediction_crop <-
mask(july2009_AcOnly_prediction_proj,predict_template_proj)
# Visual
jan2009_VisOnly_prediction_proj <-
projectRaster(jan2009_VisOnly_prediction, crs=map_proj)
july2009_VisOnly_prediction_proj <-
projectRaster(july2009_VisOnly_prediction, crs=map_proj)
#jan2009_VisOnly_prediction_SE_proj <-
# projectRaster(jan2009_VisOnly_prediction_SE, crs=map_proj)
jan2009_VisOnly_prediction_crop <-
mask(jan2009_VisOnly_prediction_proj,predict_template_proj)
july2009_VisOnly_prediction_crop <- mask(july2009_VisOnly_prediction_proj,predict_template_proj)
#jan2009_VisOnly_prediction_SE_crop <- mask(jan2009_VisOnly_prediction_SE_proj,predict_template_proj)
#Both