get_cor function

transform correlation parameters to and from glmmTMB parameterization

transform correlation parameters to and from glmmTMB parameterization

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")?

Returns

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

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 LL 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=sqrt(D)LLsqrt(D)Sigma = sqrt(D) L L' sqrt(D), where D=diag(LL)D = diag(L L'). For a single correlation parameter theta0theta0 (i.e. the correlation in a 2x2 correlation matrix), this works out to rho=theta0/sqrt(1+theta02)rho = theta0/sqrt(1+theta0^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.

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))
  • Maintainer: Mollie Brooks
  • License: AGPL-3
  • Last published: 2025-04-02