Does LinearModelFit
give an ordinary linear regression? I see lots of options, but nothing like "least squares" or OLR.
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}*)
-
$\begingroup$ If one is taking the
LeastSquares[]
route,DesignMatrix[]
is a very handy little thing. $\endgroup$ – J. M.'s ennui♦ May 7 '13 at 0:58 -
-
$\begingroup$ Is there a reason why you use
RandomReal[NormalDistribution[]
instead ofRandomVariate[NormalDistribution[],
? CanRandomReal
work with any distribution? If yes, what's the point ofRandomVariate
anyway? $\endgroup$ – Niki Estner May 7 '13 at 7:15 -
$\begingroup$ @nikie, before
RandomVariate[]
came along, one did have to useRandomReal[]
for the purpose of generating nonuniform variates. I suppose WRI has yet to remove the functionality once supported byRandomReal[]
, so it still works to this day. $\endgroup$ – J. M.'s ennui♦ 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 aroundFindFit
, and moreover that theNormFunction
option ofFindFit
could in principle be used withNonlinearModelFit
, 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
LinearModelFit
is basically just syntactic sugar for building up design matrices that go intoLeastSquares
. It additionally gives an object (rather than just a fit) so properties can be obtained. $\endgroup$ – Andy Ross May 6 '13 at 23:06Fit
andLinearModelFit
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:03LinearModelFit[]
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.'s ennui♦ May 7 '13 at 1:10