How to Perform Levene Test in R

Sheeraz Gul Feb 02, 2024
How to Perform Levene Test in R

Levene’s test evaluates the equality of variances for the variables determined for multiple groups. This tutorial demonstrates how to perform Levene’s test in R.

Levene Test in R

Levene’s test evaluates the equality of variances for the variables determined for multiple groups. The test examines the null hypothesis that the population variances are called homogeneity or homoscedasticity and will compare the variance of k samples, where k can be multiple samples.

Levene’s test is an alternative to Barlett’s test, which is less sensitive. The R language has a method leveneTest() to perform the Levene’s test; this method is from the care package of the R language.

First, we need to install the car package if it is not already installed.

install.packages('car')

Once the car package is installed, we can perform the Levene test.

Code Example:

library(car)
person_weight <- data.frame(weight_program = rep(c("Program1", "Program2", "Program3"), each = 40),
                   weight_loss = c(runif(40, 0, 4),
                                   runif(40, 0, 6),
                                   runif(40, 1, 8)))

#first six rows of data frame
head(person_weight)

#conduct Levene's Test to check equality of variances
leveneTest(weight_loss ~ weight_program, data = person_weight)

The snippet above creates a data frame with three different weight loss programs, which shows how much weight people have lost in different programs. First, the code shows the head of the data frame and then performs Levene’s test.

Output:

  weight_program weight_loss
1       Program1    1.027946
2       Program1    2.147631
3       Program1    3.181947
4       Program1    3.933521
5       Program1    3.093434
6       Program1    3.652795

Levene's Test for Homogeneity of Variance (center = median)
       Df F value   Pr(>F)
group   2  5.2128 0.006782 **
      117
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The p-value of Levene’s test is 0.006782, which is less than the significance level of 0.05. So based on Levene’s test concept, we reject the null hypothesis, so the variance among the three groups is not equal.

Let’s plot the box graph to show the weight distribution.

boxplot(weight_loss ~ weight_program,
        data = person_weight,
        main = "Weight Loss Distribution by the Weight Program",
        xlab = "Weight Program",
        ylab = "Weight Loss",
        col = "grey90",
        border = "black")

Box Plot Output:

plot the box graph to show the weight distribution

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - R Test