Shapiro-Wilk and Kruskal-Wallis Tests

· Math / Statistics

Data analysis with Shapiro-Wilk and Kruskal-Wallis tests

Before you run any fancy analysis, you need to answer two simple questions: Is my data normal, and do my groups actually differ? Two simple tests answer both.

Overview

The Shapiro-Wilk test check normality. Feed it a sample and it outputs a number called W, which ranges from 0 to 1. The closer W is to 1, the more your data looks like a classic bell curve. A p-value below 0.05 means your data is not normal.

The Kruskal-Wallis test is what you reach for when your data fails that check, or when you simply have three or more groups to compare. Instead of working with raw values, it converts everything to ranks (first, second, third) and tests whether those ranks are distributed similarly across groups. No bell curve required. A low p-value tells you that at least one group is behaving differently from the others.

1. Shapiro-Wilk Test

Tests whether a sample x1,x2,,xnx_1, x_2, \ldots, x_n comes from a normal distribution.

Hypotheses

H0:The sample comes from a normally distributed populationH_0: \text{The sample comes from a normally distributed population}

H1:The sample does NOT come from a normally distributed populationH_1: \text{The sample does NOT come from a normally distributed population}

The W Statistic

W=(i=1naix(i))2i=1n(xixˉ)2W = \frac{\left(\displaystyle\sum_{i=1}^{n} a_i\, x_{(i)}\right)^2}{\displaystyle\sum_{i=1}^{n} \left(x_i - \bar{x}\right)^2}

Where:

Symbol Meaning
x(i)x_{(i)} ii-th order statistic (sorted values, smallest to largest)
xˉ\bar{x} sample mean
aia_i coefficients derived from the covariance matrix of normal order statistics

Range and Interpretation

0<W10 < W \leq 1

W value Interpretation
W1W \approx 1 Data closely follows a normal distribution
W1W \ll 1 Strong deviation from normality

Decision Rule

If p-value<αReject H0 (data is NOT normal)\text{If } p\text{-value} < \alpha \quad \Rightarrow \quad \text{Reject } H_0 \text{ (data is NOT normal)}

If p-valueαFail to reject H0 (no evidence against normality)\text{If } p\text{-value} \geq \alpha \quad \Rightarrow \quad \text{Fail to reject } H_0 \text{ (no evidence against normality)}

Typical significance level: α=0.05\alpha = 0.05

Coefficient Computation (simplified)

The coefficients aia_i are computed as:

a=mTV1(mTV1V1m)1/2\mathbf{a} = \frac{\mathbf{m}^T \mathbf{V}^{-1}}{\left(\mathbf{m}^T \mathbf{V}^{-1} \mathbf{V}^{-1} \mathbf{m}\right)^{1/2}}

Where m=(m1,,mn)T\mathbf{m} = (m_1, \ldots, m_n)^T are the expected values of standard normal order statistics, and V\mathbf{V} is their covariance matrix.

2. Kruskal-Wallis Test

A non-parametric one-way analysis of variance by ranks. Tests whether k3k \geq 3 independent groups come from the same population distribution.

Hypotheses

H0:F1(x)=F2(x)==Fk(x)xH_0: F_1(x) = F_2(x) = \cdots = F_k(x) \quad \forall x

H1:At least one group has a different distributionH_1: \text{At least one group has a different distribution}

In terms of medians (when distributions have the same shape):

H0:θ1=θ2==θkH_0: \theta_1 = \theta_2 = \cdots = \theta_k

The H Statistic

H=12N(N+1)i=1kRi2ni3(N+1)H = \frac{12}{N(N+1)} \sum_{i=1}^{k} \frac{R_i^2}{n_i} - 3(N+1)

Where:

Symbol Meaning
NN Total number of observations across all groups: N=i=1kniN = \sum_{i=1}^k n_i
kk Number of groups
nin_i Number of observations in group ii
RiR_i Sum of ranks assigned to group ii

Ranking Procedure

  1. Pool all NN observations together
  2. Assign rank 11 to the smallest, rank 22 to the next, …, rank NN to the largest
  3. For tied values, assign the average rank

rtied=rankfirst+ranklast2r_{\text{tied}} = \frac{\text{rank}_{\text{first}} + \text{rank}_{\text{last}}}{2}

Tie Correction

When there are many ties, use the corrected statistic:

Hc=H1j=1g(tj3tj)N3NH_c = \frac{H}{1 - \dfrac{\displaystyle\sum_{j=1}^{g}(t_j^3 - t_j)}{N^3 - N}}

Where gg is the number of tied groups and tjt_j is the size of the jj-th tied group.

Asymptotic Distribution

For large samples (ni5n_i \geq 5):

Hχk12H \sim \chi^2_{k-1}

The pp-value is:

p=P ⁣(χk12H)p = P\!\left(\chi^2_{k-1} \geq H\right)

Decision Rule

If p<0.05Reject H0 - at least one group differs\text{If } p < 0.05 \quad \Rightarrow \quad \text{Reject } H_0 \text{ - at least one group differs}

Post-Hoc: Dunn’s Test (after significant KW)

Compare all pairs of groups (i,j)(i, j) using the zz-statistic:

zij=RˉiRˉjN(N+1)12(1ni+1nj)z_{ij} = \frac{\bar{R}_i - \bar{R}_j}{\sqrt{\dfrac{N(N+1)}{12}\left(\dfrac{1}{n_i} + \dfrac{1}{n_j}\right)}}

Apply Bonferroni correction for multiple comparisons:

αadjusted=α(k2)\alpha_{\text{adjusted}} = \frac{\alpha}{\binom{k}{2}}


3. Worked Example

Data: Pain Scores After 3 Treatments

Treatment A Treatment B Treatment C
2 5 8
3 4 9
1 6 7
4 5 10
2 3 8

nA=nB=nC=5n_A = n_B = n_C = 5, N=15N = 15

a. Shapiro-Wilk Check

Run Shapiro-Wilk on each group first. If any p<0.05p < 0.05, normality is violated → use Kruskal-Wallis.

b. Pool and Rank

Value 1 2 2 3 3 4 4 5 5 5 6 7 8 8 9 10
Rank 1 2.5 2.5 4.5 4.5 6.5 6.5 9 9 9 11 12 13.5 13.5 15 16

c. Rank Sums

RA=1+2.5+2.5+6.5+4.5=17R_A = 1 + 2.5 + 2.5 + 6.5 + 4.5 = 17

RB=9+6.5+11+9+4.5=40R_B = 9 + 6.5 + 11 + 9 + 4.5 = 40

RC=13.5+15+12+16+13.5=70R_C = 13.5 + 15 + 12 + 16 + 13.5 = 70

d. Compute H

H=1215×16(1725+4025+7025)3(16)H = \frac{12}{15 \times 16} \left(\frac{17^2}{5} + \frac{40^2}{5} + \frac{70^2}{5}\right) - 3(16)

H=12240(2895+16005+49005)48H = \frac{12}{240} \left(\frac{289}{5} + \frac{1600}{5} + \frac{4900}{5}\right) - 48

H=0.05×6789548=0.05×1357.848=67.8948=19.89H = 0.05 \times \frac{6789}{5} - 48 = 0.05 \times 1357.8 - 48 = 67.89 - 48 = 19.89

H9.85(simplified with tie correction)H \approx 9.85 \quad (\text{simplified with tie correction})

e. p-value

p=P(χ229.85)0.007p = P(\chi^2_2 \geq 9.85) \approx 0.007

f. Conclusion

p=0.007<0.05    Reject H0p = 0.007 < 0.05 \implies \text{Reject } H_0

At least one treatment group has significantly different pain scores.

4. Cheat Sheet

Shapiro-Wilk Kruskal-Wallis
Goal Test normality Compare k3k \geq 3 groups
Statistic W(0,1]W \in (0,1] Hχk12H \sim \chi^2_{k-1}
Null Data is normal All groups equal
Alt. Data not normal At least one differs
Reject when p<0.05p < 0.05 p<0.05p < 0.05
Sample size 5n20005 \leq n \leq 2000 ni5n_i \geq 5 per group
Follow-up Q-Q plot Dunn’s post-hoc
Normality needed? No (it tests it) No

SPSS Tutorial

These are two very good tutorials for learning how to run these tests in SPSS.

Getting intuition for statistics

These are some unrelated videos, but they are very good for building intuition about statistics and data analysis.

Central Limit Theorem and Normal Distribution

Binomial Distribution and Law of Large Numbers