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.
-
2$\begingroup$ From my quick read the result of NonLinearModelFit is a 'FittedModel' object, which the result of FindFit is a 'list of replacement rules'. reference.wolfram.com/language/tutorial/… $\endgroup$– Cameron MurrayCommented Oct 5, 2014 at 5:48
3 Answers
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:
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
.
-
1$\begingroup$ I've found one difference between
NonLinearModelFit
andFindFit
, and that is thatNonLinearModelFit
does not allow you to use theNormFunction
to adjust how normalization as weighting is done. By defaultNonLinearModelFit
seeks to reduce the sum of the squares of the residuals, so it will be equivalent toFindFit
withNormFunction -> (Norm[Abs[#], 2] &)]
, as described here. WhileNonLinearModelFit
is a bit "prettier", it does have some limitations in usage. $\endgroup$ 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
-
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.
-
10$\begingroup$ Actually,
NonlinearModelFit
callsFindFit
internally. The reason why it doesn't also support theNormFunction
option is because some of the properties one can request for theFittedModel
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 useNonlinearModelFit
rather thanFindFit
(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
FindFit
has the FitRegularization
option, which is missing from NonlinearModelFit
.