A test for homogeneity of variances across two or more groups, more robust to departures from normality than Bartlett's test.
Usage
leveneTest(x, ...)
# S3 method for class 'formula'
leveneTest(formula, data, subset, na.action = na.pass, center = median, ...)
# Default S3 method
leveneTest(x, g, center = median, .centerName = NULL, ...)Arguments
- x
a numeric vector of data values (default method), or a
formula(formula method).- ...
arguments to be passed down, e.g.
datafor the formula method; can also be used to pass arguments to the function given bycenter(e.g.trim = 0.1for a trimmed mean whencenter = mean).- formula
a formula of the form
lhs ~ rhswherelhsgives the data values andrhsthe corresponding groups.- data
an optional matrix or data frame (or similar: see
model.frame()) containing the variables in the formulaformula. By default the variables are taken fromenvironment(formula).- subset
an optional vector specifying a subset of observations to be used.
- na.action
a function which indicates what should happen when the data contain
NAs. Defaults togetOption("na.action").- center
the name of a function to compute the center of each group;
meangives the original Levene's test, the default,median, provides the more robust Brown-Forsythe test.- g
factor defining the groups; ignored if
xis a list.- .centerName
internal, not intended to be set by the user. Used to pass the deparsed name of the
centerfunction through the method dispatch chain (fromleveneTest.formulatoleveneTest.default), sincesubstitute(center)would otherwise only resolve to the literal symbol"center"rather than the original expression (e.g.meanormedian) supplied by the caller.
Details
Let \(X_{ij}\) be the jth observation of X for the ith group. Let \(Z_{ij} = |X_{ij} - X_i|\), where \(X_i\) is the center (by default the median) of X in the ith group. Levene's test statistic is $$W_0 = \frac{\sum_i n_i (\bar{Z}_i - \bar{Z})^2 / (g - 1)}{\sum_i \sum_j (Z_{ij} - \bar{Z}_i)^2 / \sum_i (n_i - 1)}$$ where \(n_i\) is the number of observations in group i and g is the number of groups. Using the median instead of the mean for \(X_i\) yields the more robust variant known as the Brown-Forsythe test, which is the default here.
Note
Based on car::leveneTest() by John Fox, with contributions by
Derek Ogle and Brian Ripley, adapted to conform to package standards.
References
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, 3rd ed., Thousand Oaks, CA: Sage.
Levene, H. (1960) Robust tests for equality of variances. In: Olkin, I. et al., eds.: Contributions to Probability and Statistics: Essays in Honor of Harold Hotelling, Stanford University Press, pp. 278-292.
See also
fligner.test() for a rank-based (nonparametric)
k-sample test for homogeneity of variances, bartlett.test()
for a parametric alternative
Other test.variance:
mosesTest(),
siegelTukeyTest(),
varCI(),
varTest()
Examples
## example from ansari.test:
## Hollander & Wolfe (1973, p. 86f):
## Serum iron determination using Hyland control sera
serum <- data.frame(
grp = rep(c("ramsay", "jung.parekh"), each = 20),
x = c(111, 107, 100, 99, 102, 106, 109, 108, 104, 99,
101, 96, 97, 102, 107, 113, 116, 113, 110, 98,
107, 108, 106, 98, 105, 103, 110, 105, 104,
100, 96, 108, 103, 104, 114, 114, 113, 108, 106, 99)
)
leveneTest(x ~ grp, data = serum)
#>
#> Levene's Test for Homogeneity of Variance (center = median)
#>
#> data: x ~ grp
#> F = 1.7865, num df = 1, denom df = 38, p-value = 0.1893
#>
leveneTest(x ~ grp, data = serum, center = mean)
#>
#> Levene's Test for Homogeneity of Variance (center = mean)
#>
#> data: x ~ grp
#> F = 1.7879, num df = 1, denom df = 38, p-value = 0.1891
#>
leveneTest(x ~ grp, data = serum, center = mean, trim = 0.1)
#>
#> Levene's Test for Homogeneity of Variance (center = mean(trim=0.1))
#>
#> data: x ~ grp
#> F = 1.7854, num df = 1, denom df = 38, p-value = 0.1894
#>
leveneTest(c(rnorm(10), rnorm(10, 0, 2)),
factor(rep(c("A", "B"), each = 10)))
#>
#> Levene's Test for Homogeneity of Variance (center = median)
#>
#> data: c(rnorm(10), rnorm(10, 0, 2)) and factor(rep(c("A", "B"), each = 10))
#> F = 1.0614, num df = 1, denom df = 18, p-value = 0.3165
#>
leveneTest(Ozone ~ factor(Month), data = airquality)
#>
#> Levene's Test for Homogeneity of Variance (center = median)
#>
#> data: Ozone ~ factor(Month)
#> F = 3.9558, num df = 4, denom df = 111, p-value = 0.004863
#>
leveneTest(count ~ spray, data = InsectSprays)
#>
#> Levene's Test for Homogeneity of Variance (center = median)
#>
#> data: count ~ spray
#> F = 3.8214, num df = 5, denom df = 66, p-value = 0.004223
#>
# Compare this to fligner.test() and bartlett.test()
