Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
4  
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
2  
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
2  
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. May 7 '13 at 1:10
add comment

1 Answer

up vote 8 down vote accepted

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}*)
share|improve this answer
    
If one is taking the LeastSquares[] route, DesignMatrix[] is a very handy little thing. –  J. M. May 7 '13 at 0:58
    
@J.M. Totally agree. –  Andy Ross May 7 '13 at 1:09
    
Thanks! Very helpful. –  lynvie May 7 '13 at 3:04
    
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? –  nikie 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. May 7 '13 at 8:31
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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