Translate a square symmetric matrix with positive diagonal elements into a vector of the logarithms of the diagonal elements with the correlations as an attribute, and vice versa.

1 |

`x` |
If a matrix, translate into a vector with a "corr" attribute. If a vector, translate into a matrix. |

`corr` |
optional vector of correlations for the
Use a "corr" attribute of |

`...` |
(not currently used) |

if(length(dim(x))==2) return `log(diag(x))`

with an attribute "corr" equal to the
`lower.tri`

of `cov2cor(x)`

.

Otherwise, return a covariance matrix from
`x`

as described above.

Spencer Graves

`log`

`diag`

`cov2cor`

`lower.tri`

`pdLogChol`

converts a k-dimensional
covariance matrix into a vector of length
`choose(k+1, 2)`

. By contrast, `logVarCor`

returns a vector of length `k`

with a "corr"
attribute of length `choose(k, 2)`

.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | ```
##
## 1. Trivial 1 x 1 matrix
##
# 1.1. convert vector to "matrix"
mat1 <- logVarCor(1)
# check
all.equal(mat1, matrix(exp(1), 1))
# 1.2. Convert 1 x 1 matrix to vector
lVCd1 <- logVarCor(diag(1))
# check
lVCd1. <- 0
attr(lVCd1., 'corr') <- numeric(0)
all.equal(lVCd1, lVCd1.)
##
## 2. simple 2 x 2 matrix
##
# 2.1. convert 1:2 into a matrix
lVC2 <- logVarCor(1:2)
# check
lVC2. <- diag(exp(1:2))
all.equal(lVC2, lVC2.)
# 2.2. Convert a matrix into a vector
lVC2d <- logVarCor(diag(1:2))
# check
lVC2d. <- log(1:2)
attr(lVC2d., 'corr') <- 0
all.equal(lVC2d, lVC2d.)
##
## 3. 3-d covariance matrix with nonzero correlations
##
# 3.1. Create matrix
(ex3 <- tcrossprod(matrix(c(rep(1,3), 0:2), 3)))
dimnames(ex3) <- list(letters[1:3], letters[1:3])
# 3.2. Convert to vector
(Ex3 <- logVarCor(ex3))
# check
Ex3. <- log(c(1, 2, 5))
names(Ex3.) <- letters[1:3]
attr(Ex3., 'corr') <- c(1/sqrt(2), 1/sqrt(5), 3/sqrt(10))
all.equal(Ex3, Ex3.)
# 3.3. Convert back to a matrix
Ex3.2 <- logVarCor(Ex3)
# check
all.equal(ex3, Ex3.2)
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.