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.
3 Answers
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.
- 901
- 7
- 13
-
10Actually,
NonlinearModelFitcallsFindFitinternally. The reason why it doesn't also support theNormFunctionoption is because some of the properties one can request for theFittedModelobject only exist/have been implemented for the 2-norm. Thus, for the time being,NonlinearModelFitis restricted to the 2-norm only. The corollary is that if one is not interested in the properties anyway, there is no reason to useNonlinearModelFitrather thanFindFit(the latter is faster, as it is kernel code, rather than being implemented at the top level). – Oleksandr R. Jan 04 '15 at 05:14
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.
- 19,693
- 4
- 52
- 138
-
1I've found one difference between
NonLinearModelFitandFindFit, and that is thatNonLinearModelFitdoes not allow you to use theNormFunctionto adjust how normalization as weighting is done. By defaultNonLinearModelFitseeks to reduce the sum of the squares of the residuals, so it will be equivalent toFindFitwithNormFunction -> (Norm[Abs[#], 2] &)], as described here. WhileNonLinearModelFitis a bit "prettier", it does have some limitations in usage. – iwantmyphd Jan 04 '15 at 03:40 -
@iwantmyphd feel free to add another answer with your discovery and get some rep for it :-) – bobthechemist Jan 04 '15 at 04:18
-
FindFit has the FitRegularization option, which is missing from NonlinearModelFit.
- 234,956
- 30
- 623
- 1,263

http://reference.wolfram.com/language/tutorial/StatisticalModelAnalysis.html
– Cameron Murray Oct 05 '14 at 05:48