How to Perform the Friedman Test in R (original) (raw)

Last Updated : 23 Jul, 2025

The Friedman Test is a non-parametric statistical test used to determine if there are statistically significant differences between multiple related groups. It is often employed when the outcome variable is ordinal or continuous and the independent variable is categorical with three or more levels. This comprehensive guide will walk you through the process of conducting the Friedman Test in R, including data preparation, hypothesis testing, and result interpretation.

Here are the step-by-step explanations of the Friedman Test in R Programming Language.

Step 1. Install and Load Required Packages

Before conducting the Friedman Test, make sure you have the necessary packages installed and loaded:

install.packages("PMCMRplus")
library(PMCMRplus)

Step 2. Prepare Your Data

Start by loading your dataset into R and ensuring that it is structured appropriately for analysis. The dataset should contain one or more outcome variables (dependent variables) and a categorical factor variable (independent variable) with three or more levels.

Step 3. Conduct the Friedman Test

Use the friedman.test() function from the PMCMRplus package to conduct the Friedman Test:

result <- friedman.test(outcome ~ group, data = your_data)

Replace outcome with the name of your outcome variable, group with the name of your categorical factor variable, and your_data with the name of your dataset.

Step 4. Interpret the Results

After conducting the Friedman Test, you can obtain the test statistics, degrees of freedom, and p-value:

If the p-value is less than your chosen significance level (e.g., 0.05), you can reject the null hypothesis and conclude that there are statistically significant differences between the groups. Otherwise, you fail to reject the null hypothesis.

Conducting the Friedman Test in R

Here's a complete example demonstrating how to conduct the Friedman Test in R using a hypothetical dataset:

R `

Load the PMCMRplus package

library(PMCMRplus)

Generate a hypothetical dataset

set.seed(123) subject <- rep(1:30, times = 3) # 30 subjects group <- rep(c("A", "B", "C"), each = 30) outcome <- c(rnorm(30), rnorm(30, mean = 1), rnorm(30, mean = 2)) data <- data.frame(subject, group, outcome)

Convert data to wide format

data_wide <- dcast(data, subject ~ group, value.var = "outcome")

Perform the Friedman test

result <- friedman.test(as.matrix(data_wide[,-1]))

`

**Output:

Friedman rank sum test

data: as.matrix(data_wide[, -1])
Friedman chi-squared = 33.8, df = 2, p-value = 4.575e-08

The p-value is (4.575e-08), we reject the null hypothesis. This means there is strong evidence to conclude that there are significant differences in the ranks of the outcome variable among the groups A, B, and C.

Conclusion

The Friedman rank sum test results indicate that the differences in ranks among the groups are statistically significant, as evidenced by the high test statistic and the extremely low p-value. This suggests that at least one of the groups is different from the others in terms of the outcome variable. The box plot visualization helps to see these differences more clearly.