Levene’s Test in R Programming - GeeksforGeeks (2024)

Last Updated : 16 Mar, 2023

Improve

Levene’s test is an inferential statistic used to check if the variances of a variable obtained for two or more groups are equal or not when data comes from a non-normal distribution.

Levene’s test is used to check the assumptions that the variances of the populations from different samples drawn are equal or not before running the test like ANOVA. It tests the null hypothesis that the population variances are equal or not, It is known as hom*oscedasticity. It’s an alternative to Bartlett’s test that is less sensitive to departures from normality.

There are several solutions to test for the hom*ogeneity of variance (or hom*oscedasticity) across groups of samples are as follows:

  • F-Test
  • Bartlett’s test
  • Levene’s test
  • Fligner-Killeen test

It is very much easy to perform these tests in R programming. In this article let’s perform Levene’s test in R.

Statistical Hypotheses for Levene’s test

A hypothesis is a statement about a given problem. Hypothesis testing is a statistical method that is used in making a statistical decision using experimental data. Hypothesis testing is basically an assumption that we make about a population parameter. It evaluates two mutually exclusive statements about a population to determine which statement is best supported by the sample data. To know more about the statistical hypothesis please refer to Understanding Hypothesis Testing. For Levene’s test, the statistical hypotheses are:

Null Hypothesis: All populations variances are equal

Levene’s Test in R Programming - GeeksforGeeks (1)

Alternative Hypothesis: At least two of them differ

Levene’s Test in R Programming - GeeksforGeeks (2)

The test statistics for Levene’s test are:

Levene’s Test in R Programming - GeeksforGeeks (3)

Levene’s Test in R

R provides a function leveneTest() which is available in the car package that can be used to compute Levene’s test. The syntax for this function is given below:

Syntax: leveneTest(formula, dataset)

Parameters:

formula: a formula of the form values ~ groups

dataset: a matrix or data frame

Example of Lavene’s test

Levene’s test with one independent variable:

Consider the R’s inbuilt PlantGrowth dataset that gives the dried weight of three groups of ten batches of plants, wherever every group of ten batches got a different treatment. The weight variable gives the weight of the batch and the group variable gives the treatment received either ctrl, trt1, or trt2. To view the random 5 rows of the PlantGrowth dataset use the sample_n() function from the dplyr library.

R

 
 

Output:

 weight group1 3.59 trt12 4.17 trt13 4.50 ctrl4 5.14 ctrl5 4.92 trt2

As mentioned above, Levene’s test is an alternative to Bartlett’s test when the data is not normally distributed. So, we consider the null and alternate hypotheses.

  • The Null hypothesis is variances across all samples are equal.
  • The alternative hypothesis is at least one sample has a different variance.
  • We will test the null hypothesis at 0.05 significance level i.e 95% percentile.

Here let’s consider only one independent variable. To perform the test, use the below command:

R

# R program to illustrate

# Levene’s test

# Import required package

library(car)

# Using leveneTest()

result = leveneTest(weight ~ group, PlantGrowth)

# print the result

print(result)

 
 

Output:

Levene's Test for hom*ogeneity of Variance (center = median) Df F value Pr(>F)group 2 1.1192 0.3412 27 

From the above result, we can observe that p-value = 0.34 which is greater than our significance level of 0.05. So, we do have not enough evidence to reject the null hypothesis. So the variance across the samples is equal at 0.05 significance level.

Df

F value

Pr(>F)

<int>

<dbl>

<dbl>

group

2

1.119186

0.3412266

27

NA

NA

Levene’s test with multiple independent variables:

Let’s consider the R’s inbuilt ToothGrowth dataset

R

#import the dplyr library

library("dplyr")

# Print the random 5 sample

print(sample_n(ToothGrowth,5))

 
 

Output:

 len supp dose1 23.6 VC 22 15.5 VC 13 16.5 VC 14 23.0 OJ 25 17.3 VC 1

If one wants to do the test with multiple independent variables then the interaction() function must be used to collapse multiple factors into a single variable containing all combinations of the factors. Here let’s take the R’s inbuilt ToothGrowth data set.

R

# R program to illustrate

# Levene’s test

# Import required package

library(car)

# Using leveneTest()

result = leveneTest(len ~ interaction(supp, dose),

data = ToothGrowth)

# print the result

print(result)

 
 

Output:

Levene's Test for hom*ogeneity of Variance (center = median) Df F value Pr(>F)group 5 1.7086 0.1484 54

From the above result, we can observe that p-value = 0.14 which is greater than our significance level of 0.05. So, we do have not enough evidence to reject the null hypothesis. So the variance across the samples is equal at 0.05 significance level.

Df

F value

Pr(>F)

<int>

<dbl>

<dbl>

group

5

1.708578

0.1483606

54

NA

NA



Like Article

Suggest improvement

Previous

TOPSIS method for Multiple-Criteria Decision Making (MCDM)

Next

MANOVA Test in R Programming

Share your thoughts in the comments

Please Login to comment...

Levene’s Test in R Programming - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 6037

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.