fitrlinear
statistics: Mdl = fitrlinear (X, Y)
statistics: Mdl = fitrlinear (Tbl, ResponseVarName)
statistics: Mdl = fitrlinear (Tbl, formula)
statistics: Mdl = fitrlinear (Tbl, Y)
statistics: Mdl = fitrlinear (…, name, value)
statistics: [Mdl, FitInfo] = fitrlinear (…)
Fit a linear regression model.
Mdl = fitrlinear (X, Y) returns a
RegressionLinear object fitted to the predictor data X and
the continuous response Y, where X is an NxP numeric
matrix and Y an Nx1 numeric vector with as many rows as
X.
Mdl = fitrlinear (…, name, value) passes
the given Name-Value pairs to the model. They are documented
under RegressionLinear, and the ones most often wanted are
'Learner', 'Epsilon', 'Regularization',
'Lambda' and 'Solver'.
[Mdl, FitInfo] = fitrlinear (…) also returns a
structure describing the optimization: what it converged to, how far it
got, and which tolerance stopped it. Its fields follow the solver, so a
dual fit reports the dual variables and a mini-batch fit the batch it
stopped on.
Mdl = fitrlinear (…, cvopt, value) returns a
RegressionPartitionedLinear
instead when one of 'CrossVal', 'KFold',
'Holdout', 'Leaveout' and 'CVPartition' is
given. A cross-validated model describes no single fit, so
FitInfo is not available beside it.
See also: RegressionLinear, RegressionKernel, fitrkernel
Source Code: fitrlinear
Fit a linear regression to fuel consumption and read what the optimization did.
load carsmall X = [Acceleration, Displacement, Horsepower, Weight]; ok = ! any (isnan ([X, MPG]), 2); [Mdl, FitInfo] = fitrlinear (X(ok,:), MPG(ok), 'Learner', 'leastsquares')
Mdl =
RegressionLinear
ResponseName: 'Y'
ResponseTransform: 'none'
Beta: [4x1 double]
Bias: 23.7258
Lambda: 0.0107527
Learner: 'leastsquares'
FitInfo =
scalar structure containing the fields:
Lambda = 0.010753
Objective = 30.658
IterationLimit = 1000
NumIterations = 1
GradientNorm = 749.12
GradientTolerance = 1.0000e-06
RelativeChangeInBeta = 2.8071e-05
BetaTolerance = 1.0000e-04
DeltaGradient = [](0x0)
DeltaGradientTolerance = [](0x0)
TerminationCode = 1
TerminationStatus =
{
[1,1] = Tolerance on coefficients satisfied.
}
History = [](0x0)
FitTime = 0
Solver =
{
[1,1] = bfgs
}
Fit from a table, and predict on one
load fisheriris
T = table (meas(:,2), meas(:,3), meas(:,4), meas(:,1), ...
'VariableNames', {'SW', 'PL', 'PW', 'SL'});
A column holding levels is a categorical predictor without being named one
T.Wide = categorical (meas(:,2) > 3, [false true], {'narrow', 'wide'});
The response is named by its column, and everything else is a predictor
Mdl = fitrlinear (T, 'SL'); Mdl.PredictorNames
ans =
1x4 cell array
{'SW'} {'PL'} {'PW'} {'Wide'}
Mdl.CategoricalPredictors
ans = 4
A model formula names them instead, holding main effects only
Mdl2 = fitrlinear (T, 'SL ~ PL + Wide'); Mdl2.PredictorNames
ans =
1x2 cell array
{'PL'} {'Wide'}
predict reads a table by the names the model was fitted on, so the columns may come in any order and may carry more than the model needs
yFit = predict (Mdl, T(1:5, [5, 4, 3, 2, 1])); yFit'
ans = 5.0164 4.6349 4.7786 4.8617 5.0725