# Does LinearModelFit perform an ordinary linear regression (least squares)?

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

• 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. – Andy Ross May 6 '13 at 23:06
• 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." – m_goldberg May 7 '13 at 1:03
• 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. – J. M. will be back soon May 7 '13 at 1:10

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}*)

• If one is taking the LeastSquares[] route, DesignMatrix[] is a very handy little thing. – J. M. will be back soon May 7 '13 at 0:58
• @J.M. Totally agree. – Andy Ross May 7 '13 at 1:09
• 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? – Niki Estner May 7 '13 at 7:15
• @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. – J. M. will be back soon May 7 '13 at 8:31
• @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? – Oleksandr R. May 8 '13 at 11:46