# Am I using corrcoef correctly?

2 views (last 30 days)
Oliver Lundblad on 18 Sep 2021
Commented: Star Strider on 18 Sep 2021
I want to find R^2 for my polyfit approximation and I read on this form that corrcoef can be used to find R^2. I am however not sure if I have the correct inputs or if I interpret the output correctly? Can someone confirm if I have correctly used corrcoef in this code and that R^2 equals 0.6327 for my polyfit approximation?
H=log([0.01 0.02 0.03 0.04 0.05]);
T=log([0.645 0.647 0.646 0.646 0.647]);
p=polyfit(H,T,1);%derivative=0.0013
f=polyval(p,H);
plot (H,T,'o',H,f,'-');
%legend('data','linear fit')
axis([-4.8 -2.8 -0.45 -0.42])
xlabel("ln(h/m)")
ylabel ("ln(T/s)")
grid on
[R1,P1] = corrcoef(H,T) %R^2=0.6327

Star Strider on 18 Sep 2021
Am I using corrcoef correctly?
Yes, except for squaring the result, and that will produce .
H=log([0.01 0.02 0.03 0.04 0.05]);
T=log([0.645 0.647 0.646 0.646 0.647]);
p=polyfit(H,T,1);%derivative=0.0013
f=polyval(p,H);
plot (H,T,'o',H,f,'-');
%legend('data','linear fit')
axis([-4.8 -2.8 -0.45 -0.42])
xlabel("ln(h/m)")
ylabel ("ln(T/s)")
grid on [R1,P1] = corrcoef(H,T) %R^2=0.6327
R1 = 2×2
1.0000 0.6327 0.6327 1.0000
P1 = 2×2
1.0000 0.2520 0.2520 1.0000
Rsq = R1.^2
ans = 2×2
1.0000 0.4003 0.4003 1.0000
And fitlm agrees —
mdl = fitlm(H,T)
mdl =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue _________ __________ _______ __________ (Intercept) -0.43194 0.0033633 -128.43 1.0409e-06 x1 0.0012893 0.00091105 1.4152 0.25196 Number of observations: 5, Error degrees of freedom: 3 Root Mean Squared Error: 0.00116 R-squared: 0.4, Adjusted R-Squared: 0.2 F-statistic vs. constant model: 2, p-value = 0.252
.
Star Strider on 18 Sep 2021
As always, my pleasure!
.

R2021a

### Community Treasure Hunt

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

Start Hunting!