How to Perform a Kruskal-Wallis Test in R

A Kruskal-Wallis Test is used to determine whether or not there is a statistically significant difference between the medians of three or more independent groups.

It is considered to be the non-parametric equivalent of the One-Way ANOVA.

This tutorial explains how to perform a Kruskal-Wallis Test in R.

Example: Kruskal-Wallis Test in R

Suppose researchers want to know if three different fertilizers lead to different levels of plant growth. They randomly select 30 different plants and split them into three groups of 10, applying a different fertilizer to each group. At the end of one month they measure the height of each plant.

Use the following steps to perform a Kruskal-Wallis Test to determine if the median growth is the same across the three groups.

Step 1: Enter the data.

First, we’ll create the following data frame that contains the growth of all 30 plants along with their fertilizer group:

#create data frame
df <- data.frame(group=rep(c('A', 'B', 'C'), each=10),
                 height=c(7, 14, 14, 13, 12, 9, 6, 14, 12, 8,
                          15, 17, 13, 15, 15, 13, 9, 12, 10, 8,
                          6, 8, 8, 9, 5, 14, 13, 8, 10, 9))

#view first six rows of data frame

  group height
1     A      7
2     A     14
3     A     14
4     A     13
5     A     12
6     A      9

Step 2: Perform the Kruskal-Wallis Test.

Next, we’ll perform a Kruskal-Wallis Test using the built-in kruskal.test() function from base R:

#perform Kruskal-Wallis Test 
kruskal.test(height ~ group, data = df) 

	Kruskal-Wallis rank sum test

data:  height by group
Kruskal-Wallis chi-squared = 6.2878, df = 2, p-value = 0.04311

Step 3: Interpret the results.

The Kruskal-Wallis Test uses the following null and alternative hypotheses:

The null hypothesis (H0): The median is equal across all groups.

The alternative hypothesis: (HA): The median is not equal across all groups.

In this case, the test statistic is 6.2878 and the corresponding p-value is 0.0431.

Since this p-value is less than 0.05, we can reject the null hypothesis that the median plant growth is the same for all three fertilizers.

This means we have sufficient evidence to conclude that the type of fertilizer used leads to statistically significant differences in plant growth.

Additional Resources

The following tutorials explain how to perform other common statistical tests in R:

How to Perform a Paired Samples t-test in R
How to Perform a One-Way ANOVA in R
How to Perform a Repeated Measures ANOVA in R

One Reply to “How to Perform a Kruskal-Wallis Test in R”

  1. Thanks for this post. You wrote a Kruskal-Wallis test is the non-parametric equivalent of the One-Way ANOVA. What is the non-parametric equivalent of the Two-Way ANOVA? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *