Learn R Programming

lavaan (version 0.5-9)

utils-matrix: Utility Functions For Matrices.

Description

Utility functions to deal with (mostly symmetric) matrices.

Usage

vech(S, diagonal = TRUE)
vechr(S, diagonal = TRUE)
vechu(S, diagonal = TRUE)
vechru(S, diagonal = TRUE)
vech.reverse(x, diagonal = TRUE)
vechru.reverse(x, diagonal = TRUE)
vechr.reverse(x, diagonal = TRUE)
vechu.reverse(x, diagonal = TRUE)
lower2full(x, diagonal = TRUE)
upper2full(x, diagonal = TRUE)
duplicationMatrix(n = 1L)
commutationMatrix(m = 1L, n = 1L)
sqrtSymmetricMatrix(S)

Arguments

S
A symmetric matrix.
x
A numeric vector containing the lower triangular or upper triangular elements of a symmetric matrix, possibly including the diagonal elements.
diagonal
Logical. If TRUE, the diagonal is included. If FALSE, the diagonal is not included.
n
Integer. Dimension of the symmetric matrix, or column dimension of a non-square matrix.
m
Integer. Row dimension of a matrix.

Details

The vech function implements the vech operator (for 'half vectorization') and transforms a symmetric matrix into a vector by stacking the columns of the matrix one underneath the other, but eliminating all supradiagonal elements. The vech.reverse function does the reverse: given the output of the vech function, it reconstructs the symmetric matrix.

The lower2full function takes the lower The duplicationMatrix function creates a duplication matrix D: it duplicates the elements in vech(S) to create vec(S) (where S is symmetric), such that D %*% vech(S) == vec(S). The commutationMatrix function creates a commutation matrix (K): this mn x mx matrix is a permutation matrix which transforms vec(A) into vec(t(A)), such that K %*% vec(A) == vec(t(A)). The sqrtSymmetricMatrix function computes the square root of a (positive definite) symmetric matrix.

References

Magnus, J. R. and H. Neudecker (1999). Matrix Differential Calculus with Applications in Statistics and Econometrics, Second Edition, John Wiley.

Examples

Run this code
# lower.tri elements (including diagonal) of a symmetric matrix
x <- c(4,1,5,2,3,6)

# reconstruct full symmetric matrix (row-wise!)
S <- lower2full(x)

# extract the same lower.tri elements again in the same order
vechr(S)

# without diagonal elements
vechr(S, diagonal=FALSE)

# duplication matrix
nvar <- ncol(S)
vec <- as.vector
Dup <- duplicationMatrix(nvar)
Dup %*% vech(S) == vec(S) # should all be true

# commutation matrix
K <- commutationMatrix(nvar, nvar)
K %*% vec(S) == vec(t(S)) # should all be true

# take sqrt root of a symmetric matrix
S.sqrt <- sqrtSymmetricMatrix(S)
S.sqrt %*% S.sqrt
# should be equal to S again (ignoring some rounding-off errors)

Run the code above in your browser using DataLab