Skip to contents

Computes Confusion Matrixes for threshold values varying from 0 to 1.

Usage

confMatrix(model, test = NULL, th = NULL, type = NULL)

Arguments

model

SDMmodel object.

test

SWD testing locations, if not provided it uses the training dataset.

th

numeric vector. If provided it computes the evaluation at the given thresholds. Default is NULL and it computes the evaluation for the unique predicted values at presence and absence/background locations.

type

character. The output type used for "Maxent" and "Maxnet" methods, possible values are "cloglog" and "logistic".

Value

The Confusion Matrix for all the used thresholds.

Details

  • For models trained with the Maxent method the argument type can be: "raw", "logistic" and "cloglog".

  • For models trained with the Maxnet method the argument type can be: "link", "exponential", "logistic" and "cloglog", see maxnet for more details.

Author

Sergio Vignali

Examples

# Acquire environmental variables
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"),
                    pattern = "grd",
                    full.names = TRUE)

predictors <- terra::rast(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")
#>  Extracting predictor information for presence locations
#>  Extracting predictor information for presence locations [37ms]
#> 
#>  Extracting predictor information for absence/background locations
#>  Extracting predictor information for absence/background locations [65ms]
#> 

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

# Get the confusion matrix for thresholds ranging from 0 to 1
cm <- confMatrix(model,
                 type = "cloglog")
head(cm)
#>            th  tp   fp fn tn
#> 1 0.000000000 400 5000  0  0
#> 2 0.002786585 400 5000  0  0
#> 3 0.003520421 400 4999  0  1
#> 4 0.003616260 400 4998  0  2
#> 5 0.003668746 400 4997  0  3
#> 6 0.003691408 400 4996  0  4
tail(cm)
#>             th tp fp  fn   tn
#> 5203 0.9933962  0  5 400 4995
#> 5204 0.9940262  0  4 400 4996
#> 5205 0.9960225  0  3 400 4997
#> 5206 0.9965570  0  2 400 4998
#> 5207 0.9970066  0  1 400 4999
#> 5208 1.0000000  0  0 400 5000

# Get the confusion matrix for a specific threshold
confMatrix(model,
           type = "logistic",
           th = 0.6)
#>    th  tp  fp  fn   tn
#> 1 0.6 126 410 274 4590