Comparing curves between groups and regions
Source:vignettes/articles/comparing-curves.Rmd
comparing-curves.RmdMany questions about accumulation are comparisons: do alien species
accumulate faster than native ones, does one region saturate earlier
than another, does the spatial curve differ from the random one. This
article tests two curves with compare(), splits a survey
into species groups that share the same site orders, runs one curve per
country with support, and ends with what c()
does for each result class. Installation is in Get started; the region examples need the
suggested packages sf and
areaOfEffect, and
the figures need ggplot2.
Test the difference between two curves with
compare().Split the species into groups along shared orders with
groups.Restrict the curves to a region, or one per country, with
support.Combine result objects with
c().
Testing two curves
The survey has 80 sites and 40 species, each species occupying a patch around its own centre. The first 20 species are given wide patches and the last 20 narrow ones, so the two halves accumulate differently.
set.seed(42)
n_sites <- 80
coords <- data.frame(x = runif(n_sites, 0, 100), y = runif(n_sites, 0, 100))
width <- rep(c(0.0005, 0.004), each = 20)
species <- sapply(width, function(a) {
cx <- runif(1, 20, 80)
cy <- runif(1, 20, 80)
rbinom(n_sites, 1, exp(-a * ((coords$x - cx)^2 + (coords$y - cy)^2)))
})
colnames(species) <- c(paste0("wide", 1:20), paste0("narrow", 1:20))compare() takes two spacc objects over the
same number of sites. It computes the area under each mean curve and
tests the difference by permutation: the seed curves of both objects are
pooled, split at random into two sets of the original sizes, and the
difference in area is recomputed n_perm times. The p-value
is the share of differences at least as large in absolute value as the
observed one, among the permuted differences and the observed one
itself, so its smallest value is 1 / (n_perm + 1) (Phipson
& Smyth 2010).
wide <- spacc(species[, 1:20], coords, n_seeds = 50, seed = 1,
progress = FALSE)
narrow <- spacc(species[, 21:40], coords, n_seeds = 50, seed = 1,
progress = FALSE)
set.seed(3)
comp <- compare(wide, narrow, n_perm = 999)
comp
#> Comparison: wide vs narrow
#> ----------------------------------------
#> Method: permutation (n=999)
#> AUC difference: 332.5 (p = 0.001**)
#> Saturation: wide at 8 sites, narrow at 44 sites
#>
#> wide saturates faster.The printout gives the area difference, the p-value and the saturation point of each curve, the first step at which the mean reaches 90% of its final value. Wide-ranging species are found in the first few sites, narrow-ranging ones keep appearing as the survey spreads.
Both groups here have 20 species, so the areas compare like with
like. When the groups differ in size, normalize = TRUE
divides each seed curve by its final value first, which compares the
shape of accumulation (how fast each group approaches its own total)
instead of the counts.
set.seed(3)
compare(wide, narrow, normalize = TRUE, n_perm = 999)
#> Comparison: wide vs narrow
#> ----------------------------------------
#> Method: permutation (n=999)
#> Normalized: yes (shape comparison)
#> AUC difference: 16.6 (p = 0.001**)
#> Saturation: wide at 8 sites, narrow at 44 sites
#>
#> wide saturates faster.The permutation test pools the seeds, so it asks whether the two sets
of curves come from the same distribution, spread included.
method = "bootstrap" tests the area difference alone and
lets the two objects differ in how much their seeds vary. It is the
bootstrap test of equal means of Efron & Tibshirani (1993, Algorithm
16.2), applied to the area under each seed curve: the areas of each
object are shifted to the mean of all areas, so that the null hypothesis
holds, and resampled with replacement within each object. The statistic
is the area difference divided by its standard error. Efron &
Tibshirani count the replicates at or above the observed statistic, a
one-sided test; compare() counts them in absolute value and
computes the p-value as for the permutation test, so both tests are
two-sided. The printout adds the 95% percentile bootstrap interval of
the area difference.
set.seed(3)
compare(wide, narrow, method = "bootstrap", n_perm = 999)
#> Comparison: wide vs narrow
#> ----------------------------------------
#> Method: bootstrap (n=999)
#> AUC difference: 332.5 (p = 0.001**)
#> 95% bootstrap interval: 292.8 to 372.4
#> Saturation: wide at 8 sites, narrow at 44 sites
#>
#> wide saturates faster.method = "auc" reports the difference without a test.
as.data.frame() returns the comparison as one row, and
plot() overlays the two curves.
as.data.frame(comp)
#> comparison auc_x auc_y auc_diff saturation_x saturation_y
#> 1 wide vs narrow 1557.5 1225.02 332.48 8 44
#> saturation_diff p_value method
#> 1 -36 0.001 permutation
plot(comp)The seeds are the unit of the permutation, so the test asks whether the two sets of seed curves could come from the same process. More seeds give a more precise mean curve and a smaller p-value for the same difference in shape; report the area difference alongside the p-value.
Species groups along shared orders
Splitting the species table by hand, as above, works because both
calls used seed = 1 and so drew the same focal points. The
groups argument does this in one call: it takes one label
per species column and accumulates every group along the same site
orders.
range_type <- rep(c("wide", "narrow"), each = 20)
grp <- spacc(species, coords, n_seeds = 50, groups = range_type,
seed = 1, progress = FALSE)
grp
#> spacc: 2 groups (knn)
#> wide: 80 sites, 20 species
#> narrow: 80 sites, 20 speciesThe result is a grouped spacc object:
curves is a list with one matrix per group.
summary() and predict() return one entry per
group, and as.data.frame() stacks the groups with a
group column.
s <- summary(grp)
sapply(s, function(g) c(saturation = g$saturation_point,
cv_mid = round(g$cv_midpoint, 1)))
#> wide narrow
#> saturation 8 44.0
#> cv_mid 0 12.3
predict(grp, n = c(10, 40))
#> n mean lower upper group
#> 1 10 18.92 16.000 20.000 wide
#> 2 40 20.00 20.000 20.000 wide
#> 3 10 6.92 0.225 14.775 narrow
#> 4 40 17.24 14.000 20.000 narrow
plot(grp)Because the groups share their orders, the two curves differ only
through the species, never through where the survey started.
compare() needs two ungrouped objects; the separate calls
with a shared seed in Testing
two curves give the same curves as the groups.
all.equal(grp$curves$wide, wide$curves)
#> [1] TRUECurves per region
support restricts the starting points to sites inside a
region. Sites outside it but close by, the halo, can still enter the
curve as it grows; include_halo = FALSE drops them, which
treats the region boundary as a hard border. The core and halo are
computed by areaOfEffect. The sites below are longitude and latitude
points along the border between Austria and Germany.
set.seed(5)
n_r <- 120
lonlat <- data.frame(x = runif(n_r, 12.2, 13.4), y = runif(n_r, 47.5, 48.2))
sp_r <- matrix(rbinom(n_r * 30, 1, 0.2), n_r, 30)support = "auto" finds the countries the sites fall in
and returns one curve per country, as a grouped object. A single country
name or ISO code restricts the curve to that country.
per_country <- spacc(sp_r, lonlat, distance = "haversine", n_seeds = 30,
support = "auto", seed = 1, progress = FALSE)
per_country
#> spacc: 2 groups (knn)
#> Germany: 120 sites, 30 species
#> Austria: 120 sites, 30 species
austria <- spacc(sp_r, lonlat, distance = "haversine", n_seeds = 30,
support = "AT", seed = 1, progress = FALSE)
austria$support[c("n_core", "n_halo")]
#> $n_core
#> [1] 65
#>
#> $n_halo
#> [1] 55Of the 120 sites, the core sites inside Austria are the only starting points; the halo sites across the border in Germany enter the curves as they grow.
support also takes an sf polygon of a study
area, a vector of countries, or a precomputed result of
areaOfEffect::aoe(). Species groups and regions combine:
with both groups and support = "auto", the
groups are named country.group.
Combining objects with c()
c() on spacc objects builds a grouped
object from named curves, the way the comparison between spatial and
random order in Get started overlays them. The curves may come from
different methods or species tables.
rnd <- spacc(species, coords, method = "random", n_seeds = 50, seed = 1,
progress = FALSE)
knn <- spacc(species, coords, n_seeds = 50, seed = 1, progress = FALSE)
c(spatial = knn, random = rnd)
#> spacc: 2 groups (knn)
#> spatial: 80 sites, 40 species
#> random: 80 sites, 40 speciesOn Hill-number, beta-diversity and coverage objects, c()
does something else: it stacks the seeds of runs over the same sites
into one object with more seeds. This adds seeds to a finished run
without repeating it.
h1 <- spaccHill(species, coords, n_seeds = 20, seed = 1, progress = FALSE)
h2 <- spaccHill(species, coords, n_seeds = 30, seed = 2, progress = FALSE)
c(h1, h2)$n_seeds
#> [1] 50Curves computed elsewhere, such as vegan::specaccum()
results or Rarefy curves, enter through as_spacc() and can
then be combined and compared like any other spacc object.
The ordering rules behind each curve are compared in Choosing an accumulation order.
References
Efron, B., & Tibshirani, R. J. (1993). An Introduction to the Bootstrap. Chapman & Hall.
Phipson, B., & Smyth, G. K. (2010). Permutation p-values should never be zero: calculating exact p-values when permutations are randomly drawn. Statistical Applications in Genetics and Molecular Biology, 9, Article 39.