Skip to contents

Start here

The pipeline in order, the function for each step, and where each default comes from.

spatialkit spatialkit-package
spatialkit: Spatial Tessellation, Modelling, and Cross-Validation Toolkit

Prepare the data

Put the observations in a projected CRS as points, and see how the package reads input geometry.

ensure_projected()
Project an sf object to a CRS suited to its extent
harmonize_crs()
Bring two spatial objects to one CRS
coerce_to_points()
Reduce each geometry to one representative point
prep_model_data()
Prepare an sf layer for model fitting
`[`(<spatialkit_rows>)
Subset a layer that carries a row record
spatialkit-geometry
How input geometry is read

Measure spatial autocorrelation

The distance over which observations stay correlated. It can size the cross-validation blocks (make_folds(auto_range = TRUE)) and sets a floor on the number of cells, which select_resolution(criterion = "reliability") and resolution_profile(range_floor = TRUE) apply.

estimate_sac_range() print(<sac_range>)
Estimate the spatial autocorrelation range from data
sac_nugget()
Extract the nugget of an estimated autocorrelation range
plot(<sac_range>)
Plot an estimated spatial autocorrelation range

Choose a resolution

How many cells the data support, and the counts the chosen criterion scores within tol of its best (the flat region).

resolution_profile() print(<resolution_profile>)
Score candidate cell counts on several criteria
select_resolution()
Choose a cell count from a resolution profile
summary(<resolution_profile>)
Compare every criterion's pick on a resolution profile
plot(<resolution_profile>)
Plot a resolution profile
determine_optimal_levels()
Choose a cell count from the elbow of the k-means WSS curve

Build the cells

Voronoi, hex, square and Delaunay cells. Start with build_tessellation(). Hex and square cells need a boundary; clip_target_for() builds one from the points. The seed functions place the points Voronoi cells are drawn around, and the lower-level builders draw cells of one kind or number them.

build_tessellation()
Build Voronoi, triangle, hex or square cells from points
clip_target_for()
Build the area to tessellate within, from points or a boundary
plot_tessellation_map()
Plot a tessellation map with optional boundary, seeds, and features

Place Voronoi seeds

get_voronoi_seeds()
Place seeds for a Voronoi tessellation
voronoi_seeds_kmeans()
Place seeds at k-means cluster centres
voronoi_seeds_random()
Place seeds at random inside a boundary

Lower-level builders and IDs

create_voronoi_polygons()
Build Voronoi cells around points
create_grid_polygons()
Create square or hexagonal grid polygons over a boundary
create_grid_polygons_cached()
Create and cache grid polygons over a boundary
ensure_stable_poly_id()
Give cells IDs that follow their position

Assign and summarise

Put each observation in a cell, reduce to one row per cell with a count, a standard error and a pooled value, and read cell values at other locations. kriging_adequacy() checks the cell mean against block kriging.

assign_features_to_polygons()
Assign features to cells and attach a cell ID
summarize_by_cell()
Summarise assigned points by cell, with standard errors and pooled values
cell_values_at()
Read cell values at new locations
kriging_adequacy()
Check whether block kriging would improve cell means

Make folds

Spatial cross-validation folds, the block size, and how far the held-out points sit from the training data, beside the map’s locations when you pass them.

make_folds()
Create spatial cross-validation folds
plot_folds()
Map a cross-validation fold scheme
fold_separation()
Measure how far the held-out points sit from their training data
cv_block_size_sweep() plot(<block_size_sweep>)
Cross-validate at a ladder of block sizes

Fit a model

Random forest, GWR and Bayesian GP backends behind one spatial_fit class, the length-scale bounds the GP uses, and the constructor for a backend of your own.

fit_rf_model()
Fit a random forest with ranger
fit_gwr_model()
Fit a geographically weighted regression (GWR)
fit_bayesian_spatial_model()
Fit a Bayesian Gaussian-process regression with brms
gp_lengthscale_bounds()
Compute length-scale bounds for a squared-exponential GP
new_spatial_fit()
Build a spatial_fit object for a backend of your own

Use a fitted model

print(), summary() and plot() for any fit; predict(), fitted() and residuals() for each backend; and coef() for GWR and the Bayesian GP, since a forest has no coefficients. A forest’s fitted values and residuals are out-of-bag.

print(<spatial_fit>) print(<rf_fit>)
Print a fitted model
summary(<spatial_fit>)
Summarise a fitted model
plot(<spatial_fit>)
Plot a fitted spatial model
predict(<gwr_fit>) predict(<bayesian_fit>) predict(<rf_fit>)
Predict from a fitted model
fitted(<gwr_fit>) fitted(<bayesian_fit>) fitted(<rf_fit>)
Extract the fitted values of a fitted model
residuals(<gwr_fit>) residuals(<bayesian_fit>) residuals(<rf_fit>)
Extract the residuals of a fitted model
coef(<gwr_fit>) coef(<bayesian_fit>) coef(<rf_fit>)
Extract the coefficients of a fitted model

Check a fit in sample

Metrics on the training data (out-of-bag for a forest), and residual Moran’s I, which tests for spatial structure the model left behind. In-sample metrics do not compare across backends; compare backends with compare_models_cv(). model_metrics(), evaluate_insample() and compare_models() also score rows you held out, passed as newdata.

model_metrics()
Compute goodness-of-fit metrics for a fitted model
evaluate_insample()
Compute the metrics of several fitted models
compare_models()
Compare fitted models in sample
residual_morans_i()
Test the residuals of a fitted model for spatial autocorrelation
print(<morans_i>)
Print a residual Moran's I result

Cross-validate and compare

Score a model on held-out blocks, compare backends on the same folds, and plot the per-fold spread and interval calibration.

cv_spatial()
Cross-validate any learner with spatial folds
cv_rf()
Cross-validate a random forest with spatial folds
cv_gwr()
Cross-validate a GWR with spatial folds
cv_bayes()
Cross-validate a Bayesian GP with spatial folds
compare_models_cv()
Compare models on the same cross-validation folds
plot_cv_metrics()
Plot one cross-validation metric fold by fold
plot_calibration()
Plot the interval calibration of a Bayesian cross-validation

Choose predictors

Forward selection scored on spatial folds for any backend, and an AICc search over predictor sets for GWR.

select_features_forward()
Select predictors by forward selection on spatial folds
plot(<feature_selection>)
Plot the path of a forward feature selection
gwr_model_selection() print(<gwr_model_selection>)
Select GWR predictors by forward selection on AICc
plot(<gwr_model_selection>)
Plot a GWR model selection

Predict a surface

Predict onto a regular grid, and mark the locations whose predictor values are too unlike the training data for the cross-validated score to hold there.

predict_surface()
Predict a fitted model onto a regular grid
area_of_applicability()
Mark where a model's predictions apply
print(<aoa>)
Print an area-of-applicability result
plot(<aoa>)
Plot the dissimilarity distribution behind an area of applicability

Set options, catch conditions and clear caches

The console log, the two work limits, every warning and error class the package raises, and two in-session caches. To free memory, clear the grid cache that create_grid_polygons_cached() fills; clear a Bayesian fit’s fitted-value cache after editing its brmsfit by hand.

spatialkit_quiet()
Turn the console log off or on
spatialkit-options
Options that bound the work of a search or a fit
spatialkit-conditions
Warnings and errors the package raises
clear_grid_cache()
Clear the in-session grid cache
clear_fitted_cache()
Clear the cached fitted values of a Bayesian GP fit