Export cross-validation blocks/folds
Closed this issue · 2 comments
HendrikHamacher commented
Hi,
it's not an issue per se, but I was wondering if it's possible to export the output from cv_spatial (such as the table with the different points for each fold + coordinates) to a csv file? I tried to do it, but R states that it cannot convert class "cv_spatial" into a datafrme.
I am relatively new to R, however I managed to create the blocks and different points per fold. I would like to use this for a classification in Google Earth Engine.
Best regards,
Hendrik
rvalavi commented
This is pretty easy, You just need to add the folds_ids
to your points and export it as a csv.
This example is from the cv_spatial
help file:
library(blockCV)
library(sf)
# 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)
# hexagonal spatial blocking by specified size and random assignment
sb1 <- cv_spatial(x = pa_data,
column = "occ",
size = 450000,
k = 5,
selection = "random",
iteration = 50)
# check to see the number of folds equal your data points (this must be TRUE)
length(sb1$folds_ids) == nrow(points)
# add the fold IDs to the original dataframe
points$folds <- sb1$folds_ids
head(points)
# export points dataframe as a CSV file
write.csv(points, "points_with_cvfolds.csv")
HendrikHamacher commented
Hi @rvalavi
thanks for clarifying with the example, that really helped!