3
$\begingroup$
a <- c(10,11)
b <- c(2,3)

The covariance (C) betweena and b =

sum ( (a-mean(a)) * (b-mean(b)) ) # equal to  cov(a,b) in this case as n=2

The sample covariance (Csamp) betweena and b =

sum ( (a-mean(a)) * (b-mean(b)) ) / n-1 # equal to  cov(a,b)

Now lets say the sum of square differences for a = SSa. This means the Variance(V) for a = SSa/n and the sample variance(Vsamp) is SSa/n-1. The sample standard deviation (SDsamp) for a would be sqrt(SSa/n-1)

The correlation coeffictent equations as I have found it are:

(1) C  /  sqrt(SSa) * sqrt(SSb)

# or

(2)  C  /  SDsamp_a * SDsamp_b

# (1) uses sqrt(SSa), not V or V samp and (2) uses SDsamp  not V and does not use Csamp

I am confused over how to calculate the correlation coefficient (CF) and the sample correlation coefficient (CFsamp). The two formulas above do not seem the same to me. can someone explain how to calculate CF and CFsamp?

$\endgroup$
5
  • $\begingroup$ From help("cov"): "The denominator n - 1 is used which gives an unbiased estimator of the (co)variance for i.i.d. observations." So, cov(a,b) actually calculates the sample covariance. $\endgroup$
    – Roland
    Commented Nov 22, 2013 at 14:25
  • $\begingroup$ @Roland yes I saw that thanks but in this case C and Csamp are not different as n=2, ill edit to make clearer $\endgroup$ Commented Nov 22, 2013 at 14:32
  • $\begingroup$ You need to divide the covariance by n if you don't estimate the means. So, C is the covariance multiplied by n. And if you consider that 2-1=1 there is no difference between SSa and SDsamp_a in your example. $\endgroup$
    – Roland
    Commented Nov 22, 2013 at 14:42
  • $\begingroup$ "divide the covariance by n if you don't estimate the means (i.e., your C)" not sure what you mean here sorry? $\endgroup$ Commented Nov 22, 2013 at 14:48
  • $\begingroup$ The population covariance is your C divided by n. $\endgroup$
    – Roland
    Commented Nov 22, 2013 at 14:49

1 Answer 1

6
$\begingroup$

The pearson product correlation coefficient is defined as:

$$r=\frac{cov(X,Y)}{\sigma_X\sigma_Y}.$$

In order to estimate it from the sample, you put in the sample estimates of covariance and standard deviation:

$$r=\frac{\frac{1}{n-1}\sum{(X_i-\bar{X})(Y_i-\bar{Y})}}{\sqrt{\frac{1}{n-1}\sum(X_i-\bar{X})^2}\sqrt{\frac{1}{n-1}\sum(Y_i-\bar{Y})^2}}$$

Fortunately, $n-1$ is cancelled out and you get

$$r=\frac{\sum{(X_i-\bar{X})(Y_i-\bar{Y})}}{\sqrt{\sum(X_i-\bar{X})^2}\sqrt{\sum(Y_i-\bar{Y})^2}}$$

Due to this last step it doesn't matter for the correlation coefficient if you divide by $n$ or by $(n-1)$ when calculating the covariance and standard deviations.

$\endgroup$
3
  • $\begingroup$ thanks, the papers I had added to my confusion by not being explicit between Sample and population estimates. However realising the cancellation of n or n-1 respectively makes everything clear. $\endgroup$ Commented Nov 22, 2013 at 14:44
  • 1
    $\begingroup$ Sorry, not able to comment. Very clear answer. I think the second $\sqrt{\Sigma (X_i - \bar X)^2}$ was meant to be $\sqrt{\Sigma (Y_i - \bar Y)^2}$. $\endgroup$ Commented Sep 23, 2015 at 20:57
  • $\begingroup$ @pentandrous Thanks. TeX is nice, but it's easy to get lost in the formula syntax. $\endgroup$
    – Roland
    Commented Sep 24, 2015 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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