Skip to contents

This vignette demonstrates helper functions for working with a matrix object.

Check correlation matrix

Is a given matrix object a correlation matrix? This can be checked in three ways as follows:

### N is a correlation matrix
N <- diag(3)
check_correlation_matrix(N, dim = 3)
#> [1] TRUE
test_correlation_matrix(N, dim = 3)
#> [1] TRUE
assert_correlation_matrix(N, dim = 3)

### M ist not a correlation matrix
M <- matrix(c(1,  0.9,  0.9, 0.9,  1,  -0.9, 0.9,  -0.9,  1), nrow = 3)
check_correlation_matrix(M)
#> [1] "Must have positive eigenvalues only"
test_correlation_matrix(M)
#> [1] FALSE
assert_correlation_matrix(M)
#> Error in eval(expr, envir, enclos): Assertion on 'M' failed: Must have positive eigenvalues only.

Check covariance matrix

The same check for a correlation matrix also works for a covariance matrix:

### N is a covariance matrix
N <- diag(3)
check_covariance_matrix(N, dim = 3)
#> [1] TRUE
test_covariance_matrix(N, dim = 3)
#> [1] TRUE
assert_covariance_matrix(N, dim = 3)

### M ist not a correlation matrix
M <- matrix(c(1, 2, 3, 2, 1, 2, 3, 2, 1), nrow = 3)
check_covariance_matrix(M)
#> [1] "Must have positive eigenvalues only"
test_covariance_matrix(M)
#> [1] FALSE
assert_covariance_matrix(M)
#> Error in eval(expr, envir, enclos): Assertion on 'M' failed: Must have positive eigenvalues only.