Levene’s Test in R Programming
In statistics, Levene’s test is an inferential statistic used to evaluate the equality of variances for a variable determined for two or more groups. Some standard statistical procedures find that variances of the populations from which various samples are formed are equal. Levene’s test assesses this assumption. It examines the null hypothesis that the population variances are equal called homogeneity of variance or homoscedasticity. It compares the variances of k samples, where k can be more than two samples. It’s an alternative to Bartlett’s test that is less sensitive to departures from normality. There are several solutions to test for the equality (homogeneity) of variance across groups, including:
- 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 the 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
- Alternative Hypothesis: At least two of them differ
Implementation in R
R provides a function leveneTest() which is available in 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 data set please type below command:
R
print (PlantGrowth) |
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
As mentioned above, Levene’s test is an alternative to Bartlett’s test when the data is not normally distributed. 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 Homogeneity of Variance (center = median) Df F value Pr(>F) group 2 1.1192 0.3412 27
Levene’s test with multiple independent variables:
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 Homogeneity of Variance (center = median) Df F value Pr(>F) group 5 1.7086 0.1484 54