Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrating Caret with Block CV #48

Open
Navvie2019 opened this issue Jun 3, 2024 · 4 comments
Open

Integrating Caret with Block CV #48

Navvie2019 opened this issue Jun 3, 2024 · 4 comments

Comments

@Navvie2019
Copy link

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

@rvalavi
Copy link
Owner

rvalavi commented Jun 4, 2024

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
Copy link
Author

Thank you so much @rvalavi - massive help!

@rvalavi
Copy link
Owner

rvalavi commented Jun 13, 2024

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
Copy link
Author

Thank you @rvalavi - much appreciated!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants