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 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=sqrt(D)LL′sqrt(D), where D=diag(LL′). For a single correlation parameter theta0 (i.e. the correlation in a 2x2 correlation matrix), this works out to rho=theta0/sqrt(1+theta02). 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.5stopifnot(all.equal(get_cor(th0), th0/sqrt(1+th0^2)))set.seed(101)## pick 6 values for a random 4x4 correlation matrixprint(C <- get_cor(rnorm(6), return_val ="mat"), digits =3)## transform a correlation matrix to a theta vectorcor_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-tripstopifnot(all.equal(get_cor(put_cor(C), return_val ="mat"), C))