Skip to content

Combines species presence/absence data with predictor variables and generates a DALEX explainer suitable for model diagnostics.

Usage

h3sdm_explain(model, data, response = "presence", label = "h3sdm workflow")

Arguments

model

A fitted workflow returned by h3sdm_fit_model().

data

A data.frame or sf object containing the original predictors and response variable. If an sf object, the geometry is dropped automatically.

response

Character string specifying the name of the response column. Must be a binary factor or numeric vector (0/1). Defaults to "presence".

label

Character string specifying a label for the explainer. Defaults to "h3sdm workflow".

Value

An object of class explainer from the DALEX package, ready to be used with feature_importance(), model_performance(), predict_parts(), and other DALEX functions.

Details

Create a DALEX explainer for h3sdm workflows

This function creates a DALEX explainer for a species distribution model fitted with h3sdm_fit_model(). It prepares the response and predictor variables, ensuring that all columns used during model training (including h3_address and coordinates) are included. The explainer can then be used for feature importance, model residuals, and other DALEX diagnostics.

Examples

# \donttest{
library(h3sdm)
library(DALEX)
#> Welcome to DALEX (version: 2.5.2).
#> Find examples and detailed introduction at: http://ema.drwhy.ai/
library(parsnip)

# Dataset de ejemplo con factor
dat <- data.frame(
  x1 = rnorm(20),
  x2 = rnorm(20),
  presence = factor(sample(0:1, 20, replace = TRUE))
)

# Modelo simple
f <- list(
  final_model = logistic_reg() |>
    fit(presence ~ x1 + x2, data = dat)
)

# Explainer
expl <- h3sdm_explain(f$final_model, data = dat, response = "presence")
#> Preparation of a new explainer is initiated
#>   -> model label       :  h3sdm workflow 
#>   -> data              :  20  rows  2  cols 
#>   -> target variable   :  20  values 
#>   -> predict function  :  custom_predict 
#>   -> predicted values  :  No value for predict function target column. (  default  )
#>   -> model_info        :  package parsnip , ver. 1.3.3 , task classification (  default  ) 
#>   -> predicted values  :  numerical, min =  0.04079885 , mean =  0.6 , max =  0.9990131  
#>   -> residual function :  difference between y and yhat (  default  )
#>   -> residuals         :  numerical, min =  -0.8074574 , mean =  5.720977e-15 , max =  0.705885  
#>   A new explainer has been created!  
feature_importance(expl)
#>       variable mean_dropout_loss          label
#> 1 _full_model_         0.1666667 h3sdm workflow
#> 2           x1         0.2333333 h3sdm workflow
#> 3           x2         0.5208333 h3sdm workflow
#> 4   _baseline_         0.5072917 h3sdm workflow
# }