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 Exchange에서 Smoothing에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2021년 9월 18일

댓글:

2021년 9월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by