Skip to contents

Generate blocked spatial index

Usage

generate_block_sp_index(covars, cv_fold = NULL, blocks = NULL, block_id = NULL)

Arguments

covars

data.frame-like object with lon, lat, and time columns.

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.

Value

data.frame-like object (same class as covars) with a sp_index column

Details

"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".

Author

Insang Song

Examples

data(spdat)
covars <- spdat
covars_block <- generate_block_sp_index(covars, cv_fold = 5)
covars_block
#>               lon      lat       time         x1         x2         y sp_index
#>             <num>    <num>     <Date>      <num>      <num>     <num>    <int>
#>     1:  -85.06633 32.42042 2021-01-01 1.21626648 -0.3342778 -2.396173        1
#>     2:  -85.06633 32.42042 2021-01-02 1.21626648 -0.3342778 -2.049283        1
#>     3:  -85.06633 32.42042 2021-01-03 1.21626648 -0.3342778 -2.116729        1
#>     4:  -85.06633 32.42042 2021-01-04 1.21626648 -0.3342778 -1.907624        1
#>     5:  -85.06633 32.42042 2021-01-05 1.21626648 -0.3342778 -1.431020        1
#>    ---                                                                        
#> 11996: -101.35941 33.86695 2021-04-26 0.04165367  0.8313810 13.450824        2
#> 11997: -101.35941 33.86695 2021-04-27 0.04165367  0.8313810 13.116766        2
#> 11998: -101.35941 33.86695 2021-04-28 0.04165367  0.8313810 13.156212        2
#> 11999: -101.35941 33.86695 2021-04-29 0.04165367  0.8313810 13.647927        2
#> 12000: -101.35941 33.86695 2021-04-30 0.04165367  0.8313810 13.602898        2

plot_cv_folds(covars_block, covars_block$sp_index)