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

Integrating Caret with Block CV #48

Open Navvie2019 opened 5 months ago

Navvie2019 commented 5 months ago

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

rvalavi commented 5 months ago

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..

Navvie2019 commented 5 months ago

Thank you so much @rvalavi - massive help!

rvalavi commented 5 months ago

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)
Navvie2019 commented 5 months ago

Thank you @rvalavi - much appreciated!!