5
$\begingroup$

Does LinearModelFit give an ordinary linear regression? I see lots of options, but nothing like "least squares" or OLR.

$\endgroup$
  • 4
    $\begingroup$ Yes. LinearModelFit is basically just syntactic sugar for building up design matrices that go into LeastSquares. It additionally gives an object (rather than just a fit) so properties can be obtained. $\endgroup$ – Andy Ross May 6 '13 at 23:06
  • 2
    $\begingroup$ From ref/LinearModelFit/Properties & Relations: "Fit and LinearModelFit fit equivalent models". Form ref/Fit: "Fit[data, funs, vars] finds a least-squares fit to a list of data as a linear combination of the functions funs of variables vars." $\endgroup$ – m_goldberg May 7 '13 at 1:03
  • 3
    $\begingroup$ In addition, the docs for LinearModelFit[] do implicitly state the least-squares assumption. Then again, most people don't bother to remember the usual assumptions for applying least-squares, and just happily chuck their data and model into the function without thinking. $\endgroup$ – J. M. will be back soon May 7 '13 at 1:10
8
$\begingroup$

As I pointed out in the comments. LinearModelFit was designed to make it easy to specify models fit using LeastSquares given some input data by providing a language for constructing design matrices via basis functions.

LinearModelFit also aims to make the workflows of plotting models, computing residuals, parameter confidence intervals, etc much easier.

Here is an example which shows the equivalence of LeastSquares and LinearModelFit. Notice that the constant basis is included by default. We have to manually add it for LeastSquares.

n = 10;
SeedRandom[1];
xdata = RandomReal[{-1, 1}, {n, 3}];
ydata = Total[xdata, {2}] + RandomReal[NormalDistribution[], n] + 2;
data = Transpose[Transpose[xdata]~Join~{ydata}];

LinearModelFit[data, {x1, x2, x3}, {x1, x2, x3}]["BestFitParameters"]

(*{1.85077, 1.48564, 1.08907, 2.36973}*)

LeastSquares[Transpose[{ConstantArray[1, n]}~Join~Transpose[xdata]], ydata]

(*{1.85077, 1.48564, 1.08907, 2.36973}*)
$\endgroup$
  • $\begingroup$ If one is taking the LeastSquares[] route, DesignMatrix[] is a very handy little thing. $\endgroup$ – J. M. will be back soon May 7 '13 at 0:58
  • $\begingroup$ @J.M. Totally agree. $\endgroup$ – Andy Ross May 7 '13 at 1:09
  • $\begingroup$ Is there a reason why you use RandomReal[NormalDistribution[] instead of RandomVariate[NormalDistribution[], ? Can RandomReal work with any distribution? If yes, what's the point of RandomVariate anyway? $\endgroup$ – Niki Estner May 7 '13 at 7:15
  • $\begingroup$ @nikie, before RandomVariate[] came along, one did have to use RandomReal[] for the purpose of generating nonuniform variates. I suppose WRI has yet to remove the functionality once supported by RandomReal[], so it still works to this day. $\endgroup$ – J. M. will be back soon May 7 '13 at 8:31
  • $\begingroup$ @Andy I was poking about inside NonlinearModelFit the other day and found that it too is syntactic sugar around FindFit, and moreover that the NormFunction option of FindFit could in principle be used with NonlinearModelFit, but in practice it is hard-coded to be overridden by the 2-norm. Is this an oversight or is it necessary because the properties calculations assume the 2-norm? $\endgroup$ – Oleksandr R. May 8 '13 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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