View source: R/crossValidate.R
| crossValidate | R Documentation |
Obtain residual mean square error (RMSE) from K-fold cross validation of bathymetry interpolation.
crossValidate(
outline,
df,
x,
y,
z,
zeros = FALSE,
separation = NULL,
k = 5,
res = 5,
seed = NULL,
method = "IDW",
nmax = 20,
idp = 2,
model = "Sph",
psill = NULL,
range = NULL,
nugget = 0,
kappa = NULL,
trend_order = 1,
zero_threshold = 0.05
)
outline |
shapefile outline of a waterbody. Accepts a SpatVector, an sf object, or anything terra::vect() can read. |
df |
dataframe of coordinates and depths for a given waterbody. Coordinates are assumed to be in the same CRS as 'outline'. |
x |
character giving name of longitude column |
y |
character giving name of latitude column |
z |
character giving name of depth column |
zeros |
logical describing if bounding zeros are needed (FALSE) or provided (TRUE), default = FALSE |
separation |
number describing distance between points, in meters |
k |
numeric value describing the number of folds to test, default = 5 |
res |
number describing desired cell resolution in meters, default = 5 |
seed |
optional numeric value used to seed the random number generator, so that fold assignment (and therefore the resulting RMSE) is reproducible across runs. Default = NULL (not seeded). |
method |
character describing method of interpolation, options include Inverse Distance Weighted ("IDW"), Ordinary Kriging ("OK"), or Universal Kriging ("UK"). Default = "IDW" |
nmax |
numeric value describing number of neighbors used in interpolation, default = 20 |
idp |
numeric value describing inverse distance power value for IDW interpolation |
model |
character describing type of model used in Ordinary Kriging, options include 'Sph', 'Exp', 'Gau', 'Sta', default = 'Sph' |
psill |
numeric value describing the partial sill value for OK interpolation, default = NULL |
range |
numeric describing distance beyond which there is no spatial correlation in Ordinary Kriging models, default = NULL |
nugget |
numeric describing variance at zero distance in Ordinary/Universal Kriging models, default = 0 |
kappa |
numeric value describing model smoothness, default = NULL |
trend_order |
numeric value (1 or 2) giving the polynomial trend order for Universal Kriging ("UK" only), default = 1 |
zero_threshold |
numeric proportion (0-1) of surface area that must interpolate to exactly 0 before the automatic zero re-interpolation pass runs - passed through to interpBathy(). Default = 0.05. |
Folds are assigned in two stages: points are first split into 5 depth strata (quintiles of observed depth), then within each stratum, points are spatially clustered into 'k' groups (via k-means on their coordinates) and each spatial cluster becomes one fold. This keeps depth ranges reasonably balanced across folds while avoiding the optimistic bias that comes from randomly scattering spatially autocorrelated points across folds. 'res' is required and is always in meters, regardless of the CRS 'outline' was originally supplied in.
For the model argument there are four different methods included here that are supported by gstat::vgm ("Sph", "Exp", "Gau", "Mat"). "Sph" = The default gstat::vgm method. Spherical model characterized by a curve that rises steeply to defined range then flattens, indicates no spatial correlation between points beyond that range. "Exp" = Exponential model characterized by spatial correlation decaying rapidly with distance, results in a rougher surface. "Gau" = Gaussian model similar to spatial model but with slower decay over distance, results in a smoother surface. "Mat" = Matern model that uses kappa to define the variogram relationship. High kappa values approach a Guassian model (smooth surface), and low kappa values approach the Exponential model (kappa = 0.5 is equivalent to Exponential). Three parameters (psill, range, kappa) are incorporated from a fitted variogram (default = NULL). If specified in function input, chosen values will overwrite variogram values.
a named numeric value giving the mean RMSE across k folds
Tristan Blechinger, Department of Zoology & Physiology, University of Wyoming
#load example outline
outline <- terra::vect(system.file("extdata", "example_outline.shp", package = 'rLakeHabitat'))
#load example xyz data
data <- read.csv(system.file("extdata", "example_depths.csv", package = 'rLakeHabitat'))
#run function
crossValidate(outline, data, "x", "y", "z", zeros = FALSE, separation = 10, k = 5,
res = 50, method = "IDW", nmax = 4, idp = 1.5, seed = 123)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.