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
Star Strider 2021 年 9 月 18 日

0 投票

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
.

4 件のコメント

Oliver Lundblad
Oliver Lundblad 2021 年 9 月 18 日
ooh nice, thanks for correcting me. By the way, this “fitlm” function does not work for me. “Undefined function 'fitlm' for input arguments of type 'double'.” any idea why?
Star Strider
Star Strider 2021 年 9 月 18 日
My pleasuire!
The fitlm function is part of the Statistics and Machine Learning Toolbox. It must be licensed and installed in order to use its functions. (There are at least three other Toolboxes that can do parameter estimation, those being the Optimization, Global Optimization, and Curve Fitting Toolboxes.)
.
Oliver Lundblad
Oliver Lundblad 2021 年 9 月 18 日
It works now. thanks for telling me which product to install.
Star Strider
Star Strider 2021 年 9 月 18 日
As always, my pleasure!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSmoothing についてさらに検索

製品

リリース

R2021a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by