How to get R^2 of a 5th order polyfit?

40 views (last 30 days)
David Pesetsky
David Pesetsky on 13 May 2018
Commented: David Pesetsky on 13 May 2018
I need to find how poorly the 5th order fit is for:
[p,S] = polyfit(x,y0,5);
yp = polyval(p,x);
An R^2 would be perfect, but I can not understand the answers I'm finding. I don't think I want correlations.
R^2.
Thanks!

Answers (1)

John D'Errico
John D'Errico on 13 May 2018
Edited: John D'Errico on 13 May 2018
polyfit does not return an R^2. A good idea, because IMHO, R^2 is of little value in determining if the fit is good. Does the fit look good? If not, then who cares what R^2 tells you? And if the fit looks like crap, then again, do you need R^2? Reliance on a single number is a bad idea. But people demand it.
You can download my polyfitn from the file exchange , which does compute an R^2.
x = linspace(0,1,50);
y = exp(x) + randn(size(x))/100;
P5 = polyfitn(x,y,5)
P5 =
struct with fields:
ModelTerms: [6×1 double]
Coefficients: [-0.34052 0.37377 0.44485 0.11449 1.1189 0.99134]
ParameterVar: [1.223 7.7232 6.3053 0.95902 0.023737 5.4246e-05]
ParameterStd: [1.1059 2.7791 2.511 0.97929 0.15407 0.0073652]
DoF: 44
p: [0.7596 0.89362 0.8602 0.90746 4.7259e-09 3.4139e-59]
R2: 0.99963
AdjustedR2: 0.99959
RMSE: 0.0096032
VarNames: {'X1'}
P5.R2
ans =
0.99963
plot(x,y,'ro',x,polyvaln(P5,x),'b-')
If you have the curve fitting toolbox, this would have worked as well:
[mdl,stuff] = fit(x',y','poly5')
mdl =
Linear model Poly5:
mdl(x) = p1*x^5 + p2*x^4 + p3*x^3 + p4*x^2 + p5*x + p6
Coefficients (with 95% confidence bounds):
p1 = -0.3405 (-2.569, 1.888)
p2 = 0.3738 (-5.227, 5.975)
p3 = 0.4448 (-4.616, 5.506)
p4 = 0.1145 (-1.859, 2.088)
p5 = 1.119 (0.8084, 1.429)
p6 = 0.9913 (0.9765, 1.006)
stuff =
struct with fields:
sse: 0.0046111
rsquare: 0.99963
dfe: 44
adjrsquare: 0.99959
rmse: 0.010237
Finally, with a little more effort, you could have used the stats toolbox. You would need to do a bit more to use regress and regstats however. But you would get more complete information about the model then too.
  1 Comment
David Pesetsky
David Pesetsky on 13 May 2018
r_sqr=power(corr2(yp,y0),2) gives the same value as the adj R^2 from fit. So that one is good enough.
Humans aren't around to lay eyes on hundreds of fits. We want to kick out only the "bad-ish" ones :)

Sign in to comment.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!