Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Kruskal-Wallis test in R Programming
Next article icon

Kruskal-Wallis test in R Programming

Last Updated : 16 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The Kruskal–Wallis test in R Programming Language is a rank-based test that is similar to the Mann–Whitney U test but can be applied to one-way data with more than two groups. It is a non-parametric alternative to the one-way ANOVA test, which extends the two-samples Wilcoxon test. A group of data samples is independent if they come from unrelated populations and the samples do not affect each other. Using the Kruskal-Wallis Test, it can be decided whether the population distributions are similar without assuming them to follow the normal distribution. It is very much easy to perform Kruskal-Wallis test in the R language.

Note: The outcome of the Kruskal–Wallis test tells that if there are differences among the groups, but doesn’t tell which groups are different from other groups. 

Examples:

  1. Let one wants to find out how socioeconomic status influences attitude towards sales tax hikes. Here the independent variable is “socioeconomic status” with three levels: working-class, middle-class, and wealthy. The dependent variable is measured on a 5-point Likert scale from strongly agree to strongly disagree.
  2. If one wants to find out how test anxiety influences actual test scores. The independent variable “test anxiety” has three levels: no anxiety, low-medium anxiety, and high anxiety. The dependent variable is the exam score and it is rated from 0 to 100%.

Assumptions for the Kruskal-Wallis test in R

The variables should have:

  • One independent variable with two or more levels. The test is more commonly used when there are three or more levels. For two levels instead of the Kruskal-Wallis test consider using the Mann Whitney U Test.
  • The dependent variable should be the Ordinal scale, Ratio Scale, or Interval scale.
  • The observations should be independent. In other words, there should be no correlation between the members in every group or within groups.
  • All groups should have identical shape distributions.

Implementation in R

R provides a method kruskal.test() which is available in the stats package to perform a Kruskal-Wallis rank-sum test.

Syntax: kruskal.test(x, g, formula, data, subset, na.action, …)

Parameters:

  • x: a numeric vector of data values, or a list of numeric data vectors.
  • g: a vector or factor object giving the group for the corresponding elements of x
  • formula: a formula of the form response ~ group where response gives the data values and group a vector or factor of the corresponding groups.
  • data: an optional matrix or data frame containing the variables in the formula .
  • subset: an optional vector specifying a subset of observations to be used.
  • na.action: a function which indicates what should happen when the data contain NA

...: further arguments to be passed to or from methods.

Example:

Let's use the built-in R data set named PlantGrowth. It contains the weight of plants obtained under control and two different treatment conditions. 

R
# Preparing the data set # to perform Kruskal-Wallis Test  # Taking the PlantGrowth data set myData = PlantGrowth print(myData)  # Show the group levels print(levels(myData$group)) 

 Output:

    weight group 1    4.17  ctrl 2    5.58  ctrl 3    5.18  ctrl 4    6.11  ctrl 5    4.50  ctrl 6    4.61  ctrl 7    5.17  ctrl 8    4.53  ctrl 9    5.33  ctrl 10   5.14  ctrl 11   4.81  trt1 12   4.17  trt1 13   4.41  trt1 14   3.59  trt1 15   5.87  trt1 16   3.83  trt1 17   6.03  trt1 18   4.89  trt1 19   4.32  trt1 20   4.69  trt1 21   6.31  trt2 22   5.12  trt2 23   5.54  trt2 24   5.50  trt2 25   5.37  trt2 26   5.29  trt2 27   4.92  trt2 28   6.15  trt2 29   5.80  trt2 30   5.26  trt2 [1] "ctrl" "trt1" "trt2"

Here the column “group” is called factor and the different categories (“ctr”, “trt1”, “trt2”) are named factor levels. The levels are ordered alphabetically. The problem statement is we want to know if there is any significant difference between the average weights of plants in the 3 experimental conditions. And the test can be performed using the function kruskal.test() as given below.

R
# R program to illustrate # Kruskal-Wallis Test  # Taking the PlantGrowth data set myData = PlantGrowth  # Performing Kruskal-Wallis test result = kruskal.test(weight ~ group,                     data = myData) print(result) 

 Output:

Kruskal-Wallis rank sum test

data:  weight by group

Kruskal-Wallis chi-squared = 7.9882, df = 2, p-value = 0.01842

Explanation:

As the p-value is less than the significance level 0.05, it can be concluded that there are significant differences between the treatment groups.


Next Article
Kruskal-Wallis test in R Programming

A

AmiyaRanjanRout
Improve
Article Tags :
  • Data Science
  • R Language
  • R Data-science

Similar Reads

    Mann Whitney U Test in R Programming
    A popular nonparametric(distribution-free) test to compare outcomes between two independent groups is the Mann Whitney U test. When comparing two independent samples, when the outcome is not normally distributed and the samples are small, a nonparametric test is appropriate. It is used to see the di
    4 min read
    Levene’s Test in R Programming
    Levene's test is an inferential statistic used to assess whether the variances of a variable are equal across two or more groups, especially when the data comes from a non-normal distribution. This test checks the assumption of homoscedasticity (equal variances) before conducting tests like ANOVA. I
    3 min read
    Shapiro–Wilk Test in R Programming
    The Shapiro-Wilk's test or Shapiro test is a normality test in frequentist statistics. The null hypothesis of Shapiro's test is that the population is distributed normally. It is among the three tests for normality designed for detecting all kinds of departure from normality. If the value of p is eq
    4 min read
    How to Perform Post Hoc Test for Kruskal-Wallis in R
    The Kruskal-Wallis test is a non-parametric statistical test used to determine if there are significant differences between the medians of three or more independent groups. While the Kruskal-Wallis test can tell us whether there's an overall significant difference, it does not pinpoint which specifi
    4 min read
    8 Coding Style Tips for R Programming
    R is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. Also, the R programming language is the latest cutting-edge to
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences