简体   繁体   English

在R中求解线性方程

[英]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. 两个随机变量的协方差矩阵是2x2对称矩阵,其对角线是两个分量的方差,非对角线元素是协方差。 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) . 也就是说,如果X1和X2的方差分别是v1和v2以及协方差v12,则X的协方差矩阵将是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. 我们可以通过cov(d)轻松地形成协方差矩阵,其中d是两列数据。 To be concrete let us the form the covariance matrix of the builtin two column data frame BOD . 具体来说,让我们形成内置两列数据帧BOD的协方差矩阵。 Then we can use the formula below to get the covariance matrix of the transformation and use cov2cor to get the correlation matrix. 然后我们可以使用下面的公式来获得变换的协方差矩阵,并使用cov2cor来获得相关矩阵。 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. 要使用M仔细检查转换BOD ,然后计算其相关性。 We see that the result is the same. 我们看到结果是相同的。

cor(as.matrix(BOD) %*% t(M))[1, 2]
## [1] -0.3023

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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