The R "stats" package provides the cov2cor
function. However, there is no cor2cov
function available.
cor2cov <- function(cor.mat, var.vec) {
# Check inputs
stopifnot(isSymmetric(cor.mat))
stopifnot(identical(diag(cor.mat), rep(1, length(diag(cor.mat)))))
stopifnot(is.vector(var.vec))
stopifnot(length(var.vec)==nrow(cor.mat))
stopifnot(all(var.vec > 0))
# Start the job
sd.mat = diag(sqrt(var.vec))
return(sd.mat %*% cor.mat %*% sd.mat)
}