5
$\begingroup$

This question already has an answer here:

I tried searching but the noise in the responses is high due to 'fit' being both a function and a valid term.

If I'm new to Mathematica should I just default to LinearModelFit (v7) and recognise Fit as a v1 version that got superceded?

My instinct is 'yes' but wondered if there were valid use cases where Fit would be preferred over LinearModelFit.

$\endgroup$

marked as duplicate by Henrik Schumacher, MarcoB, m_goldberg, eyorble, bbgodfrey Feb 25 at 22:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    $\begingroup$ You may want to take a look at mathematica.stackexchange.com/questions/182053/… for an example of some of the usage differences. LinearModelFit generates a model object which has some useful properties (e.g. "RSquared"), in addition to the fit itself. In short, I'd only recommend Fit if you just want the shortest code for getting the linear fit expression without any other baggage. $\endgroup$ – eyorble Feb 24 at 23:13
  • $\begingroup$ thx, if i'd found that i wouldn't have asked. $\endgroup$ – Joe Feb 25 at 0:52
  • 3
    $\begingroup$ See also Difference between fitting algorithms $\endgroup$ – MarcoB Feb 25 at 4:49
12
$\begingroup$

There are a few major differences between Fit and LinearModelFit.

LinearModelFit generates a full model object as its output, which maintains a large set of interesting properties (such as RSquared or ANOVATable). These can be accessed by:

lmf = LinearModelFit[{2, 3, 4, 5}, {x}, x];
lmf["Properties"]
lmf["RSquared"]

By default, LinearModelFit assumes that the zero-intercept may be non-zero. This property is controlled by the IncludeConstantBasis option. LinearModelFit has several interesting but advanced options available as well, such as the ability to define weights and perform error propagation through the model.

The final fit expression from LinearModelFit can be extracted with Normal.

Fit does not assume a constant basis, and returns the fit expression directly. It does not calculate additional properties, will automatically convert exact numbers to machine precision, and only finds the least-squares fit. LinearModelFit and Fit can work at arbitrary precision (as unlikely as that may be to be useful), but only LinearModelFit allows the specification of WorkingPrecision as an option.

The primary advantage of Fit would appear to be its simplicity and brevity. If you only need the least-squares fit expression or are doing code-golf, Fit should suffice. In most other cases, I would recommend using LinearModelFit, if only to be able to access the various quality measures it can calculate on the side.

Note, however, that I wouldn't expect either function to have significant errors -- they ultimately should find the same model, assuming that the constant basis is either included or excluded in both, and that no special options are specified in LinearModelFit to adapt the model to a specific use case. It's just a question of if you want/need the additional data of the model object.

$\endgroup$
  • $\begingroup$ I believe LinearModelFit uses Fit behind the scenes (not sure). $\endgroup$ – Szabolcs Feb 25 at 9:32
  • 2
    $\begingroup$ @Szabolcs I just spent a few minutes PrintDefinitionsing LinearModelFit and didn't see any usage of Fit specifically. $\endgroup$ – Carl Lange Feb 25 at 9:43

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