Why do regression coefficients obtained from Excel and Matlab differ?
4 views (last 30 days)
Show older comments
Hi
I would like to run a regression on the following matrices: X(m,1) and Y(m,1). I wan to run a polynomial regression of order 15. I have used: 1. Method 1: b - polynomial order (I even increased precision with vpa, but that did not make any difference)
XM=ones(size(X,1),b);
for f=1:b
H=vpa(X);
XM(:,f+1)=H.^f;
end
Beta=pinv(XM'*XM)*(XM'*Y)
ExpCont=XM*Beta
2. Method 2
BetaP=polyfit(X,Y,b)
ExpContP=polyval(BetaP,X)
Both methods resulted in the same result.
I have run regressions with the same input in Excel and Matlab. For an order 2 polynomial, the difference is insignificant. For higher order polynomials, the coefficients are not even close!!! Sometimes they even have opposite signs!!!! This results in a difference in the output.
Why are they different?
0 Comments
Accepted Answer
dpb
on 18 Apr 2014
...want to run a polynomial regression of order 15.
very, Very, VERY, bad idea!!!
The differences are caused by numerical precision with such large powers owing to differences in how the normal equations are formed and solved.
For a practical solution, use something other than such a fitting form, say piecewise splines or the like.
Why do you think you need/want such a high-order polynomial, anyway?
3 Comments
John D'Errico
on 18 Apr 2014
I'll amplify what dpb said. An insane idea.
High order polynomials like that will end up with virtually random coefficients due to numerical issues. Depending on how the data was shipped from one language to the other, you may have lost a bit or so in the least significant bits. That subtle difference is sufficient to see a difference when you compute that high order a polynomial. On top of that, the algorithm chosen by either system to compute the regression coefficients can easily have a huge impact on a problem like this.
By the way, the first method you showed was a TERRIBLE choice in terms of the numerics. Better would have been:
Beta = pinv(XM)*Y;
But even so, 15th degree is insane most of the time, and generally a complete waste of bits.
More Answers (0)
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!