I'm trying to understand the differences between LinearModelFit and NonlinearModelFit. One thing I notice is that the computation of "AdjustedRSquared" seems to be different, even when the model results are the same. Example:

data = {{1, 2}, {1, 2}, {2, 3}, {4, 6}, {5, 12}}
lm = LinearModelFit[data, x, x]
nlm = NonlinearModelFit[data, b + a*x, {a, b}, x]

gives the results

FittedModel[-0.712121+2.19697 x]
FittedModel[-0.712121+2.19697 x]

The returned FittedModels are exactly the same, however the "RSquared" differs. What exactly is going on here? Which one is a more accurate statement of $R^2$? Are my models functionally different in some way?

  • $\begingroup$ I don't know the answer but here's some information about why R-square is an invalid metric for nonlinear fits. $\endgroup$ Feb 3, 2015 at 3:34
  • $\begingroup$ Wow, thank you. I actually had no idea about this, I'm pretty green when it comes to non-linear regressions! $\endgroup$
    – Tom Hayden
    Feb 3, 2015 at 14:10
  • $\begingroup$ I wish Mathematica would throw this warning when you pull this data, similar to how they warn you not to do a t-test when your data sets are not normally distributed. $\endgroup$
    – Tom Hayden
    Feb 3, 2015 at 18:29

1 Answer 1


I mentioned in the comment above that there is a general belief that the R-squared value is not a suitable metric for nonlinear models but that leaves us with the question, what is NonlinearModelFit calculating? The font of all knowledge, Wikipedia, informs us that the definition of R squared is 1-ssres/sstot where ssres is the sum of squares of the residuals and sstot is the total sum of squares of the responses. Both of these numbers are obtained from the ANOVATable.

#["RSquared"]& /@{lm, nlm}
#["ANOVATable"] & /@ {lm, nlm}
(* {0.884891, 0.95793} *)

enter image description here

We can see here that LinearModelFit is calculating R squared using 1-8.28788/72 and NonlinearModelFit is using 1-8.28788/197. Therefore, we can conclude that the R-squared calculated using NonlinearModelFit utilizes the uncorrected Total sum of squares whereas LinearModelFit uses the generally accepted definition for this term.

  • 1
    $\begingroup$ Just noticed this is actually mentioned in tutorial/StatisticalModelAnalysis. $\endgroup$
    – xzczd
    Dec 3, 2015 at 14:10
  • $\begingroup$ To find out the proper metric(s) for the goodness-of-fit of a nonlinear model I posted a question in stats.SE. $\endgroup$
    – xzczd
    Dec 6, 2015 at 13:18
  • 1
    $\begingroup$ This has been raised internally a few times and it is as-designed. See the Possible Issues section of the NonlinearModelFit documentation. $\endgroup$
    – Ghersic
    Sep 11, 2020 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.