Skip to Main Content

MASH : Maths and Stats Help

Page Title

Kruskal Wallis H-Test

Kruskal Wallis Test

Introduction

A Kruskal Wallis Test compares the difference between more than two independent groups, such as comparing the difference between groups A, B and C. If your data only has two groups such as Male/Female or Present/Absent you should consider the Independent-Samples t-Test

It is considered a non-parametric test and is suitable for non-parametric data. To check if your data is parametric, please check out the dedicated guide: Parametric or Not Guide (PDF)

If your data is parametric you should consider using a One-Way Between-Subjects ANOVA

In order to run a Kruskal Wallis Test, you will need to install and load the "rstatix" package.

 

Test Procedure

The kruskal_test() function used to conduct Kruskal Wallis tests requires a continuous or ordinal dependent variable and a categorical grouping variable. It uses a formula notation to indicate independent and dependent variables.

The first argument input into the kruskal_test() function should be the data frame.

The second argument should be a formula that takes the following structure:

dependent variable ~ independent variable

The kruskal_test() function needs the grouping variable to be a factor, which is a type of categorical variable (in R) that contains information about the different categories present in the variable along with the values. In the code below, we convert the categorical variable “Group” from numeric to factor using the factor() function. The dependent variable, in this case “Score”, should be numeric.

kruskal_test(data = kw_data, formula = Score ~ factor(Group))
## # A tibble: 1 × 6
##   .y.       n statistic    df      p method        
## * <chr> <int>     <dbl> <int>  <dbl> <chr>         
## 1 Score    73      6.75     2 0.0343 Kruskal-Wallis

 

Pairwise comparisons (or post-hoc tests)

In addition, if your result is significant you must also carry out and report post-hoc tests to find which groups are different to each other. A Dunn’s test or Mann-Whitney could be considered here to test between the pairs of unrelated groups. You also have options for which correction (for multiple tests) to use. In the example code below we have used a series of Dunn’s Tests with the Holm adjustment.

In order to obtain a table with pairwise comparisons (post-hoc tests) for all the levels of the grouping variable, we will use the dunn_test() function.

Similar to the kruskal_test() function, the first two arguments are the data frame and the formula. Please note that the formula prefers the grouping variable to not be a factor, so we will not use the factor() function.

In addition, the dunn_test() function has a third argument. The p.adjust.method argument tells that function what type of correction for multiple comparisons to use. We will use the Holm method here, but the function has options for other corrections (e.g., Bonferroni).

The fourth argument asks for detailed output, this is to make sure we get all the needed output.

dunn_test(data = kw_data, formula = Score ~ Group,  p.adjust.method = "holm", detailed = TRUE)
## # A tibble: 3 × 13
##   .y.   group1 group2    n1    n2 estimate estimate1 estimate2 statistic      p
## * <chr> <chr>  <chr>  <int> <int>    <dbl> <dbl[1d]> <dbl[1d]>     <dbl>  <dbl>
## 1 Score A      B         27    24    -4.20      34.9      30.7    -0.707 0.480 
## 2 Score A      C         27    22    11.5       34.9      46.4     1.89  0.0581
## 3 Score B      C         24    22    15.7       30.7      46.4     2.52  0.0119
## # ℹ 3 more variables: method <chr>, p.adj <dbl>, p.adj.signif <chr>

IMPORTANT: Don’t forget to report the p-values from the p.adj column, NOT the ones from the p columns. If you can’t see the p.adj column, press the arrow button at the top right of the table.

 

Results

We use both the Kruskal-Wallis table and the post-hoc table to report the analysis. For obtaining descriptive statistics for separate groups, please see our guide on Descriptive Statistics.

Test Statistics

The output of the kruskal_test() function shows the specific test results, including the H-statistic (statistic). You may also see this statistic reported as a χ2. We also need to report the degrees of freedom (df), and the significance (P).

Post-hoc table

The dunn_test() table shows the results of the post hoc tests between all the possible pairs of groups in the grouping variable. The most important columns are the ones containing the mean ranks difference (estimate) and the Holm adjusted p-value (p.adj).

 

Reporting the Results in APA Formatting

A Kruskal-Wallis test was performed on the scores of the three groups (A, B and C). There was a significant difference between the three groups, H (2, n = 73) = 6.75, p = .034.

Post-hoc comparisons were conducted using Dunn’s Tests with a Holm correction. The difference between Group B (Mdn = 50, IQR = 8.25) and Group C (Mdn = 54.5, IQR = 9.25), ­r = 15.72, was statistically significant (p = .036). None of the other comparisons were significant after the Holm correction.