ANOVA

One-way, factorial, ANCOVA, and repeated measures ANOVA. Type I/II/III sums of squares. Post-hoc tests (Tukey HSD, Bonferroni, Dunnett). Levene’s test for homogeneity of variances.

Analysis of Variance (ANOVA).

Public API:

anova_oneway(y, group, …) -> AnovaSolution anova(y, factors, …) -> AnovaSolution # factorial / ANCOVA anova_rm(y, subject, within, …) -> AnovaRMSolution # repeated measures anova_posthoc(result, …) -> PostHocSolution # Tukey / Bonferroni / Dunnett levene_test(y, group, …) -> LeveneSolution # homogeneity of variances

pystatistics.anova.anova(y, factors, *, covariates=None, ss_type=2, interactions=True)[source]

Factorial ANOVA or ANCOVA.

Tests main effects and interactions of two or more factors, with optional continuous covariates (ANCOVA).

Parameters:
  • y (Any) – Response variable (1D numeric array-like)

  • factors (dict[str, Any]) – {name: 1D array of group labels}

  • covariates (dict[str, Any] | None) – {name: 1D numeric array} or None

  • ss_type (int) – Type of sums of squares (1, 2, or 3). Default 2. Type I: sequential (order-dependent) Type II: marginal, respects marginality (R’s car::Anova default) Type III: each term last (requires deviation coding)

  • interactions (bool) – Whether to include interaction terms. Default True.

Returns:

AnovaSolution with ANOVA table, effect sizes, and group means

Return type:

AnovaSolution

Examples

>>> result = anova(y, {'A': factor_a, 'B': factor_b})
>>> print(result.summary())
>>> result = anova(y, {'treatment': tx}, covariates={'age': age}, ss_type=2)
pystatistics.anova.anova_oneway(y, group, *, ss_type=1)[source]

One-way Analysis of Variance.

Tests whether the means of two or more groups are equal.

Parameters:
  • y (Any) – Response variable (1D numeric array-like)

  • group (Any) – Group labels (1D array-like, same length as y)

  • ss_type (int) – Type of sums of squares (1, 2, or 3). Default 1. For one-way ANOVA, all three types give identical results.

Returns:

AnovaSolution with ANOVA table, effect sizes, and group means

Return type:

AnovaSolution

Examples

>>> result = anova_oneway(y, group)
>>> print(result.summary())
>>> result.table[0].f_value  # F statistic for the group effect
>>> result.eta_squared       # effect sizes
pystatistics.anova.anova_posthoc(anova_result, *, method='tukey', factor=None, control=None, conf_level=0.95)[source]

Post-hoc pairwise comparisons following ANOVA.

Parameters:
  • anova_result (AnovaSolution) – Result from anova_oneway() or anova()

  • method (str) – ‘tukey’ (default), ‘bonferroni’, or ‘dunnett’

  • factor (str | None) – Which factor to compare (required for factorial, auto for oneway)

  • control (str | None) – Control group name (required for dunnett)

  • conf_level (float) – Confidence level (default 0.95)

Returns:

PostHocSolution with comparison table and adjusted p-values

Return type:

PostHocSolution

Examples

>>> anova_result = anova_oneway(y, group)
>>> posthoc = anova_posthoc(anova_result, method='tukey')
>>> print(posthoc.summary())
pystatistics.anova.anova_rm(y, subject, within, *, between=None, correction='auto')[source]

Repeated-measures ANOVA.

Tests within-subjects effects with optional between-subjects factors (mixed design). Includes Mauchly’s sphericity test and GG/HF corrections.

Parameters:
  • y (Any) – Response variable (1D, long format)

  • subject (Any) – Subject identifiers (1D)

  • within (dict[str, Any]) – {factor_name: 1D condition labels}

  • between (dict[str, Any] | None) – {factor_name: 1D group labels} or None

  • correction (str) – Sphericity correction: ‘none’: no correction ‘gg’: Greenhouse-Geisser ‘hf’: Huynh-Feldt ‘auto’: GG if Mauchly p < 0.05, else none (default)

Returns:

AnovaRMSolution with ANOVA table, sphericity, corrected p-values

Return type:

AnovaRMSolution

Examples

>>> result = anova_rm(y, subject=subj, within={'condition': cond})
>>> print(result.summary())
>>> result.sphericity[0].gg_epsilon  # GG correction factor
pystatistics.anova.levene_test(y, group, *, center='median')[source]

Levene’s test for homogeneity of variances.

Tests the null hypothesis that all groups have equal variances. With center=’median’ (default), this is the Brown-Forsythe variant which is more robust to non-normality.

Parameters:
  • y (Any) – Response variable (1D numeric array-like)

  • group (Any) – Group labels (1D array-like, same length as y)

  • center (str) – ‘median’ (Brown-Forsythe, default) or ‘mean’ (original Levene)

Returns:

LeveneSolution with F statistic, p-value, and group variances

Return type:

LeveneSolution

Examples

>>> result = levene_test(y, group)
>>> result.p_value > 0.05  # Can't reject equal variances
>>> print(result.summary())
class pystatistics.anova.AnovaSolution(_result)[source]

Bases: object

User-facing result for between-subjects ANOVA.

Produced by anova_oneway() and anova().

Parameters:

_result (Result[AnovaParams])

property table: tuple[AnovaTableRow, ...]

term, df, SS, MS, F, p).

Type:

ANOVA table (list of rows

property ss_type: int
property n_obs: int
property grand_mean: float
property residual_df: int
property residual_ss: float
property residual_ms: float
property eta_squared: dict[str, float]
property partial_eta_squared: dict[str, float]
property group_means: dict[str, dict[str, float]]
property info: dict[str, Any]
property timing: dict[str, float] | None
property backend_name: str
property warnings: tuple[str, ...]
summary()[source]

Generate R-style ANOVA summary table.

Return type:

str

class pystatistics.anova.AnovaRMSolution(_result)[source]

Bases: object

User-facing result for repeated-measures ANOVA.

Produced by anova_rm().

Parameters:

_result (Result[AnovaRMParams])

property table: tuple[AnovaRMTableRow, ...]
property n_subjects: int
property n_obs: int
property sphericity: tuple[SphericitySummary, ...]
property correction: str
property eta_squared: dict[str, float]
property partial_eta_squared: dict[str, float]
property info: dict[str, Any]
property timing: dict[str, float] | None
property warnings: tuple[str, ...]
summary()[source]

Generate R-style repeated-measures ANOVA summary.

Return type:

str

class pystatistics.anova.LeveneSolution(_result)[source]

Bases: object

User-facing result for Levene’s test.

Produced by levene_test().

Parameters:

_result (Result[LeveneParams])

property f_value: float
property p_value: float
property df_between: int
property df_within: int
property center: str
property group_vars: dict[str, float]
summary()[source]
Return type:

str

class pystatistics.anova.PostHocSolution(_result)[source]

Bases: object

User-facing result for post-hoc comparisons.

Produced by anova_posthoc().

Parameters:

_result (Result[PostHocParams])

property method: str
property comparisons: tuple[PostHocComparison, ...]
property conf_level: float
property factor: str
summary()[source]
Return type:

str