How to get p-val from correlation coefficient and number of sample?

77 views (last 30 days)
Hi, all!
We know that in MATLAB, [r,p]=corrcoef(A) could return the correlation coefficient and p-val.
But who knows how p-val is calculated, what is the formulas?
Thanks for your help!

Accepted Answer

Wayne King
Wayne King on 7 Nov 2011
The p-value is based on a t statistic
t = r* sqrt((n-2)/(1-r^2))
which has n-2 degrees of freedom.
You can find a discussion of the sampling distribution of r both when the null hypothesis is that r=0 and when you are testing for nonzero values of r here:

More Answers (2)

Wayne King
Wayne King on 7 Nov 2011
??? I told you that the the t above is t-distributed with n-2 degrees of freedoms. Once you know the distribution, the p-value is the probability that you would observe a value at least that extreme in that distribution.
For example, if you had a t-value of 3 and you had 20 samples, then
1-tcdf(3,18)
gives you the probability in the upper tail. Since the t-distribution is symmetric. If you are doing a two-sample hypothesis test, you have to double that probability to get the p-value (you have equal probability in the lower tail)
For example:
rng default
x = randn(18,2);
[r,p] = corrcoef(x);
gives
r = -0.0242
t = r*sqrt(16/(1-r^2));
2*tcdf(t,16)
which is exactly what MATLAB gives in the p matrix.

Gregory Pelletier
Gregory Pelletier on 24 Jan 2024
Here is how matlab calculates the p-value in corrcoef (see p_check below for the manual calculation of the p-value compared with p from corrcoef):
load hospital
X = [hospital.Weight hospital.BloodPressure];
[R, p] = corrcoef(X)
N = size(X,1);
t = sqrt(N-2).*R./sqrt(1-R.^2);
s = tcdf(t,N-2);
p_check = 2 * min(s,1-s)
% R =
% 1.0000e+00 1.5579e-01 2.2269e-01
% 1.5579e-01 1.0000e+00 5.1184e-01
% 2.2269e-01 5.1184e-01 1.0000e+00
% p =
% 1.0000e+00 1.2168e-01 2.5953e-02
% 1.2168e-01 1.0000e+00 5.2460e-08
% 2.5953e-02 5.2460e-08 1.0000e+00
% p_check =
% 0 1.2168e-01 2.5953e-02
% 1.2168e-01 0 5.2460e-08
% 2.5953e-02 5.2460e-08 0

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!