Why does "fitnlm" provide different results from R and Python?
6 views (last 30 days)
Show older comments
MathWorks Support Team
on 13 Jun 2024
Answered: MathWorks Support Team
on 15 Jul 2024
I have completed the same calculations on data in MATLAB, R, and Python to fit a nonlinear regression.
While the results of R and Python are the same, the results from using the MATLAB function "fitnlm" vary by orders of magnitude for the standard error, adjusted r-squared, and degrees of freedom. During computation, MATLAB provides a warning that the Jacobian is ill conditioned, but this difference is far greater than I would expect.
Why do the results from MATLAB's function "fitnlm" vary greatly from R and Python?
Accepted Answer
MathWorks Support Team
on 13 Jun 2024
Explanation for differing results in MATLAB
As you pointed out, MATLAB throws a warning during computation stating that the Jacobian is ill conditioned. When the Jacobian (J) is ill conditioned, MATLAB computes "inv(J'*J)" needed for standard error (SE) differently. Please visit the documentation page below for more information on computing the estimated covariance matrix:
If the Jacobian is ill-conditioned, MATLAB opts to find the inverse via Singular Value Decomposition (SVD). This seems to be a preferable approach generally in linear algebra - SVD will be more reliable/accurate even though the matrix is ill-conditioned. When the Jacobian is well conditioned, MATLAB uses the QR decomposition. On the other hand, R always finds the inverse using the QR decomposition approach even if the Jacobian is ill-conditioned.
It is also expected that the degrees of freedom is different between R and MATLAB in the ill conditioned case. MATLAB performs more robust checks to see if one model parameter is essentially the same as another. That is one of the original warnings that is generated, basically stating that there are some parameters in this model that serve the same purpose or are duplicates of other parameters, which can result in a poor fit. When that happens, MATLAB opts not to try and include that estimate in the calculations of things like the degrees of freedom.
Potential Workaround
Center the x values in your data for the linear regression to prevent ill-conditioning of the Jacobian. If you do this, you will find that the results from R and MATLAB will match.
0 Comments
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB 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!