rvalavi / blockCV

The blockCV package creates spatially or environmentally separated training and testing folds for cross-validation to provide a robust error estimation in spatially structured environments. See
https://doi.org/10.1111/2041-210X.13107
GNU General Public License v3.0
106 stars 22 forks source link

Error: perc (or CV.perc) is required when strategy = 'random' #44

Closed anackr closed 4 months ago

anackr commented 5 months ago

Hi everyone. I am using blockCV for biomod but was not able to make it run properly.

My data is on this link

Here is the code:

occ <- read.csv('sa_chloropterus_ABC.csv') %>% dplyr::select(VECTOR, lon, lat)
occ$VECTOR <- 1

pa <- read.csv('background_AD.csv') %>% select(-X)
pa$VECTOR <- 0

data <- rbind(pa, occ)

data_sf <- st_as_sf(data, coords = c('lon', 'lat'), crs = 4326)

Create CV

set.seed(26)

sb1 <- cv_spatial(x = data_sf,
                  column = "VECTOR", 
                  k = 3, 
                  hexagon = T,
                  selection = "random",
                  biomod2 = TRUE)

Biomod

myRespXY <- data.frame(X_WGS84 = data$lon,
                       Y_WGS84 = data$lat)

myBiomodData <- BIOMOD_FormatingData(
  resp.var = data$VECTOR
  expl.var = bioclimaticas, # which is a raster object
  resp.xy = myRespXY
  resp.name = 'Sabethes chloropterus',
  na.rm = T)

spatial_cv_folds <- sb1$biomod_table

L1 <- BIOMOD_ModelingOptions(MAXENT = list(
  linear = T, quadratic = F, product = F, threshold = F, hinge = F,
  betamultiplier = 1))

modelo_maxent1 <- BIOMOD_Modeling(
  bm.format = myBiomodData,
  models = 'MAXENT',
  bm.options = L1,
  data.split.table = spatial_cv_folds,
  metric.eval = c('ROC', 'POD', 'TSS')
)

The following error appears:

_Error in .bmCrossValidation.check.args(bm.format = bm.format, strategy = strategy, : perc (or CV.perc) is required when strategy = 'random'

Here is my session info:

R version 4.2.2 (2022-10-31 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] sf_1.0-12        dismo_1.3-9      dplyr_1.1.4      rasterVis_0.51.5
 [5] lattice_0.20-45  raster_3.6-20    sp_1.6-0         gridExtra_2.3   
 [9] ggplot2_3.4.1    biomod2_4.2-4-10

loaded via a namespace (and not attached):
 [1] jsonlite_1.8.4         viridisLite_0.4.1      splines_4.2.2         
 [4] foreach_1.5.2          Formula_1.2-4          latticeExtra_0.6-30   
 [7] pillar_1.9.0           glue_1.6.2             pROC_1.18.4           
[10] RColorBrewer_1.1-3     randomForest_4.7-1.1   colorspace_2.1-0      
[13] gbm_2.1.8.1            Matrix_1.5-3           plyr_1.8.8            
[16] pkgconfig_2.0.3        maxnet_0.1.4           PresenceAbsence_1.1.11
[19] earth_5.3.2            scales_1.2.1           terra_1.7-18          
[22] jpeg_0.1-10            TeachingDemos_2.12     tibble_3.2.1          
[25] proxy_0.4-27           mgcv_1.8-41            mda_0.5-3             
[28] generics_0.1.3         xgboost_1.7.5.1        withr_2.5.0           
[31] nnet_7.3-18            hexbin_1.28.3          cli_3.6.0             
[34] survival_3.5-7         magrittr_2.0.3         deldir_1.0-6          
[37] fansi_1.0.4            nlme_3.1-162           MASS_7.3-58.1         
[40] class_7.3-21           tools_4.2.2            data.table_1.14.6     
[43] lifecycle_1.0.3        stringr_1.5.0          interp_1.1-3          
[46] munsell_0.5.0          plotrix_3.8-2          compiler_4.2.2        
[49] e1071_1.7-13           rlang_1.1.0            plotmo_3.6.2          
[52] units_0.8-1            classInt_0.4-9         grid_4.2.2            
[55] iterators_1.0.14       rstudioapi_0.14        gtable_0.3.1          
[58] codetools_0.2-19       abind_1.4-5            DBI_1.1.3             
[61] reshape_0.8.9          reshape2_1.4.4         R6_2.5.1              
[64] zoo_1.8-11             rgdal_1.6-5            utf8_1.2.3            
[67] KernSmooth_2.23-20     stringi_1.7.12         parallel_4.2.2        
[70] Rcpp_1.0.10            vctrs_0.6.5            rpart_4.1.19          
[73] png_0.1-8              tidyselect_1.2.0

I know the error is about biomod, but I think it has something to do with the blockCV structure.

Could anyone help?

Thanks in advance!

rvalavi commented 5 months ago

Hi @anackr

Thanks for reporting the issue.

This error is actually due to some recent updates of biomod2 package. You can fix it by changing the name of the biomod_table and also the name of arguments in the BIOMOD_Modeling function (i.e., CV.strategy and CV.user.table):

colnames(spatial_cv_folds) <- paste0("_allData_RUN", 1:3)

modelo_maxent1 <- BIOMOD_Modeling(
  bm.format = myBiomodData,
  models = 'MAXENT',
  bm.options = L1,
  CV.strategy = "user.defined", # this is new argument
  CV.user.table = spatial_cv_folds, # also this one is new
  metric.eval = c('ROC', 'POD', 'TSS')
)

I will update the blockCV's vignettes.

anackr commented 4 months ago

Forgot to thank you! It worked :)

rvalavi commented 4 months ago

Perfect! I'm glad it was helpful.