Levene’s Test in R Programming

25 Aug, 2020
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.
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
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
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 packagelibrary(car)  # Using leveneTest()result = leveneTest(weight ~ group, PlantGrowth)  # print the resultprint(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 packagelibrary(car)  # Using leveneTest()result = leveneTest(len ~ interaction(supp, dose),                     data = ToothGrowth)  # print the resultprint(result)
Output:
Levene's Test for Homogeneity of Variance (center = median)
      Df F value Pr(>F)
group  5  1.7086 0.1484
      54               

data-science
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
R - if statement
Logistic Regression in R Programming
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to import an Excel File into R ?
Joining of Dataframes in R Programming