Skip to content

General Dynamic Model API

sar_gdm

sars.sar_gdm

sar_gdm(data: DataFrame, model: str = 'loga', mod_sel: bool = False, area_col: str = 'area', species_col: str = 'species', time_col: str = 'time') -> GDMFit | GDMMultiFit

Fit the general dynamic model of oceanic island biogeography (GDM).

The GDM (Whittaker, Triantis & Ladle 2008) describes island richness as a function of island area plus a hump-shaped function of island age: time and time squared, with a negative coefficient on time squared when richness rises and then falls over an island's life. model chooses how area (and time) enter, with S richness and T island age:

  • "loga": S = Int + A*log(area) + Ti*T + Ti2*T^2, the "ATT²" model of Whittaker et al. (R sars's "loga" and "ATT2")
  • "linear": S = Int + A*area + Ti*T + Ti2*T^2
  • "power_area": S = exp(Int + A*log(area) + Ti*T + Ti2*T^2), a power law of area (area^A) times a hump in time
  • "power_area_time": S = exp(Int + A*log(area) + Ti*log(T) + Ti2*log(T)^2), with time logged as well
  • "all": all four, with a table comparing them

These are the forms R sars's gdm fits. The first two are fitted by ordinary least squares. The power forms are fitted by nonlinear least squares on richness itself (not by regressing log richness), so every form has the same response and their AIC values are comparable.

With mod_sel=True the GDM is also compared with three simpler models of the same form: area and time (no time squared), area only, and an intercept only. A GDM that doesn't beat "A + T" shows no evidence of a hump in island age.

Parameters:

Name Type Description Default
data DataFrame

One row per island, with numeric columns for area, species richness and island age.

required
model str

"loga" (the default), "linear", "power_area", "power_area_time" or "all".

'loga'
mod_sel bool

Also fit the three simpler candidates and rank all four by AICc, in the fit's comparison table (the fits themselves are in candidates). With model="all" each form gets its own table.

False
area_col str

Names of the columns holding area, richness and island age.

'area'
species_col str

Names of the columns holding area, richness and island age.

'area'
time_col str

Names of the columns holding area, richness and island age.

'area'

Returns:

Type Description
GDMFit or GDMMultiFit

A GDMFit for a single form; for model="all", a GDMMultiFit holding the GDMFit of every form and a table ranking them.

Raises:

Type Description
TypeError

If data is not a DataFrame.

KeyError

If a named column is missing.

ValueError

If model is unknown; a column is not numeric (strings and booleans are not converted); there are fewer than 5 islands; a value is missing, infinite or out of range (area must be positive, richness and time non-negative, and time positive for "power_area_time"); or the area and time terms are collinear (fewer than 3 different ages, for one).

Warns:

Type Description
UserWarning

If the solver did not converge for a power form's GDM or, with mod_sel=True, for one of its simpler candidates. The warning names them. Such a fit has converged=False, and is listed last in the comparison table with no deltas and no weight, since its criteria are not those of a least-squares fit.

Notes

Every coefficient and the residual variance count as parameters, so the GDM has k = 5 and AICc is finite only for 7 or more islands. In the power forms the simpler candidates are power forms too: "A" is the power law exp(Int + A*log(area)) and "Intercept" is exp(Int), the mean richness, with Int on the log scale like the others.

The fit doesn't depend on the units of area or time, except through the coefficients that carry those units.

The normal likelihood grows without bound as the residuals go to zero. So that a fit passing through every island still has finite criteria, its residual sum of squares is not taken below the rounding error of the data (machine epsilon times the sum of squared richness) when AIC, AICc and BIC are computed. Exact fits then tie on the likelihood and are ranked by their number of parameters. r_squared, rse and the standard errors use the residuals as they are.

References

Whittaker RJ, Triantis KA, Ladle RJ (2008). A general dynamic theory of oceanic island biogeography. Journal of Biogeography 35, 977-994.

Examples:

>>> import sars
>>> islands = sars.load_galap()
>>> islands["time"] = [4, 1, 13, 16, 15, 2, 6, 4, 5, 11, 3, 9, 8, 10, 12, 7]
>>> fit = sars.sar_gdm(islands, model="loga", mod_sel=True)
>>> round(fit.params["Ti2"], 3)
-2.837
>>> fit.comparison["model"].tolist()
['A', 'GDM', 'A + T', 'Intercept']

GDMFit

sars.GDMFit dataclass

Result of fitting the general dynamic model with one area form.

Attributes:

Name Type Description
model str

The form fitted: "loga", "linear", "power_area" or "power_area_time".

terms str

Which terms the fit has: "GDM" (area, time and time squared), or one of the simpler candidates "A + T", "A" and "Intercept" (the fits in candidates).

params dict[str, float]

Coefficients, named as in R sars: Int (intercept), A (area), Ti (time) and Ti2 (time squared). A simpler candidate has only its own. In the power forms every coefficient, Int included, is on the log scale: richness is the exponential of the linear predictor.

std_errors dict[str, float]

Standard error of each coefficient, from the residual variance and the model's gradient at the fit (exact for the forms that are linear in their parameters, the usual large-sample approximation for the power forms).

p_values dict[str, float]

Two-sided p value of each coefficient's t statistic (coefficient over standard error, on n minus the number of coefficients degrees of freedom).

fitted ndarray

Fitted richness for each row of data.

r_squared float

1 - RSS / total sum of squares, on richness itself for every form.

rse float

Residual standard error: sqrt(RSS / (n - number of coefficients)).

aic, aicc, bic float

Information criteria from the normal likelihood, counting each coefficient and the residual variance as parameters (5 for the GDM). AICc is infinite when n is not above that count plus one. A fit that passes through every island has no residual variance to estimate; its criteria are computed as if its residuals were at the rounding error of the data (see the notes of sar_gdm).

n int

Number of islands.

converged bool

Whether the solver converged. Always True for "loga" and "linear", which have a closed-form solution.

data DataFrame

The data fitted (columns: area, species, time).

comparison DataFrame | None

With mod_sel=True, the GDM and its three simpler candidates ranked by AICc: columns model, k (number of parameters), RSE, R2, AIC, AICc, BIC, delta_AIC, delta_AICc, weight_AIC, weight (the AICc weight) and converged. A candidate that did not converge is listed last, with no deltas and no weight. None otherwise.

candidates dict[str, GDMFit] | None

With mod_sel=True, the fits behind comparison, keyed "GDM", "A + T", "A" and "Intercept". None otherwise.

formula property

formula: str

The fitted equation, e.g. S = Int + A*log(area) + Ti*time + Ti2*time^2.

residuals property

residuals: ndarray

Observed minus fitted richness for each row of data.

predict

predict(area: float | ndarray, time: float | ndarray | None = None) -> np.ndarray

Predict species richness for given area and island age value(s).

Parameters:

Name Type Description Default
area float or array - like

Island area(s), in the units of the fitted data.

required
time float or array - like

Island age(s), in the units of the fitted data. area and time are broadcast against each other, so one may be a single value. Only a candidate without time terms ("A", "Intercept") can leave it out; given one anyway, it shapes the result but doesn't change the predictions.

None

Returns:

Type Description
ndarray

Predicted richness, in the broadcast shape of area and time. The forms without an exponential can predict fewer than zero species away from the data.

Raises:

Type Description
ValueError

If time is missing from a fit that has time terms, a value is NaN or infinite, or a value is outside the domain of the fitted form (area or time not positive where the fit uses its log).

GDMMultiFit

sars.GDMMultiFit dataclass

The general dynamic model fitted with every area form (model="all").

Attributes:

Name Type Description
fits dict[str, GDMFit]

The fit for each form, keyed "loga", "linear", "power_area" and "power_area_time".

summary DataFrame

The four fits ranked by AICc: columns model, k, RSE, R2, AIC, AICc, BIC, delta_AIC, delta_AICc, weight_AIC, weight (the AICc weight) and converged. The forms share a response (richness) and a parameter count, so the ranking is the ranking by residual sum of squares. A form whose fit did not converge is listed last, with no deltas and no weight.

data DataFrame

The data fitted (columns: area, species, time).

best property

best: GDMFit

The fit ranked first in summary.