#Set the number of data points and build 3 independent variables
set.seed(0)
numdatpoi <- 7
x1 <- runif(numdatpoi)
x2 <- runif(numdatpoi)
x3 <- runif(numdatpoi)
#Build the dependent variable with some added noise
noisig <- 10
yact <- 2 + (3 * x1) + (5 * x2) + (10 * x3)
y <- yact + rnorm(n=numdatpoi, mean=0, sd=noisig)
#Fit a linear model
rmod <- lm(y ~ x1 + x2 + x3)
#Build the variance-covariance matrix. This matrix is typically what is wanted.
(vcv <- vcov(rmod))
#If needed, convert the variance-covariance matrix to a correlation matrix
(cm <- cov2cor(vcv))
3 回答
如果我已经正确理解,你有一个3列的矩阵(比如说x1到x3)和很多行(作为y值) . 您可以采取以下行动:
如果你已经在3个向量x1到x3中的值,你可以像这样制作
foo
:foo=data.frame(x1,x2,x3)
如果我错了,请纠正我,但假设这与回归问题有关,这可能就是你要找的:
从上面,这里是方差 - 协方差矩阵:
而且,这是相关的相关矩阵: