train: Train

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/train.R

Description

Train a model using one of the following methods: Artificial Neural Networks, Boosted Regression Trees, Maxent, Maxnet or Random Forest.

Usage

1
train(method, data, folds = NULL, verbose = TRUE, ...)

Arguments

method

character or character vector. Method used to train the model, possible values are "ANN", "BRT", "Maxent", "Maxnet" or "RF", see details.

data

SWD object with presence and absence/background locations.

folds

list. Output of the function randomFolds or folds object created with other packages, see details, default is NULL.

verbose

logical, if TRUE shows a progress bar during cross validation, default is TRUE.

...

Arguments passed to the relative method, see details.

Details

The folds argument accepts also objects created with other packages: ENMeval or blockCV. In this case the function converts internally the folds into a format valid for SDMtune.

When multiple methods are given as method argument, the function returns a named list of model object, with the name corresponding to the used method, see examples.

Value

An SDMmodel or SDMmodelCV or a list of model objects.

Author(s)

Sergio Vignali

References

Venables, W. N. & Ripley, B. D. (2002) Modern Applied Statistics with S. Fourth Edition. Springer, New York. ISBN 0-387-95457-0.

Brandon Greenwell, Bradley Boehmke, Jay Cunningham and GBM Developers (2019). gbm: Generalized Boosted Regression Models. https://CRAN.R-project.org/package=gbm.

A. Liaw and M. Wiener (2002). Classification and Regression by randomForest. R News 2(3), 18–22.

Hijmans, Robert J., Steven Phillips, John Leathwick, and Jane Elith. 2017. dismo: Species Distribution Modeling. https://cran.r-project.org/package=dismo.

Steven Phillips (2017). maxnet: Fitting 'Maxent' Species Distribution Models with 'glmnet'. https://CRAN.R-project.org/package=maxnet.

Muscarella, R., Galante, P.J., Soley-Guardia, M., Boria, R.A., Kass, J., Uriarte, M. and R.P. Anderson (2014). ENMeval: An R package for conducting spatially independent evaluations and estimating optimal model complexity for ecological niche models. Methods in Ecology and Evolution.

Roozbeh Valavi, Jane Elith, José Lahoz-Monfort and Gurutzeta Guillera-Arroita (2018). blockCV: Spatial and environmental blocking for k-fold cross-validation. https://github.com/rvalavi/blockCV.

See Also

randomFolds.

Examples

 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
# Acquire environmental variables
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"),
                    pattern = "grd", full.names = TRUE)
predictors <- raster::stack(files)

# Prepare presence and background locations
p_coords <- virtualSp$presence
bg_coords <- virtualSp$background

# Create SWD object
data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords,
                   env = predictors, categorical = "biome")

## Train a Maxent model
# The next line checks if Maxent is correctly configured but you don't need
# to run it in your script
if (dismo::maxent(silent = TRUE)) {
model <- train(method = "Maxent", data = data, fc = "l", reg = 1.5,
               iter = 700)

# Add samples to background. This should be done preparing the data before
# training the model without using
data <- addSamplesToBg(data)
model <- train("Maxent", data = data)
}

## Train a Maxnet model
model <- train(method = "Maxnet", data = data, fc = "lq", reg = 1.5)

## Cross Validation
# Create 4 random folds splitting only the presence data
folds <- randomFolds(data, k = 4, only_presence = TRUE)
model <- train(method = "Maxnet", data = data, fc = "l", reg = 0.8,
               folds = folds)

## Not run: 
# Run only if you have the package ENMeval installed
## Block partition using the ENMeval package
require(ENMeval)
block_folds <- get.block(occ = data@coords[data@pa == 1, ],
                         bg.coords = data@coords[data@pa == 0, ])
model <- train(method = "Maxnet", data = data, fc = "l", reg = 0.8,
               folds = block_folds)

## Checkerboard1 partition using the ENMeval package
cb_folds <- get.checkerboard1(occ = data@coords[data@pa == 1, ],
                              env = predictors,
                              bg.coords = data@coords[data@pa == 0, ],
                              aggregation.factor = 4)
model <- train(method = "Maxnet", data = data, fc = "l", reg = 0.8,
               folds = cb_folds)

## Environmental block using the blockCV package
# Run only if you have the package blockCV
require(blockCV)
# Create spatial points data frame
library(raster)
sp_df <- SpatialPointsDataFrame(data@coords, data = as.data.frame(data@pa),
                                proj4string = crs(predictors))
e_folds <- envBlock(rasterLayer = predictors,
                    speciesData = sp_df,
                    species = "data@pa",
                    k = 4,
                    standardization = "standard",
                    rasterBlock = FALSE)
model <- train(method = "Maxnet", data = data, fc = "l", reg = 0.8,
               folds = e_folds)

## End(Not run)

## Train presence absence models
# Prepare presence and absence locations
p_coords <- virtualSp$presence
a_coords <- virtualSp$absence
# Create SWD object
data <- prepareSWD(species = "Virtual species", p = p_coords, a = a_coords,
                   env = predictors[[1:5]])

## Train an Artificial Neural Network model
model <- train("ANN", data = data, size = 10)

## Train a Random Forest model
model <- train("RF", data = data, ntree = 300)

## Train a Boosted Regression Tree model
model <- train("BRT", data = data, n.trees = 300, shrinkage = 0.001)

## Multiple methods trained together with default arguments
output <- train(method = c("ANN", "BRT", "RF"), data = data, size = 10)
output$ANN
output$BRT
output$RF

## Multiple methods trained together passing extra arguments
output <- train(method = c("ANN", "BRT", "RF"), data = data, size = 10,
                ntree = 300, n.trees = 300, shrinkage = 0.001)
output

SDMtune documentation built on July 17, 2021, 9:06 a.m.