-
Notifications
You must be signed in to change notification settings - Fork 51
/
Part_3.Rmd
905 lines (672 loc) · 44.3 KB
/
Part_3.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
# **Habitat Suitability and Distribution Models**
### with Applications in R
\
**by A. Guisan (1), W. Thuiller (2), N.E. Zimmermann (3) **,\
\
with contribution by V. Di Cola, D. Georges and A. Psomas\
\
_(1) University of Lausanne, Switzerland_\
_(2) CNRS, Université Grenoble Alpes, France_\
_(3) Swiss Federal Research Institute WSL, Switzerland_\
#### Cambridge University Press
http://www.cambridge.org/gb/academic/subjects/life-sciences/quantitative-biology-biostatistics-and-mathematical-modellin/habitat-suitability-and-distribution-models-applications-r
*Citation:*
@book{
title={Habitat Suitability and Distribution Models: With Applications in R},
author={Guisan, A. and Thuiller, W. and Zimmermann, N.E.},
isbn={9780521758369},
series={Ecology, Biodiversity and Conservation},
year={2017},
publisher={Cambridge University Press}
}
*If you use any of these figures and code examples in a presentation or lecture, somewhere in your set of slides we would really appreciate if you please add the paragraph: "Some of the figures in this presentation are taken from "Habitat Suitability and Distribution Models: with applications in R" (CUP, 2017) with permission from the authors: A. Guisan, W. Thuiller and N.E. Zimmerman "
If you wish to use any of these figures in a publication, you must get permission from CUP, and each figure must be accompanied by a similar acknowledgement.*
# PART III "Modeling Approaches and Model Calibration"
## Chapter 9: Envelopes and distance-based approaches
### 9.2 Envelope approaches
```{r Envelope_approaches_b1 9.1, opts.label = 'fig_half_page', fig.cap = "Figure 9.1: Observed and potential distribution of the red fox using a rectilinear envelope model (sre function in the biomod2 package). The potential distributions differ by the use of different percentiles to delineate the envelope. In both maps, black=presence, light gray= absence."}
library(biomod2) # install.packages("biomod2", repos="http://R-Forge.R-project.org")
## Load the species and environmental datasets
mammals_data <- read.csv("tabular/species/mammals_and_bioclim_table.csv", row.names=1)
head(mammals_data)
pred_BIOCLIM = sre(Response = mammals_data$VulpesVulpes,
Explanatory = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
NewData = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
Quant = 0)
pred_BIOCLIM_025 = sre(Response = mammals_data$VulpesVulpes,
Explanatory = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
NewData = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
Quant = 0.025)
pred_BIOCLIM_05 = sre(Response = mammals_data$VulpesVulpes,
Explanatory = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
NewData = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
Quant = 0.05)
par(mfrow=c(2,2))
level.plot(mammals_data$VulpesVulpes,
XY=mammals_data[,c("X_WGS84", "Y_WGS84")],
color.gradient = "grey",
cex=0.3,show.scale=F,
title="Original data")
level.plot(pred_BIOCLIM,
XY=mammals_data[,c("X_WGS84", "Y_WGS84")],
color.gradient = "grey",
cex=0.3,show.scale=F,
title="BIOCLIM 100%")
level.plot(pred_BIOCLIM_025,
XY=mammals_data[,c("X_WGS84", "Y_WGS84")],
color.gradient = "grey",
cex=0.3,show.scale=F,
title="BIOCLIM 97.5%")
level.plot(pred_BIOCLIM_05,
XY=mammals_data[,c("X_WGS84", "Y_WGS84")],
color.gradient = "grey",
cex=0.3,show.scale=F,
title="BIOCLIM 95%")
par(mfrow=c(1,1))
```
### 9.3 Distance-based methods
```{r code_9.3_Distance-based_methods_b1}
library(adehabitatHS)
library(pROC)
```
```{r code_9.3_Distance-based_methods_b2}
pc <- dudi.pca(mammals_data[,c("bio3", "bio7", "bio11", "bio12")], scannf=FALSE, nf = 2)
```
```{r code_9.3_Distance-based_methods_b3}
en <- enfa(pc, mammals_data$VulpesVulpes, scan=FALSE)
```
```{r Distance-based_methods 9.2, opts.label = 'fig_half_page', fig.cap = 'Figure 9. 2: Ecological niche description of the red fox (function enfa() in the package adehabitatHS)'}
par(mfrow=c(2,2))
barplot(en$s) # the specialization diagram
scatterniche(en$li,mammals_data$VulpesVulpes, pts=T) # plot the niche
s.arrow(cor(pc$tab,en$li)) # meaning of the axes
```
```{r Distance-based_methods 9.3, opts.label = 'fig_half_page', fig.cap = 'Figure 9. 3: Observed and potential distribution of the red fox modeled using ENFA. The potential distribution is either expressed along a scale of habitat suitability values (light= low suitability to dark = high suitability), or in a binary form picturing presence-absence (black=presence, light gray= absence). '}
par(mfrow=c(2,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="Original data")
level.plot(en$li[,1], XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="ENFA")
roc_enfa <- roc(mammals_data$VulpesVulpes, en$li[,1])
threshold_enfa <- coords(roc_enfa, "best", ret=c("threshold"))
Pred01=as.numeric(en$li[,1] > threshold_enfa)
level.plot(Pred01, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="ENFA binary")
```
## Chapter 10 Regression-based approaches
### 10.2 Generalized Linear Models (GLM)
```{r code_10.2_Generalized_Linear_Models_GLM_b1, opts.label = 'fig_half_page', fig.cap = 'Figure 10. 1: Observed (black=presence, light gray= absence) and potential distribution of species Sp290 modeled by different GLM differing by the complexity of the parameters (linear, quadratic and 2nd order polynomials). The gray scale of predictions (up-right and lower panels) shows habitat suitability values between 0 (light, unsuitable) and 1 (dark, highly suitable)'}
glm1 = glm(VulpesVulpes ~ 1+bio3+bio7+bio11+bio12, data=mammals_data, family="binomial")
glm2 = glm(VulpesVulpes ~ 1+poly(bio3,2)+poly(bio7,2)+poly(bio11,2)+poly(bio12,2), data=mammals_data, family="binomial")
```
```{r GLM1 10.1, fig.height=8,fig.width=8}
library(biomod2)
par(mfrow=c(2,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="Original data")
level.plot(fitted(glm1), XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="GLM with linear terms")
level.plot(fitted(glm2), XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="GLM with quadratic terms")
```
```{r GLM_b2 10.2, message=FALSE,warning=FALSE, opts.label = 'fig_quarter_page', fig.cap = 'Figure 10. 2: Response curves of model glm1 (linear terms) and glm2 (only quadratic terms).'}
library(ggplot2)
## create the response plot
rp <- response.plot2(models = c('glm1','glm2'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'mean', plot = FALSE, use.formal.names = TRUE)
## define a custom ggplot2 theme
rp.gg.theme <- theme(legend.title = element_blank(),
axis.text.x = element_text(angle = 90, vjust = .5),
panel.background = element_rect(fill = NA, colour = "gray70"),
strip.background = element_rect(fill = NA, colour = "gray70"),
panel.grid.major = element_line(colour = "grey90"),
legend.key = element_rect(fill = NA, colour = "gray70"))
## display the reponse plot
gg.rp <- ggplot(rp, aes(x = expl.val, y = pred.val, lty = pred.name)) +
geom_line() + ylab("prob of occ") + xlab("") +
rp.gg.theme +
facet_grid(~ expl.name, scales = 'free_x')
print(gg.rp)
```
```{r code_10.2_Generalized_Linear_Models_GLM_b3}
library(MASS)
glmStart <- glm(VulpesVulpes~1, data=mammals_data, family=binomial)
glm.formula <- formula("VulpesVulpes ~ 1 + 1 + poly(bio3,2) + poly(bio7,2) + poly(bio11,2) + poly(bio12,2) + bio3:bio7 + bio3:bio11 + bio3:bio12 + bio7:bio11 + bio7:bio12 + bio11:bio12")
glm.formula
```
```{r GLM_b4 10.3, opts.label = 'fig_quarter_page', fig.cap = 'Figure 10. 3: 2D response curves for the different fitted models'}
glmModAIC <- stepAIC( glmStart,
glm.formula,
data = mammals_data,
direction = "both", trace = FALSE,
k = 2,
control=glm.control(maxit=100))
glmModBIC <- stepAIC( glmStart,
glm.formula,
direction="both", trace=FALSE,
k=log(nrow(mammals_data)),
control=glm.control(maxit=100))
rp <- response.plot2(models = c('glm1','glm2','glmModAIC','glmModBIC'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'mean', plot = FALSE, use.formal.names = TRUE)
gg.rp <- ggplot(rp, aes(x = expl.val, y = pred.val, lty = pred.name)) +
geom_line() + ylab("prob of occ") + xlab("") +
rp.gg.theme +
facet_grid(~ expl.name, scales = 'free_x')
print(gg.rp)
```
```{r GLM_b5 10.4, opts.label = 'fig_quarter_page', fig.cap = 'Figure 10. 4: Bivariate response curves from the model glmModAIC for four predictor variables.'}
rp.2D <- response.plot2( models = c('glmModAIC'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'median',
do.bivariate=T,
plot = FALSE, use.formal.names = TRUE )
gg.rp.2D <- ggplot(rp.2D, aes(x = expl1.val, y = expl2.val, fill = pred.val)) +
geom_raster() + rp.gg.theme + ylab("") + xlab("") + theme(legend.title = element_text()) +
scale_fill_gradient(name = "prob of occ.", low = "#f0f0f0", high = "#000000") +
facet_grid(expl2.name ~ expl1.name, scales = 'free')
print(gg.rp.2D)
```
```{r code_10.2_Generalized_Linear_Models_GLM_b6}
anova(glmModAIC)
```
```{r GLM_b7 10.5, opts.label = 'fig_half_page', fig.cap = 'Figure 10. 5: Observed (black=presence, light gray= absence) and potential distribution of red fox extracted from glmModAIC and glmModBIC models. The gray scale of predictions shows habitat suitability values between 0 (light, unsuitable) and 1 (dark, highly suitable).'}
par(mfrow=c(2,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,level.range=c(0,1), show.scale=F, title="Original data")
level.plot(fitted(glmModAIC), XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3, level.range=c(0,1), show.scale=F, title="Stepwise GLM with AIC")
level.plot(fitted(glmModBIC), XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,level.range=c(0,1), show.scale=F, title="Stepwise GLM with BIC")
```
### 10.3 Generalized Additive Models
```{r code_10.3_Generalized_Additive_Models_b1}
if(is.element("package:mgcv", search())) detach("package:mgcv") ## make sure the mgcv package is not loaded to avoid conflicts
library(gam)
gam1 = gam(VulpesVulpes ~ s(bio3,2) + s(bio7,2) + s(bio11,2) + s(bio12,2), data=mammals_data, family="binomial")
gam2 = gam(VulpesVulpes ~ s(bio3,4) + s(bio7,4) + s(bio11,4) + s(bio12,4), data=mammals_data, family="binomial")
```
```{r GAMb2 10.6, opts.label = 'fig_half_page', fig.cap = 'Figure 10. 6: Response curves of model gam1 expressed in logit scale (function plot.gam() from the gam package).'}
par(mfrow=c(2,2))
plot(gam1, se=T)
```
```{r GAMb3 10.7, message=FALSE,warning=FALSE, opts.label = 'fig_quarter_page', fig.cap = 'Figure 10. 7: Response curves of the gam1 (degree of smoothing = 2) and gam2 (degree of smoothing = 4) models.'}
rp <- response.plot2(models = c('gam1', 'gam2'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'mean', plot = FALSE, use.formal.names = TRUE)
gg.rp <- ggplot(rp, aes(x = expl.val, y = pred.val, lty = pred.name)) +
geom_line() + ylab("prob of occ") + xlab("") +
rp.gg.theme +
facet_grid(~ expl.name, scales = 'free_x')
print(gg.rp)
```
```{r code_10.3_Generalized_Additive_Models_b4}
gamStart <- gam(VulpesVulpes~1, data=mammals_data, family=binomial)
gamModAIC <- step.gam(gamStart, biomod2:::.scope(mammals_data[1:3,c("bio3", "bio7", "bio11", "bio12")], "s", 4), trace=F, direction = "both")
```
```{r code_10.3_Generalized_Additive_Models_b4b, echo=FALSE, eval = FALSE}
## test of the multiple smoother case
biomod2:::.scope(mammals_data[1:3,c("bio3", "bio7", "bio11", "bio12")], "s", 2:4)
gamModAIC.test <- step.gam(gamStart, biomod2:::.scope(mammals_data[1:3,c("bio3", "bio7", "bio11", "bio12")], "s", 2:4), trace=F, direction = "both")
```
```{r GAMb5 10.8, opts.label = 'fig_quarter_page', fig.cap = 'Figure 10. 8. Observed (black=presence, light gray= absence) and potential distribution of Vulpes vulpes extracted from gamModAIC. The gray scale of prediction illustratesshows habitat suitability values between 0 (light, unsuitable) and 1 (dark, highly suitable)'}
par(mfrow=c(1,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,level.range=c(0,1), show.scale=F, title="Original data")
level.plot(fitted(gamModAIC), XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3, level.range=c(0,1), show.scale=F, title="Stepwise GAM with AIC")
```
```{r code_10.3_Generalized_Additive_Models_b6, fig.keep = FALSE}
if(is.element("package:gam", search())) detach("package:gam") ## make sure the gam package is not loaded to avoid conflicts
library(mgcv)
gam_mgcv <- gam(VulpesVulpes~s(bio3)+s(bio7)+s(bio11)+s(bio12),data = mammals_data, family = "binomial")
## see a range of summary statistics
summary(gam_mgcv)
gam.check(gam_mgcv)
```
```{r GAMb7 10.9, opts.label = 'fig_half_page', fig.cap = 'Figure 10.9. Response curves of model gam_mgcv plotted using the internal function of mgcv().'}
plot(gam_mgcv,pages=1, seWithMean=TRUE)
```
```{r GAMb8 10.10, opts.label = 'fig_quarter_page', fig.cap = 'Figure 10.10. Response curves from the model calibrated with the mgcv package (gam_mgcv).'}
rp <- response.plot2(models = c('gam_mgcv'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'mean', plot = FALSE, use.formal.names = TRUE)
gg.rp <- ggplot(rp, aes(x = expl.val, y = pred.val, lty = pred.name)) +
geom_line() + ylab("prob of occ") + xlab("") +
rp.gg.theme +
facet_grid(~ expl.name, scales = 'free_x')
print(gg.rp)
```
```{r GAMb9 10.11, opts.label = 'fig_quarter_page', fig.cap = 'Figure 10.11. Observed (black=presence, light gray= absence) and potential distribution of Vulpes vulpes extracted from the gam_mgcv object. The gray scale of predictions illustrates shows habitat suitability values between 0 (light, unsuitable) and 1 (dark, highly suitable).'}
par(mfrow=c(1,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,level.range=c(0,1), show.scale=F, title="Original data")
level.plot(fitted(gam_mgcv), XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3, level.range=c(0,1), show.scale=F, title="GAM with mgcv")
par(mfrow=c(1,1))
```
### 10.4 Multivariate Adaptive Regression Splines
```{r code_10.4_Multivariate_Adaptive_Regression_Splines_b1}
library(earth)
Mars_int1 <- earth(VulpesVulpes ~ 1+bio3+bio7+bio11+bio12,
data = mammals_data,
degree = 1,
glm = list(family = binomial))
Mars_int2 <- earth(VulpesVulpes ~ 1+bio3+bio7+bio11+bio12,
data = mammals_data,
degree = 2,
glm = list(family = binomial))
## print the summary of objects
Mars_int1
Mars_int2
```
```{r code_10.4_Multivariate_Adaptive_Regression_Splines_b2}
summary(fitted.values(Mars_int1))
```
```{r code_10.4_Multivariate_Adaptive_Regression_Splines_b3}
pred_Mars_int1 <- predict(Mars_int1, type="response")
summary(pred_Mars_int1)
pred_Mars_int2 <- predict(Mars_int2, type="response")
summary(pred_Mars_int2)
```
```{r MARS_b4 10.12, opts.label = 'fig_half_page', fig.cap = 'Figure 10. 12. The distribution of the predicted values from MARS for both the presence and absence of Vulpes vulpes.'}
plotd(Mars_int1, hist = T)
```
```{r MARS_b5 10.13, opts.label = 'fig_quarter_page', fig.cap = 'Figure 10. 13: Differences between probability of occurrence between a MARS model with a maximum of 1 degree of interaction and a MARS model with a maximum of 2 degrees of interaction.'}
plot(pred_Mars_int1, pred_Mars_int2, xlab="MARS with max inter degree 1", ylab="MARS with max inter degree 2")
```
```{r MARS_b6 10.14, opts.label = 'fig_half_page', fig.cap = 'Figure 10. 14: Observed (black=presence, light gray= absence) and potential distribution of Vulpes vulpes extracted from the MARS 1 and MARS 2 objects. The gray scale of predictions (upper-right and lower-left panels) illustratesshows habitat suitability values between 0 (light, unsuitable) and 1 (dark, highly suitable)'}
par(mfrow=c(2,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,level.range=c(0,1),show.scale=F, title="Original data")
level.plot(pred_Mars_int1, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,level.range=c(0,1),show.scale=F, title="MARS with interaction degree 1")
level.plot(pred_Mars_int2, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,level.range=c(0,1),show.scale=F, title="MARS with interaction degree 2")
par(mfrow=c(1,1))
```
## Chapter 11: Classification approaches and machine learning systems
### 11.2 Recursive partitioning
```{r code_11.2_Recursive_partitioning_b1}
library(rpart)
RP = rpart(VulpesVulpes ~ 1+bio3+bio7+bio11+bio12, data=mammals_data, control=rpart.control(xval=10), method="class")
```
```{r RP_b2 11.1, opts.label = 'fig_half_page', fig.cap = 'Figure 11. 1: Classification tree for Vulpes vulpes using the rpart() function'}
plot(RP, uniform=F, margin = 0.1, branch = 0.5, compress=T)
text(RP, cex=0.8)
```
```{r RP_b3 11.2, opts.label = 'fig_quarter_page', fig.cap = 'Figure 11. 2: Observed (left; black=presence, light gray= absence) and potential distribution (right) of Vulpes vulpes predicted by recursive partitioning. The gray scale of predictions illustrates shows habitat suitability values between 0 (light, unsuitable) and 1 (dark, highly suitable)'}
RP.pred = predict(RP, type="prob")[,2]
par(mfrow=c(1,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="Original data")
level.plot(RP.pred, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,level.range=c(0,1),show.scale=F, title="Recursive partitioning")
```
```{r RP_b4 11.3, opts.label = 'fig_quarter_page', fig.cap = 'Figure 11.3: Response curves of a recursive partitioning (RP) model.'}
rp <- response.plot2(models = c('RP'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'mean', plot = FALSE, use.formal.names = TRUE)
gg.rp <- ggplot(rp, aes(x = expl.val, y = pred.val, lty = pred.name)) +
geom_line() + ylab("prob of occ") + xlab("") +
rp.gg.theme +
facet_grid(~ expl.name, scales = 'free_x')
print(gg.rp)
```
## 11.3 Linear Discriminant Analysis and extensions
```{r code_11.3_Linear_Discriminant_Analysis_and_extensions_b1}
library(mda)
fda_mod = fda(VulpesVulpes ~ 1+bio3+bio7+bio11+bio12, data=mammals_data,method=mars)
```
```{r code_11.3_Linear_Discriminant_Analysis_and_extensions_b2}
fda_mod$confusion
```
```{r FDA_b3 11.4, opts.label = 'fig_quarter_page', fig.cap = 'Figure 11.4: Observed and potential distribution of Vulpes vulpes predicted by flexible discriminant analysis based on MARS algorithm.'}
FDA.pred = predict(fda_mod, mammals_data[,c("bio3", "bio7", "bio11", "bio12")], type = "posterior")[,2]
par(mfrow=c(1,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="Original data")
level.plot(FDA.pred, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="FDA")
```
```{r FDA_b4 11.5, opts.label = 'fig_quarter_page', fig.cap = 'Figure 11. 5: Response curve of Vulpes vulpes modeled using flexible discriminant analysis.'}
rp <- response.plot2(models = c('fda_mod'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'mean', plot = FALSE, use.formal.names = TRUE)
gg.rp <- ggplot(rp, aes(x = expl.val, y = pred.val, lty = pred.name)) +
geom_line() + ylab("prob of occ") + xlab("") +
rp.gg.theme +
facet_grid(~ expl.name, scales = 'free_x')
print(gg.rp)
```
```{r FDA_b5 11.6, opts.label = 'fig_half_page', fig.cap = 'Figure 11. 6: Bivariate.response curve of Vulpes vulpes modeled using flexible discriminant analysis along four predictor variables.'}
rp.2D <- response.plot2( models = c('fda_mod'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'median',
do.bivariate=T,
plot = FALSE, use.formal.names = TRUE )
gg.rp.2D <- ggplot(rp.2D, aes(x = expl1.val, y = expl2.val, fill = pred.val)) +
geom_raster() + rp.gg.theme + ylab("") + xlab("") + theme(legend.title = element_text()) +
scale_fill_gradient(name = "prob of occ.", low = "#f0f0f0", high = "#000000") +
facet_grid(expl2.name ~ expl1.name, scales = 'free')
print(gg.rp.2D)
```
## 11.4 Artificial Neural Networks
```{r code_11.4_Artificial_Neural_Networks_b1}
library(nnet)
set.seed(555)
nnet.Init = nnet(mammals_data[,c("bio3", "bio7", "bio11", "bio12")], mammals_data$VulpesVulpes, size = 2, rang = 0.1, decay = 5e-4, maxit = 200)
```
Interested readers are advised to take a look at the source code for this function by typing: *biomod2:::.CV.nnet* in R
```{r code_11.4_Artificial_Neural_Networks_b2}
set.seed(555)
CV_nnet = biomod2:::.CV.nnet(Input= mammals_data[,c("bio3", "bio7", "bio11", "bio12")], Target=mammals_data$VulpesVulpes)
nnet.Final = nnet(mammals_data[,c("bio3", "bio7", "bio11", "bio12")], mammals_data$VulpesVulpes, size = CV_nnet[1,1], rang = 0.1, decay = CV_nnet[1,2], maxit = 200, trace=F)
```
```{r code_11.4_Artificial_Neural_Networks_b3}
CV_nnet
```
```{r NNET_b4 11.7, opts.label = 'fig_half_page', fig.cap = 'Figure 11. 7: Observed (black=presence, light gray= absence) and potential distribution of Vulpes vulpes modeled using a neural network algorithm with two different sets of SIZE and DECAY. The gray scale of predictions (upper-right and lower-left panels) illustrates shows habitat suitability values between 0 (light, unsuitable) and 1 (dark, highly suitable)'}
nnet.Init.pred = predict(nnet.Init, mammals_data[,c("bio3", "bio7", "bio11", "bio12")])
nnet.Final.pred = predict(nnet.Final, mammals_data[,c("bio3", "bio7", "bio11", "bio12")])
par(mfrow=c(2,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="Original data")
level.plot(nnet.Init.pred, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="nnet.Init")
level.plot(nnet.Final.pred, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="nnet.Final")
par(mfrow=c(1,1))
```
```{r NNET_b5 11.8, opts.label = 'fig_quarter_page', fig.cap = 'Figure 11. 8: Response curve of Vulpes vulpes modeled by neural networks. The red lines represent a first model with reasonable but not optimized parameters set for SIZE and DECAY, while the blue line represents the final model with optimized parameters.'}
rp <- response.plot2(models = c('nnet.Init','nnet.Final'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'mean', plot = FALSE, use.formal.names = TRUE)
gg.rp <- ggplot(rp, aes(x = expl.val, y = pred.val, lty = pred.name)) +
geom_line() + ylab("prob of occ") + xlab("") +
rp.gg.theme +
facet_grid(~ expl.name, scales = 'free_x')
print(gg.rp)
```
## Chapter 12: Boosting and bagging approaches
### 12.2 Random Forests
```{r code_12.2_ Random_Forests_b1}
set.seed(555)
RP.PantheraOnca = rpart(mammals_data$PantheraOnca ~ bio3+bio7+bio11+bio12, data=mammals_data, control = rpart.control(xval = 10), method="class")
```
```{r RF_b2 12.1, opts.label = 'fig_half_page', fig.cap = 'Figure 12.1. Classification tree for Panthera onca using the rpart() function.'}
plot(RP.PantheraOnca, uniform=F, margin = 0.1, branch = 0.5, compress=T)
text(RP.PantheraOnca, cex=0.8)
```
```{r code_12.2_ Random_Forests_b3}
trees = vector(mode = "list", length = 50)
n <- nrow(mammals_data)
boot = rmultinom(length(trees), n, rep(1, n)/n)
```
```{r code_12.2_ Random_Forests_b4}
Full_tree = rpart(mammals_data$PantheraOnca~ bio3+bio7+bio11+bio12, data=mammals_data, control = rpart.control(xval = 0), method="class" )
for(i in 1:length(trees)) {
trees[[i]] <- update(Full_tree, weights = boot[,i])
}
```
```{r code_12.2_ Random_Forests_b5}
table(sapply(trees, function(x) as.character(x$frame$var[1])))
table(sapply(trees, function(x) as.character(x$frame$var[3])))
table(sapply(trees, function(x) as.character(x$frame$var[5])))
table(sapply(trees, function(x) as.character(x$frame$var[10])))
```
```{r code_12.2_ Random_Forests_b6}
Pred <- matrix(0, nrow = n, ncol = length(trees))
for (i in 1:length(trees)) {
# extract the prediction for each of the trees
Pred[,i] <- predict(trees[[i]], newdata = mammals_data[,c("bio3", "bio7", "bio11", "bio12")], type='prob')[,2]
# remove potential predictions with a negative weight in the bootstrap procedure
Pred[boot[,i] < 0,i] <- NA
}
## calculate the average probability of occurrence (e.g. habitat suitability)
Pred.AVG <- rowMeans(Pred, na.rm = TRUE)
```
```{r code_12.2_ Random_Forests_b7}
require(pROC,quietly=T)
roc_AVG <- roc(mammals_data$PantheraOnca, Pred.AVG, percent=T)
AUC_AVG <- as.numeric(auc(roc_AVG))
AUC_AVG
roc_RP <- roc(mammals_data$PantheraOnca, predict(RP.PantheraOnca, type='prob')[,2], percent=T)
AUC_RP <- as.numeric(auc(roc_RP))
AUC_RP
```
```{r code_12.2_ Random_Forests_b8}
library(randomForest)
RF = randomForest(x = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],y = as.factor(mammals_data$VulpesVulpes), ntree = 1000, importance = TRUE)
```
```{r code_12.2_ Random_Forests_b9}
importance(RF)
```
```{r RF_b10 12.2, opts.label = 'fig_quarter_page', fig.cap = 'Figure 12. 2: Observed (left; black=presence, light gray= absence) and potential distribution (right) of red fox modeled using random forest. The gray scale of predictions illustrates shows habitat suitability values between 0 (light, unsuitable) and 1 (dark, highly suitable)'}
RF.pred = predict(RF, type="prob")[,2]
par(mfrow=c(1,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="Original data")
level.plot(RF.pred, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="RF")
par(mfrow=c(1,1))
```
```{r RF_b11 12.3, opts.label = 'fig_quarter_page', fig.cap = 'Figure 12. 3: RF response curve for the red fox.'}
rp <- response.plot2(models = c('RF'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'mean', plot = FALSE, use.formal.names = TRUE)
gg.rp <- ggplot(rp, aes(x = expl.val, y = pred.val, lty = pred.name)) +
geom_line() + ylab("prob of occ") + xlab("") +
rp.gg.theme +
facet_grid(~ expl.name, scales = 'free_x')
print(gg.rp)
```
### 12.3 Boosted Regression Trees
```{r code_12.3_Boosted_Regression_Trees_b1}
library(gbm)
### Note that this line of code takes quite a longbit of time to run.
GBM.mod <- gbm(VulpesVulpes~ bio3+bio7+bio11+bio12, data=mammals_data, distribution = "bernoulli", n.trees = 10000, interaction.depth = 3, shrinkage = 0.01, bag.fraction = 0.5, cv.folds=10)
```
```{r GBM_b2 12.4, opts.label = 'fig_quarter_page', fig.cap = 'Figure 12. 4: Optimal number of iterations (trees) for the GBM object. The Y-axis represents the error of the model in function of the total number of trees (X-axis). The black line represents the error of the calibrated model with all data, while the green line represents the error from the cross-validation runs.'}
gbm.mod.perf = gbm.perf(GBM.mod, method = "cv", plot.it = T)
```
```{r code_12.3_Boosted_Regression_Trees_b3}
summary(GBM.mod, method=relative.influence, plotit=F)
summary(GBM.mod, method=permutation.test.gbm, plotit=F)
```
```{r GBM_b4 12.5, opts.label = 'fig_half_page', fig.cap = 'Figure 12. 5: Response curves of Vulpes vulpes as a function of the explanatory variables built using the plot.gbm function in the GBM package.'}
par(mfrow=c(2,2))
for(i in 1:ncol(mammals_data[,c("bio3", "bio7", "bio11", "bio12")]))
plot(GBM.mod, n.trees =gbm.mod.perf, i.var=i)
par(mfrow=c(1,1))
```
```{r GBM_b5 12.6, opts.label = 'fig_whole_page', fig.cap = 'Figure 12. 6: Response curves of red fox as a function of one (a) or two (b) explanatory variables at a time.'}
library(cowplot)
# Univariate response curves
rp <- response.plot2(models = c('GBM.mod'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'mean', plot = FALSE, use.formal.names = TRUE)
gg.rp <- ggplot(rp, aes(x = expl.val, y = pred.val, lty = pred.name)) +
geom_line() + ylab("prob of occ") + xlab("") +
rp.gg.theme +
facet_grid(~ expl.name, scales = 'free_x')
# Bivariate response curves
rp.2D <- response.plot2( models = c('GBM.mod'),
Data = mammals_data[,c("bio3", "bio7", "bio11", "bio12")],
show.variables = c("bio3", "bio7", "bio11", "bio12"),
fixed.var.metric = 'median',
do.bivariate=T,
plot = FALSE, use.formal.names = TRUE )
gg.rp.2D <- ggplot(rp.2D, aes(x = expl1.val, y = expl2.val, fill = pred.val)) +
geom_raster() + rp.gg.theme + ylab("") + xlab("") + theme(legend.title = element_text()) +
scale_fill_gradient(name = "prob of occ.", low = "#f0f0f0", high = "#000000") +
facet_grid(expl2.name ~ expl1.name, scales = 'free')
plot_grid(gg.rp, gg.rp.2D, labels=c("(a)", "(b)"), ncol = 1, nrow = 2, rel_heights = c(1,2))
```
```{r GBM_b6 12.7, opts.label = 'fig_quarter_page', fig.cap = 'Figure 12. 7: Observed (left; black=presence, light gray= absence) and potential distributions (right) of the red fox modelled using a boosted regression tree approach. The gray scale of predictions showsshows habitat suitability values between 0 (light, unsuitable) and 1 (dark, highly suitable)'}
GBM.pred <- predict(GBM.mod, newdata=mammals_data[,c("bio3", "bio7", "bio11", "bio12")], type="response", n.trees=gbm.mod.perf)
par(mfrow=c(1,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="Original data")
level.plot(GBM.pred, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="GBM")
```
## Chapter 13: Maximum Entropy
### 13.2 Maxent in R
Maxent.jar should be placed in the same directory as the R workspace (getwd())
```{r code_13.2_Maxent_in_R_b1}
## The folders 'book.data' should be in the a directory just a before your working directory
## test if the data directory is well located (i.e. in dirname(getwd()))
parent.dir <- dirname(getwd()) ## get the name of the directory where data dir should be
any(file.exists('data', parent.dir)) ## ok if return TRUE
dir.create("MaxEnt.res")
MaxEnt.layers.dir <- "../data/tabular/bioclim"
MaxEnt.samples.dir <- "../data/tabular/species"
MaxEnt.out.dir <- "MaxEnt.res"
MaxEnt.soft.path <- "../data/maxent.jar" ## the path to maxent.jar file
Java.soft.path <- "C:/Program Files (x86)/Java/jre1.8.0_101/bin/java.exe" #
# the path to java software binaries => to be adapted according to your computer settings
```
```{r code_13.2_Maxent_in_R_b2}
list.files(MaxEnt.layers.dir, pattern=".asc", recursive=T)
```
```{r code_13.2_Maxent_in_R_b3}
list.files(MaxEnt.samples.dir, pattern=".csv")
```
```{r code_13.2_Maxent_in_R_b4}
## define the shell command we want to execute
maxent.cmd <- paste0('"', Java.soft.path, '" -mx512m -jar "', MaxEnt.soft.path, '" environmentallayers="',
file.path(MaxEnt.layers.dir, 'current', 'ascii'), '" samplesfile="',
file.path(MaxEnt.samples.dir, 'VulpesVulpes.csv'), '" projectionlayers="',
file.path(MaxEnt.layers.dir, 'current', 'bioclim_table.csv'), '" outputdirectory="',
MaxEnt.out.dir, '" outputformat=logistic maximumiterations=500 jackknife visible=FALSE redoifexists autorun nowarnings notooltips')
## run Maxent
system(command = maxent.cmd)
#system(command = maxent.cmd, wait = TRUE, intern = TRUE, ignore.stdout = FALSE, ignore.stderr = FALSE)
```
```{r code_13.2_Maxent_in_R_b5}
list.files(MaxEnt.out.dir)
```
```{r code_13.2_Maxent_in_R_b6}
Maxent.pred_AllFeatures <- read.csv(file.path(MaxEnt.out.dir, 'VulpesVulpes_bioclim_table.csv'))
```
```{r Maxent_b7 13.1, opts.label = 'fig_quarter_page', fig.cap = 'Figure 13. 1: Observed (left; black=presence, light gray= absence) and potential distribution (right) of the red fox modeled using Maxent in batch mode from R. The gray scale of predictions shows habitat suitability values between 0 (unsuitable) and 1 (highly suitable).'}
par(mfrow=c(1,2))
level.plot(mammals_data$VulpesVulpes, XY=mammals_data[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3, level.range=c(0,1), show.scale=F, title="Original data")
level.plot(Maxent.pred_AllFeatures[,3], XY=Maxent.pred_AllFeatures[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="MAXENT", level.range=c(0,1))
```
```{r code_13.2_Maxent_in_R_b8}
Maxent.results <- read.csv("MaxEnt.res/maxentResults.csv")
names(Maxent.results)
```
```{r code_13.2_Maxent_in_R_b9, opts.label = 'fig_quarter_page', fig.cap = 'Figure 13. 2: Heuristic estimate of relative contributions of the four environmental variables to the Maxent model using a jackknife procedure.'}
par(mfrow=c(1,1))
barplot(as.matrix(Maxent.results[1,c(8:11)]), horiz = F, cex.names = 1, names = sub(".contribution$", "", names(Maxent.results[1,c(8:11)])), xlab = "Jackknife importance")
```
```{r code_13.2_Maxent_in_R_b10}
## define the shell command we want to execute
maxent.cmd <- paste0('"', Java.soft.path, '" -mx512m -jar "', MaxEnt.soft.path, '" environmentallayers="',
file.path(MaxEnt.layers.dir, 'current', 'ascii'), '" samplesfile="',
file.path(MaxEnt.samples.dir, 'VulpesVulpes.csv'), '" projectionlayers="',
file.path(MaxEnt.layers.dir, 'current', 'bioclim_table.csv'), '" outputdirectory="',
MaxEnt.out.dir, '" outputformat=logistic nowarnings nolinear noquadratic nothreshold noproduct maximumiterations=500 jackknife visible=FALSE redoifexists autorun nowarnings notooltips')
## run Maxent
system(command = maxent.cmd)
Maxent.pred_Hinge <- read.csv('MaxEnt.res/VulpesVulpes_bioclim_table.csv')
```
```{r Maxent_b11 13.3, opts.label = 'fig_quarter_page', fig.cap = 'Figure 13.3 Comparison between the potential distribution (right) of the red fox modeled using Maxent with all feature (by default) and the one from Maxent with only the hinge feature selected. The gray scale of predictions shows habitat suitability values between 0 (unsuitable) and 1 (highly suitable).'}
par(mfrow=c(1,2))
level.plot(Maxent.pred_AllFeatures[,3], XY=Maxent.pred_AllFeatures[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3, level.range=c(0,1), show.scale=F, title="MAXENT - all features")
level.plot(Maxent.pred_Hinge[,3], XY=Maxent.pred_Hinge[,c("X_WGS84", "Y_WGS84")], color.gradient = "grey", cex=0.3,show.scale=F, title="MAXENT - hinge feature", level.range=c(0,1))
```
## Chapter 14: Ensemble modeling and modeling averaging
```{r code_14_Ensemble_modeling_and_modeling_averaging_b1}
library(biomod2)
### Load species and environmental data at lower resolution (100x100km)
DataSpecies <- read.csv(system.file("external/species/mammals_table.csv", package="biomod2"))
require(raster)
myExpl = stack( system.file( "external/bioclim/current/bio3.grd",package="biomod2"), system.file( "external/bioclim/current/bio4.grd", package="biomod2"), system.file( "external/bioclim/current/bio7.grd", package="biomod2"), system.file( "external/bioclim/current/bio11.grd", package="biomod2"), system.file( "external/bioclim/current/bio12.grd", package="biomod2"))
```
Extract the environmental layers for the presence and absence points.
```{r code_14_Ensemble_modeling_and_modeling_averaging_b2}
Env <- extract(myExpl,DataSpecies[,c(2,3)])
```
Combine the presence/absence data and the extracted environmental data.
```{r code_14_Ensemble_modeling_and_modeling_averaging_b3}
DataSpecies <- cbind(DataSpecies,Env)
```
Load the required packages
```{r code_14_Ensemble_modeling_and_modeling_averaging_b4}
library(MASS); library(mgcv); library(earth); library(rpart);library(mda)
library(Hmisc) # would be used for model evaluation using the AUC approach
```
```{r code_14_Ensemble_modeling_and_modeling_averaging_b5}
nCV <- 20 # Number of cross-validations
nRow <- nrow(DataSpecies)
```
```{r code_14_Ensemble_modeling_and_modeling_averaging_b6}
Test_results <- as.data.frame(matrix(0,ncol=nCV,nrow=5, dimnames=list(c("GLM","GAM","MARS","FDA","RF"), NULL)))
```
```{r code_14_Ensemble_modeling_and_modeling_averaging_b7}
Pred_results <- array(0,c(nRow, 5,nCV), dimnames=list(seq(1:nRow), c("GLM","GAM","MARS","FDA","RF"), seq(1:nCV)))
```
```{r code_14_Ensemble_modeling_and_modeling_averaging_b8}
for(i in 1:nCV){
#separate the original data in one sub set for calibration and the other for evaluation.
a <- SampleMat2(ref=DataSpecies$VulpesVulpes, ratio=0.7) # function from the biomod2 package
calib <- DataSpecies[a$calibration,]
eval <- DataSpecies[a$evaluation,]
### GLM ###
glmStart <- glm(VulpesVulpes~1, data=calib, family=binomial)
glm.formula <- makeFormula("VulpesVulpes",DataSpecies[,c("bio3", "bio7", "bio11", "bio12")],"quadratic",interaction.level=1)
glmModAIC <- stepAIC( glmStart, glm.formula, data = calib, direction = "both", trace = FALSE, k = 2, control=glm.control(maxit=100))
# prediction on the evaluation data and evaluation using the AUC approach
Pred_test <- predict(glmModAIC, eval, type="response")
Test_results["GLM",i] <- somers2(Pred_test,eval$VulpesVulpes)["C"]
# prediction on the total dataset
Pred_results[,"GLM",i] <- predict(glmModAIC, DataSpecies, type="response")
### GAM ###
gam_mgcv <- gam(VulpesVulpes~s(bio3)+s(bio7)+s(bio11)+s(bio12),data=calib, family="binomial")
# prediction on the evaluation data and evaluation using the AUC approach
Pred_test <- predict(gam_mgcv, eval, type="response")
Test_results["GAM",i] <- somers2(Pred_test,eval$VulpesVulpes)["C"]
# prediction on the total dataset
Pred_results[,"GAM",i] <- predict(gam_mgcv, DataSpecies, type="response")
### MARS ###
Mars_int2 = earth(VulpesVulpes ~ 1+bio3+bio7+bio11+bio12, data=calib, degree = 2, glm=list(family=binomial))
# prediction on the evaluation data and evaluation using the AUC approach
Pred_test <- predict(Mars_int2, eval, type="response")
Test_results["MARS",i] <- somers2(Pred_test, eval$VulpesVulpes)["C"]
# prediction on the total dataset
Pred_results[,"MARS",i] <- predict(Mars_int2, DataSpecies, type="response")
### FDA ###
fda_mod = fda(VulpesVulpes ~ 1+bio3+bio7+bio11+bio12, data=calib,method=mars)
# prediction on the evaluation data and evaluation using the AUC approach
Pred_test <- predict(fda_mod, eval, type = "posterior")[,2]
Test_results["FDA",i] <- somers2(Pred_test, eval$VulpesVulpes)["C"]
# prediction on the total dataset
Pred_results[,"FDA",i] = predict(fda_mod, DataSpecies[,c("bio3", "bio7", "bio11", "bio12")], type = "posterior")[,2]
### Random Forest ###
RF_mod = randomForest(x = calib[,c("bio3", "bio7", "bio11", "bio12")],y = as.factor(calib$VulpesVulpes), ntree = 1000, importance = TRUE)
# prediction on the evaluation data and evaluation using the AUC approach
Pred_test <- predict(RF_mod, eval, type="prob")[,2]
Test_results["RF",i] <- somers2(Pred_test,eval$VulpesVulpes)["C"]
# prediction on the total dataset
Pred_results[,"RF",i] = predict(RF_mod, DataSpecies, type="prob")[,2]
}
```
```{r EM_b9 14.1, opts.label = 'fig_quarter_page', fig.cap = 'Figure 14. 1: Variation in the area under the receiver operating characteristic curve (AUC) amongst the cross-validation runs and between the different models.'}
library(ggplot2)
AUC <- unlist(Test_results)
AUC <- as.data.frame(AUC)
Test_results_ggplot <- cbind(AUC, model=rep(rownames(Test_results), times=20))
p <- ggplot(Test_results_ggplot, aes(model, AUC))
p + geom_boxplot()
```
```{r EM_b10 14.2, opts.label = 'fig_quarter_page', fig.cap = 'Figure 14.2: Probability density functions of the habitat suitability obtained for a given pixel across the different cross-validation runs, for each of the five modeling techniques.'}
Pred_results[143,,] # select pixel 143 for which the species is predicted present by all models.
HSM <- unlist(as.data.frame(Pred_results[143,,]))
HSM <- as.data.frame(HSM)
Prob_density_ggplot <- cbind(HSM, model=rep(rownames(Pred_results[143,,]), times=20))
ggplot(Prob_density_ggplot, aes(x=HSM, fill=model)) + geom_density(alpha=0.5)
```
```{r EM_b11 , opts.label = 'fig_quarter_page', fig.cap = 'Figure: Probability density functions of the habitat suitability obtained for a given pixel across the different cross-validation runs, for each of the five modeling techniques.'}
### Average prediction (mean and median) and variance
Pred_total_mean <- apply(Pred_results,1,mean)
Pred_total_median <- apply(Pred_results,1,median)
Pred_total_sd <- apply(Pred_results,1,sd)
```
```{r EM_b12 14.3, opts.label = 'fig_half_page', fig.cap = 'Figure 13. 6: Observed presence and absence of Vulpes vulpes at the global scale (top left), together with the two model averaging predictions (mean and median; top right and bottom-left) and the ensemble modeling uncertainty (sd; bottom-right).'}
Obs <- rasterFromXYZ(DataSpecies[,c("X_WGS84", "Y_WGS84", "VulpesVulpes")])
Pred_total_mean_r <- rasterFromXYZ(cbind(DataSpecies[,c("X_WGS84", "Y_WGS84")],Pred_total_mean))
Pred_total_median_r <- rasterFromXYZ(cbind(DataSpecies[,c("X_WGS84", "Y_WGS84")],Pred_total_median))
Pred_total_sd_r <- rasterFromXYZ(cbind(DataSpecies[,c("X_WGS84", "Y_WGS84")],Pred_total_sd))
Out <- stack(Obs,Pred_total_mean_r ,Pred_total_median_r,Pred_total_sd_r)
names(Out) <- c("Observed_Vulpes_vulpes","Ensemble_modeling_mean","Ensemble_modeling_median", "Ensemble_modeling_sd")
plot(Out)
```