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.
77 stars 21 forks source link

Error in BIOMOD_EnsembleForecasting - [Error in .fun_testIfIn(TRUE, "full.name", subset.list[["full.name"]], : full.name must be 'value'] #452

Closed yinzheng128 closed 4 weeks ago

yinzheng128 commented 1 month ago

Error and context Hello, I attempted to follow the tutorial to see if biomod2 could be used properly, but I kept running into issues with BIOMOD_EnsembleForecasting functions.

Code used to get the error

## setup environment ----
setwd('../workdir')

## install the latest release of biomod2
devtools::install_github('biomodhub/biomod2')

## load the required packages
library(biomod2)
library(ggplot2)
library(gridExtra)
library(raster)
library(rasterVis)

## read data ----
ProLau_occ <- read.csv('../data/ProLau_occ.csv')
summary(ProLau_occ)

bioclim_ZA_sub <- 
  raster::stack(
    c(
      bio_5  = '../data/worldclim_ZA/worldclim_ZA_bio_5.asc',
      bio_7  = '../data/worldclim_ZA/worldclim_ZA_bio_7.asc',
      bio_11 = '../data/worldclim_ZA/worldclim_ZA_bio_11.asc',
      bio_19 = '../data/worldclim_ZA/worldclim_ZA_bio_19.asc'
    )
  )

bioclim_ZA_sub

## format the data ----
ProLau_data <- 
  BIOMOD_FormatingData(
    resp.var = ProLau_occ['Protea.laurifolia'],
    resp.xy = ProLau_occ[, c('long', 'lat')],
    expl.var = bioclim_ZA_sub,
    resp.name = "Protea.laurifolia",
    PA.nb.rep = 2,
    PA.nb.absences = 500,
    PA.strategy = 'random'
  )

# -=-=-=-=-=-=-=-=-=-=-=-=-=-= Protea.laurifolia Data Formating -=-=-=-=-=-=-=-=-=-=-=-=-=-=

      ! No data has been set aside for modeling evaluation
      ! No data has been set aside for modeling evaluation
 !!! Some data are located in the same raster cell. 
          Please set `filter.raster = TRUE` if you want an automatic filtering.

Checking Pseudo-absence selection arguments...

      ! No data has been set aside for modeling evaluation
   > random pseudo absences selection
   > Pseudo absences are selected in explanatory variables

 ! Some NAs have been automatically removed from your data
      ! No data has been set aside for modeling evaluation
 !!! Some data are located in the same raster cell. 
          Please set `filter.raster = TRUE` if you want an automatic filtering.
      ! No data has been set aside for modeling evaluation
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Done -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

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

dir.name =  .

sp.name =  Protea.laurifolia

     520 presences,  0 true absences and  932 undefined points in dataset

     4 explanatory variables

     bio_5           bio_7           bio_11           bio_19     
 Min.   :212.0   Min.   :136.0   Min.   : 47.00   Min.   :  0.0  
 1st Qu.:271.0   1st Qu.:237.0   1st Qu.: 96.75   1st Qu.: 23.0  
 Median :294.0   Median :266.5   Median :110.00   Median : 51.0  
 Mean   :295.1   Mean   :264.9   Mean   :110.19   Mean   :112.4  
 3rd Qu.:316.0   3rd Qu.:302.0   3rd Qu.:122.00   3rd Qu.:158.2  
 Max.   :386.0   Max.   :354.0   Max.   :190.00   Max.   :429.0  

 2 Pseudo Absences dataset available ( PA1, PA2 ) with  500 (PA1, PA2) pseudo absences

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

## define individual models options ---- 
ProLau_opt <- 
  BIOMOD_ModelingOptions(
    GLM = list(type = 'quadratic', interaction.level = 1),
    GBM = list(n.trees = 1000),
    GAM = list(algo = 'GAM_mgcv')
  )

## run the individual models ----
ProLau_models <- 
  BIOMOD_Modeling(
    bm.format = ProLau_data,
    models = c("GLM", "GAM", "GBM"),
    bm.options = ProLau_opt,
    CV.strategy = "random",
    CV.nb.rep = 2,
    CV.perc = 0.8, 
    CV.do.full.models = TRUE,
    metric.eval = c("KAPPA", "TSS", "ROC"),
    var.import = 3, 
    do.progress = TRUE
  )

show(ProLau_models)
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= BIOMOD.models.out -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Modeling folder : .

Species modeled : Protea.laurifolia

Modeling id : 1715090098

Considered variables : bio_5 bio_7 bio_11 bio_19

Computed Models :  Protea.laurifolia_PA1_RUN1_GLM Protea.laurifolia_PA1_RUN1_GAM 
Protea.laurifolia_PA1_RUN1_GBM Protea.laurifolia_PA1_RUN2_GLM 
Protea.laurifolia_PA1_RUN2_GAM Protea.laurifolia_PA1_RUN2_GBM 
Protea.laurifolia_PA1_allRun_GLM Protea.laurifolia_PA1_allRun_GAM 
Protea.laurifolia_PA1_allRun_GBM Protea.laurifolia_PA2_RUN1_GLM 
Protea.laurifolia_PA2_RUN1_GAM Protea.laurifolia_PA2_RUN1_GBM 
Protea.laurifolia_PA2_RUN2_GLM Protea.laurifolia_PA2_RUN2_GAM 
Protea.laurifolia_PA2_RUN2_GBM Protea.laurifolia_PA2_allRun_GLM 
Protea.laurifolia_PA2_allRun_GAM Protea.laurifolia_PA2_allRun_GBM 
Protea.laurifolia_allData_allRun_GLM Protea.laurifolia_allData_allRun_GAM 
Protea.laurifolia_allData_allRun_GBM

Failed Models :  none

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

## run the ensemble models ----
ProLau_ensemble_models <- 
  BIOMOD_EnsembleModeling(
    bm.mod = ProLau_models,
    em.by = 'all',
    metric.select = 'TSS',
    metric.select.thresh = 0.8,
    metric.eval = c('TSS','ROC'),
    em.algo = c('EMmean','EMcv'),
    var.import = 0
  )

show(ProLau_ensemble_models)
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= BIOMOD.ensemble.models.out -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

sp.name : Protea.laurifolia

expl.var.names : bio_5 bio_7 bio_11 bio_19

models computed: 
Protea.laurifolia_EMmeanByTSS_mergedData_mergedRun_mergedAlgo, Protea.laurifolia_EMcvByTSS_mergedData_mergedRun_mergedAlgo

models failed: none

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

## do models projections ----

## current projections
ProLau_models_proj_current <- 
  BIOMOD_Projection(
    bm.mod = ProLau_models,
    new.env = bioclim_ZA_sub,
    proj.name = "current",
    metric.binary = "TSS",
    output.format = ".img",
    do.stack = FALSE
  )

show(ProLau_models_proj_current)
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= BIOMOD.projection.out -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Projection directory : ./Protea.laurifolia/current

sp.name : Protea.laurifolia

expl.var.names : bio_5 bio_7 bio_11 bio_19

modeling.id : 1715090098 ( ./Protea.laurifolia/Protea.laurifolia.1715090098.models.out )

models.projected : 
Protea.laurifolia_PA1_RUN1_GLM, Protea.laurifolia_PA1_RUN1_GAM, Protea.laurifolia_PA1_RUN1_GBM, Protea.laurifolia_PA1_RUN2_GLM, Protea.laurifolia_PA1_RUN2_GAM, Protea.laurifolia_PA1_RUN2_GBM, Protea.laurifolia_PA1_allRun_GLM, Protea.laurifolia_PA1_allRun_GAM, Protea.laurifolia_PA1_allRun_GBM, Protea.laurifolia_PA2_RUN1_GLM, Protea.laurifolia_PA2_RUN1_GAM, Protea.laurifolia_PA2_RUN1_GBM, Protea.laurifolia_PA2_RUN2_GLM, Protea.laurifolia_PA2_RUN2_GAM, Protea.laurifolia_PA2_RUN2_GBM, Protea.laurifolia_PA2_allRun_GLM, Protea.laurifolia_PA2_allRun_GAM, Protea.laurifolia_PA2_allRun_GBM, Protea.laurifolia_allData_allRun_GLM, Protea.laurifolia_allData_allRun_GAM, Protea.laurifolia_allData_allRun_GBM

available binary projection : TSS

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

ProLau_ensemble_models_proj_current <- 
  BIOMOD_EnsembleForecasting(
    bm.em = ProLau_ensemble_models,
    bm.proj = ProLau_models_proj_current,
    metric.binary = "TSS",
    output.format = ".img",
    do.stack = FALSE
  )
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Do Ensemble Models Projection -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Creating suitable Workdir...
Error in .fun_testIfIn(TRUE, "full.name", subset.list[["full.name"]],  : 
full.name must be 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value', 'value' or 'value

Environment Information Please paste the output of sessionInfo() in your current R session below.

<html>
<body>
<!--StartFragment-->

--
 
> |R version 4.1.3 (2022-03-10) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 10 x64 (build 22631)  

Matrix products: default  

locale: 
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United States.1252      

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

other attached packages: 
[1] rasterVis_0.51.6 lattice_0.22-6   raster_3.6-20    sp_1.6-0         gridExtra_2.3    
[6] ggplot2_3.5.1    biomod2_4.2-4     

loaded via a namespace (and not attached):  
[1] utf8_1.2.3             tidyselect_1.2.1       grid_4.1.3              
[4] pROC_1.18.0            munsell_0.5.1          codetools_0.2-18        
[7] interp_1.1-4           xgboost_1.6.0.1        withr_3.0.0            
[10] colorspace_2.1-0       rstudioapi_0.16.0      vctrs_0.6.1            
[13] generics_0.1.3         randomForest_4.7-1.1   R6_2.5.1              
[16] cachem_1.0.7           reshape_0.8.9          scales_1.3.0           
[19] nnet_7.3-17            gtable_0.3.5           mda_0.5-3              
[22] rlang_1.1.0            splines_4.1.3          hexbin_1.28.3         
[25] earth_5.3.3            reshape2_1.4.4         abind_1.4-5            
[28] tools_4.1.3            ellipsis_0.3.2         RColorBrewer_1.1-3     
[31] Rcpp_1.0.10            plyr_1.8.8             rpart_4.1.16           
[34] deldir_1.0-6           zoo_1.8-12             fs_1.5.2              
[37] magrittr_2.0.3         data.table_1.14.8      jpeg_0.1-10            
[40] compiler_4.1.3         tibble_3.2.1           mgcv_1.8-39           
[43] Formula_1.2-5          MASS_7.3-55            Matrix_1.4-0          
[46] cli_3.6.1              parallel_4.1.3         pkgconfig_2.0.3        
[49] terra_1.7-23           foreach_1.5.2          plotmo_3.6.3           
[52] stringr_1.5.1          PresenceAbsence_1.1.11 lifecycle_1.0.4        
[55] nlme_3.1-155           jsonlite_1.8.4         viridisLite_0.4.2      
[58] fansi_1.0.4            pillar_1.9.0           fastmap_1.1.1          
[61] plotrix_3.8-4          survival_3.2-13        glue_1.6.2            
[64] gbm_2.1.8.1            png_0.1-8              iterators_1.0.14       
[67] class_7.3-20           stringi_1.7.12         maxnet_0.1.4           
[70] latticeExtra_0.6-30    memoise_2.0.1          dplyr_1.1.2 
>

<!--EndFragment-->
</body>
</html>

Additional information Thank in advance for your help. Best regards.

HeleneBlt commented 1 month ago

Hello there,

Thanks for reporting! Your code seems correct. I suspect the error may come from the fact you don't have the latest version of the terra package. Please note that you don't have the latest version of biomod2 either, but this new version will bring changes to your code for the options part. You can find more information here.

Hope it helps! Hélène