19
$\begingroup$

What are the practical differences between: FindFit,NonlinearModelFit,and Fit. Do they call different fitting algorithms and routines? How can one tell which is best to use in a certain situation. How is it best to use them once you've chosen?

$\endgroup$
1
  • 5
    $\begingroup$ Fit does not contain without support for diagnostic of the defined model. NonlinearModelFit contain, for instance, results of ANOVA, confidence intervals for parameters of model, information criteria as a BIC, AIC .... $\endgroup$ Commented Nov 2, 2018 at 10:11

1 Answer 1

27
$\begingroup$

Fit is limited to using a series of basis functions. It finds the parameters multiplied by the basis functions that fits the data in a least squares sense.

FindFitis capable of using very general functions that don't work with the Fit model. It will also find parameters that fits the data in a least squares sense.

LinearModelFit is the same as Fit with the additional ability of outputting a great deal of diagnostic information. The output can conveniently be used directly as a function.

Similarly NonlinearModelFit is the same as FindFit with the ability of outputting diagnostic information. The output can be used directly as a function.

Example:

data = Table[Prime[x], {x, 20}]
(* {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, \
59, 61, 67, 71} *)

Fit

Fit[data, {1, x, x^2}, x]
(* -1.92368 + 2.2055 x + 0.0746753 x^2 *)

Plotting the result requires either copy and pasting the function or using Evaluate within Plot.

Show[
 Plot[Evaluate[Fit[data, {1, x, x^2}, x]],  {x, 1, 20}],
 ListPlot[data, PlotStyle -> Red]
 ]

Mathematica graphics

LinearModelFit

lm = LinearModelFit[data, {1, x, x^2}, x]

Mathematica graphics

lm["BestFitParameters"]
(* {-1.92368, 2.2055, 0.0746753} *)

Some diagnostic information

lm["CorrelationMatrix"]
(* {{1., -0.888805, 0.781116}, {-0.888805, 
  1., -0.971348}, {0.781116, -0.971348, 1.}} *)

Easier to plot. Can use lm directly as a function.

Show[
 Plot[lm[x],  {x, 1, 20}],
 ListPlot[data, PlotStyle -> Red]
 ]

Mathematica graphics

FindFit

Can use general functions.

FindFit[data, a x Log[b + c x], {a, b, c}, x]
(* {a -> 1.42076, b -> 1.65558, c -> 0.534645} *)

Same problem with plotting.

Show[
 Plot[Evaluate[
   a x Log[b + c x] /. 
    FindFit[data, a x Log[b + c x], {a, b, c}, x]],  {x, 1, 20}],
 ListPlot[data, PlotStyle -> Red]
 ]

Mathematica graphics

NonlinearModelFit

nlm = NonlinearModelFit[data, a x Log[b + c x], {a, b, c}, x]

Mathematica graphics

nlm["BestFitParameters"]
(* {a -> 1.42076, b -> 1.65558, c -> 0.534645} *)

Some diagnostic information

nlm["CorrelationMatrix"]
(* {{1., 0.844101, -0.998155}, {0.844101, 
  1., -0.872743}, {-0.998155, -0.872743, 1.}} *)

As with LinearModelFit can use the output directly as a function.

Show[
 Plot[nlm[x],  {x, 1, 20}],
 ListPlot[data, PlotStyle -> Red]
 ]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Beautifully detailed and explained answer. Thank you! $\endgroup$
    – user27119
    Commented Nov 6, 2018 at 18:58
  • 1
    $\begingroup$ I agree that NonlinearModelFit is far more convenient than FindFit, because the former actually outputs a functional form, while the latter only outputs parameters that must then be substituted back into the model. However, this distinction does not exist between LinearModelFit and Fit, since, they both output functional forms that can be used directly for plotting: lm = LinearModelFit[data, {1, x, x^2}, x]; Show[Plot[lm[x], {x, 1, 20}], ListPlot[data, PlotStyle -> Red]] AND: fit = Fit[data, {1, x, x^2}, x]; Show[Plot[fit, {x, 1, 20}], ListPlot[data, PlotStyle -> Red]] $\endgroup$
    – theorist
    Commented Nov 22, 2020 at 6:16

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.