This is a more succinct question from a previous post, but I have arrived at two different answers, and need help determining which - if either - is correct.

I start with a 4*3 matrix:

5  4  -1
2  3  -3
3  4  -4
1  3  -2

and I need to compute the variance-covariance matrix. I had originally tried:

  • For the non-diagonals, calculate the covariance values using the formula:

    $cov(x,y)=\frac{1}{n-1} \sum_{i=1}^n~(x_i- \overline{x} )*(y_i- \overline{y} )$

  • For the diagonals, I caculate the variance values using the formula:

    $var(x)=\frac{1}{n-1} \sum_{i=1}^n~(x_i- \overline{x} )^2$

which lead me to an "answer" of:

2.916  4.042  -1.458
4.042  0.333  -2.917
-1.458 -2.917  1.666

However, I tried to double-check this hand-done work using R, and wrote:

m  = matrix(c(5,2,3,1,4,3,4,3,-1,-3,-4,-2), nrow=4, ncol=3)
covM = cov(m, method = "pearson")

which lead to a different "answer" of:

2.916  0.833  0.833
0.833  0.333  0.000
0.833  0.000  1.666

So, it makes me wonder if I am using the wrong R function, or if I am using the wrong equations and calculations. Any ideas are greatly appreciated!!

Edit: What I am asking is, notice that the diagonal is the same for my hand calculation and R calculation. Only the off-diagonal is incorrect. For my hand calculation of the off-diagonals, I used the covariance equation I listed above (for my hand calculation of the diagonals, I used the varaince equation I listed above). The off-diagonal use of the covariance equation is what differs with the R output. So, am I using the correct equation? Many thanks!

Here is an example of how I got an off-diagonal value, and it was different than the same cell of the R output:


In general the R-result is ok. Your formulas for the estimated variance and covariance are looking fine, too. You have to show your calculations, so that someone can proof them.

  • $\begingroup$ Thank you. I have edited for what I think you are saying. Thanks. $\endgroup$ – user84756 Sep 8 '14 at 17:27
  • $\begingroup$ @StellaJ Your term is correct. Only the calculation must be incorrect. I copied your term in here:wolframalpha.com/input/… The result is the result of R. Maybe the following rules help to get the correct result: $$\text{1. Terms in brackets have to be calculated first}$$ $$\text{2. Multiplication and division first, then addition and subtraction}$$ $\endgroup$ – callculus Sep 8 '14 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.