Getting Started with spacc
Gilles Colling
2026-03-06
Source:vignettes/quickstart.Rmd
quickstart.RmdOverview
spacc computes spatial species accumulation curves (SACs) using nearest-neighbour algorithms with a C++ backend for speed. Unlike classical random SACs, spatial SACs respect the geographic arrangement of sampling sites, revealing how species richness accumulates as you expand outward from a focal point.
Simulating Example Data
library(spacc)
set.seed(42)
n_sites <- 80
n_species <- 40
coords <- data.frame(
x = runif(n_sites, 0, 100),
y = runif(n_sites, 0, 100)
)
# Simulate presence/absence with spatial structure
species <- matrix(0L, n_sites, n_species)
for (sp in seq_len(n_species)) {
cx <- runif(1, 20, 80)
cy <- runif(1, 20, 80)
prob <- exp(-0.001 * ((coords$x - cx)^2 + (coords$y - cy)^2))
species[, sp] <- rbinom(n_sites, 1, prob)
}
colnames(species) <- paste0("sp", seq_len(n_species))Basic Spatial Accumulation Curve
The core function spacc() computes kNN-based
accumulation curves from multiple random starting sites (seeds):
sac <- spacc(species, coords, n_seeds = 30, method = "knn", progress = FALSE)
sac
#> spacc: 80 sites, 40 species, 30 seeds (knn)
plot(sac)Spatial species accumulation curve with 95% confidence ribbon.
The grey ribbon shows variability across starting points — wider ribbons indicate that species richness depends on where you start sampling.
Comparing Methods
spacc supports several accumulation methods:
knn: k-Nearest Neighbour (default, fast)
kncn: k-Nearest Centroid Neighbour (geometrically centred expansion)
random: Classical random accumulation (no spatial structure)
sac_kncn <- spacc(species, coords, n_seeds = 30, method = "kncn", progress = FALSE)
sac_rand <- spacc(species, coords, n_seeds = 30, method = "random", progress = FALSE)Combine curves for comparison:
Comparison of accumulation methods.
Extrapolation
Fit an asymptotic model to estimate total species richness:
fit <- extrapolate(sac, model = "lomolino")
#> Waiting for profiling to be done...
fit
#> Extrapolation: lomolino
#> ------------------------------
#> Estimated asymptote: 42.7 species
#> 95% CI: 42.2 - 43.3
#> AIC: 165.3
#> Observed: 40.0 species (94% of estimated)
plot(fit)Fitted Lomolino curve with asymptote estimate.
Pre-computing Distances
For repeated analyses on the same sites, pre-compute the distance matrix once:
Comparing Curves
Test whether two accumulation curves differ significantly:
comp <- compare(sac1, sac2, method = "permutation", n_perm = 199)
comp
#> Comparison: sac1 vs sac2
#> ----------------------------------------
#> Method: permutation (n=199)
#> AUC difference: -8.6 (p = 0.573)
#> Saturation: sac1 at 23 sites, sac2 at 19 sitesNext Steps
- Diversity: Hill numbers, beta diversity, phylogenetic and functional diversity accumulation
- Extrapolation: Coverage-based extrapolation, EVT models, diversity-area relationships
- Spatial Analysis: Endemism curves, fragmentation analysis, sampling-effort correction