Bartlett test for homoscedasticity (homogeneity of variance)
Enter the name for this tabbed section: Description
This test evaluates if k samples have been taken from populations with equal variances ? It can be used for instance to test if the conditions are met to carry out analysis of variance.
If normality is unlikely, it is safer to use a non-parametric test such as the Fligner-Killeen test.
If normality is unlikely, it is safer to use a non-parametric test such as the Fligner-Killeen test.
Enter the name for this tabbed section: R Syntax
In R, the test can be performed using the function:
bartlett.test {stats}
bartlett.test(var1, var2, data = my data)
where
var1 is a numerical vectors describing the variable of interest
var2 is a grouping variable (factor) with k levels.
The two variables can either be in separate vectors, or part of a data frame
Bartlett test is quite sensitive to a lack of normality of the underlying distribution. Both the non parametric Fligner-Killeen test and the Levene test are considerably more robust. The Levene test is not available from the base package but is found in both in the car package.
var1 is a numerical vectors describing the variable of interest
var2 is a grouping variable (factor) with k levels.
The two variables can either be in separate vectors, or part of a data frame
Bartlett test is quite sensitive to a lack of normality of the underlying distribution. Both the non parametric Fligner-Killeen test and the Levene test are considerably more robust. The Levene test is not available from the base package but is found in both in the car package.
bartlett.test(var1 ~ var2, data = my data)
Because the test if often performed as part of a linear model analysis lm, aov(), it may also use the formula syntax.
Enter the name for this tabbed section: Example
BartlettTest
M Claereboudt
December 3, 2015
Let’s first create two variables that will be part of the dataframe to be assessed for homoscedasticity (Equality of variance).
# a continuous variable (here counts)
count <- c(250, 260, 230, 270, 310, 330, 280, 360, 250, 230, 220, 260, 340, 270, 300, 320, 250, 240, 270, 290)
# and a grouping variable (here sample)
sample <- gl(5, 4, labels = c("A", "B","C","D","E"))
# We can create a dataframe with the two variables
mydata <- data.frame(count, sample)
Note the use of the function gl() to create the grouping factor. We are now ready to carry out bartlett’s test:
# perform the bartlett' test on the data.
bartlett.test(count, sample, data = mydata)
##
## Bartlett test of homogeneity of variances
##
## data: count and sample
## Bartlett's K-squared = 1.8709, df = 4, p-value = 0.7595
The test reveals that we should not reject the null hypothesis of equality of variance between populations.
Because the test is very often performed as part of an linear model or anova, we can also use the “formula” based syntax
# perform the bartlett' test on the data.
bartlett.test(count~sample, data = mydata)
##
## Bartlett test of homogeneity of variances
##
## data: count by sample
## Bartlett's K-squared = 1.8709, df = 4, p-value = 0.7595
We can investigate the distributions of data using the plot function
plot(count~sample, data = mydata)
We can see that visually, the “spread” of each sample suggest equality of variances.