get_cor: transform correlation parameters to and from glmmTMB...

View source: R/utils.R

get_corR Documentation

transform correlation parameters to and from glmmTMB parameterization

Description

transform correlation parameters to and from glmmTMB parameterization

Usage

get_cor(theta, return_val = c("vec", "mat"))

put_cor(C, input_val = c("mat", "vec"))

Arguments

theta

vector of internal correlation parameters (elements of scaled Cholesky factor, in row-major order)

return_val

return a vector of correlation values from the lower triangle ("vec"), or the full correlation matrix ("mat")?

C

a correlation matrix

input_val

input a vector of correlation values from the lower triangle ("vec"), or the full correlation matrix ("mat")?

Details

get_cor transforms from the glmmTMB parameterization (components of a theta parameter vector) to correlations; put_cor does the inverse transformations, from correlations to theta values.

These functions follow the definition at http://kaskr.github.io/adcomp/classdensity_1_1UNSTRUCTURED__CORR__t.html: if L is the lower-triangular matrix with 1 on the diagonal and the correlation parameters in the lower triangle, then the correlation matrix is defined as \Sigma = D^{-1/2} L L^\top D^{-1/2}, where D = \textrm{diag}(L L^\top). For a single correlation parameter \theta_0 (i.e. the correlation in a 2x2 correlation matrix), this works out to \rho = \theta_0/\sqrt{1+\theta_0^2}. The get_cor function returns the elements of the lower triangle of the correlation matrix, in column-major order.

These functions also work for AR1 correlation parameters.

Value

a vector of correlation values (get_cor) or glmmTMB scaled-correlation parameters (put_cor)

Examples

th0 <- 0.5
stopifnot(all.equal(get_cor(th0), th0/sqrt(1+th0^2)))
set.seed(101)
## pick 6 values for a random 4x4 correlation matrix
print(C <- get_cor(rnorm(6), return_val = "mat"), digits = 3)
## transform a correlation matrix to a theta vector
cor_mat <- matrix(c(1,0.3,0.1,
                    0.3,1,0.2,
                    0.1,0.2,1), ncol = 3)
put_cor(cor_mat, "mat")
put_cor(cor_mat[lower.tri(cor_mat)], "vec")
## test: round-trip
stopifnot(all.equal(get_cor(put_cor(C), return_val = "mat"), C))

glmmTMB documentation built on April 3, 2025, 9:36 p.m.