Professional Documents
Culture Documents
R Companion - Kruskal-Wallis Test
R Companion - Kruskal-Wallis Test
Kruskal–Wallis Test
Examples in Summary and Analysis of Extension Program Evaluation
SAEPER: Kruskal–Wallis Test
When to use it
See the Handbook for information on this topic.
Null hypothesis
This example shows just summary statistics, histograms by group, and the Kruskal–Wallis test. An
example with plots, post-hoc tests, and alternative tests is shown in the “Example” section below.
Kruskal–Wallis test example
### --------------------------------------------------------------
### Kruskal–Wallis test, hypothetical example, p. 159
### --------------------------------------------------------------
str(Data)
https://rcompanion.org/rcompanion/d_06.html 2/9
27/03/2024, 11:13 R Companion: Kruskal–Wallis Test
Kruskal–Wallis test
In this case, there is a significant difference in the distributions of values among groups, as is evident
both from the histograms and from the significant Kruskal–Wallis test. Only in cases where the
distributions in each group are similar in shape and spread can a significant Kruskal–Wallis test be
interpreted as a difference in medians.
kruskal.test(Value ~ Group,
data = Data)
Example
The Kruskal–Wallis test is performed on a data frame with the kruskal.test function in the native stats
package. Shown is a complete example with plots and post-hoc tests.
The example is data measuring if the mucociliary efficiency in the rate of dust removal is different among
normal subjects, subjects with obstructive airway disease, and subjects with asbestosis. For the original
citation, use the ?kruskal.test command.
For both the submissive dog example and the oyster DNA example from the Handbook, a Kruskal–Wallis
test is shown later in this chapter.
Kruskal–Wallis test example
### --------------------------------------------------------------
### Kruskal–Wallis test, asbestosis example from R help for
### kruskal.test
https://rcompanion.org/rcompanion/d_06.html 3/9
27/03/2024, 11:13 R Companion: Kruskal–Wallis Test
### --------------------------------------------------------------
Summarize(Efficiency ~ Health,
data = Data)
Health n mean sd min Q1 median Q3 max
1 Normal 5 2.840 0.2880972 2.5 2.600 2.90 3.00 3.2
2 OAD 4 3.225 0.7932003 2.4 2.625 3.25 3.85 4.0
3 Asbestosis 5 2.820 0.7362065 2.0 2.200 2.80 3.40 3.7
https://rcompanion.org/rcompanion/d_06.html 4/9
27/03/2024, 11:13 R Companion: Kruskal–Wallis Test
Kruskal–Wallis test
kruskal.test(Efficiency ~ Health,
data = Data)
https://rcompanion.org/rcompanion/d_06.html 5/9
27/03/2024, 11:13 R Companion: Kruskal–Wallis Test
Zar (2010) states that the Dunn test is appropriate for groups with unequal numbers of observations.
If there are several values to compare, it can be beneficial to have R convert this table to a compact letter
display for you. The cldList function in the rcompanion package can do this.
### Order groups by median
Data$Health = factor(Data$Health,
levels=c("OAD", "Normal", "Asbestosis"))
PT = dunnTest(Efficiency ~ Health,
data=Data,
method="bh") # Can adjust p-values;
# See ?p.adjust for options
PT
PTD = PT$res
library(rcompanion)
cldList(comparison = PTD$Comparison,
p.value = PTD$P.adj,
threshold = 0.05)
Group Letter MonoLetter
1 Asbestosis a a
2 Normal a a
3 OAD a a
### Values sharing the same letter are not significantly different
PT = NemenyiTest(x = Data$Efficiency,
g = Data$Health,
dist="tukey")
PT
PTD = as.data.frame(PT[1])
library(rcompanion)
cldList(comparison = row.names(PTD,
p.value = PTD$pval,
threshold = 0.05)
https://rcompanion.org/rcompanion/d_06.html 6/9
27/03/2024, 11:13 R Companion: Kruskal–Wallis Test
### Values sharing the same letter are not significantly different
str(Data)
### Summarize data
library(FSA)
Summarize(Rank ~ Sex,
data = Data)
kruskal.test(Rank ~ Sex,
data = Data)
DTT =PMCMRTable(DT)
DTT
Comparison p.value
1 Female - Male = 0 0.0318
Similar tests
One-way anova is presented elsewhere in this book.
https://rcompanion.org/rcompanion/d_06.html 8/9
27/03/2024, 11:13 R Companion: Kruskal–Wallis Test
### Examine data frame
str(Data)
### Summarize data
library(FSA)
Summarize(fst ~ Markertype,
data = Data)
kruskal.test(fst ~ Markertype,
data = Data)
Kruskal-Wallis chi-squared = 0.0426, df = 1, p-value = 0.8365
Power Analysis
See the Handbook for information on this topic.
References
Zar, J.H. 2010. Biostatistical Analysis, 5th ed. Pearson Prentice Hall: Upper Saddle River, NJ.
https://rcompanion.org/rcompanion/d_06.html 9/9