Learn R Programming

lgspline (version 0.2.0)

matsqrt: Calculate Matrix Square Root

Description

Calculate Matrix Square Root

Usage

matsqrt(mat)

Value

A matrix \(\textbf{B}\) such that \(\textbf{B}\textbf{B} = \textbf{M}\)

Arguments

mat

A symmetric, positive-definite matrix \(\textbf{M}\)

Details

For matrix \(\textbf{M}\), computes \(\textbf{B}\) where \(\textbf{B}\textbf{B} = \textbf{M}\) using eigenvalue decomposition:

1. Compute eigendecomposition \(\textbf{M} = \textbf{V}\textbf{D}\textbf{V}^T\)

2. Set eigenvalues below sqrt(.Machine$double.eps) to 0 for stability

3. Take elementwise square root of eigenvalues: \(\textbf{D}^{1/2}\)

4. Reconstruct as \(\textbf{B} = \textbf{V} \textbf{D}^{1/2} \textbf{V}^T\)

This provides the unique symmetric positive-definite square root.

You can use this to help construct a custom Vhalf_fxn for fitting correlation structures, see lgspline.

Examples

Run this code
## 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)

Run the code above in your browser using DataLab