Search code examples
matlab

Matlab's way of getting p-values for correlation


I have a vector A of size N and I want to calculate a correlation coefficient and p-value for the correlation of A with some other vector B.

I used corrcoef in Matlab, something like this:

[R, P] = corrcoef(A, B) 

And from what I understand, doing a t-test for this correlation R(1,2) to get a p-value equal to P(1,2) would mean calculating a test statistic t = sqrt(N-2)*R./sqrt(1-R.^2) and getting the p-value by

P = 1 - tcdf(t, N-2). 

However, if I proceed in this way, the p-value that I get is not the same as the p-value Matlab calculated. Could someone explain why, or what am I missing in the calculation? Thanks!

EDIT: Even if I do a two-sided test (P = 2*(1-tcdf(abs(t), N-2))), there's still a lot of differences in mine and Matlab's result.


Solution

  • I check the relevant source codes of matlab and octave for p-value. The source code of octave is more clear.

    Changing

    P = 2*(1-tcdf(abs(t), N-2))
    

    to

    s = tcdf(t,N-2);
    P = 2 * min(s,1-s);
    

    does the trick. Then you get same p results as corrcoef.