简体   繁体   中英

Solving Linear equation in R

给定X1的方差,X2的方差以及X1和X2之间的协方差,是否有一种方法可以使用R(不是手动)来计算U = 2X1- X2和V = X1 + 2X2的相关性?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM