Title: Statistical Tests for Covariance and Correlation Matrices and their Structures
Version: 1.0.0
Maintainer: Svenja Jedhoff <jedhoff@statistik.tu-dortmund.de>
Description: A compilation of tests for hypotheses regarding covariance and correlation matrices for one or more groups. The hypothesis can be specified through a corresponding hypothesis matrix and a vector or by choosing one of the basic hypotheses, while for the structure test, only the latter works. Thereby Monte-Carlo and Bootstrap-techniques are used, and the respective method must be chosen, and the functions provide p-values and mostly also estimators of calculated covariance matrices of test statistics. For more details on the methodology, see Sattler et al. (2022) <doi:10.1016/j.jspi.2021.12.001>, Sattler and Pauly (2024) <doi:10.1007/s11749-023-00906-6>, and Sattler and Dobler (2025) <doi:10.48550/arXiv.2310.11799>.
License: GPL (≥ 3)
Imports: MANOVA.RM, matrixcalc
Suggests: testthat (≥ 3.0.0)
Config/testthat/edition: 3
Encoding: UTF-8
RoxygenNote: 7.3.2
URL: https://github.com/sjedhoff/CovCorTest
BugReports: https://github.com/sjedhoff/CovCorTest/issues
NeedsCompilation: no
Packaged: 2025-06-05 08:10:37 UTC; jedhoff
Author: Paavo Sattler ORCID iD [aut], Svenja Jedhoff ORCID iD [cre, aut], Markus Pauly ORCID iD [ctb], Arne C Bathke ORCID iD [ctb], Dennis Dobler ORCID iD [ctb]
Repository: CRAN
Date/Publication: 2025-06-06 13:30:13 UTC

Anova-Type-statistic

Description

Anova-Type-statistic

Usage

ATS(N, vVarData, C, HatCov, Xi = 0)

Arguments

N

number of observations

vVarData

a matrix of vectorized covariance/correlation data

C

hypothesis matrix for calculating the ATS

HatCov

covariance matrix

Xi

a vector defining together with C the investigated hypothesis

Value

a vector


ATS for transformed vectors

Description

A function which calculates the Anova-type-statistic based on a transformation function

Usage

ATS_fun(N, X, C, v, a, d, p, fun)

Arguments

N

sample size

X

matrix containing the bootstrap observations as columns

C

the hypothesis matrix

v

vectorized empirical covariance matrix of the original data

a

vector containing the indices which belongs to the diagonal of the covariance matrix

d

dimension of the covariance matrix

p

dimension of the vectorized matrix

fun

transformation function, that should be used. subdiagonal_mean_ratio_fct or subdiagonal_mean_ratio_cor

Value

a scalar, the value of the ATS


Anova-Type-Statistic with weighted sum

Description

Calculation of a Anova-Type-Statistic using

Usage

ATSwS(A, repetitions)

Arguments

A

a matrix

repetitions

a scalar, number of runs

Value

a vector of the length of repetitions


Bootstrap for one and multiple groups

Description

This function generates normal distributed random vectors. For one group, nv random vectors with covariance matrix HatCov are generated and the corresponding value of the ATS is generated. For multiple groups the corresponding sample sizes from nv are used. The weighted sum of covariance matrices is calculated and used to calculate the value of the ATS.

Usage

Bootstrap(N.sim, nv, C, MSrootHatCov)

Arguments

N.sim

control variable for using sapply

nv

scalar (one group) or vector (multiple groups) of sample sizes for the bootstrap samples

C

hypothesis matrix for calculating the ATS

MSrootHatCov

matrix (one group) or list of matrices (multiple groups) of roots of the covariance matrices, to generate the bootstrap sample

Value

a scalar, the value of the ATS


Bootstrap using transformation for one group

Description

This function generates n1 normal distributed random vectors with covariance matrix HatCov, which matrix root MSrootHatCov is given and expectation vector vX. For the generated bootstrap sample the value of the ATS based on a transformation is calculated

Usage

Bootstrap_trans(N.sim, n1, a, d, p, C, MSrootHatCov, vX, fun)

Arguments

N.sim

control variable for using sapply

n1

a scalar, declaring the sample size for the bootstrap sample

a

vector containing the indices which belong to the diagonal of the covariance matrix

d

dimension of the covariance matrix

p

dimension of the vectorized matrix

C

a hypothesis matrix for calculating the ATS

MSrootHatCov

matrix root of the covariance matrix HatCov, to generate the bootstrap sample

vX

the expectation vector for the bootstrap sample

fun

transformation function, that should be used. subdiagonal_mean_ratio_fct or subdiagonal_mean_ratio_cor

Value

a scalar, the value of the ATS


CombTest Object

Description

This help page describes the structure of the CombTest class, which is used to represent the results the combined covariance and correlation test.

Usage

CombTest()

Details

A CombTest object is a list with the following components:

method

Character. Either 'Covariance' or 'Correlation'.

pvalue-Variances

Numeric. The p-value of the test regarding the covariances.

pvalue-Correlations

Numeric. The p-value of the test regarding the correlations.

pvalue-Total

Numeric. The p-value of the whole test of the global hypothesis.

Teststatistic

Numeric. The test statistic.

resampling_method

Character. The resampling method used in the test.

repetitions

Integer. The number of repetitions used in resampling.

nv

Numeric. The sample size or the number of variables.

Value

An object of class CombTest.


CovTest Object

Description

This help page describes the structure of the CovTest class, which is used to represent the results of a covariance and correlation test.

Usage

CovTest()

Details

A CovTest object is a list with the following components:

method

Character. Either 'Covariance' or 'Correlation'.

pvalue

Numeric. The p-value of the test.

Teststatistic

Numeric. The test statistic.

CovarianceMatrix

Matrix. The covariance estimator for the teststatistic.

C

Numeric. A constant or vector of constants used in the test.

Xi

Numeric. A parameter related to the test.

resampling_method

Character. The resampling method used in the test.

repetitions

Integer. The number of repetitions used in resampling.

hypothesis

Character. The hypothesis being tested.

nv

Numeric. The sample size or the number of variables.

Value

An object of class CovTest.


Jacobian matrix for transformation functions

Description

A function which calculates the Jacobian matrix for a given transformation function subdiagonal_mean_ratio_cor or subdiagonal_mean_ratio_fct

Usage

Jacobian(X, a, d, p, fun)

Arguments

X

vectorized covariance matrix for which the Jacobian matrix is applied

a

vector containing the indices which belong to the diagonal of the covariance matrix

d

dimension of the covariance matrix

p

dimension of the vectorized matrix

fun

transformation function, that should be used. subdiagonal_mean_ratio_fct or subdiagonal_mean_ratio_cor

Value

the Jacobian matrix applied for the given vector


Function to transform the data into a list, if there are not already

Description

Function to transform the data into a list, if there are not already

Usage

Listcheck(X, nv)

Arguments

X

object that should be checked

nv

number of subjects per group

Value

A list with two components

X

Dataset in the right format: for a single group, a single matrix. For multiple groups, a list with an element for each group containing a matrix.

nv

Number of subjects per group: NV for a single group and a vector for multiple groups.


Auxiliary function to calculate the covariance of the vectorized correlation matrix

Description

Auxiliary function to calculate the covariance of the vectorized correlation matrix

Usage

Qvech(X, n)

Arguments

X

matrix

n

number of columns

Value

matrix


Function for the Taylor-based Monte-Carlo-approximation for one group

Description

An auxiliary function to calculate the values for the Taylor-based Monte-Carlo-approximation for one group. After receiving some auxiliary matrices and data, the Monte-Carlo observations are generated and different parts of the final sum are defined. Based on this a number of the Taylor-based ATS are calculated, where the number can be chosen.

Usage

Tayapp1G(
  repetitions,
  C,
  MSrootStUpsi,
  CorData,
  MvrH,
  Trace,
  M4,
  L,
  P,
  Q,
  n1,
  Atilde = NULL,
  Jacobi = NULL
)

Arguments

repetitions

a number specifying the number of runs for the approximation

C

the used hypothesis matrix

MSrootStUpsi

the matrix root of the covariance matrix for the Taylor observations

CorData

the calculated correlation matrix

MvrH

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

Trace

a trace used in the ATS for the test statistic

M4

a auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

L

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

P

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

Q

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

n1

the total sample size, a scalar

Atilde

an auxiliary matrix for the transformation from row-wise vectorization

Jacobi

the Jacobian matrix of the transformation function applied for the diagonal vectorized correlation to diagonalwise vectorization. used for the transformation function 'subdiagonal_mean_ratio_cor', else NULL

Value

a matrix containing the values of the Taylor ATS for a number of repetitions


Function for the Taylor-based Monte-Carlo-approximation for multiple groups

Description

An auxiliary function to calculate the values for the Taylor-based Monte-Carlo-approximation for multiple groups. After receiving some auxiliary matrices and data, the Monte-Carlo observations are generated and different parts of the final sum are defined. Based on this a number of the Taylor-based ATS are calculated, where the number can be chosen.

Usage

TayappMG(repetitions, C, MSrootStUpsi, CorData, MvrH, Trace, M4, L, P, Q, nv)

Arguments

repetitions

a number specifying the number of runs for the approximation

C

the used hypothesis matrix

MSrootStUpsi

the matrix root of the covariance matrix for the Taylor observations

CorData

the calculated correlation matrix

MvrH

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

Trace

a trace used in the ATS for the test statistic

M4

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

L

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

P

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

Q

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

nv

vector of sample sizes

Value

a matrix containing the values of the Taylor ATS for a number of repetitions


The Taylor-based Monte-Carlo-approximation for a combined test

Description

An auxiliary function to calculate the values for the Taylor-based Monte-Carlo-approximation for the combined test. After receiving some auxiliary matrices and data, the Monte-Carlo observations are generated, and different parts of the final sum are defined. Based on this, a number of the Taylor-based vectors are calculated, where the number can be chosen.

Usage

TaylorCombined(
  repetitions,
  MSrootHatCov,
  CorData,
  MvrH1,
  MvrH2,
  M4,
  L,
  P,
  Q,
  nv
)

Arguments

repetitions

a number specifying the number of runs for the approximation

MSrootHatCov

the matrix root of the covariance matrix for the Taylor observations

CorData

the calculated correlation matrix

MvrH1

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

MvrH2

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

M4

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

L

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

P

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

Q

an auxiliary matrix for the transformation from vectorized covariances to vectorized correlations

nv

vector of sample sizes

Value

a matrix containing the values of the test vector for a number of repetitions


Weighted direct sums for lists

Description

Hereby the matrices which are part of a list are multiplied with the corresponding components of a matrix w, containing the weights. These, now weighted matrices are put together to one larger block-diagonal matrix.

Usage

WDirect.sumL(X, w)

Arguments

X

matrix

w

weight matrix

Value

matrix


Diagonal vectorization

Description

Diagonal vectorization of the upper triangular matrix

Usage

dvech(X, a, d, p, inc_diag)

Arguments

X

quadratic matrix which should be diagonalized

a

vector containing the indices which belong to the diagonal of the matrix

d

dimension of the matrix which should be vectorized

p

dimension of the vectorized matrix

inc_diag

TRUE or FALSE: should the diagonal be included?

Value

vector


Function to generate bootstrap observations

Description

Function to generate bootstrap observations

Usage

generateData(WSigma, nv)

Arguments

WSigma

weight matrix

nv

number of observations in the groups

Value

a matrix


Print function for CombTest object

Description

Print function for CombTest object

Usage

## S3 method for class 'CombTest'
print(x, ...)

Arguments

x

an CombTest object

...

additional parameters

Value

no return, just print


Print function for CovTest object

Description

Print function for CovTest object

Usage

## S3 method for class 'CovTest'
print(x, ...)

Arguments

x

an CovTest object

...

additional parameters

Value

no return, just print


Root transformation of the vectorized correlation matrix

Description

A function calculating the roots of a vectorized covariance matrix. The roots increasing, so square root for the first secondary diagonals, third root for the second secondary diagonal and so on. For roots with even order the absolute value of the argument is used, since the arguments can be negative.

Usage

subdiagonal_mean_ratio_cor(v, a, d)

Arguments

v

vectorized correlation matrix which should be transformed

a

vector containing the indices which belong to the diagonal of the correlation matrix

d

dimension of the correlation matrix

Value

a transformed vector


Transformation of the vectorized covariance matrix by quotients of means

Description

A function which calculates the mean of the secondary diagonals and divide them through the next one. Since the elements can be negative, for the denominator absolute values are used.

Usage

subdiagonal_mean_ratio_fct(v, a, d)

Arguments

v

vectorized covariance matrix which should be transformed

a

vector containing the indices which belong to the diagonal of the covariance matrix

d

dimension of the covariance matrix

Value

a transformed vector


Combined test for equality of covariance matrices and correlation matrices

Description

For two groups a combined test for equality of covariance matrices and equality of correlation matrices between these groups is conducted. Both hypotheses can be rejected or only the larger one, the equality of the covariance matrices

Usage

test_combined(X, nv = NULL, repetitions = 1000)

Arguments

X

a list or matrix containing the observation vectors. In case of a list,each matrix in this list is another group, where the observation vectors are the columns. For a matrix, all groups are together in one matrix and nv is used to indicate the group sizes.

nv

vector of group sizes

repetitions

a scalar, indicate the number of runs for the chosen method. The predefined value is 1.000, and the number should not be below 500.

Value

an object of the class CovTest.

Examples

# Load the data
data("EEGwide", package = "MANOVA.RM")

vars <- colnames(EEGwide)[1:6]

# Part the data into six groups of sex and diagnosis
X_list <- list(t(EEGwide[EEGwide$sex=="M" & EEGwide$diagnosis=="AD",vars]),
               t(EEGwide[EEGwide$sex=="M" & EEGwide$diagnosis=="MCI",vars]))

nv <- unlist(lapply(X_list, ncol))
set.seed(31415)
test_combined(X_list, nv)


Test for Correlation Matrices

Description

This function conducts statistical tests for hypotheses regarding correlation matrices. Users can either select from predefined hypotheses or provide their own contrast matrix C and vector Xi for custom hypotheses. It supports both bootstrap and Monte Carlo resampling methods to obtain the p-value of the ANOVA-type statistic (ATS).

Usage

test_correlation(
  X,
  nv = NULL,
  C = NULL,
  Xi = NULL,
  hypothesis = NULL,
  method = "BT",
  repetitions = 1000
)

Arguments

X

A list or a matrix containing the observation vectors. If a list, each entry is a group, with observations as columns. If a matrix, all groups are combined, and nv must be used to indicate group sizes.

nv

(Optional) A vector indicating group sizes, needed when X is a combined matrix or for multiple groups.

C

(Optional) A user-defined contrast matrix for testing custom hypotheses. Must match dimensions with Xi.

Xi

(Optional) A numeric vector used in combination with C to specify a custom hypothesis.

hypothesis

A character string describing the null hypothesis. Must be one of "equal-correlated" or "uncorrelated". If supplied, C and Xi are ignored.

method

A character indicating the resampling method: "BT" (Bootstrap) or "MC" (Monte Carlo).

repetitions

Number of repetitions to use for the resampling method (default: 1000, should be >= 500).

Value

An object of class "CovTest".

References

Sattler, P. and Pauly, M. (2024). Testing hypotheses about correlation matrices in general MANOVA designs. TEST, 33(2), 496–516. doi:10.1007/s11749-023-00906-6

Examples

# Example with one group:
set.seed(31415)
X <- matrix(rnorm(5 * 100), nrow = 5)
test_correlation(X, hypothesis = "uncorrelated",
                  method = "BT", repetitions = 100)


Test for structure of data's correlation matrix

Description

With this function the correlation matrix of data can be checked for one of the predefined structures. Depending on the chosen method a bootstrap, the Taylor-based Monte-Carlo approach or Monte-Carlo-technique is used to calculate the p-value of the Anova-type-statistic(ATS) based on a specified number of runs.

Usage

test_correlation_structure(X, structure, method = "BT", repetitions = 1000)

Arguments

X

a matrix containing the observation vectors as columns (one group)

structure

a character specifying the structure regarding them the correlation matrix should be checked. Options are "Hautoregressive" ("Har"), "diagonal" ("diag"), "Hcompoundsymmetry" ("Hcs") and "Htoeplitz" ("Hteop").

method

a character, to chose whether bootstrap("BT") or Taylor-based Monte-Carlo-approach("TAY") or Monte-Carlo-technique("MC") is used, while bootstrap is the predefined method.

repetitions

a scalar, indicate the number of runs for the chosen method. The predefined value is 1,000, and the number should not be below 500.

Value

an object of the class CovTest

References

Sattler, P. and Dobler, D. (2025). Testing for patterns and structures in covariance and correlation matrices. arXiv preprint https://arxiv.org/abs/2310.11799

Examples

# Load the data
data("EEGwide", package = "MANOVA.RM")

# Select only the males with the diagnosis AD
X <- as.matrix(EEGwide[EEGwide$sex == "W" & EEGwide$diagnosis == "AD",
             c("brainrate_temporal", "brainrate_frontal","brainrate_central",
             "complexity_temporal","complexity_frontal",
             "complexity_central")])

test_correlation_structure(X = X, structure = "diagonal", method = "MC")


Test for Covariance Matrices

Description

This function conducts statistical tests for hypotheses regarding covariance matrices. Users can either select from predefined hypotheses (e.g., equal covariance, equal trace, etc.) or provide their own contrast matrix C and vector Xi for custom hypotheses. It supports both bootstrap and Monte Carlo resampling methods to obtain the p-value of the ANOVA-type statistic (ATS).

Usage

test_covariance(
  X,
  nv = NULL,
  C = NULL,
  Xi = NULL,
  hypothesis = NULL,
  A = NULL,
  method = "MC",
  repetitions = 1000
)

Arguments

X

A list or a matrix containing the observation vectors. If a list, each entry is a group, with observations as columns. If a matrix, all groups are combined, and nv must be used to indicate group sizes.

nv

(Optional) A vector indicating group sizes, needed when X is a combined matrix or for multiple groups.

C

(Optional) A user-defined contrast matrix for testing custom hypotheses. Must match dimensions with Xi.

Xi

(Optional) A numeric vector used in combination with C to specify a custom hypothesis.

hypothesis

A character specifying one of the predefined hypotheses:

  • "equal" — equal covariance matrices

  • "equal-trace" — equal traces across groups

  • "equal-diagonals" — equal variances across groups

  • "given-trace" — test against a given trace (single group only)

  • "given-matrix" — test against a given covariance matrix (single group only)

  • "uncorrelated" — test if variables are uncorrelated (single group only)

If C and Xi are provided, this can be set to NULL.

A

Optional scalar or matrix to define the hypothesis value when hypothesis is "given-trace" (scalar) or "given-matrix" (matrix). Ignored for other hypotheses.

method

A character indicating the resampling method: "BT" (Bootstrap) or "MC" (Monte Carlo).

repetitions

Number of repetitions to use for the resampling method (default: 1000, should be >= 500).

Value

An object of class CovTest.

References

Sattler, P., Bathke, A. C., and Pauly, M. (2022). "Testing hypotheses about covariance matrices in general MANOVA designs." Journal of Statistical Planning and Inference, 219, 134–146. doi:10.1016/j.jspi.2021.12.001

Examples

# Load the data
data("EEGwide", package = "MANOVA.RM")

vars <- colnames(EEGwide)[1:6]

X <- t(EEGwide[EEGwide$sex == "M" & EEGwide$diagnosis == "AD",vars])

# Testing the trace
C <- matrix(c(1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,0,1),
            nrow = 1, ncol = 21)
Xi <- 2
set.seed(31415)
test_covariance(X = X, nv = NULL, C = C, Xi = Xi, method = "BT",
            repetitions = 1000)

Test for structure of data's covariance matrix

Description

This function conducts the test for the covariance matrix of data regarding structures. Depending on the chosen method a bootstrap or Monte-Carlo-technique is used to calculate p-value of the Anova-type-statistic(ATS) based on a specified number of runs.

Usage

test_covariance_structure(X, structure, method = "BT", repetitions = 1000)

Arguments

X

a matrix containing the observation vectors as columns (one group only)

structure

a character specifying the structure regarding the covariance matrix should be checked. Options are "autoregressive" ("ar"), "FO-autoregressive" ("FO-ar"), "diagonal" ("diag"), "sphericity" ("spher"), "compoundsymmetry" ("cs") and "toeplitz" ("toep").

method

a character, to chose whether bootstrap("BT") or Monte-Carlo-technique("MC") is used, while bootstrap is the predefined method.

repetitions

a scalar, indicate the number of runs for the chosen method. The predefined value is 1,000, and the number should not be below 500.

Value

an object of the class CovTest

References

Sattler, P. and Dobler, D. (2025). Testing for patterns and structures in covariance and correlation matrices. arXiv preprint https://arxiv.org/abs/2310.11799

Examples

# Load the data
data("EEGwide", package = "MANOVA.RM")

# Select only the males with the diagnosis AD
X <- as.matrix(EEGwide[EEGwide$sex == "W" & EEGwide$diagnosis == "AD",
                          c("brainrate_temporal", "brainrate_frontal",
                          "brainrate_central","complexity_temporal",
                          "complexity_frontal", "complexity_central")])

test_covariance_structure(X = X, structure = "diagonal", method = "MC")


Function to calculate dvech(X t(X))

Description

Function to calculate dvech(X t(X))

Usage

vdtcrossprod(X, a, d, p)

Arguments

X

matrix

a

indices that belong to the diagonal of the matrix

d

dimension of the matrix

p

dimension of the vectorized matrix

Value

vector


Vectorization of the upper triangular part of the matrix

Description

Vectorization of the upper triangular part of the matrix

Usage

vechp(X)

Arguments

X

Value

vector


Function to calculate vech(X t(X))

Description

Function to calculate vech(X t(X))

Usage

vtcrossprod(X)

Arguments

X

matrix

Value

vector