Open Navvie2019 opened 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..
Thank you so much @rvalavi - massive help!
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)
Thank you @rvalavi - much appreciated!!
Hi Roozbeh - Any update on being able to use block CV spatial blocking outputs with the caret traincontrol function? Cheers