How to perform post hoc test for Kruskal-Wallis in R?


The Kruskal-Wallis test is the non-parametric analogue of one-way analysis of variance. The non-parametric tests are used in situations when the assumptions of parametric tests are not met. If we find significant difference in Kruskal-Wallis then post hoc tests are done to find where the difference exists. For this purpose, we can perform dunn test. The function of dunn test can be accessed through FSA package.

Example1

Loading FSA package:

> library(FSA)

Consider the below data frame:

Live Demo

> x1<-sample(LETTERS[1:5],20,replace=TRUE)
> y1<-rnorm(20,1,0.35)
> df1<-data.frame(x1,y1)
> df1

Output

  x1    y1
1 E 1.1191117
2 D 1.1276032
3 D 1.5610692
4 E 1.1585054
5 E 1.0239322
6 C 0.8000165
7 C 1.2009313
8 B 1.1928228
9 A 0.7421504
10 B 0.7212436
11 C 1.4088902
12 B 1.3171291
13 D 0.9434812
14 A 0.7986718
15 C 1.0394762
16 A 0.9239324
17 E 1.1447561
18 D 1.0192032
19 B 0.8772467
20 A 0.5723085

Performing dunnTest:

Example

> dunnTest(y1~x1,data=df1)
Dunn (1964) Kruskal-Wallis multiple comparison
p-values adjusted with the Holm method.

Output

Comparison Z P.unadj P.adj
1  A - B -1.61355862 0.10662320 0.7463624
2  A - C -2.21117293 0.02702386 0.2702386
3  B - C -0.59761430 0.55009732 1.0000000
4  A - D -2.09165007 0.03646983 0.2917586
5  B - D -0.47809144 0.63258512 1.0000000
6  C - D  0.11952286 0.90486113 1.0000000
7  A - E -2.15141150 0.03144373 0.2829936
8  B - E -0.53785287 0.59067863 1.0000000
9  C - E  0.05976143 0.95234564 1.0000000
10 D - E -0.05976143 0.95234564 0.9523456
Warning message:
x1 was coerced to a factor.

Example2

Live Demo

> x2<-sample(c("India","Russia","China","Croatia"),20,replace=TRUE)
> y2<-rpois(20,5)
> df2<-data.frame(x2,y2)
> df2

Output

   x2    y2
1 Russia 0
2 Russia 6
3 Croatia 8
4 Croatia 5
5 Russia 5
6 Croatia 9
7 India 9
8 Croatia 6
9 India 4
10 China 1
11 Croatia 7
12 China 3
13 India 3
14 India 4
15 Croatia 6
16 China 7
17 China 8
18 Croatia 10
19 India 8
20 China 7

Example

> dunnTest(y2~x2,data=df2)
Dunn (1964) Kruskal-Wallis multiple comparison
p-values adjusted with the Holm method.

Output

Comparison Z P.unadj P.adj
1 China - Croatia -1.18245422 0.23702552 1.0000000
2 China - India -0.08066504 0.93570834 0.9357083
3 Croatia - India 1.09532601 0.27337384 1.0000000
4 China - Russia 0.77619975 0.43763106 0.8752621
5 Croatia - Russia 1.82479827 0.06803148 0.4081889
6 India - Russia 0.84605772 0.39752054 1.0000000
Warning message:
x2 was coerced to a factor.

Example3

Live Demo

> x3<-sample(c("G1","G2","G3","G4","G5"),20,replace=TRUE)
> y3<-rexp(20,1.34)
> df3<-data.frame(x3,y3)
> df3

Output

   x3    y3
1  G2 1.89169184
2  G3 2.74074462
3  G3 0.17273122
4  G2 0.34856852
5  G2 0.80544065
6  G1 0.54582070
7  G2 0.24551988
8  G5 0.02546690
9  G3 2.86315652
10 G1 0.43704405
11 G1 1.89036598
12 G5 0.02423629
13 G5 0.03848270
14 G1 1.01897322
15 G1 0.44416202
16 G5 0.96637068
17 G2 0.74919567
18 G5 3.24106689
19 G1 1.22994992
20 G3 0.84658591

Example

> dunnTest(y3~x3,data=df3)
Dunn (1964) Kruskal-Wallis multiple comparison
p-values adjusted with the Holm method.

Output

Comparison Z P.unadj P.adj
1 G1 - G2 0.4745469 0.6351099 1.0000000
2 G1 - G3 -0.4582576 0.6467674 0.6467674
3 G2 - G3 -0.8693183 0.3846731 1.0000000
4 G1 - G5 1.0328375 0.3016800 1.0000000
5 G2 - G5 0.5345225 0.5929801 1.0000000
6 G3 - G5 1.3732709 0.1696681 1.0000000
Warning message:
x3 was coerced to a factor.

Updated on: 21-Nov-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements