31
$\begingroup$

I cannot get the difference between these two fitting functions in Mathematica. I even read in MMA that the result of these two are the same.

$\endgroup$
1

3 Answers 3

20
$\begingroup$

Note that FindFit was first introduced in version 5 and updated in version 7. Since then, FindFit has not been modified. NonlinearModelFit was introduced in 7 and appears to have remained unchanged since then. The two use the same format for arguments; however, FindFit returns the best fit parameters whereas NonlinearModelFit returns a model. The model has a wealth of statistical information included in it, and is referenced in current versions of the FindFit Documentation:

enter image description here

The two functions seem to behave similarly both in their default behavior:

data = Table[{i, Exp[RandomReal[{i - 1, i}]]}, {i, 10}];
nlm = NonlinearModelFit[data, Exp[a + b x], {a, b}, x][
   "BestFitParameters"];
ff = FindFit[data, Exp[a + b x], {a, b}, x];
nlm == ff
(* True *)

..and in their possible issues

model = a1 Exp[-(b1 (x - x1))^2] + a2 Exp[-(b2 (x - x2))^2];
data = Block[{a1 = 1, b1 = 5, x1 = -.5, a2 = 2, b2 = 10, x2 = .25, 
    x = Sort[RandomReal[{-1, 1}, 100]]},
   Transpose[{x, model + RandomReal[{-.1, .1}, 100]}]];
fit = FindFit[data, model, {a1, b1, x1, a2, b2, x2}, x];
ffplot = Show[ ListPlot[data, PlotRange -> All], 
  Plot[Evaluate[model /. fit], {x, -1, 1}, 
   PlotStyle -> Directive[Thick, Red]]]
nlm = NonlinearModelFit[data, model, {a1, b1, x1, a2, b2, x2}, x]
nlmplot = 
 Show[ListPlot[data, PlotRange -> All], 
  Plot[nlm[x], {x, -1, 1}, PlotStyle -> Directive[Thick, Red]]]
ffplot === nlmplot
(* True *)

The bottom line, NonlinearModelFit is the next generation of FindFit which provides more functionality and information about the fitted model, and there is likely no reason at this point in time to use FindFit.

$\endgroup$
3
  • 1
    $\begingroup$ I've found one difference between NonLinearModelFit and FindFit, and that is that NonLinearModelFit does not allow you to use the NormFunction to adjust how normalization as weighting is done. By default NonLinearModelFit seeks to reduce the sum of the squares of the residuals, so it will be equivalent to FindFit with NormFunction -> (Norm[Abs[#], 2] &)], as described here. While NonLinearModelFit is a bit "prettier", it does have some limitations in usage. $\endgroup$
    – iwantmyphd
    Commented Jan 4, 2015 at 3:40
  • $\begingroup$ @iwantmyphd feel free to add another answer with your discovery and get some rep for it :-) $\endgroup$ Commented Jan 4, 2015 at 4:18
  • $\begingroup$ Good idea, will do. :-) $\endgroup$
    – iwantmyphd
    Commented Jan 4, 2015 at 4:55
25
$\begingroup$

I've found one difference between NonLinearModelFit and FindFit, and that is that NonLinearModelFit does not allow you to use the NormFunction to adjust how normalization as weighting is done. By default NonLinearModelFit seeks to reduce the sum of the squares of the residuals, so it will be equivalent to FindFit with NormFunction -> (Norm[Abs[#], 2] &)], as described here. While NonLinearModelFit is a bit "prettier", it does have some limitations in usage.

$\endgroup$
1
  • 10
    $\begingroup$ Actually, NonlinearModelFit calls FindFit internally. The reason why it doesn't also support the NormFunction option is because some of the properties one can request for the FittedModel object only exist/have been implemented for the 2-norm. Thus, for the time being, NonlinearModelFit is restricted to the 2-norm only. The corollary is that if one is not interested in the properties anyway, there is no reason to use NonlinearModelFit rather than FindFit (the latter is faster, as it is kernel code, rather than being implemented at the top level). $\endgroup$ Commented Jan 4, 2015 at 5:14
3
$\begingroup$

FindFit has the FitRegularization option, which is missing from NonlinearModelFit.

$\endgroup$

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.