# What is the sample variance-covariance matrix?

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.

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:

cov(1,2)=((5-2.75)(4-3.5)+(2-2.75)(3-3.5)+(3-2.75)(4-3.5)+(1-2.75)(3-3.5))/(4-1)=4.042


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.

• Thank you. I have edited for what I think you are saying. Thanks. Sep 8 '14 at 17:27
• @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}$$ Sep 8 '14 at 20:52

The R output is correct. The reason for the diverging results must be a computation error when you calculated the cov by hand.

Also, one could use the following formula to compute the matrix by hand instead of computing each cov per variable:

$$S_{XY} = \frac{1}{n-1} ( \sum^n \textbf{x}_i\textbf{x}_j^T - n\bar{\textbf{x}}\bar{\textbf{x}}^T )$$,

where $$n$$ is the number of observations/rows and the sum is iterating over the 4 rows in you example.

This will yield the same as the R output when calculated by hand.