8
$\begingroup$

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]
lm["AdjustedRSquared"]
nlm = NonlinearModelFit[data, b + a*x, {a, b}, x]
nlm["AdjustedRSquared"]

gives the results

{{1,2},{1,2},{2,3},{4,6},{5,12}}
FittedModel[-0.712121+2.19697 x]
0.846521
FittedModel[-0.712121+2.19697 x]
0.929883

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?

$\endgroup$
  • $\begingroup$ I don't know the answer but here's some information about why R-square is an invalid metric for nonlinear fits. $\endgroup$ – bobthechemist Feb 3 '15 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 '15 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 '15 at 18:29
5
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ Just noticed this is actually mentioned in tutorial/StatisticalModelAnalysis. $\endgroup$ – xzczd Dec 3 '15 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 '15 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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