# \donttest{
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
path <- system.file("extdata/au/", package = "blockCV")
files <- list.files(path, full.names = TRUE)
covars <- terra::rast(files)
# spatial clustering
set.seed(6)
sc <- cv_cluster(x = pa_data,
column = "occ", # optional; name of the column with response
k = 5)
# environmental clustering
set.seed(6)
ec <- cv_cluster(r = covars, # if provided will be used for environmental clustering
x = pa_data,
column = "occ", # optional; name of the column with response
k = 5,
scale = TRUE)
# spatially constrained environmental clustering
set.seed(6)
sec <- cv_cluster(r = covars,
x = pa_data,
column = "occ",
k = 5,
scale = TRUE,
spatial_weight = 0.4) # blend geography into the environmental clusters
# spatial clustering with balanced folds
set.seed(6)
bc <- cv_cluster(x = pa_data,
column = "occ",
k = 5,
balance = TRUE, # balance the records across the folds
k_multiplier = 3) # cluster into k * k_multiplier groups, then merge into k folds
# presence-background data: balance folds based on presences
points_pb <- read.csv(system.file("extdata/", "species_pb.csv", package = "blockCV"))
pb_data <- sf::st_as_sf(points_pb, coords = c("x", "y"), crs = 7845)
# pb_data <- pb_data[c(which(pb_data$occ == 1)[1:50], which(pb_data$occ == 0)[1:200]), ]
bc_pb <- cv_cluster(x = pb_data,
column = "occ",
k = 5,
presence_bg = TRUE,
balance = TRUE,
k_multiplier = 5)
# }
Run the code above in your browser using DataLab