The Wayback Machine - https://web.archive.org/web/20240828110756/https://www.geeksforgeeks.org/homogeneity-of-variance-test-in-r-programming/
Open In App

Homogeneity of Variance Test in R Programming

Last Updated : 12 Oct, 2020
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In statistics, a sequence of random variables is homoscedastic if all its random variables have the same finite variance. This is also known as homogeneity of variance. In this article, let’s explain methods for checking the homogeneity of variances test in R programming across two or more groups. Some statistical tests, such as two independent samples T-test and ANOVA test, assume that variances are equal across groups. There are various variance tests that can be used to evaluate the equality of variances. These include:

  • F-test: It compares the variances of two groups. The data must be normally distributed in this test.
  • Bartlett’s test: It compares the variances of two or more groups. The data must be normally distributed in this test also.
  • Levene’s test: A robust alternative to Bartlett’s test that is less sensitive to deviations from normality.
  • Fligner-Killeen test: A non-parametric test that is very robust against departures from normality.

Preparing the Data Set

Before explaining each test let’s prepare and understand the data set first. Consider one of the standard learning data sets included in R is the “ToothGrowth” data set. The tooth growth data set is the length of the teeth in each of 10 guinea pigs at three vitamin C dosage levels (0.5, 1, and 2 mg) with two delivery methods (orange juice or ascorbic acid). The file contains 60 observations of 3 variables

  • len: Tooth length
  • supp: Supplement type (VC or OJ)
  • dose: Dose in milligrams

R




# Exploring the ToothGrowth data set
print(head(ToothGrowth, 10))
print(str(ToothGrowth))


Output:

    len  supp dose
1   4.2   VC  0.5
2  11.5   VC  0.5
3   7.3   VC  0.5
4   5.8   VC  0.5
5   6.4   VC  0.5
6  10.0   VC  0.5
7  11.2   VC  0.5
8  11.2   VC  0.5
9   5.2   VC  0.5
10  7.0   VC  0.5
'data.frame':    60 obs. of  3 variables:
 $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
 $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
 $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
NULL

F-test

It is used to compare the variances of the two groups. The F-test is used to evaluate whether the variances of two populations are equal or not. The data must be normally distributed in this test.

Statistical Hypothesis:

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 F-test the statistical hypotheses are:

  • Null Hypothesis: The variances of the two groups are equal
  • Alternative Hypothesis: The variances are different

Implementation in R:

With the help of var.test() method, one can perform the f-test between two normal populations with some hypothesis that variances of two populations are equal in R programming.

Syntax:

var.test(formula, dataset)

 

Parameters:

formula: a formula of the form values ~ groups

dataset: a matrix or data frame

Example:

R




# R program to illustrate
# F-test
  
# Using var.test()
result = var.test(len ~ supp, data = ToothGrowth)
  
# print the result
print(result)


Output:

F test to compare two variances

data:  len by supp
F = 0.6386, num df = 29, denom df = 29, p-value = 0.2331
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.3039488 1.3416857
sample estimates:
ratio of variances 
         0.6385951 

Interpretation:

The p-value is p = 0.2 which is greater than the significance level 0.05. In conclusion, there is no significant difference between the two variances.

Bartlett’s test

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 samples. The Bartlett test can be used to verify that assumption. Bartlett’s test enables us to compare the variance of two or more samples to decide whether they are drawn from populations with equal variance. It is fitting for normally distributed data.

Statistical Hypothesis:

  • Null Hypothesis: All populations variances are equal
  • Alternative Hypothesis: At least two of them differ

Implementation in R:

The R provides a function bartlett.test() which is available in stats package can be used to compute Barlett’s test. The syntax for this function is given below:

Syntax:

bartlett.test(formula, dataset)

 

Parameters:

formula: a formula of the form values ~ groups

dataset: a matrix or data frame

Example:

R




# R program to illustrate
# Barlett’s test
  
# Using bartlett.test()
result = bartlett.test(len ~ supp, data = ToothGrowth)
  
# print the result
print(result)


Output:

Bartlett test of homogeneity of variances

data:  len by supp
Bartlett's K-squared = 1.4217, df = 1, p-value = 0.2331

Levene’s test

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. 

Statistical Hypothesis:

  • Null Hypothesis: All populations variances are equal
  • Alternative Hypothesis: At least two of them differ

Implementation in R:

The 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:

R




# R program to illustrate
# Levene's test
  
# Import required package
library(car)
  
# Using leveneTest()
result = leveneTest(len ~ supp, data = ToothGrowth)
  
# print the result
print(result)


Output:

Levene's Test for Homogeneity of Variance (center = median)
      Df F value Pr(>F)
group  1  1.2136 0.2752
      58     

Fligner-Killeen test

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 most robust against departures from normality.

Statistical Hypothesis:

  • Null Hypothesis: All populations variances are equal
  • Alternative Hypothesis: At least two of them differ

Implementation in R:

The R provides a function fligner.test() which is available in stats package that can be used to compute the Fligner-Killeen test. The syntax for this function is given below:

Syntax:

fligner.test(formula, dataset)

 

Parameters:

formula: a formula of the form values ~ groups

dataset: a matrix or data frame

Example:

R




# R program to illustrate
# Fligner-Killeen test
  
# Import required package
library(stats)
  
# Using fligner.test()
result = fligner.test(len ~ supp, data = ToothGrowth)
  
# print the result
print(result)


Output:

Fligner-Killeen test of homogeneity of variances

data:  len by supp
Fligner-Killeen:med chi-squared = 0.97034, df = 1, p-value = 0.3246



Similar Reads

ANOVA (Analysis of Variance) Test in R Programming
ANOVA also known as Analysis of variance is used to investigate relations between categorical variables and continuous variables in the R Programming Language. It is a type of hypothesis testing for population variance. It enables us to assess whether observed variations in means are statistically significant or merely the result of chance by compa
8 min read
Upper Tail Test of Population Mean with Unknown Variance in R
A statistical hypothesis test is a method of statistical inference used to decide whether the data at hand sufficiently support a particular hypothesis. The conventional steps that are followed while formulating the hypothesis test, are listed as follows State null hypothesis (Ho) and alternate hypothesis (Ha)Collect a relevant sample of data to te
3 min read
Lower Tail Test of Population Mean with Unknown Variance in R
This article will discuss the lower Two-Tailed Test of Population Mean with Unknown Variance in R Programming Language. Conventionally, In a lower tail test is used in null-hypothesis testing. The lower tail test of the population means the null hypothesis can be expressed as follows μ ≥ μo. A statistical hypothesis test is a method of statistical
3 min read
Type II Error in Lower Tail Test of Population Mean with Unknown Variance in R
Conventionally, In a lower-tail test, the null hypothesis states that the true population mean (μo) is greater than the hypothesized mean value (μ). We fail to reject the null hypothesis if the test statistic is greater than the critical value at the chosen significance level. In this article let us discuss the probability percentage of type II err
3 min read
Type II Error in Two-Tailed Test of Population Mean with Known Variance in R
Conventionally, In a two-tail test, the null hypothesis states that the true population mean (μo) is equal to the hypothesized mean value (μ). We fail to reject the null hypothesis if the test statistic lies within the range of critical values at the chosen significance level. In this article let us discuss the probability percentage of type II err
2 min read
Type II Error in Upper Tail Test of Population Mean with Unknown Variance in R
Conventionally, In an upper-tail test, the null hypothesis states that the true population mean (μo) is less than the hypothesized mean value (μ). We fail to reject the null hypothesis if the test statistic is lesser than the critical value at the chosen significance level. In this article let us discuss the probability percentage of type II error
3 min read
Two-Tailed Test of Population Mean with Unknown Variance in R
Conventionally, In a two-tail test, the null hypothesis states that the true population mean (μo) is equal to the hypothesized mean value (μ). We fail to reject the null hypothesis if the test statistic lies within the range of critical values at the chosen significance level. In this article let us discuss the probability percentage of type II err
3 min read
Type II Error in Upper Tail Test of Population Mean with Known Variance in R
Conventionally, In an upper-tail test, the null hypothesis states that the true population mean (μo) is less than the hypothesized mean value (μ). We fail to reject the null hypothesis if the test statistic is lesser than the critical value at the chosen significance level. In this article let us discuss the probability percentage of type II error
3 min read
Type II Error in Two-Tailed Test of Population Mean with Unknown Variance in R
In this article, we will cover type II Errors in the Two-Tailed Test of Population Mean with Unknown Variance. Conventionally, In a two-tail test is used in null-hypothesis testing. The null hypothesis (μo) is equal to the hypothesized mean value (μ). We fail to reject the null hypothesis if the test statistic lies within the range of critical valu
3 min read
Lower Tail Test of Population Mean with Known Variance in R
A statistical hypothesis test is a method of statistical inference used to decide whether the data at hand sufficiently support a particular hypothesis. The conventional steps that are followed while formulating the hypothesis test, are listed as follows State null hypothesis (Ho) and alternate hypothesis (Ha)Collect a relevant sample of data to te
2 min read
Upper Tail Test of Population Mean with Known Variance in R
A statistical hypothesis test is a method of statistical inference used to decide whether the data at hand sufficiently support a particular hypothesis. The conventional steps that are followed while formulating the hypothesis test, are listed as follows State null hypothesis (Ho) and alternate hypothesis (Ha)Collect a relevant sample of data to te
3 min read
Two-Tailed Test of Population Mean with Known Variance in R
A statistical hypothesis test is a method of statistical inference used to decide whether the data at hand sufficiently support a particular hypothesis. The conventional steps that are followed while formulating the hypothesis test, are listed as follows State null hypothesis (Ho) and alternate hypothesis (Ha)Collect a relevant sample of data to te
3 min read
Type II Error in Lower Tail Test of Population Mean with Known Variance in R
Conventionally, In a lower-tail test, the null hypothesis states that the true population mean (μo) is greater than the hypothesized mean value (μ). We fail to reject the null hypothesis if the test statistic is greater than the critical value at the chosen significance level. In this article let us discuss the probability percentage of type II err
3 min read
Compute Variance and Standard Deviation of a value in R Programming - var() and sd() Function
var() function in R Language computes the sample variance of a vector. It is the measure of how much value is away from the mean value. Syntax: var(x) Parameters: x : numeric vector Example 1: Computing variance of a vector # R program to illustrate # variance of vector # Create example vector x <- c(1, 2, 3, 4, 5, 6, 7) # Apply var function in
1 min read
Calculate the Average, Variance and Standard Deviation in R Programming
R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. R language provides very easy methods to calculate the average, variance, and standard devi
4 min read
Performing Binomial Test in R programming - binom.test() Method
With the help of binom.test() method, we can get the binomial test for some hypothesis of binomial distribution in R Programming. Syntax: binom.test(x, n, p-value) Return: Returns the value of binomial test. Example 1: # Using binom.test() method gfg <- binom.test(58, 100) print(gfg) Output: Exact binomial test data: 58 and 100 number of success
1 min read
Performing F-Test in R programming - var.test() Method
var.test() Method in R Programming language perform the f-test between two normal populations with some hypothesis that variances of two populations are equal in R programming. var.test() Method in R Syntax Syntax: var.test() Return: Returns the F-Test score for some hypothesis. var.test() Method in R Programming language Example Example 1: C/C++ C
1 min read
Calculate the average, variance and standard deviation in Python using NumPy
Numpy in Python is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Numpy provides very easy methods to calculate the average, variance, and standard deviation. Average Average a number
5 min read
Ordinary Least Squares and Ridge Regression Variance in Scikit Learn
In statistical modeling, Ordinary Least Squares (OLS) and Ridge Regression are two widely used techniques for linear regression analysis. OLS is a traditional method that finds the line of best fit through the data by minimizing the sum of the squared errors between the predicted and actual values. However, OLS can suffer from high variance and ove
7 min read
Differences between Bias, Variance and Residuals?
Answer: Bias is the error introduced by approximating a real-world problem, variance is the model's sensitivity to small fluctuations in the training data, and residuals are the differences between predicted and actual values.Differences between Bias, Variance and Residuals in Tabular form : AspectBiasVarianceResidualsDefinitionError due to simplif
1 min read
What is Zero Mean and Unit Variance Normalization
Answer: Zero Mean and Unit Variance normalization rescale data to have a mean of zero and a standard deviation of one.Explanation:Mean Centering: The first step of Zero Mean normalization involves subtracting the mean value of each feature from all data points. This centers the data around zero, meaning that the new mean of the feature becomes zero
2 min read
Function that calculates mean, variance, and skewness simultaneously in a dataframe in R
In statistical analysis, understanding the central tendency (mean), dispersion (variance), and asymmetry (skewness) of data is essential for gaining insights into its distribution and characteristics. This article explores how to compute these three statistical measures simultaneously across multiple variables in a data frame using R Programming La
3 min read
Differences Between two-sample, t-test and paired t-test
Statistical tests are essential tools in data analysis, helping researchers make inferences about populations based on sample data. Two common tests used to compare the means of different groups are the two-sample t-test and the paired t-test. Both tests are based on the t-distribution, but they have distinct use cases and assumptions. In this arti
7 min read
T-Test Approach in R Programming
We will be trying to understand the T-Test in R Programming with the help of an example. Suppose a businessman with two sweet shops in a town wants to check if the average number of sweets sold in a day in both stores is the same or not. So, the businessman takes the average number of sweets sold to 15 random people in the respective shops. He foun
6 min read
One-Proportion Z-Test in R Programming
The One proportion Z-test is used to compare an observed proportion to a theoretical one when there are only two categories. For example, we have a population of mice containing half male and half females (p = 0.5 = 50%). Some of these mice (n = 160) have developed spontaneous cancer, including 95 males and 65 females. We want to know, whether canc
4 min read
Kolmogorov-Smirnov Test in R Programming
The Kolmogorov-Smirnov Test is a type of non-parametric test of the equality of discontinuous and continuous a 1D probability distribution that is used to compare the sample with the reference probability test (known as one-sample K-S Test) or among two samples (known as two-sample K-S test). A K-S Test quantifies the distance between the cumulativ
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 equal to or less than 0.05, then the hypothesis of n
4 min read
Two-Proportions Z-Test in R Programming
A two-proportion z-test allows us to compare two proportions to see if they are the same. It calculates the range of values that is likely to include the difference between the population proportions. The z-test is based on a standard normal distribution. It has a critical value i.e. 1.96. for 5% two-tailed. In R, the function used for performing a
6 min read
Fisher’s F-Test in R Programming
Fisher’s F test calculates the ratio between the larger variance and the smaller variance. We use the F test when we want to check whether the means of three or more groups are different or not. F-test is used to assess whether the variances of two populations (A and B) are equal. The method is simple; it consists of taking the ratio between the la
4 min read
Wilcoxon Signed Rank Test in R Programming
The Wilcoxon signed-rank test is a non-parametric statistical hypothesis test used to compare two related samples, matched samples, or repeated measurements on a single sample to estimate whether their population means ranks differ e.g. it is a paired difference test. It can be applied as an alternative to the paired Student's t-test also known as
7 min read