Skip to content

Box's M Test for Equality of Covariance Matrices

Source: man/performBoxMTest.Rd · Help: ?performBoxMTest

Test whether multiple groups have equal covariance matrices, by Box's (1949) chi-square approximation to his M statistic.

Usage

performBoxMTest(data, group)

Arguments

data
A numeric data frame or matrix.
group
A factor indicating group membership. At least two groups must have observations, and each of them more observations than data has variables.

Value

An object of class htest: statistic, named M, is Box's corrected statistic \(M (1 - c_1)\); parameter, named df, its degrees of freedom \(f_1\); p.value the upper tail of the chi-square distribution; and method and data.name.

Details

performBoxMTest(data, group) is the one test that does not take (model, data, ...): it tests the covariance matrices of the data themselves, not the residuals of a fitted model.

With \(g\) groups of \(n_i\) observations of \(p\) variables, \(\nu_i = n_i - 1\), \(\nu = \sum_i \nu_i\), \(S_i\) the covariance matrix of group \(i\) and \(S\) the pooled one, Box's statistic is

\[ M = \nu \log|S| - \sum_i \nu_i \log|S_i|, \]

his correction factor is

\[ c_1 = \left(\sum_i \frac{1}{\nu_i} - \frac{1}{\nu}\right) \frac{2p^2 + 3p - 1}{6 (p + 1) (g - 1)}, \]

and \(M (1 - c_1)\) is referred to a chi-square distribution with \(f_1 = (g - 1) p (p + 1) / 2\) degrees of freedom: Box's (1949) chi-square approximation. The test assumes that each group is multivariate normal. It gives the values of biotools::boxM(), heplots::boxM() and rstatix::box_m(): 140.9430 on 20 degrees of freedom for the example on iris.

Before 0.16.0 the correction factor was \((g - 1) c_1\): the same for two groups, whose values do not change, and too large for three or more, which made the statistic too small and the test conservative. The example on iris gave 135.2229.

The groups are the levels of group that have observations: a level with none, as subset() leaves on a factor, is dropped (since 0.16.0; before, it made the result NA). There must be at least two of them, and each group needs more observations than there are variables, \(n_i > p\): a group of \(n_i \le p\) observations has a singular covariance matrix (a group of one has none), whose log-determinant is \(-\infty\), and the statistic is not defined, whatever the data. Since 0.16.0 the test refuses such data with an error that names the groups; before, rounding made the statistic a finite number (133.74, p = 7.7e-19, for a group of three observations of four variables beside two species of iris), a group of one gave NA and a single group NaN. With more observations than variables a group's covariance matrix is still singular when a variable is constant in the group, or a linear combination of the others in it; the test refuses that too, naming the group and saying which, where it returned Inf (p = 0) or NaN with warnings. A group is checked by the rank of its centred data at the tolerance of qr(). A missing value in data or in group still gives NA.

Size

In inst/validation/box-m-approximations-size.R, at 5000 replications per cell and the 5% level, with \(g\) = 2, 3 or 5 groups of 10 to 100 observations of \(p\) = 1 to 10 variables, the test held its level under Gaussian data in 60 of 67 cells, inside the release gate's interval, 0.042 to 0.058.

  • It is liberal when the groups are small for the number of variables. With groups of 10 and 4 or 6 variables it rejected a true null hypothesis 0.055 to 0.106 of the time, outside the gate in four of those six cells (0.062 to 0.106, the most with five groups of 10 and six variables), and with groups of 20 and 10 variables 0.071 to 0.087. Box's F approximation, which the package does not compute, was closer to the level in each of those nine cells (0.051 to 0.070); in the other Gaussian cells the two differed by at most 0.005.
  • It is not robust to heavy tails. With independent \(t_5\) coordinates, three groups of 20, 50 or 100 and two or four variables, it rejected 0.26 to 0.48 of the time (0.18 to 0.46 with the factor of 0.15.0), more with larger groups: the test then detects the tails as well as unequal covariance matrices.
  • It is conservative with light tails. On the data of the full sweep (inst/validation/full-sweep-size-power.R), 150 observations of (y, x) with x uniform and grouped by cut(x, 3), it rejected 0.017 of the time, as Box's F did. The sweep's 0.012, at 400 replications, was measured with the factor of 0.15.0, which gives 0.016 in that design at 5000 replications: the low size comes from the uniform, light-tailed data, not from the approximation.

When the groups are small for the number of variables, or the data may have heavy tails, a rejection is weak evidence of unequal covariance matrices.

Validation

tests/testthat/test-boxm.R checks the statistic against Box's approximation written out for two to five groups and one to four variables, and against the values of biotools::boxM() 4.3 and heplots::boxM() 1.8.6 on iris and on mtcars, and that two groups give the values of 0.15.0. inst/validation/box-m-approximations-size.R compares the chi-square and the F approximation with biotools, heplots and DFA.CANCOR on ten data sets, and measures the sizes above.

References

Box, G. E. P. (1949). A general distribution theory for a class of likelihood criteria. Biometrika, 36(3/4), 317–346. doi:10.1093/biomet/36.3-4.317

Examples

data(iris)
performBoxMTest(iris[,1:4], iris$Species)

# Two species: with two groups the values are those of 0.15.0
two <- droplevels(subset(iris, Species != "virginica"))
performBoxMTest(two[, 1:4], two$Species)