Creates spatial cross-validation folds by clustering observations spatially and assigning clusters to folds. This method is useful for data with complex spatial structure.
Arguments
- data
Spatial observations (data.frame or sf object)
- x
Name of the x coordinate column (required for data.frame, ignored for sf)
- y
Name of the y coordinate column (required for data.frame, ignored for sf)
- k
Number of folds (default: 5)
- n_clusters
Number of spatial clusters (default: k)
- seed
Random seed for reproducibility (default: NULL)
Details
The clustering method groups spatially proximate observations into clusters using k-means clustering on coordinates, then assigns clusters to folds. This ensures spatial coherence within folds while maintaining separation between folds.
See also
Other spatial cross-validation functions:
spatial_block_folds(),
spatial_buffer_folds(),
spatial_folds(),
spatial_split()
Examples
if (FALSE) { # \dontrun{
data(sample_spatial_data)
folds <- spatial_cluster_folds(
data = sample_spatial_data,
x = "longitude",
y = "latitude",
k = 5,
n_clusters = 10
)
} # }