How can I perform a Kruskal-Wallis test in R?

How can I perform a Kruskal-Wallis test in R?

The Kruskal-Wallis test is a non-parametric statistical test used to determine if there are significant differences between three or more independent groups. In order to perform this test in R, you will need to first load the necessary packages, such as “pgirmess” or “agricolae”, which contain the function for conducting the test. Next, you will need to input your data in the appropriate format, with each group being represented as a separate column. Then, using the function “kruskal.test()”, you can perform the Kruskal-Wallis test and obtain the test statistic, p-value, and other relevant information. This test is useful for comparing groups that do not follow a normal distribution or have unequal variances. It is commonly used in research and data analysis, and can provide valuable insights into the differences between groups.

Perform a Kruskal-Wallis Test in R


A 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 .

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
head(df)

  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.

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:

Leave a Reply

Slide Up
x
Scroll to Top