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
109 stars 24 forks source link

"Error in 0:nx : result would be too long a vector" while using "cv_spatial_autocor" function #41

Closed ozgurhsyndgn closed 1 year ago

ozgurhsyndgn commented 1 year ago

Dear Roozbeh,

I'm having an error using "cv_spatial_autocor" for my species "shapefile":

castanea_range <- cv_spatial_autocor(
  x = castanea_shp, # species data
  column = "Species", # column storing presence-absence records (0s and 1s)
  plot = FALSE
)

Error message: "Error in 0:nx : result would be too long a vector"

The shapefile information that I use:

Simple feature collection with 126 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 28.2008 ymin: 40.00867 xmax: 29.4516 ymax: 40.6423
Geodetic CRS:  WGS 84
First 10 features:
   species        y        x                  geometry
1        1 40.16144 29.18120  POINT (29.1812 40.16144)
2        1 40.13972 29.20258 POINT (29.20258 40.13972)
3        1 40.15186 29.10758 POINT (29.10758 40.15186)
4        1 40.10812 29.05537 POINT (29.05537 40.10812)
5        1 40.15392 29.18981 POINT (29.18981 40.15392)
6        1 40.15216 29.19982 POINT (29.19982 40.15216)
7        1 40.14940 29.17710   POINT (29.1771 40.1494)
8        1 40.16266 29.10236 POINT (29.10236 40.16266)
9        1 40.15477 29.09221 POINT (29.09221 40.15477)
10       1 40.16089 29.15719 POINT (29.15719 40.16089)

I'm trying to calculate spatial autocorrelation for my high resolution (120meter grid size) habitat suitability models. The working area of my models are 136 kilometers from west to east and 116 kilometers from south to north.

I'm giving you the size of my area because I can use the same function for my country sized models and not facing any errors at all. The working area size of my country wide models are 1620km from west to east and 700km from south to north. Using the mentioned function I'm having the result of: 139284.8

The shapefile of my locations for the country sized models:

Simple feature collection with 394 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 26.69494 ymin: 37.66206 xmax: 41.9981 ymax: 41.98326
Geodetic CRS:  WGS 84
First 10 features:
   species        x        y                  geometry
1        1 26.69494 39.64662 POINT (26.69494 39.64662)
2        1 26.77856 39.72514 POINT (26.77856 39.72514)
3        1 26.75171 39.69105 POINT (26.75171 39.69105)
4        1 26.79545 39.71365 POINT (26.79545 39.71365)
5        1 26.82056 39.73083 POINT (26.82056 39.73083)
6        1 26.91713 39.76641 POINT (26.91713 39.76641)
7        1 26.93108 39.68854 POINT (26.93108 39.68854)
8        1 27.05665 39.70475 POINT (27.05665 39.70475)
9        1 27.02133 39.76579 POINT (27.02133 39.76579)
10       1 27.13439 39.73947 POINT (27.13439 39.73947)

My session info:

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

other attached packages:
 [1] remotes_2.4.2.1      blockCV_3.1-3        tidyterra_0.4.0      usdm_2.1-6           terra_1.7-46        
 [6] sf_1.0-14            rasterVis_0.51.5     gridExtra_2.3        viridis_0.6.4        xgboost_1.7.5.1     
[11] randomForest_4.7-1.1 maxnet_0.1.4         earth_5.3.2          plotmo_3.6.2         TeachingDemos_2.12  
[16] plotrix_3.8-2        Formula_1.2-5        gbm_2.1.8.1          mgcv_1.9-0           nlme_3.1-163        
[21] gam_1.22-2           foreach_1.5.2        mda_0.5-4            class_7.3-22         rpart_4.1.19        
[26] nnet_7.3-19          biomod2_4.2-5        ggtext_0.1.2         zoom_2.0.6           RColorBrewer_1.1-3  
[31] corrplot_0.92        latticeExtra_0.6-30  lattice_0.21-8       lubridate_1.9.2      forcats_1.0.0       
[36] stringr_1.5.0        dplyr_1.1.2          purrr_1.0.2          readr_2.1.4          tidyr_1.3.0         
[41] tibble_3.2.1         tidyverse_2.0.0      ggpubr_0.6.0         ggplot2_3.4.3        spThin_0.2.0        
[46] knitr_1.43           fields_15.2          viridisLite_0.4.2    spam_2.9-1           qpcR_1.4-1          
[51] Matrix_1.6-1         robustbase_0.99-0    rgl_1.2.1            minpack.lm_1.2-4     MASS_7.3-60         
[56] devtools_2.4.5       usethis_2.2.2       

loaded via a namespace (and not attached):
 [1] later_1.3.1            pROC_1.18.4            xts_0.13.1             lifecycle_1.0.3       
 [5] rstatix_0.7.2          doParallel_1.0.17      processx_3.8.2         backports_1.4.1       
 [9] magrittr_2.0.3         httpuv_1.6.11          sp_2.0-0               sessioninfo_1.2.2     
[13] pkgbuild_1.4.2         DBI_1.1.3              maps_3.4.1             abind_1.4-5           
[17] pkgload_1.3.2.1        units_0.8-3            PresenceAbsence_1.1.11 codetools_0.2-19      
[21] gstat_2.1-1            xml2_1.3.5             tidyselect_1.2.0       raster_3.6-23         
[25] base64enc_0.1-3        jsonlite_1.8.7         e1071_1.7-13           ellipsis_0.3.2        
[29] survival_3.5-7         iterators_1.0.14       tools_4.3.1            Rcpp_1.0.11           
[33] glue_1.6.2             xfun_0.40              withr_2.5.1            fastmap_1.1.1         
[37] fansi_1.0.4            callr_3.7.3            digest_0.6.33          timechange_0.2.0      
[41] R6_2.5.1               mime_0.12              colorspace_2.1-0       jpeg_0.1-10           
[45] utf8_1.2.3             generics_0.1.3         hexbin_1.28.3          intervals_0.15.4      
[49] data.table_1.14.8      FNN_1.1.3.2            prettyunits_1.1.1      htmlwidgets_1.6.2     
[53] pkgconfig_2.0.3        gtable_0.3.4           htmltools_0.5.6        carData_3.0-5         
[57] profvis_0.3.8          dotCall64_1.0-2        automap_1.1-9          scales_1.2.1          
[61] png_0.1-8              rstudioapi_0.15.0      tzdb_0.4.0             reshape2_1.4.4        
[65] spacetime_1.3-0        proxy_0.4-27           cachem_1.0.8           zoo_1.8-12            
[69] KernSmooth_2.23-22     parallel_4.3.1         miniUI_0.1.1.1         pillar_1.9.0          
[73] reshape_0.8.9          vctrs_0.6.3            urlchecker_1.0.1       promises_1.2.1        
[77] car_3.1-2              xtable_1.8-4           cli_3.6.1              compiler_4.3.1        
[81] rlang_1.1.1            crayon_1.5.2           ggsignif_0.6.4         interp_1.1-4          
[85] classInt_0.4-9         ps_1.7.5               plyr_1.8.8             fs_1.6.3              
[89] stringi_1.7.12         deldir_1.0-9           stars_0.6-3            munsell_0.5.0         
[93] hms_1.1.3              shiny_1.7.5            gridtext_0.1.5         broom_1.0.5           
[97] memoise_2.0.1          lwgeom_0.2-13          DEoptimR_1.1-2   

My best regards and thanks in advance..

Hüseyin

PS: I can share the both shapefiles with you if you'd ask for.

ozgurhsyndgn commented 1 year ago

We've fixed the problem, the problem was the WGS coordinate system.

Converting our points from degree (WGS) to meters (mollweide), function finally worked.

For whom would face the same problem, I don't remove the topic.

rvalavi commented 1 year ago

Hi @ozgurhsyndgn I’m glad you find a solution. Reading your issue I also guessed it should be CRS👍

Farewe commented 11 months ago

Hey @rvalavi , I just fell in the same trap and ended up with the same error!

What do you think of putting a warning when people are using cv_spatial_autocor() on WGS84 data?