Partitioning spatial structure
Source:vignettes/articles/spatial-structure.Rmd
spatial-structure.RmdA per-site diversity map raises two questions: how much of its variation is spatially structured, and at which spatial scales that structure lives. Moran eigenvector maps (MEM) answer both. They build a set of uncorrelated spatial patterns, from broad gradients to fine tiles, directly from the site coordinates; regressing a per-site response on a selection of these patterns splits its variance into a spatial share and a residual. This article builds the patterns for a simulated region and partitions the per-site accumulation slope from Mapping per-site metrics and wavefronts. Installation is covered in Get started; the plots need ggplot2.
Build the eigenvector maps from coordinates with
spatialEigenvectors().Partition a per-site response on them with
spatialPartition().
Example
Simulating a region and a per-site response
The simulated region holds 80 sites scattered over a 100 by 100 unit
square and 40 species. Each species occupies a Gaussian neighbourhood
around a random centre: its occurrence probability at a site is \(e^{-s r^2}\), where \(r\) is the distance to the centre and \(s\) (spread) sets the range
size. The first 10 species use spread = 0.005 and form
tight clusters (occurrence probability halves about 12 units from the
centre); the other 30 use spread = 0.001 (about 26 units)
and appear across much of the region.
Simulation code
set.seed(42)
n_sites <- 80
n_species <- 40
coords <- data.frame(
x = runif(n_sites, 0, 100),
y = runif(n_sites, 0, 100)
)
species <- matrix(0L, n_sites, n_species)
for (sp in seq_len(n_species)) {
cx <- runif(1, 10, 90)
cy <- runif(1, 10, 90)
spread <- if (sp <= 10) 0.005 else 0.001
prob <- exp(-spread * ((coords$x - cx)^2 + (coords$y - cy)^2))
species[, sp] <- rbinom(n_sites, 1, prob)
}
colnames(species) <- paste0("sp", seq_len(n_species))The response is the initial accumulation slope of each site
(slope_10, the slope over the first 10 percent of sites of
an accumulation started there), computed with spaccMetrics().
met <- spaccMetrics(species, coords, metrics = "slope_10",
progress = FALSE)
slope <- met$metrics$slope_10
summary(slope)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.6429 1.3304 1.5893 1.6336 1.9494 2.7024Building the eigenvector maps
spatialEigenvectors() builds the MEM basis from the coordinates alone (How the eigenvector maps are built gives the construction). The print method reports the truncation threshold, the number of eigenvectors retained and the range of Moran’s I across them.
mem <- spatialEigenvectors(coords)
mem
#> Spatial eigenvectors (PCNM): 80 sites
#> Threshold: 17.5122
#> Eigenvectors retained: 50
#> Moran's I range: [-0.168, 1.419]summary() lists each eigenvector’s eigenvalue, Moran’s I
and share of the total variance. The leading vectors carry the
broad-scale, strongly autocorrelated patterns; later vectors describe
progressively finer structure.
head(summary(mem), 5)
#> vector eigenvalue moran_i variance_explained
#> 1 MEM1 22573.08 1.4189143 0.09330907
#> 2 MEM2 18796.70 1.1530522 0.07769883
#> 3 MEM3 16042.37 0.9561648 0.06631344
#> 4 MEM4 14292.83 0.8373798 0.05908148
#> 5 MEM5 13568.98 0.7816404 0.05608932
plot(mem, type = "map", n_vectors = 4)The leading maps here are concentrated on the dense group of sites in
the upper half of the region: MEM1 contrasts its western and eastern
parts and is near zero elsewhere. With irregular sampling, the leading
eigenvectors follow the densest part of the network, so they need not
split the whole region into halves. type = "eigenvalues"
and type = "moran" plot the eigenvalues and Moran’s I of
all vectors.
Partitioning the per-site slope
spatialPartition() regresses the per-site response on the eigenvectors with forward selection and reports the spatial and non-spatial shares of its variance.
part <- spatialPartition(slope, mem)
part
#> Spatial Variance Partitioning
#> -----------------------------------
#> Spatial R-squared: 0.600
#> Non-spatial: 0.400
#> MEMs selected: 9
#> Selected: MEM6, MEM20, MEM7, MEM13, MEM15, MEM14, MEM24, MEM5, MEM11
plot(part)Nine eigenvectors explain 60% of the variation in the per-site slope. None of the four broadest patterns (MEM1 to MEM4) is selected; the selection runs from MEM5 to MEM24, so the slope varies at intermediate and finer scales.
The spatial R-squared is the fraction of slope variation explained by
the selected eigenvectors, and the selected MEMs name the scales that
carry it. A large spatial share carried mostly by low-numbered MEMs
means the hotspots are organised at broad scales; a share spread across
high-numbered MEMs means the structure is patchy and fine-grained. The
maps of the selected vectors (plot(mem, type = "map")) show
where those scales lie.
Partitioning in this way separates the question “is the pattern
spatial” from “is the pattern explained by a covariate”. The two
analyses can be combined to ask how much spatial structure remains after
an environmental predictor is accounted for.
spatialPartition() also takes a curve object such as a
spacc_metrics, spacc_alpha or
spacc_hill result directly, with metric naming
the column to use.
Forward selection
Forward selection adds the eigenvector that most improves the model
AIC at each step and stops when no remaining vector improves AIC by more
than 2 units or when its p-value exceeds alpha (default
0.05). The result is a parsimonious set rather than the full basis, so
the reported spatial R-squared reflects structure the data support.
forward = FALSE includes every eigenvector and inflates the
fit; use it only to remove all spatial signal, not to describe it.
part_all <- spatialPartition(slope, mem, forward = FALSE)
c(selected = part$r_squared_spatial, all = part_all$r_squared_spatial)
#> selected all
#> 0.5995530 0.7572954All 50 eigenvectors raise the R-squared from 0.60 to 0.76, with 41 more parameters than the selected model.
How the eigenvector maps are built
The construction follows Borcard & Legendre (2002) and Dray et al. (2006). The pairwise distance matrix is truncated at a threshold: distances above it are set to four times the threshold. By default the threshold is the largest edge of the minimum spanning tree, the smallest distance that keeps all sites connected. The squared truncated matrix is double-centred, and its eigenvectors with positive eigenvalues are kept. Each eigenvector is a map: broad-scale patterns carry large eigenvalues and strong positive Moran’s I, fine-scale patterns carry small eigenvalues. Moran’s I for eigenvector \(v\) with connectivity matrix \(W\) is
\[ I = \frac{n}{\sum_{ij} w_{ij}} \frac{\sum_{ij} w_{ij}\,(v_i - \bar v)(v_j - \bar v)} {\sum_i (v_i - \bar v)^2}, \]
with \(n\) sites and weights \(w_{ij} = 1\) when distinct sites \(i\) and \(j\) lie within the threshold, 0 otherwise.
Two methods share this construction. method = "pcnm"
(the default) keeps every eigenvector with a positive eigenvalue,
including those that describe negative autocorrelation,
checkerboard-like patterns where neighbours differ.
method = "dbmem" keeps only the eigenvectors with positive
Moran’s I, the patterns where neighbours resemble each other, which are
the usual target for modelling ecological gradients.
The threshold matters as much as the method. A small
threshold connects only the nearest sites and yields many
fine-scale vectors; a large one connects distant sites and emphasises
broad gradients. The minimum-spanning-tree default is the smallest
threshold that leaves no site isolated, a starting point that adapts to
irregular sampling.
Practical guidance
For a network of \(n\) sites, retain on the order of \(n/2\) candidate eigenvectors at most and let forward selection prune from there. Each MEM consumes a degree of freedom, so selecting more than half of them risks fitting noise. With more eigenvectors than roughly half the site count, the basis has enough degrees of freedom to fit any per-site response and the spatial R-squared loses its meaning. The default threshold retained 50 eigenvectors for the 80 sites above, more than \(n/2 = 40\), and forward selection kept 9 of them.
| Method | Minimum input | Comfortable input |
|---|---|---|
| MEM partition | 20 sites | 50 or more sites |
References
Borcard, D. & Legendre, P. (2002). All-scale spatial analysis of ecological data by means of principal coordinates of neighbour matrices. Ecological Modelling, 153, 51-68.
Dray, S., Legendre, P. & Peres-Neto, P.R. (2006). Spatial modelling: a comprehensive framework for principal coordinate analysis of neighbour matrices (PCNM). Ecological Modelling, 196, 483-493.