Unsatisfying 2nd order fit - fitlm
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dear users,
I have been struggling with a fitting problem in quite a while. I have small set of 8 points on which I would like to do a least square fit. I tried both 1st and 2nd order fit using "Reg = fitlm(X,Y);" with the arguments "linear", "quadratic" and "purequadratic". The linear fit gives me an R^2 of 0.975 and the quadratic fit 0.982, which makes sense.
If I plot the two fits however, optically the fit becomes worse when using a quadratic function:
1st Order:
2nd Order:
I plotted the fitted lines by plot(xRange,polyval(flip(Reg.Coefficients.Estimate)));
Is there anything I did wrong or could consider?
Thanks for your support,
Frank
0 Kommentare
Akzeptierte Antwort
dpb
am 15 Mai 2017
Bearbeitet: dpb
am 15 Mai 2017
W/o the data itself can't really tell, but certainly doesn't appear that the plot is the actual LS quadratic for the data given, no.
...
Oh, reread the post again to see if missed something -- and I see I did, and it's undoubtedly the cause of the problem--
plot(xRange,polyval(flip(Reg.Coefficients.Estimate)));
is a problem. First, POLYVAL is missing an array of x values at which to be evaluated so that'll have not worked at all as is; I'm guessing that what you had was
plot(xRange,polyval(flip(Reg.Coefficients.Estimate,xRange)));
and xRange is just the two end points of X. If so, you simply displayed the line between those points and not the actual curvefit result between...that would produce the symptom as it appears what little curvature there is is upwards at the ends so those two fitted points would be expected to be above the observed data there, thus raising the whole line above the group as whole.
But, I'd suggest for only eight points and with the very little evidence of curvature that the quadratic is overfitting the data.
If you do want the extra diagnostics and all of the linear model tool, use feval or predict to evaluate it, though.
plot(predict(Reg))
would have drawn the curve thru the points as they're kept as part of the object. Or,
plot(Reg)
would include confidence bounds to indicate something about the fit quality...
But, I'll grant it's really, really tough to find this out from the documentation; you have to dig into the LinearMddel class description itself to actually get to the properties and list of properties and a succinct comprehensive list of methods and what they do; all the examples are somewhat helpful in seeing an overview but trying to find something specific is pretty-much obfuscated by all the clutter.
If you also happen to have the Curve Fit TB, the cfit object is somewhat easier to use, albeit without all the statistics stuff.
Or, if you only need the fit and results and don't care about the niceties of significance and all that, then just the venerable ol' polyfit|polyval are as simple as it gets (the coefficients are even in self-consistent order then :)).
2 Kommentare
dpb
am 17 Mai 2017
[OPs Answer moved to comment...dpb]
Dear dpb,
thanks for your detailed answer. The missing argument Xrange was a mistake in transfering the code to this thread. You were right with your second idea that I handed over the range, in this case [1 7] instead of the points, rooky mistake...
Thanks for your help!
dpb
am 17 Mai 2017
No problem...when I realized what had missed in first reading and removed a long pontificating speech, the real answer was pretty-much self-evident... :)
I wish TMW could figure out a way to implement the Statistics Toolbox in a much more user-friendly syntax--it's a bugger of a problem given need to be Matlab compatible with results and all for further use, but it is an uncomfortable mishmash between competing objectives now...
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!