# Scheirer–Ray–Hare Test

The **Scheirer–Ray–Hare Test** is the non-parametric alternative to Two-Way ANOVA. It tests the effects of two categorical factors and their interaction on a continuous outcome by **ranking the data** before partitioning variance — no normality assumption required.

---

## 📌 When to Use
- Two categorical independent variables (factors)
- One continuous outcome
- Data violates normality assumptions or contains outliers
- Same design as Two-Way ANOVA but without the normality requirement

> ✅ Use when Two-Way ANOVA's normality assumption is not met.
> See [Two-Way ANOVA](two-way-anova.md) for the parametric version.

---

## 📐 Method

All outcome values are replaced by their ranks. The total sum of squares of ranks is then partitioned:

$$
H_A = \frac{SS_A}{SS_{total}} \times (N - 1)
$$

$$
H_B = \frac{SS_B}{SS_{total}} \times (N - 1)
$$

$$
H_{A \times B} = \frac{SS_{A \times B}}{SS_{total}} \times (N - 1)
$$

Each H statistic is tested against a chi-square distribution with appropriate degrees of freedom.

---

## 🔬 What StatInsight Reports
- H statistic and p-value for Factor 1
- H statistic and p-value for Factor 2
- H statistic and p-value for the interaction
- Total N and group-level median, IQR, N
- Post-hoc Dunn tests (Bonferroni-corrected) for each significant main effect
- Stratified post-hoc: Factor 1 within each level of Factor 2, and vice versa

---

## 🧩 Post-Hoc Tests (Auto)
- **Dunn's test** with Bonferroni correction for significant main effects
- **Stratified Dunn tests**: comparisons within each level of the other factor (mirrors Two-Way ANOVA's stratified Tukey HSD)

---

## 🧪 Assumptions
- Two independent categorical factors
- One continuous (or ordinal) outcome
- Independent observations
- No normality assumption ✅
- Groups should have similar distribution shapes (but this is a weaker requirement than normality)

---

## 🧠 Interpretation
- **Significant Factor A**: outcome ranks differ across levels of Factor A
- **Significant Factor B**: outcome ranks differ across levels of Factor B
- **Significant interaction**: the effect of one factor depends on the level of the other
- Group statistics show medians (not means) because the test is rank-based

---

## 🐍 Python Implementation

```python
from scipy.stats import chi2, rankdata
import pandas as pd

# Rank all outcomes
df["_rank"] = df["outcome"].rank()
ss_total = df["_rank"].var(ddof=0) * len(df)

# Compute SS for each factor and interaction
# then H = SS / ss_total * (N - 1), tested with chi2
```

---

## 📎 Related Tests

[Two-Way ANOVA](two-way-anova.md) — parametric counterpart

[Kruskal–Wallis Test](kruskal-wallis.md) — non-parametric one-way

[Friedman Test](friedman.md) — non-parametric repeated measures
