Is there a way to compute correlation of U = 2X1 − X2 and V = X1 + 2X2 using R (not manually) given the variance of X1, variance of X2 and Covariance between X1 and X2?
1 Answers
The covariance matrix of two random variables is the 2x2 symmetric matrix whose diagonals are the variances of the two components and whose off-diagonal elements are the covariances. That is, if the variances of X1 and X2 were v1 and v2 and the covariance v12 then the covariance matrix of X would be matrix(c(v1, v12, v12, v2), 2)
. We can readily form a covariance matrix via cov(d)
where d
is a two column matrix of data. To be concrete let us the form the covariance matrix of the builtin two column data frame BOD
. Then we can use the formula below to get the covariance matrix of the transformation and use cov2cor
to get the correlation matrix. The upper (and also by symmetry the lower) off-diagonal element of the correlation matrix will be the desired correlation. No packages are used.
# inputs: covariance matrix V and transformation matrix M
V <- cov(BOD)
M <- matrix(c(2, 1, -1, 2), 2)
cov2cor(M %*% V %*% t(M))[1, 2]
## [1] -0.3023
To double check transform BOD
using M
and then calculate the correlation of that. We see that the result is the same.
cor(as.matrix(BOD) %*% t(M))[1, 2]
## [1] -0.3023

- 254,981
- 17
- 203
- 341