Train a model

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
options(knitr.table.format = "html")
default_model <- SDMtune:::bm_maxent

Intro

In the previous article you have learned how to prepare the data for the analysis using the virtualSp dataset and the WorldClim environmental variables. Now it's time to train your first model, let's do it!

SDMtune supports four methods for model training:

The code necessary to train a model is the same for all the implementations. We will show how to train a Maxent model, you can adapt the code for the other methods or check this article.

Check that MaxEnt is correctly configured

Before training a model we check that MaxEnt is correctly configured for the dismo package:

dismo::maxent()

In case you get an error, check the Home page to see how to configure MaxEnt.

Train a model with default settings

First we load the SDMtune package:

library(SDMtune)

We use the function train() to train a Maxent model. We need to provide two arguments:

default_model <- SDMtune:::bm_maxent
default_model <- train(method = "Maxent", data = data)

The function trains the model using default settings that are:

We will see later how to change the default settings, for the moment let's have a look at the default_model object.

Explore an SDMmodel object

The output of the function train() is an object of class SDMmodel(). Let's print it:

default_model

When we print an SDMmodel object we get the following information:

An SDMmodel() object has two slots:

slotNames(default_model)

The slot model contains the configurations of the model plus other information used to make predictions.

slotNames(default_model@model)

For the moment the most important are: fc, reg and iter that contain the values of the model configuration. We will explore the others later in another article.

Train a model changing the default settings

The function train() accepts optional arguments that can be used to change the default model settings. In our previous example we could have trained the same model using:

default_model <- train(method = "Maxent", data = data, fc = "lqph", reg = 1, iter = 500)

Try yourself

Try to change the default settings and train a model using linear and hinge as feature class combination, 0.5 as regularization multiplier and 700 iterations. To see the solution highlight the next cell:

model <- train(method = "Maxent", data = data, fc = "lh", reg = 0.5, iter = 700)

By default Maxent models are trained using the arguments "removeduplicates=false" and "addsamplestobackground=false". The user should have the full control of the data used to train the model, so is expected that duplicated locations are already removed and that the presence locations are already included in the background locations, when desired. You can use the function thinData() to remove duplicated locations and the function addSamplesToBg() to add the presence locations to the background locations.

Train a Maxnet model

Train a model using the Maxnet method is as simple as changing the name of the method in the train() function, the only difference here is that we cannot set the number of iteration.

Try yourself

Try to train a model using the Maxnet method. To see the solution highlight the following cell:

maxnet_model <- train("Maxnet", data = data)

Conclusion

In this article you have learned:

In the next article you will learn how to use the model that you have just trained to get the predicted value for new localities.

References



Try the SDMtune package in your browser

Any scripts or data that you put into this service are public.

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