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

Error in BIOMOD_4.2-4- bm_PlotEvalMean returns "Error in names(x) <- value : ..." #319

Closed silveiratcl closed 1 year ago

silveiratcl commented 1 year ago

Hello,

I´m updating an old code to the new version 4.3-4. I changed the names of the arguments to the new version. This is a sample of my updated code:

myRespName <- 'Tubastraea coccinea'
DataSpecies <- as.numeric(df$occ_abs)
myRespXY <- df[,c("lon_dd", "lat_dd")]

predictors1 <- variables
names(predictors1) <- c ('bat', 'velc', 'sst', 'd_cost', 'd_mar', 'd_traf')
predictors1

myBiomodData1 <- BIOMOD_FormatingData(resp.var = DataSpecies,
                                      expl.var = predictors1,
                                      resp.xy = myRespXY,
                                      resp.name = myRespName)

myBiomodOption <- BIOMOD_ModelingOptions()
myBiomodOption

myBiomodModelOut1 <- BIOMOD_Modeling(myBiomodData1,
                                     models = c('GLM','RF'),
                                     bm.options = myBiomodOption,
                                     CV.nb.rep = 12,
                                     CV.strategy = "random",
                                     CV.perc = 0.7,
                                     prevalence = 0.5,
                                     var.import = 3,
                                     metric.eval = c('ROC'),
                                     scale.models = TRUE,
                                     CV.do.full.models = FALSE,
                                     modeling.id = 'Model1',
                                     seed.val = 42)

bm_PlotEvalMean(bm.out = myBiomodModelOut1)

Them I got the error message:

"Error in names(x) <- value : 'names' attribute [5] must be the same length as the vector [3]"

Any idea?

rpatin commented 1 year ago

Hello @silveiratcl, Thank you for reporting :pray: Could you share a bit more information so that I can try to reproduce the error ? e.g. the output of:

In the case I fail at reproducing the issue, it would help if you could share your data (or a subset with a reproducible example). Thanks in advance, Best, Rémi

silveiratcl commented 1 year ago

Hello Remi, thanks!

 show(myBiomodData1)
-=-=-=-=-=-=-=-=-=-=-=-=-=-= BIOMOD.formated.data -=-=-=-=-=-=-=-=-=-=-=-=-=-=
dir.name =  .

sp.name =  Tubastraea.coccinea

     8 presences,  13 true absences and  0 undefined points in dataset

     6 explanatory variables

      bat              velc             sst            d_cost      
 Min.   :0.3992   Min.   :0.1158   Min.   :21.69   Min.   :  0.00  
 1st Qu.:2.1034   1st Qu.:0.1583   1st Qu.:21.77   1st Qu.:  0.00  
 Median :3.3900   Median :0.1699   Median :21.84   Median :  0.00  
 Mean   :3.3562   Mean   :0.1661   Mean   :21.88   Mean   : 35.84  
 3rd Qu.:4.6848   3rd Qu.:0.1833   3rd Qu.:21.85   3rd Qu.: 82.52  
 Max.   :9.0676   Max.   :0.2508   Max.   :22.31   Max.   :165.11  
     d_mar             d_traf     
 Min.   :  198.3   Min.   : 1193  
 1st Qu.: 7792.2   1st Qu.: 1539  
 Median :15056.4   Median : 2223  
 Mean   :12360.9   Mean   : 7000  
 3rd Qu.:16643.2   3rd Qu.: 5728  
 Max.   :29385.5   Max.   :30406  

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Warning message:
Not a validObject(): no slot of name "srs" for this object of class "RasterLayer" 

> show(myBiomodModelOut1)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= BIOMOD.models.out -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Modeling folder : .

Species modeled : Tubastraea.coccinea

Modeling id : Model1

Considered variables : bat velc sst d_cost d_mar d_traf

Computed Models :  Tubastraea.coccinea_allData_RUN1_GLM 
Tubastraea.coccinea_allData_RUN1_RF Tubastraea.coccinea_allData_RUN2_GLM 
Tubastraea.coccinea_allData_RUN2_RF Tubastraea.coccinea_allData_RUN3_GLM 
Tubastraea.coccinea_allData_RUN3_RF Tubastraea.coccinea_allData_RUN4_GLM 
Tubastraea.coccinea_allData_RUN4_RF Tubastraea.coccinea_allData_RUN5_GLM 
Tubastraea.coccinea_allData_RUN5_RF Tubastraea.coccinea_allData_RUN6_GLM 
Tubastraea.coccinea_allData_RUN6_RF Tubastraea.coccinea_allData_RUN7_GLM 
Tubastraea.coccinea_allData_RUN7_RF Tubastraea.coccinea_allData_RUN8_GLM 
Tubastraea.coccinea_allData_RUN8_RF Tubastraea.coccinea_allData_RUN9_GLM 
Tubastraea.coccinea_allData_RUN9_RF Tubastraea.coccinea_allData_RUN10_GLM 
Tubastraea.coccinea_allData_RUN10_RF Tubastraea.coccinea_allData_RUN11_GLM 
Tubastraea.coccinea_allData_RUN11_RF Tubastraea.coccinea_allData_RUN12_GLM 
Tubastraea.coccinea_allData_RUN12_RF

Failed Models :  none

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

> sessionInfo()
R version 4.2.3 (2023-03-15 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

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

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

other attached packages:
 [1] biomod2_4.2-4 mgcv_1.9-0    nlme_3.1-162  psych_2.3.6   car_3.1-2     carData_3.0-5
 [7] terra_1.7-39  gtools_3.9.4  sf_1.0-14     raster_3.6-23 sp_1.6-0     

loaded via a namespace (and not attached):
 [1] lubridate_1.9.2        tools_4.2.3            backports_1.4.1        utf8_1.2.2            
 [5] R6_2.5.1               rpart_4.1.19           KernSmooth_2.23-20     DBI_1.1.3             
 [9] colorspace_2.0-3       nnet_7.3-18            maxnet_0.1.4           gbm_2.1.8.1           
[13] mnormt_2.1.1           tidyselect_1.2.0       curl_4.3.2             compiler_4.2.3        
[17] mda_0.5-4              cli_3.6.1              xml2_1.3.3             scales_1.2.1          
[21] classInt_0.4-9         randomForest_4.7-1.1   proxy_0.4-27           rappdirs_0.3.3        
[25] plotmo_3.6.2           PresenceAbsence_1.1.11 stringr_1.5.0          digest_0.6.29         
[29] rmarkdown_2.14         pkgconfig_2.0.3        htmltools_0.5.4        plotrix_3.8-2         
[33] fastmap_1.1.1          maps_3.4.1             rlang_1.1.0            rstudioapi_0.14       
[37] httpcode_0.3.0         heatwaveR_0.4.6        generics_0.1.2         jsonlite_1.8.4        
[41] dplyr_1.1.1            magrittr_2.0.3         Formula_1.2-5          Matrix_1.5-3          
[45] Rcpp_1.0.11            munsell_0.5.0          fansi_1.0.3            abind_1.4-5           
[49] lifecycle_1.0.3        stringi_1.7.6          pROC_1.18.4            yaml_2.3.5            
[53] MASS_7.3-58.2          plyr_1.8.8             grid_4.2.3             parallel_4.2.3        
[57] earth_5.3.2            lattice_0.20-45        cowplot_1.1.1          splines_4.2.3         
[61] knitr_1.39             pillar_1.9.0           ggpubr_0.6.0           ggsignif_0.6.4        
[65] xgboost_1.7.5.1        reshape2_1.4.4         codetools_0.2-19       crul_1.3              
[69] glue_1.6.2             evaluate_0.15          hoardr_0.5.3           data.table_1.14.2     
[73] vctrs_0.6.1            foreach_1.5.2          gtable_0.3.0           purrr_1.0.1           
[77] tidyr_1.3.0            reshape_0.8.9          rerddap_1.0.2          ggplot2_3.4.2         
[81] TeachingDemos_2.12     xfun_0.31              broom_1.0.4            e1071_1.7-13          
[85] rstatix_0.7.2          class_7.3-21           ncdf4_1.21             survival_3.5-3        
[89] tibble_3.2.1           iterators_1.0.14       units_0.8-1            timechange_0.2.0
rpatin commented 1 year ago

Thank you for the update, I had no problem to reproduce your issue :pray: We had a hole in bm_PlotEvalMean checks that is now corrected (on the github version that you can get with devtools::install_github('biomodhub/biomod2').

The problem was that you were trying to use bm_PlotEvalMean while you had only one evaluation metric (ROC) which is not possible. You can forego using bm_PlotEvalMean or use an additional evaluation metric in BIOMOD_Modeling, e.g. TSS.

Additionally, I noticed that you have a very small number of presences (8) and absences (13) and even less if you consider the cross-validation (6 presence and 9 absences in each calibration dataset). Meanwhile you are using 6 explanatory variables, which is way too much. With such a low number of presences you should not use more than one or two variables. Alternatively you can also use other techniques such as Ensemble of small models (ESM) for instance.

Best, Rémi

silveiratcl commented 1 year ago

Hello Remi, thanks for your help.

I got the github version and added the TSS metric myBiomodModelOut1. Also I reduced the number of predictor to 2 for this check. Unfortunately now I got the following message:

> bm_PlotEvalMean(bm.out = myBiomodModelOut2)
Error in draw_axis(break_positions = guide$key[[aesthetic]], break_labels = guide$key$.label,  : 
  lazy-load database 'C:/Users/silve/AppData/Local/R/win-library/4.2/gtable/R/gtable.rdb' is corrupt
In addition: Warning messages:
1: In draw_axis(break_positions = guide$key[[aesthetic]], break_labels = guide$key$.label,  :
  restarting interrupted promise evaluation
2: In draw_axis(break_positions = guide$key[[aesthetic]], break_labels = guide$key$.label,  :
  internal error -3 in R_decompress1

Any idea? Thanks. Thiago

silveiratcl commented 1 year ago

Hello Remi,

Unfortunately, I´ve tried with the example on documentation and got the same issue.

rpatin commented 1 year ago

Hello Thiago, Can you try again after restarting R/Rstudio ? the lazy-load database issue is common to many package and is generally solved be restarting R (see e.g. https://github.com/thomasp85/patchwork/issues/36) Best, Rémi

silveiratcl commented 1 year ago

Hello Rémi, Now worked! Thanks by the help! Cheers, Thiago