View source: R/HelperFunctions.R
| matsqrt | R Documentation |
Calculate Matrix Square Root for Symmetric Matrices
matsqrt(mat)
mat |
A symmetric matrix |
Uses an eigenvalue-decomposition-based approach.
Non-positive eigenvalues are set to 0 before taking fourth roots.
This implementation is particularly useful for whitening procedures in GLMs with correlation structures and for computing variance-covariance matrices under constraints.
You can use this to help construct a custom Vhalf_fxn, or more
directly to build the \mathbf{V}^{1/2} input supplied to
lgspline for correlation-aware fits.
A matrix \textbf{B} such that \textbf{B}\textbf{B}
equals \textbf{M} on the positive-eigenvalue subspace, with
non-positive components truncated to 0.
## Identity matrix
m1 <- diag(2)
matsqrt(m1) # Returns identity matrix
## Compound symmetry correlation matrix
rho <- 0.5
m2 <- matrix(rho, 3, 3) + diag(1-rho, 3)
B <- matsqrt(m2)
# Verify: B %**% B approximately equals m2
all.equal(B %**% B, m2)
## Example for correlation structure
n_blocks <- 2 # Number of subjects
block_size <- 3 # Measurements per subject
rho <- 0.7 # Within-subject correlation
# Correlation matrix for one subject
R <- matrix(rho, block_size, block_size) +
diag(1-rho, block_size)
# Full correlation matrix for all subjects
V <- kronecker(diag(n_blocks), R)
Vhalf <- matsqrt(V)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.