Haynes, Maurice (NIH/NICHD)
2004-Dec-21 13:09 UTC
[R] R code for var-cov matrix given variances and correlations
Dear list members, Where can I find code for computing the p*p variance-covariance matrix given a vector of p variances (ordered varA, varB, ..., varp) and a vector of all possible correlations (ordered corAB, corAC, ..., corp-1,p)? I know that the covariance between 2 variables is equal to the product of their correlation and their standard deviations: corAB * varA^.5 * varB^.5 and so: covAB <- function(corAB, varA, varB) { corAB * varA^.5 * varB^.5 } If the vector of variances were var.vec <- c(14, 12, 7) and the vector of correlations were cor.vec <- c(.4, .2, .5), then the vector of covariances would be:> covAB(c(.4, .2, .5),c(14, 14, 12), c(12, 7, 7))[1] 5.184593 1.979899 4.582576>and the variance-covariance matrix with covariances rounded to the first decimal place would be:> vmat <- matrix(c(14, 5.2, 2.0, 5.2, 12, 4.6, 2.0, 4.6, 7),+ nrow=3)> vmat[,1] [,2] [,3] [1,] 14.0 5.2 2.0 [2,] 5.2 12.0 4.6 [3,] 2.0 4.6 7.0>So the question is: How can I generate a p*p variance-covariance matrix from a vector of variances and a vector of correlations without resorting to a construction like: vmat <- matrix(rep(0, p*p), nrow=p) if (p == 2) { vmat[1,1] <- var[1] vmat[1,2] <- cor[1] * (var[1]^.5 * var[2]^.5) vmat[2,1] <- cor[1] * (var[2]^.5 * var[1]^.5) vmat[2,2] <- var[2] } if (p == 3) { vmat[1,1] <- var[1] vmat[1,2] <- cor[1] * (var[1]^.5 * var[2]^.5) vmat[1,3] <- cor[2] * (var[1]^.5 * var[3]^.5) vmat[2,1] <- cor[1] * (var[2]^.5 * var[1]^.5) vmat[2,2] <- var[2] vmat[2,3] <- cor[3] * (var[2]^.5 * var[3]^.5) vmat[3,1] <- cor[2] * (var[3]^.5 * var[1]^.5) vmat[3,2] <- cor[3] * (var[3]^.5 * var[2]^.5) vmat[3,3] <- var[3] } and so forth? Thanks, Maurice Haynes National Institute of Child Health and Human Development Child and Family Research Section 6705 Rockledge Drive, Suite 8030 Bethesda, MD 20892 Voice: 301-496-8180 Fax: 301-496-2766 E-mail: mh192j at nih.gov
Peter Dalgaard
2004-Dec-21 13:34 UTC
[R] R code for var-cov matrix given variances and correlations
"Haynes, Maurice (NIH/NICHD)" <haynesm at cfr.nichd.nih.gov> writes:> If the vector of variances were > var.vec <- c(14, 12, 7) > and the vector of correlations were > cor.vec <- c(.4, .2, .5), > then the vector of covariances would be: > > covAB(c(.4, .2, .5),c(14, 14, 12), c(12, 7, 7)) > [1] 5.184593 1.979899 4.582576 > > > and the variance-covariance matrix with covariances rounded to > the first decimal place would be: > > vmat <- matrix(c(14, 5.2, 2.0, 5.2, 12, 4.6, 2.0, 4.6, 7), > + nrow=3) > > vmat > [,1] [,2] [,3] > [1,] 14.0 5.2 2.0 > [2,] 5.2 12.0 4.6 > [3,] 2.0 4.6 7.0 > ># First fill in the correlation matrix: V <- matrix(NA,3,3) diag(V) <- 1 V[lower.tri(V)] <- c(.4, .2, .5) V[upper.tri(V)] <- t(V)[upper.tri(V)] # then scale rows and columns D <- diag(sqrt( c(14, 12, 7))) D %*% V %*% D # or, more efficiently s <- sqrt( c(14, 12, 7)) sweep(sweep(V,1,s,"*"),2,s,"*") -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
David Firth
2004-Dec-21 13:38 UTC
[R] R code for var-cov matrix given variances and correlations
On 21 Dec, 2004, at 13:09, Haynes, Maurice (NIH/NICHD) wrote:> Dear list members, > > Where can I find code for computing the p*p variance-covariance > matrix given a vector of p variances (ordered varA, varB, ..., > varp) and a vector of all possible correlations (ordered corAB, > corAC, ..., corp-1,p)?Something like this (not tested): sd.vec <- sqrt(var.vec) n <- length(sd.vec) cor.mat <- matrix(1, n, n) for (i in 2:n){ for (j in 1:i-1){ cor.mat[i,j] <- cor.vec[(i-1)*(i-2)/2 + j] cor.mat[j,i] <- cor.mat[i,j]}} cov.mat <- cor.mat * outer(sd.vec, sd.vec) I hope that helps. David> > I know that the covariance between 2 variables is equal to the > product of their correlation and their standard deviations: > corAB * varA^.5 * varB^.5 > and so: > covAB <- function(corAB, varA, varB) { > corAB * varA^.5 * varB^.5 > } > > If the vector of variances were > var.vec <- c(14, 12, 7) > and the vector of correlations were > cor.vec <- c(.4, .2, .5), > then the vector of covariances would be: >> covAB(c(.4, .2, .5),c(14, 14, 12), c(12, 7, 7)) > [1] 5.184593 1.979899 4.582576 >> > and the variance-covariance matrix with covariances rounded to > the first decimal place would be: >> vmat <- matrix(c(14, 5.2, 2.0, 5.2, 12, 4.6, 2.0, 4.6, 7), > + nrow=3) >> vmat > [,1] [,2] [,3] > [1,] 14.0 5.2 2.0 > [2,] 5.2 12.0 4.6 > [3,] 2.0 4.6 7.0 >> > > So the question is: How can I generate a p*p variance-covariance > matrix from a vector of variances and a vector of correlations > without resorting to a construction like: > vmat <- matrix(rep(0, p*p), nrow=p) > if (p == 2) { > vmat[1,1] <- var[1] > vmat[1,2] <- cor[1] * (var[1]^.5 * var[2]^.5) > vmat[2,1] <- cor[1] * (var[2]^.5 * var[1]^.5) > vmat[2,2] <- var[2] > } > if (p == 3) { > vmat[1,1] <- var[1] > vmat[1,2] <- cor[1] * (var[1]^.5 * var[2]^.5) > vmat[1,3] <- cor[2] * (var[1]^.5 * var[3]^.5) > vmat[2,1] <- cor[1] * (var[2]^.5 * var[1]^.5) > vmat[2,2] <- var[2] > vmat[2,3] <- cor[3] * (var[2]^.5 * var[3]^.5) > vmat[3,1] <- cor[2] * (var[3]^.5 * var[1]^.5) > vmat[3,2] <- cor[3] * (var[3]^.5 * var[2]^.5) > vmat[3,3] <- var[3] > } > and so forth? > > Thanks, > > Maurice Haynes > National Institute of Child Health and Human Development > Child and Family Research Section > 6705 Rockledge Drive, Suite 8030 > Bethesda, MD 20892 > Voice: 301-496-8180 > Fax: 301-496-2766 > E-mail: mh192j at nih.gov > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
(Ted Harding)
2004-Dec-21 13:54 UTC
[R] R code for var-cov matrix given variances and correlatio
On 21-Dec-04 Haynes, Maurice (NIH/NICHD) wrote:> Dear list members, > > Where can I find code for computing the p*p variance-covariance > matrix given a vector of p variances (ordered varA, varB, ..., > varp) and a vector of all possible correlations (ordered corAB, > corAC, ..., corp-1,p)? > [...]example:> X<-cbind(rnorm(10),rnorm(10)) > cov(X)[,1] [,2] [1,] 0.90141614 0.09364018 [2,] 0.09364018 0.88476825> cor(X)[,1] [,2] [1,] 1.0000000 0.1048540 [2,] 0.1048540 1.0000000> vars<-apply(X,2,var) > vars[1] 0.9014161 0.8847682> diag(sqrt(vars))%*%cor(X)%*%diag(sqrt(vars))[,1] [,2] [1,] 0.90141614 0.09364018 [2,] 0.09364018 0.88476825 Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 21-Dec-04 Time: 13:54:45 ------------------------------ XFMail ------------------------------