| cv_nndm | R Documentation |
A fast implementation of the Nearest Neighbour Distance Matching (NNDM) algorithm (Milà et al., 2022) in C++. Similar
to cv_buffer, this is a variation of leave-one-out (LOO) cross-validation. It tries to match the
nearest neighbour distance distribution function between the test and training data to the nearest neighbour
distance distribution function between the target prediction and training points (Milà et al., 2022).
cv_nndm(
x,
column = NULL,
r = NULL,
size,
pred_points = NULL,
model_domain = NULL,
num_sample = 10000L,
sampling = "random",
min_train = 0.05,
presence_bg = FALSE,
add_bg = FALSE,
num_bins = 4L,
plot = interactive(),
report = interactive()
)
x |
a simple features (sf) or SpatialPoints object of spatial sample data (e.g., species data or ground truth sample for image classification). |
column |
character; indicating the name of the column in which response variable (e.g. species data as a binary
response i.e. 0s and 1s) is stored. This is required when |
r |
a terra SpatRaster object of a predictor variable (optional). This defines the area that
the model is going to predict; prediction points are sampled from it when neither |
size |
numeric value of the range of spatial autocorrelation (the |
pred_points |
a simple features (sf) object of prediction points (optional). If provided, these are
used directly as the prediction locations instead of sampling from |
model_domain |
an sf polygon of the prediction area (optional). If provided (and |
num_sample |
integer; the number of sample points from predictor ( |
sampling |
either |
min_train |
numeric; between 0 and 1. A constraint on the minimum proportion of train points in each fold. |
presence_bg |
logical; whether to treat data as species presence-background data. For all other data
types (presence-absence, continuous, count or multi-class responses), this option should be |
add_bg |
logical; add background points to the test set when |
num_bins |
integer; the number of quantile bins used to stratify a continuous numeric |
plot |
logical; whether to plot the G functions. Defaults to |
report |
logical; whether to print summary of records in each fold.
Defaults to |
When working with presence-background data (presences and background points sampled across the study
area to represent the available conditions rather than confirmed absences, specified by
presence_bg = TRUE), only presence records are used
for specifying the folds (recommended). The testing fold comprises only the target presence point (optionally,
all background points within the distance are also included when add_bg = TRUE; this is the
distance that matches the nearest neighbour distance distribution function of training-testing presences and
training-presences and prediction points; often lower than size).
Any non-target presence points inside the distance are excluded.
All points (presence and background) outside of distance are used for the training set.
The methods cycles through all the presence data, so the number of folds is equal to
the number of presence points in the dataset.
For all other types of data (including presence-absence, count, continuous, and multi-class)
set presence_bg = FALSE, and the function behaves similar to the methods
explained by Milà and colleagues (2022).
An object of class S3. A list of objects including:
folds_list - a list containing the folds. Each fold has two vectors with the training (first) and testing (second) indices
k - number of the folds
size - the distance band to separate training and testing folds)
exclusion - a data.frame with one row per fold: the fold number, the row index of its
test point in x (test_id), and the exclusion_distance matched to that point.
Unlike cv_buffer, where the exclusion distance is the constant size, NNDM
matches a distinct radius to each test point: every point within exclusion_distance of the
test point is held out, and the points beyond it form the training set. Note this is not
capped by size: the matching only thins neighbours closer than size, so a point
whose nearest neighbour already lies beyond size is left untouched and reports its
(larger) natural nearest-neighbour distance
column - the name of the column if provided
presence_bg - whether this was treated as presence-background data
records - a table with the number of points in each category of training and testing
C. Milà, J. Mateu, E. Pebesma, and H. Meyer, Nearest Neighbour Distance Matching Leave-One-Out Cross-Validation for map validation, Methods in Ecology and Evolution (2022).
cv_buffer, cv_knndm and cv_spatial_autocor;
cv_plot to visualise, and cv_distance and cv_similarity to evaluate, the folds
library(blockCV)
# import presence-absence species data
points <- read.csv(system.file("extdata/", "species.csv", package = "blockCV"))
# make an sf object from data.frame
pa_data <- sf::st_as_sf(points, coords = c("x", "y"), crs = 7845)
# load raster data
path <- system.file("extdata/au/bio_5.tif", package = "blockCV")
covar <- terra::rast(path)
nndm <- cv_nndm(x = pa_data,
column = "occ", # optional
r = covar,
size = 350000, # size in metres no matter the CRS
num_sample = 10000,
sampling = "regular",
min_train = 0.1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.