-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pm_model_runs_NN_density.Rmd
2246 lines (1811 loc) · 104 KB
/
Pm_model_runs_NN_density.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: 'GoMx sperm whale habitat models: Density with neural nets'
author:
- name: Kaitlin Frasier
affiliation: Scripps Institution of Oceanography, UC San Diego
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
html_document:
toc: true
toc_depth: 4
toc_float: true
theme: spacelab
fig_caption: true
bibliography: exportlist.bibtex
csl: plos-computational-biology.csl
---
```{r setup, echo = FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r load packages, echo = FALSE, message = FALSE, warning = FALSE}
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(nnet)
library(caret)
library(parallel)
library(MLmetrics)
library(pracma)
library(grid)
library(knitr)
library(matrixStats)
library(zoo)
library(tmap)
library(scales)
source('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/HabitatProject/R/plot_covarDensity.R')
source('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/HabitatProject/R/weighted_logloss.R')
source('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/HabitatProject/R/multiplot.R')
source('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/HabitatProject/R/plot_missingdata.R')
source('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/HabitatProject/R/plot_cleveland.R')
source('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/HabitatProject/R/plot_timeseries.R')
source('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/HabitatProject/R/transform_covars.R')
source('E:/NASData/AcoustoVisualDE/AcoustoVisualDE/nnet_plot_update.r')
options(stringsAsFactors = FALSE)
# load some preferences
load('E:/NASData/ModelData/Pm/setup_info_Pm.Rdata')
load('E:/NASData/ModelData/Pm/PmMergedData.Rdata')
outDir <- file.path('E:/NASData/ModelData',SP,'log//')
inDir <- file.path('E:/NASData/ModelData',SP,'//')
```
<br>
# 1. Exploratory analysis
<br>
## 1.1 Data Inputs
NOAA SEFSC 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, echo = FALSE}
plotCols1 <-c(7:10,14,16,19,22,23)
covarList<-names(mergedSegments[c(plotCols1)])
varUnits <- c("SST (C)","SSH (m)",expression("Chlorophyll A (mg/m"^3*")"),"MLD (m)", "Salinity (ppm)",
"Current Magnitude (m/sec)", "Upwelling (m/sec)",
"Dist. to Anti-Cyclonic Eddy (km)","Dist. to Cyclonic Eddy (km)")
names(varUnits)<-covarList
percFilled <- plot.missingdata(mergedSegments,covarList,
paste0(outDir,'AcousticAndVisual_',SP),varUnits)
percFilled <- plot.missingdata(AcOnlySegments,covarList,
paste0(outDir,'AcousticOnly_',SP),varUnits)
percFilled <- plot.missingdata(VisOnlySegments,covarList,
paste0(outDir,'VisualOnly_',SP),varUnits)
visDataAvailPlot <- paste0(savePath,'/VisualOnly_',SP,'_missingData.png')
```
**Visual data predictor variable availability:**
![](`r visDataAvailPlot`)
<br>
### 1.1.1 Splitting into testing and training sets
The data are split into training and testing sets. In this case, visual data from 2009 and acoustic data from 2013 were used only for testing. Only observations from 2003 or later were used for modeling due to covariate limitations.
```{r test train split, echo = FALSE}
# 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"))
isVisual <- mergedSegments$Category
keepDates.train <- which(yearListIdx != 2009 &
yearListIdx >= 2003 &
yearListIdx != 2013)
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 |
yearListIdx_VisOnly == 2013)
Train_Joint.set<- mergedSegments[keepDates.train,]
Train_AcOnly.set <- AcOnlySegments[keepDates_AcOnly.train,]
Train_VisOnly.set<- VisOnlySegments[keepDatesVisOnly.train,]
Test_Joint.set<- mergedSegments[keepDates.test,]
Test_AcOnly.set<- AcOnlySegments[keepDates_AcOnly.test,]
Test_VisOnly.set<- VisOnlySegments[keepDatesVisOnly.test,]
```
<br>
### 1.1.2 Map of visual sightings data
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, echo = 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>
### 1.1.3 Time series of acoustic data
The time series below show timeseries of estimated densities from passive acoustic data used for modeling (Densities were calculated following methods detailed in @RN806). Data from 2011 and 2012 were used for training, and 2013 data was held back for testing.
<br>
**Acoustic Timeseries:**
```{r plot timeseries, message = FALSE, echo = FALSE}
plot.timeseries(siteList,outDir,AcOnlySegments)
MCTS <- paste0(outDir,SP,'_Timeseries_Site_MC.png')
GCTS <- paste0(outDir,SP,'_Timeseries_Site_GC.png')
DTTS <- paste0(outDir,SP,'_Timeseries_Site_DT.png')
```
![](`r MCTS`)
![](`r GCTS`)
![](`r DTTS`)
<br>
## 1.2 Examination of covariates
```{r remove outliers, message = FALSE, results = 'hide', echo = FALSE}
### Identify Outliers
# Replace extreme outliers (bad data) with NaNs.
outlierList <-which(Train_Joint.set$CHL< -10)
Train_Joint.set$CHL[outlierList] <- NaN
#outlierList <-which(Train_Joint.set$FrontDist_Cayula>800000)
#Train_Joint.set$FrontDist_Cayula[outlierList] <- NaN
outlierList <-which(Train_Joint.set$Density>100000)
Train_Joint.set$Density[outlierList] <- NaN
outlierList <-which(Test_Joint.set$CHL< -10)
Test_Joint.set$CHL[outlierList] <- NaN
#outlierList <-which(Test_Joint.set$FrontDist_Cayula > 800000)
#Test_Joint.set$FrontDist_Cayula[outlierList] <- NaN
outlierList <-which(Test_Joint.set$Density>100000)
Test_Joint.set$Density[outlierList] <- NaN
outlierList <-which(Train_AcOnly.set$CHL< -10)
Train_AcOnly.set$CHL[outlierList] <- NaN
#outlierList <-which(Train_AcOnly.set$FrontDist_Cayula > 800000)
#Train_AcOnly.set$FrontDist_Cayula[outlierList] <- NaN
outlierList <-which(Train_AcOnly.set$Density > 100000)
Train_AcOnly.set$Density[outlierList] <- NaN
outlierList <-which(Test_AcOnly.set$CHL< -10)
Test_AcOnly.set$CHL[outlierList] <- NaN
#outlierList <-which(Test_AcOnly.set$FrontDist_Cayula>800000)
#Test_AcOnly.set$FrontDist_Cayula[outlierList] <- NaN
outlierList <-which(Test_AcOnly.set$Density>100000)
Test_AcOnly.set$Density[outlierList] <- NaN
outlierList <-which(Train_VisOnly.set$CHL< -10)
Train_VisOnly.set$CHL[outlierList] <- NaN
#outlierList <-which(Train_VisOnly.set$FrontDist_Cayula>800000)
#Train_VisOnly.set$FrontDist_Cayula[outlierList] <- NaN
outlierList <-which(Train_VisOnly.set$Density>100000)
Train_VisOnly.set$Density[outlierList] <- NaN
outlierList <-which(Test_VisOnly.set$CHL< -10)
Test_VisOnly.set$CHL[outlierList] <- NaN
#outlierList <-which(Test_VisOnly.set$FrontDist_Cayula>800000)
#Test_VisOnly.set$FrontDist_Cayula[outlierList] <- NaN
outlierList <-which(Test_VisOnly.set$Density>100000)
Test_VisOnly.set$Density[outlierList] <- NaN
```
<br>
### 1.2.1 Covariate distribution check
<br>
**Distributions of covariates from acoustic observations (training data only):**
```{r dot plots, eval = TRUE, message = FALSE, results = 'hide', echo = FALSE}
plot.cleveland(Train_AcOnly.set,covarList,FALSE,paste0(outDir,'AcousticOnly_',SP),varUnits)
plot.cleveland(Train_VisOnly.set,covarList,FALSE,paste0(outDir,'VisualOnly_',SP),varUnits)
plot.cleveland(Train_Joint.set,covarList,FALSE,paste0(outDir,'AcousticAndVisual_',SP),varUnits)
ACclevelandPlot<-paste0(outDir,'AcousticOnly_',
SP,'_clevelandDots_noTransform.png')
VisclevelandPlot<-paste0(outDir,'VisualOnly_',
SP,'_clevelandDots_noTransform.png')
```
![](`r ACclevelandPlot`)
<br>
**Distributions of covariates from the visual observations (training data only):**
![](`r VisclevelandPlot`)
<br>
Some of these covariates are more or less interrelated. Correlations are examined in the figure below. Numbers closer to 1 above the diagonal in the figure below represent correlation coefficients. If a pair of covariates is highly-correlated only one should typically be used in the model.
```{r correlation plots, eval = TRUE, message = FALSE, warning = FALSE, results = 'hide', echo = FALSE}
covarList2 <- c("Density","SST","SSH","CHL",
"HYCOM_MLD","HYCOM_SALIN_0","HYCOM_MAG_0",
"HYCOM_UPVEL_50",
"Neg_EddyDist","Pos_EddyDist",
"fac1","fac2","EffectiveArea")
# restrict covariates again to limited set
Train_Joint.set2<- Train_Joint.set[,covarList2]
Test_Joint.set2<- Test_Joint.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(Train_Joint.set2[,1:(length(covarList2)-3)], 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)-3)], 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)-3)], ellipses=FALSE, method = "spearman",cex.cor=.75)
dev.off()
covarPlot <-paste0(outDir,SP,'_correlations_noTransform.png')
```
<br>
**Covariate Correlations:**
![](`r covarPlot`)
<br>
### 1.2.2 Transformation of predictor variables
Some variables, including chlorophyll, mixed layer depth and distance to fronts are highly skewed and were log-transformed for input to GAMs.
```{r transform covars, message = FALSE, results = 'hide', echo = FALSE, warning = FALSE}
# covarList2 <- c("Density","SST","SSH","CHL",
# "HYCOM_MLD","HYCOM_SALIN_0","HYCOM_MAG_0",
# "HYCOM_UPVEL_50",
# "Neg_EddyDist", "PosEddyDist",
# "fac1","fac2")
transformList <- c("none","none","none","log10",
"log10","none","log10",
"none",
"none","none",
"none","none","none")
transformedCovars.train <-
transform.covars(Train_Joint.set2,covarList2,transformList)
transformedCovars.test <-
transform.covars(Test_Joint.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()
```
```{r transformed dot plots, eval = TRUE, message = FALSE, results = 'hide', echo = FALSE}
# Plotting the transformed variables:
plotCols = colnames(transformedCovars.train)[c(2:10)]
names(varUnits)<-plotCols
varUnits["log10_HYCOM_MAG_0"]<-expression("log"[10]*"(Current Magnitude (m/sec))")
varUnits["log10_HYCOM_MLD"]<- expression("log"[10]*"(MLD (m))")
varUnits["log10_CHL"] <-expression("log"[10]*"(Chorophyll A (mg/m"^3*"))")
plot.cleveland(transformedCovars.train,
plotCols,TRUE,paste0(outDir,'AcousticAndVisual_',SP),varUnits)
plot.cleveland(transformedCovars_AcOnly.train,
plotCols,TRUE,paste0(outDir,'AcousticOnly_',SP),varUnits)
plot.cleveland(transformedCovars_VisOnly.train,
plotCols,TRUE,paste0(outDir,'VisualOnly_',SP),varUnits)
clevlandJointTransformed<-paste0(outDir,'AcousticAndVisual_',
SP,'_clevelandDots_withTransform.png')
```
Below, the two sets of covariates have been combined and transformed:
![](`r clevlandJointTransformed`)
<br>
### 1.2.3 Preliminary check of predictive power
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 `r SPLong` were present, and solid lines indicate the distribution when `r SPLong` were absent. Note that these plots do not account for effort.
```{r presence absence histograms, eval = TRUE, message = FALSE, results = 'hide', echo = FALSE}
plot.covarDensity(transformedCovars.train[,2:10],
colnames(transformedCovars.train[,2:10]),
transformedCovars.train$Density,paste0(outDir,'Both_',SP),varUnits)
plot.covarDensity(transformedCovars_AcOnly.train[,2:10],
colnames(transformedCovars_AcOnly.train[2:10]),
transformedCovars_AcOnly.train$Density,paste0(outDir,'AcousticOnly_',SP),varUnits)
plot.covarDensity(transformedCovars_VisOnly.train[,2:10],
colnames(transformedCovars_VisOnly.train[,2:10]),
transformedCovars_VisOnly.train$Density,paste0(outDir,'VisualOnly_',SP),varUnits)
acKernels <-paste0(savePath,'/AcousticOnly_',SP,'_density_pres_abs.png')
visKernels <- paste0(savePath,'/VisualOnly_',SP,'_density_pres_abs.png')
```
<br>
**Acoustic kernel densities:**
![](`r acKernels`)
<br>
**Visual kernel densities:**
![](`r visKernels`)
<br>
### 1.2.4 Estimation of relative weights
```{r load detection prob, echo = FALSE}
visDetProbData<-paste0(inDir,SP,'sightwTrunc_GU.Rdata')
load(visDetProbData)
visDetProb <- detFun[[bestModelIdx]]$fitted[1]
visDetProbFigure <- paste0(inDir,SP,'sightwTrunc_GU.png')
```
To train the model, we need to know how much power the various data points have relative to one another. This is important because the duration, spatial coverage, and detection probabilities are quite different between the visual and acoustic data sets. If an animal is seen or heard, we know for certain that the species was present. However, if it was not heard, then it either wasn't present, or it was present but missed.
"Zero inflation" or an excess of false zeros more common in the visual survey data because each data point represents a 10km transect section, traversed at survey speed (>10 knots) or approximately 30 minutes of observation effort. In contrast, the acoustic data are binned by day with a stationary instrument, therefore the probability of missing a group over the course of a day is lower.
For each data type, we estimated the probability of a missed detection to account for differences in zero-inflation, downweighting zeros according to the probability of a recording false negative.
The visual data represent wether or not `r SPLong` were seen during each transect segment. The probability of missing a sighting of `r SPLong` was estimated as
\[P_{V}(detect|present) = \mu_{det} * g0\ = `r round(visDetProb*100)/100` * `r round(visG0*100)/100` = `r round(visDetProb* visG0*100)/100`\]
where \mu_{det} is the mean detection probability as estimated by a model fit using the mrds package, and g0 is the probability of observing an animal on the transect line [@RN862]. We assume that reported absences are likely to be true absences `r round(visDetProb* visG0*100)` % of the time, therefore zeros are given a weight of `r round(visDetProb* visG0*100)/100` on a scale of [0,1].
The acoustic data represent presence or absence of `r SPLong` in one day bins. Given that a group of animals is present near the sensor, the probability of detecting them in a 5 minute period within a `r r_sp` km range is estimated at `r Ac_pDet`, therefore the probability of missing an encounter is 1 - `r Ac_pDet` = `r 1-Ac_pDet` [@RN806; @RN506]. Given that animals were present, the probability of missing a group for a full day (288 5-minute periods) is estimated as
\[P_{A}(detect|present) = (1-`r Ac_pDet`)^{288} \approx 1\]
Therefore we assume that there are no false negative days in the passive acoustic timeseries, and all acoustic observations are given weight = 1.
<br>
**Best visual detection probability model for `r SPLong`:**
![](`r visDetProbFigure`)
<br>
# 2. Model Fitting
Models were fit using avnnet from the caret package in R.
```{r load starting data, echo = FALSE,message = FALSE, results = 'hide'}
pOccur <- read.csv(pOccurenceFile, header = TRUE,na.strings=c('',' ','NA','NaN'))
```
```{r model setup, echo = FALSE, message = FALSE, results = 'hide'}
# set up weighting
yAcOnly <- transformedCovars_AcOnly.train$Density
yVisOnly <- transformedCovars_VisOnly.train$Density
y <- transformedCovars.train$Density
visDetProb <- detFun[[bestModelIdx]]$fitted[1]
transformedCovars_AcOnly.train$yAcOnly<- transformedCovars_AcOnly.train$Density
transformedCovars_VisOnly.train$yVisOnly<-transformedCovars_VisOnly.train$Density
transformedCovars.train$y <- transformedCovars.train$Density
transformedCovars_AcOnly.train$yAcOnlySqrt <-sqrt(transformedCovars_AcOnly.train$Density)
transformedCovars_VisOnly.train$yVisOnlySqrt <-sqrt(transformedCovars_VisOnly.train$Density)
transformedCovars.train$ySqrt <-sqrt(transformedCovars.train$Density)
transformedCovars_AcOnly.test$yAcOnly <- transformedCovars_AcOnly.test$Density
transformedCovars_VisOnly.test$yVisOnly <- transformedCovars_VisOnly.test$Density
transformedCovars.test$y <- transformedCovars.test$Density
transformedCovars_AcOnly.test$yAcOnlySqrt <-sqrt(transformedCovars_AcOnly.test$Density)
transformedCovars_VisOnly.test$yVisOnlySqrt <-sqrt(transformedCovars_VisOnly.test$Density)
transformedCovars.test$ySqrt <-sqrt(transformedCovars.test$Density)
# make some factors and calculate introduce column of g0 weights
joint_train_weightsG0<- array(data = 1, dim = c(length(transformedCovars.train$fac1),1))
joint_test_weightsG0<- array(data = 1, dim = c(length(transformedCovars.test$fac1),1))
for (iFac in 1:length(transformedCovars.train$fac1)) {
if (!is.na(transformedCovars.train$fac1[iFac]) & !is.na(transformedCovars.train$Density[iFac])){
if (transformedCovars.train$fac1[iFac]>5) {
if (transformedCovars.train$Density[iFac]==0){
# if it's visual data and it's a zero, adjust by g0 ie, only a X% chance it was a true zero.
joint_train_weightsG0[iFac,1] <- visG0*visDetProb
}
}
}
}
maxEffectiveAreaJoint <- max(c(transformedCovars.train$EffectiveArea,
transformedCovars.test$EffectiveArea))
# do additional adjustment for effective area
joint_train_weightsG0 <- joint_train_weightsG0*
(transformedCovars.train$EffectiveArea/maxEffectiveAreaJoint)
for (iFac in 1:length(transformedCovars.test$fac1)) {
if (!is.na(transformedCovars.test$fac1[iFac]) & !is.na(transformedCovars.test$Density[iFac])){
if (transformedCovars.test$fac1[iFac]>5) {
if (transformedCovars.test$Density[iFac]==0){
# if it's visual data and it's a zero, adjust by g0 ie, only a X% chance it was a true zero.
joint_test_weightsG0[iFac,1] <- visG0*visDetProb
}
}
}
}
# do additional adjustment for effective area
joint_test_weightsG0 <- joint_test_weightsG0*
(transformedCovars.test$EffectiveArea/maxEffectiveAreaJoint)
VisOnly.train_weightsG0<- array(data = 1, dim = c(length(transformedCovars_VisOnly.train$fac1),1))
VisOnly.test_weightsG0<- array(data = 1, dim = c(length(transformedCovars_VisOnly.test$fac1),1))
for (iFac in 1:length(transformedCovars_VisOnly.train$fac1)) {
if (!is.na(transformedCovars_VisOnly.train$Density[iFac]) &
transformedCovars_VisOnly.train$Density[iFac]==0){
# if it's visual data and it's a zero, adjust by g0 ie, only a X% chance it was a true zero.
VisOnly.train_weightsG0[iFac,1] <- visG0*visDetProb
}
}
maxEffectiveAreaVis <- max(c(transformedCovars_VisOnly.train$EffectiveArea,
transformedCovars_VisOnly.test$EffectiveArea))
# do additional adjustment for effective area
VisOnly.train_weightsG0 <- VisOnly.train_weightsG0*
(transformedCovars_VisOnly.train$EffectiveArea/maxEffectiveAreaVis)
for (iFac in 1:length(transformedCovars_VisOnly.test$fac1)) {
if (!is.na(transformedCovars_VisOnly.test$Density[iFac]) &
transformedCovars_VisOnly.test$Density[iFac]==0){
# if it's visual data and it's a zero, adjust by g0 ie, only a X% chance it was a true zero.
VisOnly.test_weightsG0[iFac,1] <- visG0*visDetProb
}
}
# do additional adjustment for effective area
VisOnly.test_weightsG0 <- VisOnly.test_weightsG0*
(transformedCovars_VisOnly.test$EffectiveArea/maxEffectiveAreaVis)
# Remove NaNs
goodData_Ac <- which(!is.na(rowSums(transformedCovars_AcOnly.train)))
AcOnly.train.NoNa <- transformedCovars_AcOnly.train[goodData_Ac,]
goodData_Vis <- which(!is.na(rowSums(transformedCovars_VisOnly.train)))
VisOnly.train.NoNa <- transformedCovars_VisOnly.train[goodData_Vis,]
goodData_Joint <- which(!is.na(rowSums(transformedCovars.train)))
Joint.train.NoNa <- transformedCovars.train[goodData_Joint,]
goodData_Ac_test <- which(!is.na(rowSums(transformedCovars_AcOnly.test)))
AcOnly.test.NoNa <- transformedCovars_AcOnly.test[goodData_Ac_test,]
goodData_Vis_test <- which(!is.na(rowSums(transformedCovars_VisOnly.test)))
VisOnly.test.NoNa <- transformedCovars_VisOnly.test[goodData_Vis_test,]
goodData_Joint_test <- which(!is.na(rowSums(transformedCovars.test)))
Joint.test.NoNa <- transformedCovars.test[goodData_Joint_test,]
```
<br>
Case weights:
```{r plot weights, echo = FALSE}
png(paste(outDir,SP,'_caseWeights_NNet_density.png',sep=''), width = 4, height = 4, units = 'in',res = 300)
# par(mar = c(5.1, 4.1, 4.1, 3.1))
visIdx <-length(VisOnly.train_weightsG0)
jointN <- length(joint_train_weightsG0)
plot(c(1:visIdx), joint_train_weightsG0[c(1:visIdx)],pch = 1,col = "red",
ylim =c(0,1 ),ylab='Case Weight',xlab = 'Case Index',cex = 1,xlim= c(0,jointN+1))
points(c((1+visIdx):jointN),
joint_train_weightsG0[c((1+visIdx):jointN)],pch = 4,
ylim =c(0,1 ),ylab = 'Case Weight',xlab = 'Case Index',cex = 1,xlim= c(0,jointN+1))
abline(v = visIdx+.5,col = "darkgrey",lty = 2, lwd = 3)
dev.off()
wghtFig <-paste0(outDir,SP,'_caseWeights_NNet_density.png')
```
![](`r wghtFig`)
```{r Scale all the data, echo = FALSE}
# NNs don't do well with unscaled data. Scale it and then unscale it at the end.
# Scale Joint training data for the NN
covars_Joint_max.train <- apply(Joint.train.NoNa, 2, max, na.rm = TRUE)
covars_Joint_min.train <- apply(Joint.train.NoNa, 2, min, na.rm = TRUE)
Joint_train_scaled <- as.data.frame(scale(Joint.train.NoNa,
center = covars_Joint_min.train,
scale = covars_Joint_max.train-covars_Joint_min.train))
Joint_train_scaled$y<- log(Joint.train.NoNa$y+1)/log(covars_Joint_max.train['y'])
Joint_train_scaled$y[is.infinite(Joint_train_scaled$y)]<-0
Joint_train_scaled$ySqrt <- Joint.train.NoNa$ySqrt
Joint_train_scaled$weightsG0<-joint_train_weightsG0[goodData_Joint]
# Scale Ac only training data for the NN
AcOnly_train_scaled <- as.data.frame(scale(AcOnly.train.NoNa,
center = covars_Joint_min.train,
scale = covars_Joint_max.train-covars_Joint_min.train))
AcOnly_train_scaled$yAcOnly<-log(AcOnly.train.NoNa$yAcOnly+1)/log(covars_Joint_max.train['y'])
AcOnly_train_scaled$yAcOnly[is.infinite(AcOnly_train_scaled$yAcOnly )]<-0
AcOnly_train_scaled$yAcOnlySqrt <- AcOnly.train.NoNa$yAcOnlySqrt
# Scale Vis only training data for the NN
VisOnly_train_scaled <- as.data.frame(scale(VisOnly.train.NoNa,
center = covars_Joint_min.train,
scale = covars_Joint_max.train-covars_Joint_min.train))
VisOnly_train_scaled$yVisOnly <- log(VisOnly.train.NoNa$yVisOnly+1)/log(covars_Joint_max.train['y'])
VisOnly_train_scaled$yVisOnly[is.infinite(VisOnly_train_scaled$yVisOnly)]<-0
VisOnly_train_scaled$yVisOnlySqrt <- VisOnly.train.NoNa$yVisOnlySqrt
VisOnly_train_scaled$weightsG0<-VisOnly.train_weightsG0[goodData_Vis]
# Scale Ac only test data for the NN
AcOnly_test_scaled <- as.data.frame(scale(AcOnly.test.NoNa,
center = covars_Joint_min.train,
scale = covars_Joint_max.train-covars_Joint_min.train))
AcOnly_test_scaled$yAcOnly <- log(AcOnly.test.NoNa$yAcOnly+1)/log(covars_Joint_max.train['y'])
AcOnly_test_scaled$yAcOnly[is.infinite(AcOnly_test_scaled$yAcOnly)]<-0
AcOnly_test_scaled$yAcOnlySqrt <- AcOnly.test.NoNa$yAcOnlySqrt
# Scale Vis only test data for the NN
VisOnly_test_scaled <- as.data.frame(scale(VisOnly.test.NoNa,
center = covars_Joint_min.train,
scale = covars_Joint_max.train-covars_Joint_min.train))
VisOnly_test_scaled$yVisOnly <- log(VisOnly.test.NoNa$yVisOnly+1)/log(covars_Joint_max.train['y'])
VisOnly_test_scaled$yVisOnly[is.infinite(VisOnly_test_scaled$yVisOnly )]<-0
VisOnly_test_scaled$yVisOnlySqrt <- VisOnly.test.NoNa$yVisOnlySqrt
VisOnly_test_scaled$weightsG0<-VisOnly.test_weightsG0[goodData_Vis_test]
# Scale Joint test data for the NN
Joint_test_scaled <- as.data.frame(scale(Joint.test.NoNa,
center = covars_Joint_min.train,
scale = covars_Joint_max.train-covars_Joint_min.train))
Joint_test_scaled$y <-log(Joint.test.NoNa$y)/log(covars_Joint_max.train['y'])
Joint_test_scaled$y[is.infinite(Joint_test_scaled$y+1)]<-0
Joint_test_scaled$ySqrt <- Joint.test.NoNa$ySqrt
Joint_test_scaled$weightsG0<-joint_test_weightsG0[goodData_Joint_test]
n <- names(Joint_test_scaled)
# save ranges of each scaled covariate in training set (max and min)
JointRangesMax <- apply(Joint_train_scaled, MARGIN = 2,
function(x) max(x, na.rm =TRUE))
JointRangesMin <- apply(Joint_train_scaled, MARGIN = 2,
function(x) min(x, na.rm =TRUE))
AcOnlyRangesMax <- apply(AcOnly_train_scaled, MARGIN = 2,
function(x) max(x, na.rm =TRUE))
AcOnlyRangesMin <- apply(AcOnly_train_scaled, MARGIN = 2,
function(x) min(x, na.rm =TRUE))
VisOnlyRangesMax <- apply(VisOnly_train_scaled, MARGIN = 2,
function(x) max(x, na.rm =TRUE))
VisOnlyRangesMin <- apply(VisOnly_train_scaled, MARGIN = 2,
function(x) min(x, na.rm=TRUE))
```
<br>
## 2.1 Run Models
```{r, echo = FALSE}
# set up model params
model1.indices <- c(2:10)
nMax1 <- length(model1.indices)
layerSizeList <- c(4,6,8,10,12,14)
trainRepeats <- 25
```
Run NNs Acoustic only, Visual only, and joint Acoustic/Visual datasets.
Models have the following characteristics:
* `R trainRepeats` averaged repeats with random node initalization
* Include `r length(model1.indices)` covariates
* One hidden layer
* Weighted training data
* Hidden node layer sizes from `r min(layerSizeList)` to `r max(layerSizeList)` were tested in 2 node increments to search for optimal network size.
<br>
```{r, echo = FALSE}
# initialize empty structure for model storage
nn_AcOnly<-NULL
nn_VisOnly<-NULL
nn_Joint<-NULL
# initialize empty structure for error scores and predictions
MSE <- NULL
pr <- NULL
```
```{r Ac. only Model 1, results = 'hide', message = FALSE, eval = TRUE, echo = FALSE}
## ACOUSTIC ONLY
AcCounter <- 0
f.AcOnly_NN1 <- as.formula(paste("yAcOnly ~", paste(n[model1.indices], collapse = " + ")))
# Iterate over a range of hidden layer sizes between 2 and 14 nodes.
for (layerSize in layerSizeList){
AcCounter <- AcCounter + 1
# put together the formula
# train network
nn_AcOnly[[AcCounter]] <- avNNet(f.AcOnly_NN1, data = AcOnly_train_scaled,
size = layerSize,
repeats = trainRepeats,
na.action = na.omit,
rang = 0.7,
decay = 0.0001,
maxit = 500,
trace = FALSE)
# predict on train data and estimate Mean Squared Error (MSE)
pr$nn_AcOnly_train[[AcCounter]] <- predict(nn_AcOnly[[AcCounter]],
AcOnly_train_scaled[,model1.indices])
pr$nn_AcOnly_train[[AcCounter]][pr$nn_AcOnly_train[[AcCounter]] <0]<-0
MSE$nn_AcOnly_train[[AcCounter]] <- mean(abs(AcOnly_train_scaled$yAcOnly -
pr$nn_AcOnly_train[[AcCounter]]),na.rm = TRUE)
# predict on test data and estimate MSE
pr$nn_AcOnly_test[[AcCounter]] <- predict(nn_AcOnly[[AcCounter]],
AcOnly_test_scaled[,model1.indices])
pr$nn_AcOnly_test[[AcCounter]][pr$nn_AcOnly_test[[AcCounter]] <0]<-0
MSE$nn_AcOnly_test[[AcCounter]] <- mean(abs(exp(log(covars_Joint_max.train['y'])*
(AcOnly_test_scaled$yAcOnly -
pr$nn_AcOnly_test[[AcCounter]]))),na.rm = TRUE)
## how well does the model predict joint test data?
pr$nn_AcOnly_test_allData[[AcCounter]] <- predict(nn_AcOnly[[AcCounter]],
Joint_test_scaled[,model1.indices])
pr$nn_AcOnly_test_allData[[AcCounter]][pr$nn_AcOnly_test_allData[[AcCounter]]<0]<-0
MSE$nn_AcOnly_test_allData[[AcCounter]] <- mean(Joint_test_scaled$weightsG0*
abs(exp(log(covars_Joint_max.train['y'])*(Joint_test_scaled$y -
pr$nn_AcOnly_test_allData[[AcCounter]]))),na.rm = TRUE)
cat(paste("Done with AcOnly model iteration ",
AcCounter, " of ", length(layerSizeList),
": Layer Size = ", layerSize, "\n"))
}
```
```{r other models 1, eval = TRUE, echo = FALSE}
## VISUAL ONLY
modelCounter <- 0
# put together the formula
f.VisOnly_NN1 <- as.formula(paste("yVisOnly ~", paste(n[model1.indices], collapse = " + ")))
f.Joint_NN1 <- as.formula(paste("y ~", paste(n[model1.indices], collapse = " + ")))
for (layerSize in layerSizeList){
modelCounter <- modelCounter + 1
# train network
nn_VisOnly[[modelCounter]] <- avNNet(f.VisOnly_NN1, VisOnly_train_scaled,
weights = VisOnly_train_scaled$weightsG0,
size = layerSize,
repeats = trainRepeats,
na.action = na.omit,
rang = 0.7,
decay = 0.0001,
maxit = 500,
trace = FALSE)
# weights =
# predict on train data and estimate MSE
pr$nn_VisOnly_train[[modelCounter]] <- predict(nn_VisOnly[[modelCounter]],
VisOnly_train_scaled[,model1.indices])
pr$nn_VisOnly_train[[modelCounter]][ pr$nn_VisOnly_train[[modelCounter]]<0]<-0
MSE$nn_VisOnly_train[[modelCounter]] <- mean(VisOnly_train_scaled$weightsG0*
abs(exp(log(covars_Joint_max.train['y'])*
(VisOnly_train_scaled$yVisOnly -
pr$nn_VisOnly_train[[modelCounter]]))),na.rm = TRUE)
# predict on test data and estimate MSE
pr$nn_VisOnly_test[[modelCounter]] <- predict(nn_VisOnly[[modelCounter]],
VisOnly_test_scaled[,model1.indices])
pr$nn_VisOnly_test[[modelCounter]][ pr$nn_VisOnly_test[[modelCounter]]<0]<-0
MSE$nn_VisOnly_test[[modelCounter]] <- mean(VisOnly_test_scaled$weightsG0*
abs(exp(log(covars_Joint_max.train['y'])*
(VisOnly_test_scaled$yVisOnly -
pr$nn_VisOnly_test[[modelCounter]]))),na.rm = TRUE)
## how well does the model predict joint test data?
pr$nn_VisOnly_test_allData[[modelCounter]] <- predict(nn_VisOnly[[modelCounter]],
Joint_test_scaled[,model1.indices],
na.action = na.omit)
pr$nn_VisOnly_test_allData[[modelCounter]][pr$nn_VisOnly_test_allData[[modelCounter]]<0]<-0
MSE$nn_VisOnly_test_allData[[modelCounter]] <- mean(Joint_test_scaled$weightsG0*
abs(exp(log(covars_Joint_max.train['y'])*(Joint_test_scaled$y -
pr$nn_VisOnly_test_allData[[modelCounter]]))),na.rm = TRUE)
## JOINT
nn_Joint[[modelCounter]] <- avNNet(f.Joint_NN1, Joint_train_scaled,
weights = Joint_train_scaled$weightsG0,
size = layerSize,
repeats = trainRepeats,
na.action = na.omit,
rang = 0.7,
decay = 0.0001,
maxit = 10000,
trace = FALSE)
pr$nn_Joint_train[[modelCounter]] <- predict(nn_Joint[[modelCounter]],
Joint_train_scaled[,model1.indices],
na.action=na.omit)
pr$nn_Joint_train[[modelCounter]][pr$nn_Joint_train[[modelCounter]]<0]<-0
MSE$nn_Joint_train[[modelCounter]] <- mean(Joint_train_scaled$weightsG0*
abs(covars_Joint_max.train['y']*(Joint_train_scaled$y -
pr$nn_Joint_train[[modelCounter]])),na.rm = TRUE)
pr$nn_Joint_test[[modelCounter]] <- predict(nn_Joint[[modelCounter]],
Joint_test_scaled[,model1.indices],
na.action=na.omit)
pr$nn_Joint_test[[modelCounter]][pr$nn_Joint_test[[modelCounter]]<0]<-0
MSE$nn_Joint_test[[modelCounter]] <- mean(Joint_test_scaled$weightsG0*
abs(covars_Joint_max.train['y']*(Joint_test_scaled$y -
pr$nn_Joint_test[[modelCounter]])),na.rm = TRUE)
cat(paste("Done with VisOnly and Joint model iteration ",
modelCounter, " of ", length(layerSizeList),
": Layer Size = ", layerSize, "\n"))
}
```
<br>
```{r save models, eval = TRUE, echo = FALSE}
# Save models if re-calculating everything
save(nn_AcOnly,MSE,pr,
file = paste(outDir,SP,'_AcOnly_NN_density.Rdata',sep=''))
save(nn_VisOnly,MSE,pr,
file = paste(outDir,SP,'_VisOnly_NN_density.Rdata',sep=''))
save(nn_Joint,MSE,pr,
file = paste(outDir,SP,'_Joint_NN_density.Rdata',sep=''))
```
```{r load models, eval = TRUE, echo = FALSE}
# alternative if models are already calculated
load(paste0(outDir,SP,'_AcOnly_NN_density.Rdata'))
load(paste0(outDir,SP,'_VisOnly_NN_density.Rdata'))
load(paste0(outDir,SP,'_Joint_NN_density.Rdata'))
```
<br>
## 2.2 Model Comparisons
Models were compared using mean absolute error (MAE) to compare predicted and observed density in the test data.
```{r Model comparison, echo = FALSE}
XEntropy.AcOnly_train <- NULL
XEntropy.AcOnly_test <- NULL
XEntropy.VisOnly_train <- NULL
XEntropy.VisOnly_test <- NULL
XEntropy.Joint_train <- NULL
XEntropy.Joint_test <- NULL
for (iM in 1: length(nn_AcOnly)){
XEntropy.AcOnly_train[[iM]] <- LogLoss(round(pr$nn_AcOnly_train[[iM]],digits = 0),AcOnly_train_scaled$yAcOnly)
XEntropy.AcOnly_test[[iM]] <- LogLoss(round(pr$nn_AcOnly_test[[iM]],digits = 0),AcOnly_test_scaled$yAcOnly)
XEntropy.VisOnly_train[[iM]] <- weighted_logloss(round(pr$nn_VisOnly_train[[iM]],
digits = 0),VisOnly_train_scaled$yVisOnly,
VisOnly_train_scaled$weightsG0)
XEntropy.VisOnly_test[[iM]] <- weighted_logloss(round(pr$nn_VisOnly_test[[iM]],digits = 0),
VisOnly_test_scaled$yVisOnly,
VisOnly_test_scaled$weightsG0)
XEntropy.Joint_train[[iM]] <- weighted_logloss(round(pr$nn_Joint_train[[iM]], digits = 0),
Joint_train_scaled$y,
Joint_train_scaled$weightsG0)
XEntropy.Joint_test[[iM]] <- weighted_logloss(round(pr$nn_Joint_test[[iM]], digits = 0),
Joint_test_scaled$y,
Joint_test_scaled$weightsG0)
}
KSStat <- NULL
for (iM in 1: length(nn_AcOnly)){
KSStat$AcOnly_train[[iM]] <- KS_Stat(round(pr$nn_AcOnly_train[[iM]],digits = 0),AcOnly_train_scaled$yAcOnly)
KSStat$AcOnly_test[[iM]] <- KS_Stat(round(pr$nn_AcOnly_test[[iM]],digits = 0),AcOnly_test_scaled$yAcOnly)
KSStat$VisOnly_train[[iM]] <- KS_Stat(round(pr$nn_VisOnly_train[[iM]],digits = 0),VisOnly_train_scaled$yVisOnly)
KSStat$VisOnly_test[[iM]] <- KS_Stat(round(pr$nn_VisOnly_test[[iM]],digits = 0),VisOnly_test_scaled$yVisOnly)
KSStat$Joint_train[[iM]] <- KS_Stat(round(pr$nn_Joint_train[[iM]],digits = 0),Joint_train_scaled$y)
KSStat$Joint_test[[iM]] <- KS_Stat(round(pr$nn_Joint_test[[iM]],digits = 0),Joint_test_scaled$y)
}
RMSE <- NULL
MAE <- NULL
wRMSE <- NULL
wMAE <- NULL
modelCounter <-1
for (modelCounter in 1:length(nn_AcOnly)){
pr$nn_VisOnly_test_allData[[modelCounter]] <-(exp(log(covars_Joint_max.train['y'])*
predict(nn_VisOnly[[modelCounter]],
Joint_test_scaled[,model1.indices],
na.action = na.omit)))-1
pr$nn_VisOnly_test_allData[[modelCounter]][pr$nn_VisOnly_test_allData[[modelCounter]]<0]<-0
MAE$nn_VisOnly_test_allData[[modelCounter]] <- mean(
abs(Joint.test.NoNa$y -
pr$nn_VisOnly_test_allData[[modelCounter]]),na.rm = TRUE)
RMSE$nn_VisOnly_test_allData[[modelCounter]] <- sqrt(mean(
(Joint.test.NoNa$y -
pr$nn_VisOnly_test_allData[[modelCounter]])^2,na.rm = TRUE))
wMAE$nn_VisOnly_test_allData[[modelCounter]] <- mean(Joint_test_scaled$weightsG0*
abs(Joint.test.NoNa$y -
pr$nn_VisOnly_test_allData[[modelCounter]]),na.rm = TRUE)
wRMSE$nn_VisOnly_test_allData[[modelCounter]] <- sqrt(mean(Joint_test_scaled$weightsG0*
(Joint.test.NoNa$y -
pr$nn_VisOnly_test_allData[[modelCounter]])^2,na.rm = TRUE))
## how well does the Acoustic only model predict joint test data?
pr$nn_AcOnly_test_allData[[modelCounter]] <- (exp(log(covars_Joint_max.train['y'])*
predict(nn_AcOnly[[modelCounter]],
Joint_test_scaled[,model1.indices],
na.action = na.omit)))-1
pr$nn_AcOnly_test_allData[[modelCounter]][pr$nn_AcOnly_test_allData[[modelCounter]]<0]<-0
MAE$nn_AcOnly_test_allData[[modelCounter]] <- mean(
abs(Joint.test.NoNa$y -
pr$nn_AcOnly_test_allData[[modelCounter]]),na.rm = TRUE)
RMSE$nn_AcOnly_test_allData[[modelCounter]] <- sqrt(mean(
(Joint.test.NoNa$y -
pr$nn_AcOnly_test_allData[[modelCounter]])^2,na.rm = TRUE))
wMAE$nn_AcOnly_test_allData[[modelCounter]] <- mean(Joint_test_scaled$weightsG0*
abs(Joint.test.NoNa$y -
pr$nn_AcOnly_test_allData[[modelCounter]]),na.rm = TRUE)
wRMSE$nn_AcOnly_test_allData[[modelCounter]] <- sqrt(mean((Joint_test_scaled$weightsG0*
(Joint.test.NoNa$y -
pr$nn_AcOnly_test_allData[[modelCounter]]))^2,na.rm = TRUE))
## how well does the Joint only model predict joint test data?
pr$nn_Joint_test_allData[[modelCounter]] <- (exp(log(covars_Joint_max.train['y'])*
predict(nn_Joint[[modelCounter]],
Joint_test_scaled[,model1.indices],
na.action = na.omit)))-1
pr$nn_Joint_test_allData[[modelCounter]][pr$nn_Joint_test_allData[[modelCounter]]<0]<-0
MAE$nn_Joint_test_allData[[modelCounter]] <- mean(
abs(Joint.test.NoNa$y -
pr$nn_Joint_test_allData[[modelCounter]]),na.rm = TRUE)
RMSE$nn_Joint_test_allData[[modelCounter]] <- sqrt(mean(
(Joint.test.NoNa$y -
pr$nn_Joint_test_allData[[modelCounter]])^2,na.rm = TRUE))
wMAE$nn_Joint_test_allData[[modelCounter]] <- mean(Joint_test_scaled$weightsG0*
abs(Joint.test.NoNa$y -
pr$nn_Joint_test_allData[[modelCounter]]),na.rm = TRUE)
wRMSE$nn_Joint_test_allData[[modelCounter]] <- sqrt(mean((Joint_test_scaled$weightsG0*
(Joint.test.NoNa$y -
pr$nn_Joint_test_allData[[modelCounter]]))^2,na.rm = TRUE))
}
RMSEtable <- rbind(RMSE$nn_AcOnly_test_allData,RMSE$nn_VisOnly_test_allDat,RMSE$nn_Joint_test_allData)
wRMSEtable <- rbind(wRMSE$nn_AcOnly_test_allData,wRMSE$nn_VisOnly_test_allDat,wRMSE$nn_Joint_test_allData)
MAEtable <- rbind(MAE$nn_AcOnly_test_allData,MAE$nn_VisOnly_test_allDat,MAE$nn_Joint_test_allData)
wMAEtable <- rbind(wMAE$nn_AcOnly_test_allData,wMAE$nn_VisOnly_test_allDat,wMAE$nn_Joint_test_allData)
colnames(RMSEtable)<- layerSizeList
rownames(RMSEtable)<- c("Acoustic - Joint test data","Visual - Joint test data","Joint - Test")
colnames(wRMSEtable)<- layerSizeList
rownames(wRMSEtable)<- c("Acoustic - Joint test data","Visual - Joint test data","Joint - Test")
colnames(MAEtable)<- layerSizeList
rownames(MAEtable)<- c("Acoustic - Joint test data","Visual - Joint test data","Joint - Test")
colnames(wMAEtable)<- layerSizeList
rownames(wMAEtable)<- c("Acoustic - Joint test data","Visual - Joint test data","Joint - Test")
print('RMSE (lower is better)')
print(RMSEtable, digits = 3)