Kruskal-Wallis

Introduction

  • Previous: one sample, two samples

  • Now: more than two groups, continuous outcome

    • One-way ANOVA

    • Kruskal-Wallis

    • posthoc testing

  • This lecture: Kruskal-Wallis (nonparametric alternative)

R Setup

library(tidyverse)
library(ssstats)
commute_times <- read_csv("https://raw.githubusercontent.com/samanthaseals/SDSI/refs/heads/main/files/data/lectures/4-2-commute_times.csv")
citations <- read_csv("https://raw.githubusercontent.com/samanthaseals/SDSI/refs/heads/main/files/data/lectures/4-4-citations.csv")
academy_scores <- read_csv("https://raw.githubusercontent.com/samanthaseals/SDSI/refs/heads/main/files/data/lectures/4-1-academy_scores.csv")

Example 2

  • Do assessment scores differ across the three training academies (Meadowbrook, Coldharbor, and Northgate)?
academy_scores |> anovas(outcome = score,
                         grouping = academy)
One-way ANOVA

Source     df         SS        MS      F  p-value 
───────────────────────────────────────────── 
academy     2   786.9215  393.4607  5.898    0.004 
Residuals  93  6204.2681   66.7126                 
───────────────────────────────────────────── 


Hypotheses (academy):
H₀: All group means are equal (no effect of academy)
H₁: At least one group mean differs (effect of academy exists)
Test statistic: F(2, 93) = 5.898
p-value: 0.004
Conclusion: Reject the null hypothesis (p = 0.004 < α = 0.050)

Example 2

  • Do assessment scores differ across the three training academies (Meadowbrook, Coldharbor, and Northgate)?

Kruskal-Wallis

  • If we break either ANOVA assumption, we should implement the nonparametric version, the Kruskal-Wallis.

    • The Kruskal-Wallis is an extension of the Wilcoxon rank sum (as ANOVA is an extension of the two-sample t-test).
  • The Kruskal-Wallis test determines if k independent samples come from populations with the same distribution.

  • Note: we can transform the data to make the variance “behave”

    • Box-Cox, or power, transformation

Hypothesis Test for M_1 = M_2 = ... = M_k

  • Hypotheses

    • H_0: M_1 = ... = M_k
    • H_1: at least one M_i is different

Hypothesis Testing: Kruskal-Wallis

  • Test Statistic

\chi^2_0 = \frac{12}{n(n+1)} \sum_{i=1}^k \frac{R_i^2}{n_i} - 3(n+1) \sim \chi^2_{\text{df}}

  • where

    • R_i is the sum of the ranks for group i,
    • n_i is the sample size for group i,
    • n = \sum_{i=1}^k n_i = total sample size,
    • k is the number of groups, and
    • \text{df} = k-1

Statistical Inference on k Medians (R)

  • We will use the kruskal_wallis() function from library(ssstats) to perform the Kruskal-Wallis test.
dataset_name |> kruskal_wallis(outcome = continuous_variable,
                               grouping = grouping_variable,
                               alpha = specified_alpha)

Example 2

academy_scores |> kruskal_wallis(outcome = score,
                                 grouping = academy)
Kruskal-Wallis test

M[Coldharbor] = 80.9 (n = 32)
M[Meadowbrook] = 74.75 (n = 32)
M[Northgate] = 82.6 (n = 32)

Hypotheses:
H₀: All group distributions are equal (no effect of academy)
H₁: At least one group distribution differs in location (effect of academy exists)
Test statistic: H(2) = 9.801
p-value: 0.007
Conclusion: Reject the null hypothesis (p = 0.007 < α = 0.050)

Example 2

  • Hypotheses

    • H_0: M_{\text{M}} = M_{\text{C}} = M_{\text{N}}
    • H_1: at least one district’s academy is different
  • Test Statistic and p-Value

    • \chi^2_0 = 9.801
    • p = 0.007
  • Rejection Region

    • Reject H_0 if p < \alpha; \alpha = 0.05
  • Conclusion and Interpretation

    • Reject H_0. There is sufficient evidence to suggest that there is a difference in assessment scores between the three academies.

Example 3

  • Does the number of citations issued per officer differ across the four districts?
citations |> anova_check(outcome = citations, grouping = district)

Example 3

  • Does the number of citations issued per officer differ across the four districts?
citations |> filter(district == "Meadowbrook") |> one_qq(citations)

Example 3

citations |> kruskal_wallis(outcome = citations,
                            grouping = district)
Kruskal-Wallis test

M[Coldharbor] = 7 (n = 32)
M[Fairview] = 6.5 (n = 32)
M[Meadowbrook] = 6 (n = 32)
M[Northgate] = 10 (n = 32)

Hypotheses:
H₀: All group distributions are equal (no effect of district)
H₁: At least one group distribution differs in location (effect of district exists)
Test statistic: H(3) = 43.119
p-value: < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.050)

Example 3

  • Hypotheses

    • H_0: M_{\text{M}} = M_{\text{C}} = M_{\text{N}} = M_{\text{F}}
    • H_1: at least one district is different
  • Test Statistic and p-Value

    • \chi^2_0 = 43.119
    • p < 0.001
  • Rejection Region

    • Reject H_0 if p < \alpha; \alpha = 0.05
  • Conclusion and Interpretation

    • Reject H_0. There is sufficient evidence to suggest that there is a difference in citations between the four districts.

Wrap Up

  • This lecture introduced the nonparametric alternative to one-way ANOVA.

    • Like ANOVA, the Kruskal-Wallis examines the effect of a single factor.
  • The Kruskal-Wallis only allows us to determine if there is an overall, or omnibus, effect.

    • It does not tell us about pairwise differences.
  • In the future:

    • Posthoc testing