| Title: | Lightweight rsample extension for spatiotemporal cross-validation |
|---|---|
| Description: | The main functions provide rset generators for spatiotemporal cross-validation with low footprint. |
| Authors: | Insang Song [aut, cre] (ORCID: <https://orcid.org/0000-0001-8732-3256>) |
| Maintainer: | Insang Song <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.1.9000 |
| Built: | 2026-10-09 10:27:21 UTC |
| Source: | https://github.com/sigmafelix/stcvlite |
Generate manual rset object from spatiotemporal cross-validation indices
convert_cv_index_rset(cvindex, data, cv_mode)convert_cv_index_rset(cvindex, data, cv_mode)
cvindex |
integer. Output of |
data |
data.frame-like object. Should be the
same object as what was used for |
cv_mode |
character(1). Spatiotemporal cross-validation indexing method.
See |
rset object of rsample package. A tibble with a list column of
training-test data.frames and a column of labels.
Insang Song
Generate blocked spatial index
generate_block_sp_index(covars, cv_fold = NULL, blocks = NULL, block_id = NULL)generate_block_sp_index(covars, cv_fold = NULL, blocks = NULL, block_id = NULL)
covars |
data.frame-like object with |
cv_fold |
integer(1). Number of folds for cross-validation. |
blocks |
numeric(2)/sf/SpatVector configuration of blocks. |
block_id |
character(1). The unique identifier of each block. |
"Block" in this function refers to a group of contiguous spatial/temporal entities.
Regardless of mode value (should be one of "spatial" or "spatiotemporal"),
the index column will be named "sp_index". block_id should be set with a proper field name
when blocks is a sf or SpatVector object. When cv_fold is an integer, then the unique
lon/lat coordinates in covars are clustered (not every row), so the result is
unaffected by how many times each location was observed — this keeps the block
assignment correct for irregular/unbalanced spacetime panels. Location matching when
broadcasting the cluster/block id back onto covars is by exact lon/lat equality;
coordinates that jitter slightly across repeated observations of "the same" site should be
rounded/snapped beforehand. When cv_fold is an integer, the result object will include
attributes (accessible with attr function) about "dbscan_eps" and "dbscan_minPts".
data.frame-like object (same class as covars) with a sp_index column
Insang Song
data(spdat) covars <- spdat covars_block <- generate_block_sp_index(covars, cv_fold = 5) covars_block plot_cv_folds(covars_block, covars_block$sp_index)data(spdat) covars <- spdat covars_block <- generate_block_sp_index(covars, cv_fold = 5) covars_block plot_cv_folds(covars_block, covars_block$sp_index)
Generate spatio-temporal cross-validation index
generate_cv_index( covars, cv_mode = c("lolo", "loto", "random", "lblo", "lbto", "lblto", "lolto", "loo"), cv_fold = 5L, sp_fold = NULL, t_fold = NULL, blocks = NULL, block_id = NULL )generate_cv_index( covars, cv_mode = c("lolo", "loto", "random", "lblo", "lbto", "lblto", "lolto", "loo"), cv_fold = 5L, sp_fold = NULL, t_fold = NULL, blocks = NULL, block_id = NULL )
covars |
data.frame-like object with |
cv_mode |
character(1). One of |
cv_fold |
integer(1). Number of folds for cross-validation. |
sp_fold |
integer(1). Number of subfolds for spatial blocks. |
t_fold |
integer(1). Number of subfolds for temporal blocks. |
blocks |
integer(2)/sf/SpatVector object. |
block_id |
character(1). The unique identifier of each block. |
blocks is NULL as default
(i.e., no block structure is considered); then "lb*" cv_mode
is not working.
blocks should be one of integer vector (length 2),
sf, sftime, or SpatVector object.
Please be advised that we cannot provide error messages
related to the blocks type invalidity
due to the cyclometric complexity.
If any valid object is assigned here,
cv_fold will not make sense and be ignored.
Definition of unit grid (horizontal, vertical) size,
arbitrary shape of blocks (sf/SpatVector case)
where users can consider common spatial hierarchies such as
states, counties, hydrological unit code areas, etc.
lblto mode will accept sp_fold and t_fold.
The maximum of results in this case is sp_fold * t_fold.
A numeric vector with length of the number of input rows
Insang Song
data(spdat) lolo <- generate_cv_index(spdat, cv_mode = "lolo") loto <- generate_cv_index(spdat, cv_mode = "loto") lolto <- generate_cv_index(spdat, cv_mode = "lolto") lblo <- generate_cv_index(spdat, cv_mode = "lblo", cv_fold = 5) lbto <- generate_cv_index(spdat, cv_mode = "lbto", cv_fold = 5) lblto <- generate_cv_index(spdat, cv_mode = "lblto", sp_fold = 5, t_fold = 4) random <- generate_cv_index(spdat, cv_mode = "random", cv_fold = 5) plot_cv_folds(spdat, lblto)data(spdat) lolo <- generate_cv_index(spdat, cv_mode = "lolo") loto <- generate_cv_index(spdat, cv_mode = "loto") lolto <- generate_cv_index(spdat, cv_mode = "lolto") lblo <- generate_cv_index(spdat, cv_mode = "lblo", cv_fold = 5) lbto <- generate_cv_index(spdat, cv_mode = "lbto", cv_fold = 5) lblto <- generate_cv_index(spdat, cv_mode = "lblto", sp_fold = 5, t_fold = 4) random <- generate_cv_index(spdat, cv_mode = "random", cv_fold = 5) plot_cv_folds(spdat, lblto)
Generate spatio-temporal cross-validation index (leave-block-location-out)
generate_cv_index_lblo(covars, cv_fold = NULL, blocks = NULL, block_id = NULL)generate_cv_index_lblo(covars, cv_fold = NULL, blocks = NULL, block_id = NULL)
covars |
data.frame-like object with |
cv_fold |
integer(1). Number of folds for cross-validation. |
blocks |
integer(2)/sf/SpatVector object. |
block_id |
character(1). The unique identifier of each block. |
An integer vector.
Insang Song
Generate spatio-temporal cross-validation index (leave-block-location-time-out)
generate_cv_index_lblto(covars, sp_fold, t_fold, blocks, block_id = NULL)generate_cv_index_lblto(covars, sp_fold, t_fold, blocks, block_id = NULL)
covars |
data.frame-like object with |
sp_fold |
integer(1). Number of subfolds for spatial blocks. |
t_fold |
integer(1). Number of subfolds for temporal blocks. |
blocks |
integer(2)/sf/SpatVector object. |
block_id |
character(1). The unique identifier of each block. |
The maximum of results is sp_fold * t_fold.
An integer vector.
Insang Song
Generate spatio-temporal cross-validation index (leave-block-time-out)
generate_cv_index_lbto(covars, cv_fold = NULL)generate_cv_index_lbto(covars, cv_fold = NULL)
covars |
data.frame-like object with |
cv_fold |
integer(1). Number of folds for cross-validation. |
An integer vector.
Insang Song
Generate spatio-temporal cross-validation index (leave-one-location-out)
generate_cv_index_lolo(covars)generate_cv_index_lolo(covars)
covars |
data.frame-like object with |
An integer vector.
Insang Song
Generate spatio-temporal cross-validation index leave-one-location-time-out)
generate_cv_index_lolto(covars)generate_cv_index_lolto(covars)
covars |
data.frame-like object with |
An integer vector.
Insang Song
Generate spatio-temporal cross-validation index (leave-one-time-out)
generate_cv_index_loto(covars)generate_cv_index_loto(covars)
covars |
data.frame-like object with |
An integer vector.
Insang Song
Generate spatio-temporal cross-validation index (random)
generate_cv_index_random(covars, cv_fold = NULL)generate_cv_index_random(covars, cv_fold = NULL)
covars |
data.frame-like object with |
cv_fold |
integer(1). Number of folds for cross-validation. |
An integer vector with unique values of seq(1, cv_fold)
Insang Song
It generates unique spatiotemporal identifier in the
input stdt object. Regardless of mode values
(should be one of "spatial" or "spatiotemporal"),
the index column will be named "sp_index".
generate_spt_index(covars, mode = c("spatial", "spatiotemporal"))generate_spt_index(covars, mode = c("spatial", "spatiotemporal"))
covars |
data.frame-like object with |
mode |
One of |
stdt with a new index named "sp_index"
Insang Song
Creates a plotly scatterplot (x, y, fold index as z) with fold/cluster
number shown by color. For large datasets, points are downsampled to
at most max_points for display.
plot_cv_folds(covars, cv_index, max_points = 1200L, seed = 2026L)plot_cv_folds(covars, cv_index, max_points = 1200L, seed = 2026L)
covars |
A |
cv_index |
Fold assignment vector returned by |
max_points |
Maximum number of points to display (default |
seed |
Random seed used when downsampling. |
A plotly 3D scatter object.
data(spdat) cv_index <- generate_cv_index(spdat, cv_mode = "lblo", cv_fold = 6) if (requireNamespace("plotly", quietly = TRUE)) { p <- plot_cv_folds(spdat, cv_index) p }data(spdat) cv_index <- generate_cv_index(spdat, cv_mode = "lblo", cv_fold = 6) if (requireNamespace("plotly", quietly = TRUE)) { p <- plot_cv_folds(spdat, cv_index) p }
Convert sf and sftime objects to stdt format
prep_input(input)prep_input(input)
input |
An sf, sftime, SpatVector or SpatRasterDataset object |
A data.table object with lon, lat, time and a crs attribute
Eva Marques, Insang Song
This dataset contains 12,000 spatiotemporal points with
lon, lat, time, and value columns. The points can be used
to demonstrate the functions in stcvlite.
spdatspdat
A data.table with 12,000 rows (100 locations * 120 times) and
six variables, plus a "crs" attribute (attr(spdat, "crs")):
Unique point identifier. Arbitrarily generated.
Longitude
Latitude
covariate 1
covariate 2
response variable
Coordinates are in EPSG:4326
data("spdat", package = "stcvlite")data("spdat", package = "stcvlite")