fitlm
statistics: mdl = fitlm (X, y)
statistics: mdl = fitlm (tbl)
statistics: mdl = fitlm (tbl, ResponseVarName)
statistics: mdl = fitlm (tbl, y)
statistics: mdl = fitlm (…, modelspec)
statistics: mdl = fitlm (…, Name, Value, …)
Fit a linear regression model to data and return a LinearModel
object.
The returned object stores the fitted coefficients, their standard errors,
t-statistics, and p-values, summary statistics of the fit (,
RMSE, F-statistic, etc.), and the residuals and diagnostics of the fit, and
exposes methods such as predict, plotResiduals,
coefTest, addTerms, and removeTerms for further
analysis of the fitted model.
mdl = fitlm (X, y) fits a linear regression model
of the response y to the predictor data X. Unless removed via
the 'Intercept' option, the fitted model contains a constant
(intercept) term and one linear term for every column of X.
'x1', 'x2', …,
'xP'.
'x1' by default.
'y'.
mdl = fitlm (tbl) fits a linear regression model using
the variables contained in the table (or dataset) tbl. By default,
the last variable in tbl is used as the response and all other
variables are used as predictors. Variables that are categorical
arrays, cell arrays of character vectors, or logical arrays are
automatically treated as categorical predictors.
mdl = fitlm (tbl, ResponseVarName) fits a model
using the variable named ResponseVarName in tbl as the
response, and all remaining variables in tbl as predictors.
mdl = fitlm (tbl, y) fits a model using the
variables in tbl as predictors and the external numeric vector
y as the response. y must have height (tbl)
elements.
mdl = fitlm (…, modelspec) additionally specifies
the terms of the model to fit, using any of the input combinations shown
above. modelspec can be any of the following.
| Value | Description | |
|---|---|---|
'constant' | Model contains only an intercept term. | |
'linear' | Model contains an intercept and one term for each predictor variable. This is the default when modelspec is not specified. | |
'interactions' | Model contains an intercept, all linear terms, and all pairwise products of distinct predictor variables (no squared terms). | |
'purequadratic' | Model contains an intercept, all linear terms, and all squared terms. | |
'quadratic' | Model contains an intercept, all linear terms, all pairwise products of distinct predictor variables, and all squared terms. | |
'full' | Model contains an intercept and all terms up to and including the full -way interaction of the predictor variables, i.e. every combination of one or more distinct predictors. | |
| terms matrix | A or numeric matrix, where is the number of terms and is the number of predictor variables. Each row represents one term, and the value in column is the exponent to which predictor is raised in that term; a row of all zeros represents the intercept. If a matrix is supplied, its last column (representing the response variable) must be all zeros. | |
| Wilkinson formula | A character vector of the form
'y ~ terms' describing the response and predictor terms using
Wilkinson notation. The variable name to the left of '~' is used
as the response, overriding any response implied elsewhere in the call. |
When modelspec is given as a Wilkinson formula, the following
operators may be used on its right-hand side to build up terms:
| Operator | Meaning | Example |
|---|---|---|
+ | add a term | 'x1 + x2' adds x1 and
x2 as separate terms |
- | remove a term | 'x1*x2 - x1:x2' removes the
interaction, leaving only x1 and x2 |
* | cross two terms | 'x1*x2' expands to
x1, x2, x1:x2 |
: | interaction only | 'x1:x2' adds only the
interaction term between x1 and x2 |
^ | power / crossing limit | 'x^2' adds
x and x^2; '(x1+x2)^2' expands to x1,
x2, x1:x2 |
-1 | remove intercept | 'x1 + x2 - 1' fits the
model without a constant term |
A formula includes an intercept term by default; append '- 1' to
the formula to omit it. For a categorical predictor, fitlm
generates the necessary indicator (dummy) variables automatically from the
formula, so a formula does not need to be changed when the underlying
design matrix changes.
mdl = fitlm (…, Name, Value, …)
specifies additional options using one or more Name-Value pair
arguments, which may be combined with modelspec or used on their own.
| Name | Value | |
|---|---|---|
'Intercept' | A logical scalar indicating whether to
include a constant (intercept) term in the model. Default is true.
This option only applies when modelspec is a character vector model
name (or omitted); it is ignored when modelspec is a terms matrix or
a Wilkinson formula, where the intercept is instead controlled by the
matrix/formula itself. | |
'Weights' | A numeric vector of nonnegative observation weights, with one element per observation, used to fit a weighted least squares model. Default is a vector of ones, i.e. an unweighted ordinary least squares fit. | |
'Exclude' | A numeric or logical vector specifying
observations to exclude from the fit, given as row indices into the
original data or as a logical mask the same length as the number of
observations. Excluded observations, together with any observation that
contains a missing (NaN) value in a predictor or the response, are
recorded in the ObservationInfo property of the fitted model but do
not contribute to the fitted coefficients or summary statistics. | |
'CategoricalVars' | Specifies which predictor
variables are treated as categorical, given as a vector of column indices,
a logical vector, or a cell array of variable names (only valid for table
input). Each categorical predictor with distinct categories is
expanded into indicator (dummy) variables, using the first
category (in sorted or original order) as the reference level that is
omitted from the design matrix. Variables that are already
categorical arrays or cell arrays of character vectors are always
treated as categorical, regardless of this option. | |
'VarNames' | A cell array of character vectors
naming the predictor and response variables, listed in order with the
response variable name last, e.g. {"x1", "x2", "y"} for two
predictors. Only applies when X and y (or a categorical
vector and y) are supplied directly, since table variables already
carry their own names. By default, predictors are named 'x1',
'x2', etc. and the response is named 'y'. | |
'ResponseVar' | A character vector naming the
response variable, used to override the response variable name that would
otherwise be inferred (the last table variable, or 'y' for matrix
input). | |
'PredictorVars' | A cell array of character vectors naming which variables in tbl to use as predictors. By default, all variables in tbl other than the response variable are used as predictors. | |
'RobustOpts' | Selects ordinary least squares or
robust regression fitting. This value can be 'off' (default,
ordinary least squares), 'on' (robust fitting using the
'bisquare' weighting function), the name of one of the weighting
functions below, a function handle for a custom weighting function, or a
scalar structure with fields RobustWgtFun and Tune
specifying the weighting function and its tuning constant. Robust fitting
uses Iteratively Reweighted Least Squares (IRLS), refitting the model with
updated observation weights until the coefficients converge. Supported
weighting function names: 'andrews', 'bisquare',
'cauchy', 'fair', 'huber', 'logistic',
'ols', 'talwar', 'welsch', each with its own default
tuning constant. |
fitlm solves the (weighted) least squares problem by applying a
pivoted QR decomposition to the design matrix, which remains numerically
stable even when predictors are collinear; coefficients corresponding to
columns beyond the numerically detected rank of the design matrix are set
to zero. Robust fits refine this ordinary least squares solution using
IRLS as described above. Observations with missing values in any variable
used by the model, or explicitly excluded via 'Exclude', are
omitted from the fit entirely and flagged in ObservationInfo, but
are otherwise not counted as errors.
mdl is returned as a LinearModel object. If
'RobustOpts' is anything other than 'off', the returned
model is a robust fit rather than an ordinary least squares fit, and its
Robust property is populated accordingly.
See also: LinearModel
Source Code: fitlm
y = [ 8.706 10.362 11.552 6.941 10.983 10.092 6.421 14.943 15.931 ...
22.968 18.590 16.567 15.944 21.637 14.492 17.965 18.851 22.891 ...
22.028 16.884 17.252 18.325 25.435 19.141 21.238 22.196 18.038 ...
22.628 31.163 26.053 24.419 32.145 28.966 30.207 29.142 33.212 ...
25.694 ]';
X = [1 1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5]';
mdl = fitlm (X, y, 'linear', 'CategoricalVars', 1)
mdl =
Linear regression model:
y ~ 1 + x1
Coefficients:
5x4 table
Estimate SE tStat pValue
________ _______ _______ ___________
(Intercept) 10 1.01775 9.82556 3.48093e-11
x1_2 8 1.64108 4.87484 2.85812e-05
x1_3 9 1.43932 6.25295 5.22937e-07
x1_4 11.0001 1.48984 7.38344 2.12795e-08
x1_5 19.0001 1.39877 13.5835 7.82092e-15
Number of observations: 37, Error degrees of freedom: 32
Root Mean Squared Error: 2.87864
R-squared: 0.854819, Adjusted R-Squared: 0.836672
F-statistic vs. constant model: 47.1038, p-value = 5.71708e-13
popcorn = [5.5, 4.5, 3.5; 5.5, 4.5, 4.0; 6.0, 4.0, 3.0; ...
6.5, 5.0, 4.0; 7.0, 5.5, 5.0; 7.0, 5.0, 4.5];
brands = {'Gourmet', 'National', 'Generic'; ...
'Gourmet', 'National', 'Generic'; ...
'Gourmet', 'National', 'Generic'; ...
'Gourmet', 'National', 'Generic'; ...
'Gourmet', 'National', 'Generic'; ...
'Gourmet', 'National', 'Generic'};
popper = {'oil', 'oil', 'oil'; 'oil', 'oil', 'oil'; 'oil', 'oil', 'oil'; ...
'air', 'air', 'air'; 'air', 'air', 'air'; 'air', 'air', 'air'};
T = table (brands(:), popper(:), 'VariableNames', {'brands', 'popper'});
mdl = fitlm (T, popcorn(:), 'interactions')
mdl =
Linear regression model:
y ~ 1 + brands + popper + brands:brands + brands:popper
Coefficients:
7x4 table
Estimate SE tStat pValue
_________ ________ _________ ___________
(Intercept) 4.5 0.215166 20.9141 8.27238e-11
brands_Gourmet 2.33333 0.30429 7.66812 5.78606e-06
brands_National 0.666667 0.30429 2.19089 0.0489297
popper_oil -1 0.30429 -3.28634 0.00650333
brands_Gourmet:brands_National 0 0 NaN NaN
brands_Gourmet:popper_oil -0.166667 0.430331 -0.387298 0.705317
brands_National:popper_oil 0.166667 0.430331 0.387298 0.705317
Number of observations: 18, Error degrees of freedom: 12
Root Mean Squared Error: 0.372678
R-squared: 0.924242, Adjusted R-Squared: 0.892677
F-statistic vs. constant model: 29.28, p-value = 2.50646e-06