Accumulating Hill numbers and partitioning diversity
Source:vignettes/articles/hill-numbers.Rmd
hill-numbers.RmdRichness counts a species seen once and a species seen ten thousand times the same, so two regions with the same species list score the same even when one is dominated by a single species. Hill numbers (Hill 1973, Jost 2007, Chao et al. 2014) put richness, Shannon diversity and Simpson diversity on one scale, the effective number of species, indexed by an order \(q\) that sets how much weight rare species receive. This article accumulates Hill numbers along spatial orders, splits the diversity of all sites into a local and a turnover part, and reads dominance from evenness and diversity profiles. It ends with an index you define yourself. Installation is covered in Get started; no further packages are needed.
Accumulate Hill numbers of orders 0, 1 and 2 along spatial orders with
spaccHill().Partition regional diversity into alpha and beta with
diversityPartition().Profile dominance across orders with
evenness()anddiversityProfile().Track any other index along the same orders with
spaccDiversity().
Example
Simulating clustered communities
The example uses 60 sites in a 100 by 100 plane and 30 species. Each species has a random centre, and its expected count at a site falls off as a Gaussian kernel of the squared distance from that centre, with a peak of 5 individuals. Counts are drawn from a Poisson distribution with that expectation, so species are common near their centre and absent far from it.
Simulation code
set.seed(123)
n_sites <- 60
n_species <- 30
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)
lambda <- 5 * exp(-0.001 * ((coords$x - cx)^2 + (coords$y - cy)^2))
species[, sp] <- rpois(n_sites, lambda)
}
colnames(species) <- paste0("sp", seq_len(n_species))The decay rate 0.001 sets the range over which a species stays
common. At a squared distance of 1000, about 32 units from the centre,
the expected count has dropped to \(5 e^{-1}
\approx 1.8\); by 60 units it is close to zero. The mean
nearest-neighbour spacing of 60 sites in this plane is about 6 units,
well inside each species’ range, so neighbouring sites share many
species. species is a sites-by-species matrix of integer
counts and coords holds the site positions in columns
x and y.
dim(species)
#> [1] 60 30
species[1:4, 1:8]
#> sp1 sp2 sp3 sp4 sp5 sp6 sp7 sp8
#> [1,] 0 0 4 0 3 0 1 3
#> [2,] 1 2 0 0 0 3 1 0
#> [3,] 2 1 5 1 7 0 1 3
#> [4,] 6 3 0 0 0 3 0 0
mean(species > 0) # share of nonzero cells
#> [1] 0.5022222
range(colSums(species)) # individuals per species, all sites
#> [1] 46 106Accumulating Hill numbers
spaccHill() orders the
sites by nearest-neighbour distance from a random starting site, pools
their counts step by step, and computes the Hill number of the pooled
community at each step for every requested order. Each of the
n_seeds starting sites gives one curve; the printed and
plotted values are the mean over those curves.
hill <- spaccHill(species, coords, q = c(0, 1, 2),
n_seeds = 20, progress = FALSE)
hill
#> spacc Hill numbers: 60 sites, 30 species, 20 seeds
#> Orders (q): 0, 1, 2
#> Method: knn
plot(hill)The \(q = 0\) curve is richness. It reaches all 30 simulated species after about 30 sites, while the \(q = 1\) and \(q = 2\) curves, the effective numbers of common and of dominant species, keep rising until every site is pooled. A new site adds a species to the \(q = 0\) count at once, but moves \({}^{1}D\) and \({}^{2}D\) only by changing the relative abundances. The first sites of a walk are dominated by the few species centred nearby; each further site adds individuals of species centred elsewhere, so the pooled counts become more even and the higher orders keep growing after the species list is complete. The gap between the curves at a given number of sites is the dominance at that spatial scale.
as.data.frame() returns the mean and the 2.5% and 97.5%
quantiles of the seed curves at each step, stacked by order.
hill_df <- as.data.frame(hill)
head(hill_df, 3)
#> q sites mean lower upper sd
#> 1 0 1 17.25 9.95 24.000 4.115120
#> 2 0 2 20.85 15.00 27.000 3.787445
#> 3 0 3 22.50 16.95 28.525 3.220003
hill_df[hill_df$sites == max(hill_df$sites),
c("q", "mean", "lower", "upper")]
#> q mean lower upper
#> 60 0 30.00000 30.00000 30.00000
#> 120 1 29.23513 29.23513 29.23513
#> 180 2 28.56626 28.56626 28.56626The last rows give the effective diversity of all 60 sites pooled at
each order. \({}^{1}D\) and \({}^{2}D\) are close to the 30 species
because the pooled totals of the species are similar, between 46 and 106
individuals. summary() returns the same table with an
adjustable ci_level, here a 90% band.
hs <- summary(hill, ci_level = 0.90)
tail(hs, 3)
#> q sites mean lower upper
#> 178 2 58 28.48275 28.36059 28.50306
#> 179 2 59 28.52547 28.44862 28.54513
#> 180 2 60 28.56626 28.56626 28.56626Reading the orders together shows what further sampling adds. Here the \(q = 0\) curve has flattened while the \(q = 2\) curve still rises, so more sites no longer add species but still change how evenly the individuals are spread. The reverse, a \(q = 0\) curve that rises while \(q = 2\) has flattened, means sampling adds rare species without changing which species make up most of the individuals.
Reading the uncertainty band
The band around each curve is the spread of the seed curves; no parametric error model is involved. The nearest-neighbour walk depends on where it starts, so each starting site gives a different curve. The band is widest early, where the starting site decides which species are seen, and narrows as every walk approaches the same pooled community.
ci0 <- hill_df[hill_df$q == 0, ]
at <- c(5, 20, 40, 60)
data.frame(sites = at, width = ci0$upper[at] - ci0$lower[at])
#> sites width
#> 1 5 10.525
#> 2 20 2.000
#> 3 40 0.000
#> 4 60 0.000From 40 sites on every walk has found all 30 species, so the width of the richness band is zero. A band that stays wide at high site counts says the order of accumulation still matters near saturation, which happens when the region has large-scale structure that some walks cross early and others late. Too few seeds can produce the same pattern; the guidance below gives seed numbers.
Partitioning alpha, beta and gamma
The accumulation curve follows the pooled community as it grows. The multiplicative partition looks at the fixed set of all sites. alphaDiversity() returns the Hill numbers of each site, gammaDiversity() those of all sites pooled, and diversityPartition() splits regional diversity as gamma = alpha × beta.
alpha <- alphaDiversity(species, q = c(0, 1, 2))
colMeans(alpha)
#> q0 q1 q2
#> 15.06667 12.39927 10.61170
gamma <- gammaDiversity(species, q = c(0, 1, 2))
gamma
#> q0 q1 q2
#> 30.00000 29.23513 28.56626
partition <- diversityPartition(species, q = c(0, 1, 2))
partition
#> Alpha-Beta-Gamma Diversity Partitioning
#> 60 sites, 30 species
#>
#> q alpha beta gamma
#> 0 15.07 1.99 30.00
#> 1 11.42 2.57 29.41
#> 2 8.81 3.27 28.85
#>
#> Interpretation:
#> Alpha = mean effective species per site
#> Beta = effective number of communities (1 to n_sites)
#> Gamma = regional effective species (gamma = alpha x beta)Alpha is about half of gamma at \(q =
0\) because each site holds only the species centred nearby.
Beta, the ratio of the two, is the effective number of distinct
communities among the 60 sites. The partition’s alpha is a power mean of
the per-site values (defined below), so for \(q > 0\) it lies below the arithmetic
means that colMeans() printed. Its gamma gives every site
the same weight, as its alpha does: it is the Hill number of the mean of
the sites’ relative abundances. gammaDiversity() pools the
counts, which weights each site by its number of individuals, so the two
differ a little for \(q > 0\), 29.41
against 29.24 at \(q = 1\).
summary() adds a normalised beta, \(({}^{q}D_{\beta} - 1)/(n - 1)\) for \(n\) sites, which is 0 for one homogeneous
community and 1 when no two sites share a species.
summary(partition)
#> q alpha beta gamma n_sites beta_normalized
#> q0 0 15.066667 1.991150 30.00000 60 0.01679916
#> q1 1 11.422875 2.574456 29.40769 60 0.02668569
#> q2 2 8.813323 3.273934 28.85424 60 0.03854126Beta rises with \(q\) here, from 1.99 at \(q = 0\) to 3.27 at \(q = 2\). Each site is dominated by the species centred nearby, so per-site diversity falls steeply with \(q\), while the pooled community is nearly even and its diversity hardly falls; the evenness values in the next section show both. Beta at \(q = 2\) therefore counts communities that differ in which species dominate, even where they share most of their species list. In data where the rare species are locally restricted and the dominant ones widespread, beta falls with \(q\) instead.
Alpha and gamma weight every site equally by default.
weights = "proportional" weights sites by their total
count, which makes gamma the Hill number of the pooled counts, and a
numeric vector sets custom weights.
Profiling dominance and evenness
evenness() divides the Hill number of each order by richness, \(E_q = {}^{q}D / {}^{0}D\). The value lies between 0 and 1: 1 for a community in which every species has the same count, small values when a few species hold most individuals.
ev <- evenness(species, q = seq(0.1, 3, by = 0.1))
ev
#> Evenness (hill): 60 sites, 30 species
#> q range: [0.1, 3.0]
#> Mean evenness: E_1 = 0.823, E_2 = 0.704
#> Regional evenness: E_1 = 0.975, E_2 = 0.952
plot(ev)The mean per-site evenness at \(q =
2\) is 0.70, the evenness of the pooled region 0.95: single sites
are dominated by a few species, the region is not. The profile falls
with \(q\) because higher orders divide
a dominance-weighted diversity by full richness, and a steep drop means
strong dominance. For a single number, type = "pielou"
gives Pielou’s \(J\) (an evenness at
\(q = 1\)) and
type = "simpson" gives Simpson evenness (at \(q = 2\)).
evenness(species, type = "pielou")
#> Evenness (pielou): 60 sites, 30 species
#> Mean Pielou's J: 0.923
#> Regional J: 0.992diversityProfile() evaluates \({}^{q}D\) over a grid of orders, for each site and for all sites pooled. It shows the whole decline from richness to inverse Simpson instead of three points.
prof <- diversityProfile(species, q = seq(0, 3, by = 0.2))
prof
#> Diversity profile: 60 sites, 30 species
#> q range: [0.0, 3.0] (16 values)
#> Per-site: mean D_0 = 15.1, mean D_1 = 12.4, mean D_2 = 10.6
#> Regional: D_0 = 30.0, D_1 = 29.2, D_2 = 28.6
plot(prof)The solid line is the mean per-site profile and the dashed line the regional profile. Their ratio at \(q = 0\) is the partition’s beta. At higher orders it is smaller, because the partition divides by a power mean that lies below the arithmetic mean plotted here. Two regions with the same richness, the value at \(q = 0\), can have different profiles, and the one whose profile drops faster is more dominated. Comparing whole profiles avoids committing to one value of \(q\) before looking at the data.
Accumulating a custom index
spaccDiversity() accumulates an index that spacc does not compute itself. At each step it passes the cumulative community to your function, which returns one number. Here the function computes Shannon entropy.
shannon <- function(comm) {
p <- comm[comm > 0] / sum(comm)
-sum(p * log(p))
}
div <- spaccDiversity(species, coords, shannon,
method = "knn", n_seeds = 20, progress = FALSE)
plot(div, ylab = "Cumulative Shannon entropy")The function receives a named vector of cumulative counts, or 0/1
incidences with incidence = TRUE, plus any extra arguments
given through .... The orders are "knn",
"kncn", "nn_walk", "random" and
"collector". The result has class spacc, so
summary(), as.data.frame() and
predict() apply; the plot uses a neutral axis label unless
you set ylab. Because the index is an R function called at
every step, it runs slower than the compiled metrics, so keep it for
indices that are not built in.
Standardising by coverage
A fixed number of sites can sample very different shares of the community. spaccHillCoverage() records sample coverage next to the Hill numbers, so the curves can be read at equal completeness instead of at equal site count.
hc <- spaccHillCoverage(species, coords, q = c(0, 1, 2),
n_seeds = 15, progress = FALSE)
plot(hc, xaxis = "coverage")Standardising by sample coverage covers the coverage estimators and comparisons at a fixed coverage.
Hill numbers and the multiplicative partition
For a community with species \(i = 1, \dots, S\) and counts \(n_i\), let \(N = \sum_i n_i\) be the total count and \(p_i = n_i / N\) the relative abundance of species \(i\). The Hill number of order \(q\) is
\[ {}^{q}D = \left( \sum_{i=1}^{S} p_i^{q} \right)^{1/(1-q)}. \]
It is the number of equally abundant species that would give the same value. At \(q = 0\) every present species contributes \(p_i^0 = 1\), so \({}^{0}D = S\), richness. At \(q = 1\) the exponent \(1/(1-q)\) diverges, but the limit exists and is the exponential of Shannon entropy,
\[ {}^{1}D = \exp\!\left( -\sum_{i=1}^{S} p_i \log p_i \right), \]
which weights each species by its abundance and reads as the effective number of common species. At \(q = 2\) the value is the inverse Simpson concentration, \({}^{2}D = 1 / \sum_i p_i^2\), the effective number of dominant species. Hill numbers do not increase with \(q\): \({}^{q}D \ge {}^{q'}D\) whenever \(q \le q'\), because a higher order shifts weight from the rare species to the most abundant ones. A wide gap between \({}^{0}D\) and \({}^{2}D\) means a few species hold most individuals.
Following Jost (2007), regional (gamma) diversity is the product of local (alpha) diversity and turnover (beta):
\[ {}^{q}D_{\gamma} = {}^{q}D_{\alpha} \times {}^{q}D_{\beta}. \]
\({}^{q}D_{\gamma}\) is the Hill number of the regional community, the weighted mean of the sites’ relative abundances (equal weights by default; the pooled counts when sites are weighted by their totals), \({}^{q}D_{\alpha}\) the effective number of species in an average site, and \({}^{q}D_{\beta} = {}^{q}D_{\gamma} / {}^{q}D_{\alpha}\) the effective number of completely distinct communities. Beta ranges from 1, when all sites have the same composition, to the number of sites, when no two sites share a species. Alpha is a generalised (power) mean of the per-site values, which with equal site weights makes the decomposition exact at every \(q\). Beta is then independent of alpha in Jost’s sense, so a region can combine high local diversity with low turnover or the reverse.
Practical guidance
Which \(q\) to report. Report \(q = 0\), 1 and 2 together. The three values show richness, common-species diversity and dominant-species diversity in one row, and their gaps show dominance. If one number is required, \(q = 1\) weights species exactly by abundance, and with unequal site weights it is the only order at which Jost (2007) shows alpha and beta to be independent. Use \(q = 0\) when rare species are the conservation target and \(q = 2\) when the question is dominance or invasion.
Abundance or incidence. Every order above 0 needs
counts. Presence/absence data collapse all orders to richness and drop
the dominance information the Hill framework exists to capture, so feed
spaccHill() counts, cover or biomass.
Number of seeds. More seeds give a smoother,
narrower band. Use at least 50 seeds for reporting and 100 or more when
the band itself is the result. This article uses 15 to 20 to keep the
build fast, which is too few for publication. The seed sites are drawn
at random, so set seed for a reproducible result.
Number of sites. Beta in the partition is unstable below about 5 sites, because alpha is then a mean over very few values. Aim for at least 20 sites for any diversity accumulation and 30 or more before reading turnover.
When not to use these. A wide band with few seeds is
a likely artefact; rerun with more seeds before reading it as spatial
structure. spaccDiversity() is slower than the compiled
functions, so use those for indices they already cover.
| Question | Function | Input | Result |
|---|---|---|---|
| How effective diversity grows with area | spaccHill() |
abundance | \({}^{q}D\) per order |
| How regional diversity splits into local and turnover parts | diversityPartition() |
abundance | alpha, beta, gamma |
| How dominated the communities are |
evenness(), diversityProfile()
|
abundance | \(E_q\), \({}^{q}D\) over \(q\) |
| How diversity compares at equal completeness | spaccHillCoverage() |
abundance | \({}^{q}D\) against coverage |
| An index spacc does not compute | spaccDiversity() |
any | your function |
Where next
Accumulating beta diversity tracks turnover and nestedness along the curve and the Hill-number beta at each step.
Accumulating phylogenetic and functional diversity replaces species counts by lineages and traits.
Standardising by sample coverage compares surveys of unequal effort at the same completeness.
References
- Chao, A., Gotelli, N.J., Hsieh, T.C., et al. (2014). Rarefaction and extrapolation with Hill numbers. Ecological Monographs, 84, 45-67.
- Hill, M.O. (1973). Diversity and evenness: a unifying notation and its consequences. Ecology, 54, 427-432.
- Jost, L. (2007). Partitioning diversity into independent alpha and beta components. Ecology, 88, 2427-2439.