Search code examples
tidymodelsensemble-learningmaxent

Setting maxent parameters manually within TidySDM Package in R


Within the TidySDM package, you can define what models you wish to include in your ensemble with the "workflow_set" function. When including a maxent model you can use the argument "sdm_spec_maxent" and set the "tune" parameter to sdm, all, custom, or none. I wish to use the custom tune setting and manually set the feature class and regularization multiplier options. I wish to provide a range of values which I have based on the help file Maxent_params. However I receive an error each time that says, "the condition has length > 1".

Here is the exact code I am using for the workflow set, as based on the tidySDM github overview example and the help files.

dive_models <- workflow_set(
  preproc = list(default = dive.rec), 
  models = list(
    glm = sdm_spec_glm(), 
    gbm = sdm_spec_boost_tree(
      tune = "sdm"
    ), 
    maxent = maxent(
      mode = "classification", 
      engine = "maxnet", 
      feature_classes(values = c("L","Q", "H", "T", "LQ", "LH", "LT", "QH", "QT",     "HT", "LQH", "LQT", "QHT", "LHT", "LQHT")),
      regularization_multiplier(range = c(0.5, 3), trans = NULL)
    ), 
    mars = mars(
      mode = "classification", 
      engine = "earth", 
      prod_degree = 4
    )
  ), 
  cross = T
) |> 
  option_add(control = control_ensemble_grid())

Does anyone see what i may be missing since I have formatted this exactly as it is in the help file?


Solution

  • To use a custom range for tuning parameters, you need to create a custom object with parameters. Here is a reprex changing the two parameters for maxent:

    library(tidysdm)
    #> Loading required package: tidymodels
    #> Loading required package: spatialsample
    lacerta_thin <- terra::readRDS(system.file("extdata/lacerta_thin_all_vars.rds",
                                                                 package = "tidysdm"))
    # create a recipe
    lacerta_rec <- recipe(lacerta_thin, formula = class ~ .)
    # set up custom parameters ranges for maxent
    maxent_param <- parameters(regularization_multiplier(),feature_classes()) %>% 
      update(regularization_multiplier = regularization_multiplier(c(0.1,0.5))) %>%
      update(feature_classes = feature_classes(c("l","h")))
    # create the workflow set
    lacerta_models <-
      workflow_set(
        preproc = list(default = lacerta_rec),
        models = list(
          # the standard glm specs
          glm = sdm_spec_glm(),
          # maxent specs with tuning
          maxent = maxent(
            mode = "classification", 
            engine = "maxnet",
            regularization_multiplier = tune(),
            feature_classes = tune()
          )
        ),
        # make all combinations of preproc and models,
        cross = TRUE
      ) %>%
      # tweak controls to store information needed later to create the ensemble
      option_add(control = control_ensemble_grid()) %>%
      # add the custom maxent parameter ranges
      option_add(param_info = maxent_param,id="default_maxent")
    # spatial block
    lacerta_cv <- spatial_block_cv(data = lacerta_thin, v = 3, n = 5)
    # tune the models
    set.seed(1234567)
    lacerta_models <-
      lacerta_models %>%
      workflow_map("tune_grid",
                   resamples = lacerta_cv, grid = 3,
                   metrics = sdm_metric_set(), verbose = TRUE
      )
    #> i    No tuning parameters. `fit_resamples()` will be attempted
    #> i 1 of 2 resampling: default_glm
    #> → A | warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
    #> There were issues with some computations   A: x1
    #> There were issues with some computations   A: x1
    #> 
    #> ✔ 1 of 2 resampling: default_glm (128ms)
    #> i 2 of 2 tuning:     default_maxent
    #> ✔ 2 of 2 tuning:     default_maxent (1.2s)
    # collect the results for the maxent model
    workflowsets::extract_workflow_set_result(lacerta_models,id="default_maxent") %>% collect_metrics()
    #> # A tibble: 9 × 8
    #>   regularization_multip…¹ feature_classes .metric .estimator  mean     n std_err
    #>                     <dbl> <chr>           <chr>   <chr>      <dbl> <int>   <dbl>
    #> 1                   0.230 h               boyce_… binary     0.317     3  0.269 
    #> 2                   0.230 h               roc_auc binary     0.812     3  0.0356
    #> 3                   0.230 h               tss_max binary     0.589     3  0.0513
    #> 4                   0.252 l               boyce_… binary     0.533     3  0.139 
    #> 5                   0.252 l               roc_auc binary     0.817     3  0.0421
    #> 6                   0.252 l               tss_max binary     0.594     3  0.0865
    #> 7                   0.451 l               boyce_… binary     0.614     3  0.0578
    #> 8                   0.451 l               roc_auc binary     0.815     3  0.0423
    #> 9                   0.451 l               tss_max binary     0.601     3  0.0937
    #> # ℹ abbreviated name: ¹​regularization_multiplier
    #> # ℹ 1 more variable: .config <chr>
    

    Created on 2025-02-13 with reprex v2.1.1