biomodhub / biomod2

BIOMOD is a computer platform for ensemble forecasting of species distributions, enabling the treatment of a range of methodological uncertainties in models and the examination of species-environment relationships.
85 stars 22 forks source link

Help with BIOMOD2_4.2-5 . - Error in { : task 70 failed - "task 3 failed - "'threshold' can not be negative"" #468

Closed gsatta closed 3 months ago

gsatta commented 3 months ago

Hello everyone, for a few days now, when I run the BIOMOD_Modeling() function, the following error appears:

### Errore in { : task 70 failed - "task 3 failed - "'threshold' can not be negative""

What does it mean? what exactly is the option that can cause this problem?

Thank You

Here is the code:

# Load the necessary packages
library(biomod2)

#  Define the PA3 length
n_PA_3 <- length(myResp_train) 

# Format Data with pseudo - absences: random method
myBiomodData <- BIOMOD_FormatingData(expl.var = myExpl,
                                     resp.var = myResp_train,
                                     eval.resp.var = myResp_test,
                                     resp.name = "Phytophthora",
                                     PA.nb.rep = 3,
                                     PA.nb.absences = c(n_PA_3, 100, 1000),
                                     PA.strategy = 'random',
                                     filter.raster = TRUE,
                                     dir.name = getwd())
myBiomodData

# Define column names available within bm.format@PA.table
PA_used <- colnames(myBiomodData@PA.table)

# Define vectors for each model that indicate which pseudo-absence columns to use.
{
CTA <- c("PA1")
FDA <- c("PA1")
RF <- c("PA1")
XGBOOST <- c("PA1")
MARS <- c("PA2")
SRE <- c("PA2")
GLM <- c("PA3")
GAM <- c("PA3")
MAXENT <- c("PA3")
MAXNET <- c("PA3")
GBM <- c("PA3")

}

# Build the models.pa list containing these vectors for each model.
models.pa <- list(CTA = CTA, FDA = FDA, GBM = GBM, RF = RF, XGBOOST = XGBOOST, 
                  MARS = MARS, 
                  GLM = GLM, GAM = GAM, MAXENT = MAXENT, MAXNET = MAXNET  )

allModels  <- c("CTA", "FDA", "GAM", "GBM", "GLM", "MARS", "MAXENT", "MAXNET", "RF", "XGBOOST", "SRE")

user.rf <- list('_allData_allRun' = list(type = 'classification', importance = TRUE, 
                                         nodesize = 1, oob.prox = T, ntree = 250, 
                                         mtry = 2))

user.maxent <- list('_allData_allRun' = list(visible = TRUE,
                                             beta_threshold = 0))

user.XGBOOST <- list('_allData_allRun' = list(objective = "binary:logistic",
                                              params =list(max_depth = 5, eta = 0.2, gamma = 1 ),
                                              nrounds = 20, subsample = 0.5))

form.GLM <- bm_MakeFormula(resp.name = myBiomodData@sp.name,
                           expl.var = head(myBiomodData@data.env.var),
                           type = 's_smoother',
                           interaction.level = 0)

user.GLM <- list('_allData_allRun' = list(formula = form.GLM))

form.GAM <- bm_MakeFormula(resp.name = myBiomodData@sp.name,
                           expl.var = head(myBiomodData@data.env.var),
                           type = 's_smoother',
                           interaction.level = 0)

user.GAM <- list('_allData_allRun' = list(formula = form.GAM))

user.val <- list(RF.binary.randomForest.randomForest = user.rf,
                 MAXENT.binary.MAXENT.MAXENT = user.maxent,
                 XGBOOST.binary.xgboost.xgboost = user.XGBOOST,
                 GLM.binary.stats.glm= user.GLM,
                 GAM.binary.mgcv.gam= user.GAM)

# bigboss parameters
myBiomodOption <- bm_ModelingOptions(data.type = 'binary',
                            models = allModels,
                            strategy = "user.defined",
                            user.base = 'bigboss',
                            user.val = user.val)

############################# Run the single models ############################
# single models
myBiomodModelOut <- BIOMOD_Modeling(
  bm.format = myBiomodData,
  bm.options = myBiomodOption,
  modeling.id = as.character(format(Sys.time(), "%Y%m%d_%H%M_%S")),
  models = allModels,
  models.pa = models.pa,
  CV.strategy = 'kfold',
  CV.nb.rep = 2,
  CV.k = 5,
  var.import = 3,
  metric.eval = c("FAR", "SR", "BOYCE", "ROC", "TSS", "KAPPA", "ACCURACY", "BIAS"),
  seed.val = 123,
  do.progress = TRUE,
)

Environment Information

> sessionInfo()
R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=Italian_Italy.utf8 
[2] LC_CTYPE=Italian_Italy.utf8   
[3] LC_MONETARY=Italian_Italy.utf8
[4] LC_NUMERIC=C                  
[5] LC_TIME=Italian_Italy.utf8    

time zone: Europe/Rome
tzcode source: internal

attached base packages:
[1] splines   stats     graphics  grDevices
[5] utils     datasets  methods   base     

other attached packages:
 [1] terra_1.7-71        
 [2] sf_1.0-16           
 [3] readr_2.1.5         
 [4] xgboost_1.7.7.1     
 [5] randomForest_4.7-1.1
 [6] maxnet_0.1.4        
 [7] earth_5.3.3         
 [8] plotmo_3.6.3        
 [9] plotrix_3.8-4       
[10] Formula_1.2-5       
[11] gbm_2.1.9           
[12] mgcv_1.9-0          
[13] nlme_3.1-163        
[14] gam_1.22-3          
[15] foreach_1.5.2       
[16] mda_0.5-4           
[17] class_7.3-22        
[18] rpart_4.1.21        
[19] nnet_7.3-19         
[20] biomod2_4.2-5       
[21] dplyr_1.1.4         
[22] ggpubr_0.6.0        
[23] ggplot2_3.5.1       
[24] cowplot_1.1.3       
[25] gridExtra_2.3       

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.1      
 [2] farver_2.1.1          
 [3] reshape_0.8.9         
 [4] pROC_1.18.5           
 [5] lifecycle_1.0.4       
 [6] survival_3.5-7        
 [7] magrittr_2.0.3        
 [8] compiler_4.3.2        
 [9] rlang_1.1.3           
[10] tools_4.3.2           
[11] utf8_1.2.4            
[12] data.table_1.15.4     
[13] ggsignif_0.6.4        
[14] labeling_0.4.3        
[15] sp_2.1-3              
[16] classInt_0.4-10       
[17] PresenceAbsence_1.1.11
[18] plyr_1.8.9            
[19] abind_1.4-5           
[20] KernSmooth_2.23-22    
[21] withr_3.0.0           
[22] purrr_1.0.2           
[23] grid_4.3.2            
[24] fansi_1.0.6           
[25] e1071_1.7-14          
[26] colorspace_2.1-0      
[27] scales_1.3.0          
[28] iterators_1.0.14      
[29] cli_3.6.2             
[30] generics_0.1.3        
[31] rstudioapi_0.15.0     
[32] reshape2_1.4.4        
[33] tzdb_0.4.0            
[34] DBI_1.2.2             
[35] proxy_0.4-27          
[36] stringr_1.5.1         
[37] vctrs_0.6.5           
[38] glmnet_4.1-8          
[39] Matrix_1.6-1.1        
[40] jsonlite_1.8.8        
[41] carData_3.0-5         
[42] car_3.1-2             
[43] hms_1.1.3             
[44] rstatix_0.7.2         
[45] tidyr_1.3.1           
[46] units_0.8-5           
[47] glue_1.7.0            
[48] codetools_0.2-19      
[49] dismo_1.3-14          
[50] stringi_1.8.3         
[51] gtable_0.3.4          
[52] shape_1.4.6.1         
[53] raster_3.6-26         
[54] munsell_0.5.0         
[55] tibble_3.2.1          
[56] pillar_1.9.0          
[57] R6_2.5.1              
[58] lattice_0.22-5        
[59] backports_1.4.1       
[60] broom_1.0.5           
[61] Rcpp_1.0.12           
[62] pkgconfig_2.0.3 
MayaGueguen commented 3 months ago

Hello Gabriele,

Thank you for posting with a lot of informations :pray:

Could you send all the information / error / warning messages you get when running the BIOMOD_Modeling function please ? :eyes:

Also, the output of print(myBiomodData) and summary(myBiomodData) might help !

Maya

PS : cool to see that you are trying and using multiple PA datasets !

gsatta commented 3 months ago

Hi Maya Thank you so much !

Here are the requested additions:

Information about error / warning messages

Errore in { : 
  task 68 failed - "task 3 failed - "'threshold' can not be negative""
In aggiunta: Ci sono 50 o più avvertimenti (utilizza warnings() per visualizzare i primi 50)
> warnings()
Messaggi di avvertimento:
1: In .bm_ModelingOptions.check.args(data.type = data.type,  ... :
  Only one GAM model can be activated. 'GAM.mgcv.gam' has been set (other available : 'GAM.gam.gam' or 'GAM.mgcv.bam')
2: executing %dopar% sequentially: no parallel backend registered
3: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
4: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
5: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
6: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
7: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
8: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
9: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
10: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
11: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
12: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
13: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
14: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
15: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
16: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
17: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
18: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
19: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
20: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
21: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
22: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
23: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
24: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
25: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
26: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
27: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
28: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
29: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
30: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
31: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
32: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
33: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
34: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
35: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
36: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
37: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
38: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
39: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
40: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
41: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
42: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
43: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
44: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
45: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
46: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
47: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
48: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
49: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero
50: In cor(x = ref, y = shuffled.pred, use = "pairwise.complete.obs",  ... :
  la deviazione standard è zero

Information about myBiomodData

> print(myBiomodData)

-=-=-=-=-=-= BIOMOD.formated.data -=-=-=-=-=-=

dir.name =  
D:/DOTTORATO/PROGETTI/PAULLILATINO/PAPER_2/SDM_R_PROJECT/SDM_biomod2_50m

sp.name =  Phytophthora

     31 presences,  0 true absences and  1126 
undefined points in dataset

     8 explanatory variables

     green             BIO03      
 Min.   :0.07933   Min.   :36.80  
 1st Qu.:0.09700   1st Qu.:37.89  
 Median :0.10352   Median :38.32  
 Mean   :0.10638   Mean   :38.47  
 3rd Qu.:0.11286   3rd Qu.:38.79  
 Max.   :0.18495   Max.   :41.85  
     BIO06             wo          
 Min.   :3.838   Min.   :-9999.00  
 1st Qu.:4.968   1st Qu.:    1.00  
 Median :5.184   Median :    1.00  
 Mean   :5.110   Mean   :  -85.57  
 3rd Qu.:5.285   3rd Qu.:    1.00  
 Max.   :5.752   Max.   :    1.00  
     rivers               fla         
 Min.   :   0.7018   Min.   :   99.6  
 1st Qu.: 116.3116   1st Qu.: 1062.1  
 Median : 244.6965   Median : 1932.6  
 Mean   : 310.2860   Mean   : 3399.7  
 3rd Qu.: 432.4567   3rd Qu.: 3821.2  
 Max.   :1532.9263   Max.   :53784.6  
      soc            roads        
 Min.   : 0.00   Min.   :  1.127  
 1st Qu.:21.36   1st Qu.: 46.911  
 Median :22.79   Median :104.932  
 Mean   :22.96   Mean   :137.221  
 3rd Qu.:24.67   3rd Qu.:193.788  
 Max.   :32.02   Max.   :899.282  

Evaluation data :

     14 presences,  14 true absences and  0 
undefined points in dataset

     green             BIO03      
 Min.   :0.08691   Min.   :37.05  
 1st Qu.:0.09492   1st Qu.:37.81  
 Median :0.10230   Median :38.32  
 Mean   :0.10441   Mean   :38.47  
 3rd Qu.:0.11226   3rd Qu.:38.73  
 Max.   :0.13800   Max.   :41.01  
     BIO06             wo   
 Min.   :4.540   Min.   :1  
 1st Qu.:5.096   1st Qu.:1  
 Median :5.216   Median :1  
 Mean   :5.192   Mean   :1  
 3rd Qu.:5.354   3rd Qu.:1  
 Max.   :5.668   Max.   :1  
     rivers            fla         
 Min.   : 28.98   Min.   :  259.8  
 1st Qu.:101.76   1st Qu.:  750.4  
 Median :270.17   Median : 1467.9  
 Mean   :309.61   Mean   : 2066.2  
 3rd Qu.:506.55   3rd Qu.: 2503.7  
 Max.   :745.11   Max.   :10850.0  
      soc            roads       
 Min.   :17.99   Min.   : 13.30  
 1st Qu.:20.93   1st Qu.: 44.14  
 Median :22.26   Median : 91.11  
 Mean   :22.66   Mean   :111.03  
 3rd Qu.:24.33   3rd Qu.:151.07  
 Max.   :30.30   Max.   :432.51  

 3 Pseudo Absences dataset available ( 
PA1, PA2, PA3 ) with  
31 (PA1), 100 (PA2), 1000 (PA3) 
pseudo absences

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> summary(myBiomodData)
      dataset run   PA Presences
1     initial  NA <NA>        31
2  evaluation  NA <NA>        14
3 calibration  NA  PA1        31
4 calibration  NA  PA2        31
5 calibration  NA  PA3        31
  True_Absences Pseudo_Absences Undefined
1             0               0      1126
2            14               0         0
3             0              31        NA
4             0             100        NA
5             0            1000        NA
MayaGueguen commented 3 months ago

Thank you Gabriele for the supplementary informations :pray:

We think it might come from your variable wo :

      wo          
Min.   :-9999.00  
1st Qu.:    1.00  
Median :    1.00  
Mean   :  -85.57  
3rd Qu.:    1.00  
Max.   :    1.00 

2 things that might be wrong with it :

Is this variable coming from an ASCII file (.asc extension) ? Often this format replaces NA value by -9999 which falsifies your variable.

This might explain your problem : as you have few points, most of the time, you use only points with 1 value, but sometimes for validation, you might have a point with value = -9999 which leads to extrapolation and probably strange predicted values, and hence a negative value for the threshold to transform predictions into 0 and 1.

So try either removing this variable, or making sure that you transform -9999 values into NA :eyes:

Maya

gsatta commented 3 months ago

Perfect, it's working now!

I changed the values -9999 to NA.

Thank you