Open In App

Homoscedasticity Test for Two-Way ANOVA in R

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Two-Way ANOVA is used to examine the effect of two independent variables (factors) on a single continuous dependent variable. For example, if you are studying how Gender and Exercise affect Weight, you could perform a Two-Way ANOVA to analyze the interaction between these two factors.

What is Homoscedasticity?

Homoscedasticity (or homogeneity of variances) refers to the assumption that all groups being compared have the same variance. If this assumption is violated, the ANOVA results might be inaccurate, leading to false conclusions. There are 2 types of tests available for calculating Homoscedasticity.

  1. Levene's Test: Levene's test is a widely used method to test for homogeneity of variances. It is less sensitive to departures from normality compared to other tests, such as Bartlett's test. Levene's test assesses whether the variance across groups is equal.
  2. Bartlett's Test: Bartlett's test is another method for testing the homogeneity of variances. However, it is sensitive to deviations from normality. Bartlett’s test may falsely indicate heteroscedasticity when the data is non-normal.

Both tests return a p-value, and if the p-value is less than the significance level (e.g., 0.05), we reject the null hypothesis that the variances are equal, indicating heteroscedasticity.

Performing Two-Way ANOVA and Testing for Homoscedasticity

We will use the warpbreaks dataset, which includes data on the number of breaks in yarn during weaving for different types of wool and tension levels. This dataset is suitable for performing a Two-Way ANOVA with wool type and tension as the two independent variables.

Step 1: Load the Dataset and Fit a Two-Way ANOVA Model

The aov() function can be used to perform a Two-Way ANOVA in R.

R
# Load the warpbreaks dataset
data("warpbreaks")

# Perform Two-Way ANOVA with wool and tension as factors
aov_model <- aov(breaks ~ wool * tension, data = warpbreaks)

# View the summary of the Two-Way ANOVA
summary(aov_model)

Output:

             Df Sum Sq Mean Sq F value   Pr(>F)    
wool 1 451 450.7 3.765 0.058213 .
tension 2 2034 1017.1 8.498 0.000693 ***
wool:tension 2 1003 501.4 4.189 0.021044 *
Residuals 48 5745 119.7
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

This code fits a Two-Way ANOVA model with an interaction term (wool * tension) and prints the summary of the model.

Step 2: Levene's Test for Homogeneity of Variance

The car package provides the leveneTest() function to perform Levene's test for homoscedasticity.

R
# Install the car package if not already installed
# install.packages("car")

# Load the car package
library(car)

# Perform Levene's test
leveneTest(breaks ~ wool * tension, data = warpbreaks)

Output:

Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 5 2.891 0.02322 *
48
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

The leveneTest() function will output a test statistic and p-value. If the p-value is greater than 0.05, we do not reject the null hypothesis of equal variances, implying homoscedasticity.

Step 3: Bartlett's Test for Homogeneity of Variance

You can also use Bartlett's test for homogeneity of variances, available through the bartlett.test() function in base R.

R
# Perform Bartlett's test with interaction of wool and tension
bartlett_test <- bartlett.test(breaks ~ interaction(wool, tension), data = warpbreaks)
print(bartlett_test)

Output:

	Bartlett test of homogeneity of variances

data: breaks by interaction(wool, tension)
Bartlett's K-squared = 12.977, df = 5, p-value = 0.0236
  • interaction(wool, tension): This creates a single grouping factor that represents the interaction between the two factors, wool and tension. It will create unique groups for each combination of wool type and tension level.
  • bartlett.test(breaks ~ interaction(wool, tension)): This applies Bartlett’s test to the response variable breaks based on the new interaction grouping.

In this case, the p-value (0.2667) is greater than 0.05, meaning we fail to reject the null hypothesis, and the assumption of homoscedasticity holds. The variances across the groups (combinations of wool and tension) are approximately equal.

Conclusion

When testing for homogeneity of variances in a Two-Way ANOVA using Bartlett's test in R, you need to create an interaction term between the two factors to ensure the correct grouping structure. The interaction function allows you to test whether the variances are equal across all groups defined by the combinations of the two factors.


Article Tags :

Similar Reads