MATLAB: Unsatisfying 2nd order fit – fitlm

fitting

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

Best Answer

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 :)).