How to get p-val from correlation coefficient and number of sample?
Show older comments
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!
1 Comment
R. Gokulakannan
on 1 Nov 2022
>>m=8+4
Accepted Answer
More Answers (2)
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
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
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!