---
title: "Main functions"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Main functions}
%\VignetteEngine{knitr::knitr}
%\VignetteEncoding{UTF-8}
---
## Complete code example
Here are presented, in a full and complete example, all main functions (starting with `BIOMOD_[...]`) of `biomod2`.
### Load dataset and variables
```R
library(biomod2)
library(terra)
# Load species occurrences (6 species available)
data("DataSpecies")
head(DataSpecies)
# Select the name of the studied species
myRespName <- 'GuloGulo'
# Get corresponding presence/absence data
myResp <- as.numeric(DataSpecies[, myRespName])
# Get corresponding XY coordinates
myRespXY <- DataSpecies[, c('X_WGS84', 'Y_WGS84')]
# Load environmental variables extracted from BIOCLIM (bio_3, bio_4, bio_7, bio_11 & bio_12)
data("bioclim_current")
myExpl <- rast(bioclim_current)
```
### Prepare data & parameters
#### Format data (observations & explanatory variables)
```R
# Format Data with true absences
myBiomodData <- BIOMOD_FormatingData(resp.var = myResp,
expl.var = myExpl,
resp.xy = myRespXY,
resp.name = myRespName)
myBiomodData
plot(myBiomodData)
```
#### Pseudo-absences extraction
Single or multiple set of pseudo-absences can be selected with the [`BIOMOD_FormatingData`](../reference/BIOMOD_FormatingData.html) function, which calls the [`bm_PseudoAbsences`](../reference/bm_PseudoAbsences.html) function to do so. More examples are presented on the [Secondary functions webpage](examples_2_secundaryFunctions.html).
```R
# # Transform true absences into potential pseudo-absences
# myResp.PA <- ifelse(myResp == 1, 1, NA)
#
# # Format Data with pseudo-absences : random method
# myBiomodData.r <- BIOMOD_FormatingData(resp.var = myResp.PA,
# expl.var = myExpl,
# resp.xy = myRespXY,
# resp.name = myRespName,
# PA.nb.rep = 4,
# PA.nb.absences = 1000,
# PA.strategy = 'random')
#
# myBiomodData.r
# plot(myBiomodData.r)
```
```R
# # Select multiple sets of pseudo-absences
#
# # Transform true absences into potential pseudo-absences
# myResp.PA <- ifelse(myResp == 1, 1, NA)
#
# # Format Data with pseudo-absences : random method
# myBiomodData.multi <- BIOMOD_FormatingData(resp.var = myResp.PA,
# expl.var = myExpl,
# resp.xy = myRespXY,
# resp.name = myRespName,
# PA.nb.rep = 4,
# PA.nb.absences = c(1000, 500, 500, 200),
# PA.strategy = 'random')
# myBiomodData.multi
# summary(myBiomodData.multi)
# plot(myBiomodData.multi)
```
#### Cross-validation datasets
Several cross-validation methods are available and can be selected with the [`BIOMOD_Modeling`](../reference/BIOMOD_Modeling.html) function, which calls the [`bm_CrossValidation`](../reference/bm_CrossValidation.html) function to do so. More examples are presented on the [Secondary functions webpage](examples_2_secundaryFunctions.html).
```R
# # k-fold selection
# cv.k <- bm_CrossValidation(bm.format = myBiomodData,
# strategy = "kfold",
# nb.rep = 2,
# k = 3)
#
# # stratified selection (geographic)
# cv.s <- bm_CrossValidation(bm.format = myBiomodData,
# strategy = "strat",
# k = 2,
# balance = "presences",
# strat = "x")
# head(cv.k)
# head(cv.s)
```
#### Retrieve modeling options
Modeling options are automatically retrieved from selected models within the [`BIOMOD_Modeling`](../reference/BIOMOD_Modeling.html) function, which calls the [`bm_ModelingOptions`](../reference/bm_ModelingOptions.html) function to do so. Model parameters can also be automatically tuned to a specific dataset, by calling the [`bm_Tuning`](../reference/bm_Tuning.html) function, however it can be quite long. More examples are presented on the [Secondary functions webpage](examples_2_secundaryFunctions.html).
```R
# # bigboss parameters
# opt.b <- bm_ModelingOptions(data.type = 'binary',
# models = c('SRE', 'XGBOOST'),
# strategy = 'bigboss')
#
# # tuned parameters with formated data
# opt.t <- bm_ModelingOptions(data.type = 'binary',
# models = c('SRE', 'XGBOOST'),
# strategy = 'tuned',
# bm.format = myBiomodData)
#
# opt.b
# opt.t
```