blockCV icon indicating copy to clipboard operation
blockCV copied to clipboard

Integrating Caret with Block CV

Open Navvie2019 opened this issue 1 year ago • 6 comments

Hi Roozbeh - Any update on being able to use block CV spatial blocking outputs with the caret traincontrol function? Cheers

Navvie2019 avatar Jun 03 '24 04:06 Navvie2019

Hi @Navvie2019, thanks for asking. Oh it's been a while I added this. I'll provide you with an example in the next couple of days..

rvalavi avatar Jun 04 '24 23:06 rvalavi

Thank you so much @rvalavi - massive help!

Navvie2019 avatar Jun 06 '24 10:06 Navvie2019

Hi @Navvie2019

Here is an example of how to use blockCV for trainControl and param tuning of caret. I'll finaly add this as a vignette.

library(caret)
library(blockCV)

# import presence-absence species data
points <- read.csv(system.file("extdata/", "species.csv", package = "blockCV"))
# make an sf object from data.frame
pa_data <- sf::st_as_sf(points, coords = c("x", "y"), crs = 7845)

# load raster data
covars <- terra::rast(
    list.files(
        system.file("extdata/au/", package = "blockCV"), 
        full.names = TRUE
    )
)

# extract covariate for modelling
training <- terra::extract(covars, pa_data, ID = FALSE)
training$occ <- as.factor(pa_data$occ)
head(training)


# spatial blocking
sb1 <- cv_spatial(
    x = pa_data,
    column = "occ",
    size = 450000,
    k = 5,
    selection = "random",
    iteration = 50
)


control <- trainControl(
    # method = "cv", # this doesn't seem to change anything
    index = lapply(sb1$folds_list, function(x) x[[1]]),
    indexOut = lapply(sb1$folds_list, function(x) x[[2]]),
    search = "random"
)

model <- train(
    occ ~ .,
    data = training,
    method = "rf",
    trControl = control,
    tuneLength = 10
)

model$resample
model$control
plot(model)

rvalavi avatar Jun 13 '24 05:06 rvalavi

Thank you @rvalavi - much appreciated!!

Navvie2019 avatar Jun 16 '24 23:06 Navvie2019

If it is of any use, with tidysdm, you can use the function blockcv2rsample() to use blockCV within a tidymodels workflow.

dramanica avatar Aug 19 '25 05:08 dramanica

Oh that's great! Thanks, @dramanica!

rvalavi avatar Aug 22 '25 00:08 rvalavi