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,…,xn comes from a normal distribution.
Hypotheses
H0:The sample comes from a normally distributed population
H1:The sample does NOT come from a normally distributed population
The W Statistic
W=i=1∑n(xi−xˉ)2(i=1∑naix(i))2
Where:
Symbol
Meaning
x(i)
i-th order statistic (sorted values, smallest to largest)
xˉ
sample mean
ai
coefficients derived from the covariance matrix of normal order statistics
Range and Interpretation
0<W≤1
W value
Interpretation
W≈1
Data closely follows a normal distribution
W≪1
Strong deviation from normality
Decision Rule
If p-value<α⇒Reject H0 (data is NOT normal)
If p-value≥α⇒Fail to reject H0 (no evidence against normality)
Typical significance level: α=0.05
Coefficient Computation (simplified)
The coefficients ai are computed as:
a=(mTV−1V−1m)1/2mTV−1
Where m=(m1,…,mn)T are the expected values of standard normal order statistics, and V is their covariance matrix.
2. Kruskal-Wallis Test
A non-parametric one-way analysis of variance by ranks. Tests whether k≥3 independent groups come from the same population distribution.
Hypotheses
H0:F1(x)=F2(x)=⋯=Fk(x)∀x
H1:At least one group has a different distribution
In terms of medians (when distributions have the same shape):
H0:θ1=θ2=⋯=θk
The H Statistic
H=N(N+1)12i=1∑kniRi2−3(N+1)
Where:
Symbol
Meaning
N
Total number of observations across all groups: N=∑i=1kni
k
Number of groups
ni
Number of observations in group i
Ri
Sum of ranks assigned to group i
Ranking Procedure
Pool all N observations together
Assign rank 1 to the smallest, rank 2 to the next, …, rank N to the largest
For tied values, assign the average rank
rtied=2rankfirst+ranklast
Tie Correction
When there are many ties, use the corrected statistic:
Hc=1−N3−Nj=1∑g(tj3−tj)H
Where g is the number of tied groups and tj is the size of the j-th tied group.
Asymptotic Distribution
For large samples (ni≥5):
H∼χk−12
The p-value is:
p=P(χk−12≥H)
Decision Rule
If p<0.05⇒Reject H0 - at least one group differs
Post-Hoc: Dunn’s Test (after significant KW)
Compare all pairs of groups (i,j) using the z-statistic:
zij=12N(N+1)(ni1+nj1)Rˉi−Rˉj
Apply Bonferroni correction for multiple comparisons:
αadjusted=(2k)α
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=5, N=15
a. Shapiro-Wilk Check
Run Shapiro-Wilk on each group first. If any p<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=17
RB=9+6.5+11+9+4.5=40
RC=13.5+15+12+16+13.5=70
d. Compute H
H=15×1612(5172+5402+5702)−3(16)
H=24012(5289+51600+54900)−48
H=0.05×56789−48=0.05×1357.8−48=67.89−48=19.89
H≈9.85(simplified with tie correction)
e. p-value
p=P(χ22≥9.85)≈0.007
f. Conclusion
p=0.007<0.05⟹Reject H0
At least one treatment group has significantly different pain scores.
4. Cheat Sheet
Shapiro-Wilk
Kruskal-Wallis
Goal
Test normality
Compare k≥3 groups
Statistic
W∈(0,1]
H∼χk−12
Null
Data is normal
All groups equal
Alt.
Data not normal
At least one differs
Reject when
p<0.05
p<0.05
Sample size
5≤n≤2000
ni≥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.