Levene’s Test in R Programming
Last Updated :
16 Apr, 2025
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.
In this article let's perform Levene’s test in R.
Statistical Hypothesis for Levene's test
A hypothesis is a statement about a given problem, and hypothesis testing is a statistical method used to make decisions based on experimental data. It involves evaluating two mutually exclusive statements about a population to determine which is best supported by the sample data. For Levene's test, the statistical hypotheses areFor Levene's test, the statistical hypotheses are:
Null Hypothesis:
All populations variances are equal
H_0 :\sigma_1^2 = \sigma_2^2 = ...=\sigma_n^2
Alternative Hypothesis:
At least two of them differ
H_1 : \sigma_i^2 \neq \sigma_j^2
The test statistics for Levene’s test are:
W = \frac{\left ( N - k \right )\sum_{i=1}^{K}N_{i} \left ( Z_{i} - Z.. \right )^2}{ \left ( K-1 \right )\sum_{i=1}^{K} \sum_{j=1}^{N_i}\left ( Z_{ij}- Z_i \right )^2}
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.
Syntax: leveneTest(formula, dataset)
1. 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
library("dplyr")
print(sample_n(PlantGrowth,5))
Output:
Sample_DataWe will test the null hypothesis at 0.05 significance level i.e 95% percentile, considering only one independent variable.
R
install.packages("car")
library(car)
result = leveneTest(weight ~ group, PlantGrowth)
print(result)
Output:
OutputFrom the 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.
2. Levene's test with multiple independent variables:
Let's consider the R's inbuilt ToothGrowth dataset
R
library("dplyr")
print(sample_n(ToothGrowth,5))
Output:
Sample DataIf 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.
R
install.packages("car")
library(car)
result = leveneTest(len ~ interaction(supp, dose),
data = ToothGrowth)
print(result)
Output:
OutputFrom the 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.
Other common methods to test for Homoscedasticity of variance across groups include:
Similar Reads
Fligner-Killeen Test in R Programming
The Fligner-Killeen test is a non-parametric test for homogeneity of group variances based on ranks. It is useful when the data are non-normally distributed or when problems related to outliers in the dataset cannot be resolved. It is also one of the many tests for homogeneity of variances which is
3 min read
Bartlettâs Test in R Programming
In statistics, Bartlett's test is used to test if k samples are from populations with equal variances. Equal variances across populations are called homoscedasticity or homogeneity of variances. Some statistical tests, for example, the ANOVA test, assume that variances are equal across groups or sam
5 min read
Fisherâs F-Test in R Programming
In this article, we will delve into the fundamental concepts of the F-Test, its applications, assumptions, and how to perform it using R programming. We will also provide a step-by-step guide with examples and visualizations to help you master the F-Test in the R Programming Language.What is Fisherâ
4 min read
Kolmogorov-Smirnov Test in R Programming
Kolmogorov-Smirnov (K-S) test is a non-parametric test employed to check whether the probability distributions of a sample and a control distribution, or two samples are equal. It is constructed based on the cumulative distribution function (CDF) and calculates the greatest difference between the em
4 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
T-Test Approach in R Programming
The T-Test is a statistical method used to determine whether there is a significant difference between the means of two groups or between a sample and a known value.For Example: businessman who owns two sweet shops in a town. He wants to know if there's a significant difference in the average number
5 min read
Unit Testing in R Programming
The unit test basically is small functions that test and help to write robust code. From a robust code we mean a code which will not break easily upon changes, can be refactored simply, can be extended without breaking the rest, and can be tested with ease. Unit tests are of great use when it comes
5 min read
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
Kruskal-Wallis test in R Programming
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 s
4 min read
Learn R Programming
R is a Programming Language that is mostly used for machine learning, data analysis, and statistical computing. It is an interpreted language and is platform independent that means it can be used on platforms like Windows, Linux, and macOS. In this R Language tutorial, we will Learn R Programming La
15+ min read