# bart_spher: Bartlett's Test of Sphericity In REdaS: Companion Package to the Book 'R: Einführung durch angewandte Statistik'

## Description

Implements Barlett's Test of Sphericity which tests whether a matrix is significantly different from an identity matrix.

## Usage

 ```1 2 3 4 5``` ```bart_spher(x, use = c("everything", "all.obs", "complete.obs", "na.or.complete", "pairwise.complete.obs")) ## S3 method for class 'bart_spher' print(x, ...) ```

## Arguments

 `x` a data matrix or the object to be printed. `use` defines the method to use if missing values are present (see Examples and `cor`). `...` further arguments for the `print` method.

## Details

The test statistic as defined in Eq. (3) in Bartlett (1951) is X² = − [(n − 1) − (2k + 5) ∕ 6] · log(|R|) where n is the number of observations, k the number of variables, and R the correlation matrix of the data supplied in `x`. |R| is the determinant of R.

Bartlett's is asymptotically χ²-distributed with df = k (k − 1) ∕ 2 under the null hypothesis.

Note that, because the bias-corrected correlation matrix is used, (n − 1) is employed instead of n, as in the paper.

#### Treatment of Missing Values

If no missing values are present in the data matrix `x`, `use` will work with any setting and no adjustments are necessary. In this case, n is the number of rows in `x`.

For listwise deletion (`use = "complete.obs"` or `"na.or.complete"`), n is the number of remaining rows in `x`.

When `use = "pairwise.complete.obs"`, n is approximated as the sum of relative non-missing responses for all observations with 2 or more valid responses.

If listwise/pairwise methods are used to compute the correlation matrix and the test statistic, a warning will be issued when printing the object.

## Value

A list object of class `'bart_spher'`

 `call` the issued function call `x` the original data `cormat` the correlation matrix computed from the data `use` treatment of `NA`s `n` the number of used observations `k` the number of variables/items `X2` the computed X² value `df` degrees of freedom `p.value` the p-value `warn` logical value indicating whether a warning regarding missing values will be issued (see Details)

Marco J. Maier

## References

Bartlett, M. S. (1951). The Effect of Standardization on a χ² Approximation in Factor Analysis. Biometrika 38(3/4), 337–344.

`cor()` and `KMOS()`
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51``` ```# generate a data frame with 3 variables and 100 observations set.seed(5L) datamatrix <- data.frame("A" = rnorm(100), "B" = rnorm(100), "C" = rnorm(100)) head(datamatrix) # correlation matrix cor(datamatrix) # bartlett's test bart_spher(datamatrix) # effects of missing observations on correlations: to illustrate this, the first # observation on variable A is set to NA datamatrix[1, 1] <- NA head(datamatrix) # "everything" (the default) causes all correlations involving a variable with # missing values to be NA (in this case, all pairwise correlations with the # variable "A") cor(datamatrix) # "all.obs" generates an error if missing values are present. ## Not run: cor(datamatrix, use = "all.obs") ## End(Not run) # "complete.obs" and "na.or.complete" delete complete observations if there are # NA (in this case, the first case would be deleted). If there are no complete # cases left after the listwise deletion, "complete.obs" results in an error # while "na.or.complete" returns a matrix with all elements being NA. cor(datamatrix, use = "complete.obs") cor(datamatrix, use = "na.or.complete") # "pairwise.complete.obs" uses all non-missing pairwise values. If there are no # non-missing value pairs in two variables, the results will be NA. # It is possible that correlation matrices are not positive semi-definite. cor(datamatrix, use = "pairwise.complete.obs") # with the missing value in the first cell, the test does not work anymore: ## Not run: bart_spher(datamatrix) ## End(Not run) # deleting the whole first observation (listwise) gives bart_spher(datamatrix, use = "na.or.complete") # using pairwise-correlation, the result is bart_spher(datamatrix, use = "pairwise.complete.obs") ```