Skip to content

Habitat Models API

sar_habitat

sars.sar_habitat

sar_habitat(data: DataFrame, mod_type: str = 'power_log', area_col: str = 'area', habitats_col: str = 'habitats', species_col: str = 'species', con: float | None = None, log_base: str | float = 'e') -> HabitatFit

Fit the habitat species-area models, as R sars's sar_habitat.

Richness may depend on the number of habitats on an island as well as on its area. Four models are compared:

  • choros (Triantis et al. 2003): richness depends on the product of area and habitats, K = A·H. Power form S = c·K^z.
  • jigsaw (Tjørve 2009): S = c·H^d·(A/H)^z, the habitats as pieces of area each with its own species.
  • Kallimanis (Kallimanis et al. 2008): the slope rises with habitat heterogeneity, S = c·A^(z + d·H).
  • the classical power (or logarithmic) curve on area alone.

Parameters:

Name Type Description Default
data DataFrame

One row per island.

required
mod_type str

"power_log" (the default, as in R): the power forms fitted as linear regressions on the logs (log S on log A and log H). "power": the power forms fitted by non-linear least squares. "logarithmic": the logarithmic (S = c + z·log A) forms.

'power_log'
area_col str

The columns holding area, the number of habitats (or another heterogeneity measure), and species richness.

'area'
habitats_col str

The columns holding area, the number of habitats (or another heterogeneity measure), and species richness.

'area'
species_col str

The columns holding area, the number of habitats (or another heterogeneity measure), and species richness.

'area'
con float

For the power_log form, a constant added to every richness value before taking logs when any island has no species (it must make every value positive). As in R, it is ignored (with a warning) otherwise.

None
log_base str or float

The logarithm: "e" (default), 2 or 10.

'e'

Returns:

Type Description
HabitatFit

Raises:

Type Description
ValueError

For an unknown mod_type or log_base, missing or non-finite values, or richness of zero in the power_log form without con.

References

Triantis KA, Mylonas M, Lika K, Vardinoyannis K (2003). A model for the species-area-habitat relationship. Journal of Biogeography 30, 19-27. Tjørve E (2009). Shapes and functions of species-area curves (II). Journal of Biogeography 36, 1435-1445. Kallimanis AS et al. (2008). How does habitat diversity affect the species-area relationship? Global Ecology and Biogeography 17, 532-538.

Examples:

>>> fit = sars.sar_habitat(sars.load_habitat())
>>> fit.summary[["model", "z", "d", "AICc"]]

HabitatFit

sars.HabitatFit dataclass

The habitat species-area models fitted to one dataset.

Attributes:

Name Type Description
mod_type str

"power", "logarithmic" or "power_log" (see sar_habitat).

summary DataFrame

One row per model, best first by AICc: its parameters c, z and d (NaN where the model has none), R2, adjR2, AIC, BIC, AICc and rss. In the power_log form c is the intercept on the log scale, and the jigsaw model's d is the coefficient of log habitats plus z (so d - z is that coefficient, as R reports it).

params dict[str, dict[str, float]]

Each fitted model's parameters.

data DataFrame

The data: columns area, habitats and species.

log_base str

The logarithm used by the logarithmic and power_log forms.

con float or None

The constant added to richness before taking logs (power_log form, data with zeros).

not_fitted list[str]

Models that couldn't be fitted.

predict

predict(area: float | ArrayLike, habitats: float | ArrayLike, model: str | None = None) -> np.ndarray

Richness predicted by a model (the best by AICc by default) at given areas and numbers of habitats. In the power_log form the prediction is back-transformed from the log scale (minus con).

sar_countryside

sars.sar_countryside

sar_countryside(data: DataFrame, habitats: list[str], species: list[str], mod_type: str = 'power', grid_start: str = 'partial', z_lower: float = 0.0, n_starts: int | None = 40) -> CountrysideFit

Fit the countryside species-area model, as R sars's sar_countryside.

In a landscape of several habitats, species differ in which habitats they use. The countryside SAR (Pereira & Daily 2006; Proença & Pereira 2013) splits the species into groups by habitat affinity (forest species, farmland species, ...) and fits each group's richness as a curve of a weighted sum of the habitat areas, the weights (h) saying how much each habitat counts for that group. The fit then predicts richness for any mix of habitats, such as a landscape after land-use change.

Parameters:

Name Type Description Default
data DataFrame

One row per site.

required
habitats list[str]

The columns holding each habitat's area in the site.

required
species list[str]

The columns holding each species group's richness in the site.

required
mod_type str

"power" (default), S = (Σ h_i·A_i)^z, or "logarithmic", S = z·log(Σ h_i·A_i).

'power'
grid_start str

The starting values tried for each group, as in R: "partial" (the default), "exhaustive" (more, slower) or "none" (few).

'partial'
z_lower float

The smallest z allowed (0 by default). The h's are at least 0.

0.0
n_starts int or None

How many of the grid's starting values to fit from: the n_starts whose curves are closest to the data before fitting, plus the closest for each habitat as the most weighted (40 by default). None fits from every one, as R does: on the bundled data the two give the same fits, and the full grid takes 50 to 70 times as long.

40

Returns:

Type Description
CountrysideFit

Raises:

Type Description
ValueError

For unknown columns or arguments, non-finite values, or a site with no area under the logarithmic model.

Warns:

Type Description
UserWarning

If a group's model couldn't be fitted, or a site has no area.

References

Pereira HM, Daily GC (2006). Modeling biodiversity dynamics in countryside landscapes. Ecology 87, 1877-1885. Proença V, Pereira HM (2013). Species-area models to assess biodiversity change in multi-habitat landscapes: the importance of species habitat affinity. Basic and Applied Ecology 14, 102-114.

Examples:

>>> cs = sars.load_countryside()
>>> fit = sars.sar_countryside(
...     cs, habitats=["Area_AG", "Area_SH", "Area_QF"],
...     species=["Spcs_AG", "Spcs_SH", "Spcs_QF", "Spcs_UB"])
>>> fit.affinity

CountrysideFit

sars.CountrysideFit dataclass

The countryside species-area model fitted to one dataset.

Each group of species (by habitat affinity) has its own curve of the habitat areas: S = (h_1·A_1 + ... + h_k·A_k)^z in the power form, or S = z·log(h_1·A_1 + ... + h_k·A_k) in the logarithmic form. The h's say how much each habitat counts for that group.

Attributes:

Name Type Description
mod_type str

"power" or "logarithmic".

habitats list[str]

The habitat (area) columns, in order.

groups list[str]

The species groups (richness columns) that could be fitted.

params DataFrame

One row per group: h_<habitat> for each habitat, z, rss and the group's AIC.

affinity DataFrame

One row per group: each h divided by the group's largest, so 1 marks the habitat the group depends on most and values near 0 habitats it barely uses.

c Series

Each group's c: (largest h)^z (power) or z·log(largest h) (logarithmic), the richness a unit area of its best habitat holds.

predicted_total ndarray

The model's total richness at each site (the sum over groups); NaN if any group couldn't be fitted.

rss dict[str, float]

The total richness's RSS over every site under the countryside model ("countryside") and under a single power (or logarithmic) curve of total area ("power" or "logarithmic"); empty if any group couldn't be fitted.

data DataFrame

The data's habitat area and richness columns.

not_fitted list[str]

Groups whose model couldn't be fitted.

predict

predict(areas: ArrayLike) -> pd.DataFrame

Each group's richness, and the total, for given habitat areas.

Parameters:

Name Type Description Default
areas array - like

One value per habitat, in the order of habitats, or a 2-D array with one row per site.

required

Returns:

Type Description
DataFrame

One row per site: a column per group and total (NaN if any group couldn't be fitted).