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.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 8fe3292 Dec 11, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
R SpatialPoints with no df Dec 11, 2018
inst/extdata Change the raster to grid format Jun 20, 2018
man Adding foldID Sep 18, 2018
tests Change the raster to grid format Jun 20, 2018
vignettes vignette Oct 20, 2018
.RData accepting sf object as input spatial data Sep 18, 2018
.Rbuildignore code coverage5 Jan 6, 2018
.Rhistory badges Nov 25, 2018
.gitignore Initiating blockCV project Jan 5, 2018
.travis.yml Update .travis.yml Jun 20, 2018
DESCRIPTION Update badges Oct 10, 2018
NAMESPACE Update NAMESPACE Jan 24, 2018 Initiating blockCV project Jan 5, 2018 badges Nov 26, 2018
blockCV.Rproj Initiating blockCV project Jan 5, 2018
codecov.yml code coverage Jan 9, 2018


Build Status codecov License GitHub version DOI

In a nutshell, package blockCV provides functions to build train and test data sets using three general strategies: buffers, spatial and environmental blocks. It offers several options for how those blocks are constructed and how blocks are allocated to cross-validation folds. It includes a function that applies geostatistical techniques to investigate the existing level of spatial autocorrelation in the chosen predictor variables to inform the choice of block and buffer size. In addition, visualization tools further aid selection of block size and provide understanding of the spread of species data across generated folds.


To install the package from GitHub use:

# install.packages("devtools")

Bsic usage

The following is an example of using spatial block cross-validation for evaluation of species distribution modelling. You can find a comprehensive tutorial in the vignette of the package.

# loading the package

# investigate spatial autocorrelation in raster covariates
spatialAutoRange(rasterLayer = awt, # raster file
                 sampleNumber = 5000, # number of cells to be used
                 doParallel = TRUE,
                 showPlots = TRUE)

# spatial blocking by specified range and random assignment
sb <- spatialBlock(speciesData = pa_data,
                   species = "Species",
                   rasterLayer = awt,
                   theRange = 68000, # size of the blocks
                   k = 5,
                   selection = "random",
                   iteration = 250, # find evenly dispersed folds
                   biomod2Format = TRUE)

# explor the generated folds
foldExplorer(sb, awt, pa_data)


To see the vignette of the package use:


The vignette is also available via this link.


Compared to other available packages, blockCV provides more strategies and control over fold generation including:

  • The assignment of the spatial blocks to cross-validation folds can be done in two different ways.
  • The position of the spatial blocks can be modified.
  • The buffering function can account for presence-absence and presence-background data types.
  • The variables are standardized to avoid wide range variables to dominate the environmental blocks
  • Using geostatistical techniques to inform the choice of a suitable distance band by which to separate the data sets


To cite package ‘blockCV’ in publications use:

Valavi, R., Elith, J., Lahoz-Monfort, J. J., & Guillera-Arroita, G. (2018). blockCV: an R package for generating spatially or environmentally separated folds for k-fold cross-validation of species distribution models. Methods in Ecology and Evolution, doi: 10.1111/2041‐210X.13107.