sjevelazco / flexsdm

Useful tools for constructing species distribution models
https://sjevelazco.github.io/flexsdm/
52 stars 5 forks source link

sdm_eval fails for presence-only tune_max #308

Closed wardfont closed 2 years ago

wardfont commented 2 years ago

When using tune_max with presence-only data and background, the evaluation step fails since both the number of absences and presences have to be greater than zero in line 152 of sdm_eval: if (na == 0 | np == 0).

Is there a way to use tune_max with presence-only data?

sjevelazco commented 2 years ago

Hi Thank you so much for reporting it. Now fit_max, tune_max, and esm_max functions can fit maxent model with presences and background points. However, if you intend to compare maxent with other algorithms (e.g., RF, SVM, GAM), I strongly recommend fitting maxent with presences-pseudo-absences (or presences-absences) and background points. If those three data types are used in fit_max, tune_max, and esm_max, models will be fitted with presences and background points and validated with presence and pseudo-absences (or absences). See in the new package documentation about these fitting options.

Here is an example of how to fit maxent with only with presences and background points

Cheers

require(flexsdm)
require(dplyr)

data("abies")
data("backg")
abies # environmental conditions of presence-absence data
backg # environmental conditions of background points

# Using k-fold partition method
# Remember that the partition method, number of folds or replications must
# be the same for presence-absence and background points datasets

abies <- abies %>% dplyr::filter(pr_ab==1) %>% dplyr::slice_sample(prop = 0.5)
abies2 <- part_random(
  data = abies,
  pr_ab = "pr_ab",
  method = c(method = "kfold", folds = 3)
)
abies2

backg <- backg %>% dplyr::slice_sample(prop = 0.5)
set.seed(1)
backg <- dplyr::sample_n(backg, size = 2000, replace = FALSE)
backg2 <- part_random(
  data = backg,
  pr_ab = "pr_ab",
  method = c(method = "kfold", folds = 3)
)
backg

gridtest <-
  expand.grid(
    regmult = seq(0.1, 3, 0.5),
    classes = c("l", "lq", "lqh")
  )

max_t1 <- tune_max(
  data = abies2,
  response = "pr_ab",
  predictors = c("aet", "pH", "awc", "depth"),
  predictors_f = c("landform"),
  partition = ".part",
  background = backg2,
  grid = gridtest,
  thr = "max_sens_spec",
  metric = "TSS",
  clamp = TRUE,
  pred_type = "cloglog",
  n_cores = 6 # activate six cores for speed up this process
)

max_t1
plot(max_t1$model, type="cloglog")