Skip to content

Random Placement API

coleman

sars.coleman

coleman(abundance: DataFrame | ArrayLike, area: ArrayLike) -> ColemanFit

Compare site richness with Coleman's random placement model.

Under random placement every individual lands on a site with probability equal to the site's share of the total area, independently of the others. A species with n individuals is then absent from a site of relative area alpha with probability (1 - alpha)^n, which gives the expected richness S(alpha) = sum_i [1 - (1 - alpha)^n_i] and the variance sum_i (1 - alpha)^n_i - sum_i (1 - alpha)^(2 n_i) (Coleman 1981). The model has no parameters to estimate.

Parameters:

Name Type Description Default
abundance pd.DataFrame or 2-D array-like

Species-by-site abundance table: one row per species, one column per site, each value the number of individuals of that species counted at that site. This is the orientation R sars's coleman(data, area) takes. A table with sites in rows needs transposing first (df.T). Counts must be whole numbers, zero or more. Presence-absence data won't do: the model needs the numbers of individuals.

required
area array - like

Area of each site; all must be positive. A list or array is matched to abundance's columns by position. A pd.Series with its own labels, given with a DataFrame whose columns are labelled, is matched by label instead, whatever its order, and must hold exactly the column labels. A Series or DataFrame with the default 0, 1, ... labels counts as unlabelled and is matched by position.

required

Returns:

Type Description
ColemanFit

The per-site table (sites), the share of sites more than one standard deviation from the expected curve (fraction_outside) and the verdict (rejected: True when that share is over a third).

Raises:

Type Description
ValueError

If abundance isn't two-dimensional, holds a missing, negative or fractional value or text (numbers stored as strings included), or holds no individuals at all; if there are fewer than two sites; if area doesn't have one positive, finite value per site; or if a labelled area doesn't carry abundance's column labels.

Warns:

Type Description
UserWarning

If no value in abundance is above 1, which is what presence-absence data looks like.

Notes

Species absent from every site are dropped. They add nothing to the expected richness or to its variance ((1 - alpha)^0 = 1), so the result is the same with or without them; ColemanFit.n_species counts only the species that were seen.

A site counts as within one standard deviation when |observed - expected| <= sd.

The one-third rule is a convention, not a test with a known error rate. One standard deviation either side holds roughly two thirds of the spread, so about a third of the sites are expected outside it under random placement itself, and with few sites the rule often rejects data that were placed at random. Read rejected alongside the plot (plot_coleman) and the pattern of the sites that fall outside.

References

Coleman BD (1981). On random placement and species-area relations. Mathematical Biosciences 54, 191-215.

Coleman BD, Mares MA, Willig MR, Hsieh Y-H (1982). Randomness, area, and species richness. Ecology 63, 1121-1133.

Examples:

>>> import numpy as np
>>> import sars
>>> rng = np.random.default_rng(1)
>>> area = np.array([1.0, 2.0, 5.0, 10.0, 20.0, 50.0])
>>> totals = np.array([1, 2, 3, 5, 8, 13, 21, 34, 55, 89])
>>> counts = np.array([rng.multinomial(n, area / area.sum()) for n in totals])
>>> fit = sars.coleman(counts, area)
>>> fit.n_sites
6

ColemanFit

sars.ColemanFit dataclass

Coleman's random placement model compared with observed richness.

Attributes:

Name Type Description
sites DataFrame

One row per site, in the order given, indexed by site label (the abundance table's column labels, or 0, 1, ... for an array). Columns: area, relative_area (area over total area), observed (species present at the site), expected (richness under random placement), sd (its standard deviation) and within_sd (whether observed lies within one sd of expected, bounds included).

abundance Series

Total abundance of each species over all sites, indexed by species label (the abundance table's row labels, or row positions for an array). Species absent from every site are left out.

total_area float

Sum of the site areas.

Notes

Two fits are equal only if they are the same object: the tables they hold have no single true-or-false comparison.

fraction_outside property

fraction_outside: float

Share of the sites whose observed richness lies more than one standard deviation from the expected value.

n_sites property

n_sites: int

Number of sites.

n_species property

n_species: int

Number of species present at one site or more.

rejected property

rejected: bool

Whether random placement is rejected: True when more than a third of the sites lie more than one standard deviation from the expected curve.

This is the conventional rule, and a rough one: about a third of the sites are expected outside one standard deviation when placement is random, so with few sites it often rejects data that are.

predict

predict(area: ArrayLike) -> np.ndarray

Expected species richness of a site of the given area(s).

Parameters:

Name Type Description Default
area float or array - like

Area value(s), in the units of the site areas, between 0 and total_area.

required

Returns:

Type Description
ndarray

Expected richness under random placement, with the shape of area.

Raises:

Type Description
ValueError

If an area is negative, not finite, or larger than total_area.

predict_sd

predict_sd(area: ArrayLike) -> np.ndarray

Standard deviation of the richness of a site of the given area(s).

Parameters:

Name Type Description Default
area float or array - like

Area value(s), in the units of the site areas, between 0 and total_area.

required

Returns:

Type Description
ndarray

Standard deviation of richness under random placement, with the shape of area.

Raises:

Type Description
ValueError

If an area is negative, not finite, or larger than total_area.

placement_test

placement_test(n_sim: int = 999, rng: Generator | None = None) -> ColemanTest

A calibrated test of random placement, by simulation.

The one-third rule (rejected) has no stated error rate, and with few sites it often rejects data placed at random. This test simulates random placement itself: each species' individuals, as many as were counted, are scattered over the sites in proportion to their areas, n_sim times. The statistic is the sum over sites of the squared distance of observed from expected richness, in standard deviations, and the p value is the share of simulations at least as far from the curve as the data, counting the data as one of them.

Parameters:

Name Type Description Default
n_sim int

Number of simulated placements. The smallest p value possible is 1 / (n_sim + 1).

999
rng Generator or int

Random number generator, or a seed for one, for a reproducible result.

None

Returns:

Type Description
ColemanTest

The statistic, its p value, and how often the one-third rule rejected the simulated random placements: that rule's false rejection rate for these species totals and site areas.

Raises:

Type Description
ValueError

If n_sim is not a positive whole number.

ColemanTest

sars.ColemanTest dataclass

A simulation test of Coleman's random placement model (ColemanFit.placement_test).

Attributes:

Name Type Description
statistic float

Sum over sites of the squared distance of observed from expected richness, in standard deviations.

p_value float

Share of the simulated random placements, the data counted among them, whose statistic is at least the data's. A small value is evidence against random placement.

n_sim int

Number of simulated placements.

rule_rejection_rate float

Share of the simulated random placements that the one-third rule rejects: how often that rule would reject random placement for these species totals and site areas when it holds.