Create linear regression model
fitlm
creates a LinearModel
object.
Once you create the object, you can see it in the workspace. You can
see all the properties the
object contains by clicking on it. You can create plots and do further
diagnostic analysis by using methods such as plot
, plotResiduals
, and plotDiagnostics
.
For a full list of methods for LinearModel
, see methods.
returns
a linear model with additional options specified by one or more mdl
= fitlm(___,Name,Value
)Name,Value
pair
arguments.
For example, you can specify which variables are categorical, perform robust regression, or use observation weights.
Load the sample data.
load carsmall
Store the variables in a table.
tbl = table(Weight,Acceleration,MPG,'VariableNames',{'Weight','Acceleration','MPG'});
Display the first five rows of the table.
tbl(1:5,:)
ans = Weight Acceleration MPG ______ ____________ ___ 3504 12 18 3693 11.5 15 3436 11 18 3433 12 16 3449 10.5 17
Fit a linear regression model for miles per gallon (MPG).
lm = fitlm(tbl,'MPG~Weight+Acceleration')
lm = Linear regression model: MPG ~ 1 + Weight + Acceleration Estimated Coefficients: Estimate SE tStat pValue __________ __________ _______ __________ (Intercept) 45.155 3.4659 13.028 1.6266e22 Weight 0.0082475 0.00059836 13.783 5.3165e24 Acceleration 0.19694 0.14743 1.3359 0.18493 Number of observations: 94, Error degrees of freedom: 91 Root Mean Squared Error: 4.12 Rsquared: 0.743, Adjusted RSquared 0.738 Fstatistic vs. constant model: 132, pvalue = 1.38e27
This syntax uses formula to
specify the modelspec
.
The model 'MPG~Weight+Acceleration'
in
this example is equivalent to fitting the model using the string 'linear'
as modelspec
.
For example,
lm2 = fitlm(tbl,'linear');
When you use a string as modelspec
and
do not specify the response variable, fitlm
by
default accepts the last variable in tbl
as the
response variable and the other variables as the predictor variables.
If there are any categorical variables and you use 'linear'
as
the modelspec
, then you must explicitly specify
those variables as categorical variables using the CategoricalVars
namevalue
pair argument.
Load the sample data.
load carsmall
Store the variables in a table.
tbl = table(Weight,Acceleration,Model_Year,MPG,'VariableNames',{'Weight','Acceleration','Model_Year','MPG'});
Fit a linear regression model for miles per gallon (MPG) with weight and acceleration as the predictor variables.
lm = fitlm(tbl,'MPG~Weight+Acceleration')
lm = Linear regression model: MPG ~ 1 + Weight + Acceleration Estimated Coefficients: Estimate SE tStat pValue __________ __________ _______ __________ (Intercept) 45.155 3.4659 13.028 1.6266e22 Weight 0.0082475 0.00059836 13.783 5.3165e24 Acceleration 0.19694 0.14743 1.3359 0.18493 Number of observations: 94, Error degrees of freedom: 91 Root Mean Squared Error: 4.12 Rsquared: 0.743, Adjusted RSquared 0.738 Fstatistic vs. constant model: 132, pvalue = 1.38e27
The pvalue of 0.18493 indicates that Acceleration
does
not have a significant impact on MPG
.
Remove Acceleration
from the model,
and try improving the model by adding the predictor variable Model_Year
.
First define Model_Year
as a nominal variable.
tbl.Model_Year = categorical(tbl.Model_Year)
lm = fitlm(tbl,'MPG~Weight+Model_Year')
lm = Linear regression model: MPG ~ 1 + Weight + Model_Year Estimated Coefficients: Estimate SE tStat pValue __________ __________ _______ __________ (Intercept) 40.11 1.5418 26.016 1.2024e43 Weight 0.0066475 0.00042802 15.531 3.3639e27 Model_Year_76 1.9291 0.74761 2.5804 0.011488 Model_Year_82 7.9093 0.84975 9.3078 7.8681e15 Number of observations: 94, Error degrees of freedom: 90 Root Mean Squared Error: 2.92 Rsquared: 0.873, Adjusted RSquared 0.868 Fstatistic vs. constant model: 206, pvalue = 3.83e40
Specifying modelspec
using formula enables you to update
the model without having to change the design matrix. fitlm
uses
only the variables that are specified in the formula. It also creates
the necessary two Dummy Indicator Variables for the categorical variable Model_Year
.
Fit a quadratic linear regression model to variables in a table. The data includes continuous and categorical predictor variables.
Load the sample data.
load carsmall
Construct a table containing continuous predictor variable Weight
and
response variable MPG
. Add the nominal predictor
variable Year
.
tbl = table(MPG,Weight); tbl.Year = nominal(Model_Year);
Create a fitted model of MPG
as a function
of Year
, Weight
, and Weight
^{2}.
You don't have to include Weight
explicitly
in your formula because it is a lowerorder term of Weight
^{2}.
For details, see Formula.
mdl = fitlm(tbl,'MPG ~ Year + Weight^2')
mdl = Linear regression model: MPG ~ 1 + Weight + Year + Weight^2 Estimated Coefficients: Estimate SE tStat __________ __________ _______ (Intercept) 54.206 4.7117 11.505 Weight 0.016404 0.0031249 5.2493 Year_76 2.0887 0.71491 2.9215 Year_82 8.1864 0.81531 10.041 Weight^2 1.5573e06 4.9454e07 3.149 pValue __________ (Intercept) 2.6648e19 Weight 1.0283e06 Year_76 0.0044137 Year_82 2.6364e16 Weight^2 0.0022303 Number of observations: 94, Error degrees of freedom: 89 Root Mean Squared Error: 2.78 Rsquared: 0.885, Adjusted RSquared 0.88 Fstatistic vs. constant model: 172, pvalue = 5.52e41
When you use formula to specify modelspec
,
you do not have to explicitly specify the categorical variables. fitlm
creates
two dummy (indicator) variables for the nominal variable, Year
.
The dummy variable Year_76
takes the value 1 if
the model year is 1976 and takes the value 0 if it is not. The dummy
variable Year_82
takes the value 1 if model year
is 1982 and takes the value 0 if it is not. 1970 is the reference
year (for details on dummy variables, see Dummy Indicator Variables). The fitted
model is
$$M\widehat{P}G=54.2060.0164\left(Weight\right)+2.0887(Year\_76)+8.1864(Year\_82)+\left(1.557e06\right){\left(Weight\right)}^{2}$$
Simultaneously identify response and predictor variables and specify the model using a formula in linear regression.
Load sample data.
load hospital
Fit a linear model with interaction terms to the data. Indicate which variable is the response variable and identify the continuous and categorical predictors using namevalue pair arguments.
mdl = fitlm(hospital,'Weight~1+Age*Sex*SmokerAge:Sex:Smoker','ResponseVar','Weight','PredictorVars',{'Sex','Age','Smoker'},'CategoricalVar',{'Sex','Smoker'})
mdl = Linear regression model: Weight ~ 1 + Sex*Age + Sex*Smoker + Age*Smoker Estimated Coefficients: Estimate SE tStat pValue (Intercept) 118.7 7.0718 16.785 6.821e30 Sex_Male 68.336 9.7153 7.0339 3.3386e10 Age 0.31068 0.18531 1.6765 0.096991 Smoker_1 3.0425 10.446 0.29127 0.77149 Sex_Male:Age 0.49094 0.24764 1.9825 0.050377 Sex_Male:Smoker_1 0.9509 3.8031 0.25003 0.80312 Age:Smoker_1 0.07288 0.26275 0.27737 0.78211 Number of observations: 100, Error degrees of freedom: 93 Root Mean Squared Error: 8.75 Rsquared: 0.898, Adjusted RSquared 0.892 Fstatistic vs. constant model: 137, pvalue = 6.91e44
The tstatistics in tStat
and
corresponding pvalues in pValue
indicate
that patient weights do not seem to differ significantly according
to age, or the status of smoking, or the interaction of these factors
with gender at the 5% significance level.
Fit a linear regression model of the Hald data using robust fitting.
Load the data.
load hald X = ingredients; % Predictor variables y = heat; % Response
Fit a robust linear model to the data.
mdl = fitlm(X,y,'linear','RobustOpts','on')
mdl = Linear regression model (robust fit): y ~ 1 + x1 + x2 + x3 + x4 Estimated Coefficients: Estimate SE tStat pValue (Intercept) 60.09 75.818 0.79256 0.4509 x1 1.5753 0.80585 1.9548 0.086346 x2 0.5322 0.78315 0.67957 0.51596 x3 0.13346 0.8166 0.16343 0.87424 x4 0.12052 0.7672 0.15709 0.87906 Number of observations: 13, Error degrees of freedom: 8 Root Mean Squared Error: 2.65 Rsquared: 0.979, Adjusted RSquared 0.969 Fstatistic vs. constant model: 94.6, pvalue = 9.03e07
tbl
— Input datatable  dataset arrayInput data, specified as a table or dataset array. When modelspec
is
a formula
, it specifies the variables to be used
as the predictors and response. Otherwise, if you do not specify the
predictor and response variables, the last variable is the response
variable and the others are the predictor variables by default.
Predictor variables can be numeric, or any grouping variable type, such as logical or categorical (see Grouping Variables). The response must be numeric or logical.
To set a different column as the response variable, use the ResponseVar
namevalue
pair argument. To use a subset of the columns as predictors, use the PredictorVars
namevalue
pair argument.
Data Types: single
 double
 logical
X
— Predictor variablesmatrixPredictor variables, specified as an nbyp matrix,
where n is the number of observations and p is
the number of predictor variables. Each column of X
represents
one variable, and each row represents one observation.
By default, there is a constant term in the model, unless you
explicitly remove it, so do not include a column of 1s in X
.
Data Types: single
 double
 logical
y
— Response variablevectorResponse variable, specified as an nby1
vector, where n is the number of observations.
Each entry in y
is the response for the corresponding
row of X
.
Data Types: single
 double
modelspec
— Model specification'linear'
(default)  string naming the model  tby(p + 1) terms matrix  string of the form 'Y ~ terms'
Model specification, specified as one of the following. The
choice is the starting model for stepwiselm
. Default
is 'linear'
.
A string naming the model.
String  Model Type 

'constant'  Model contains only a constant (intercept) term. 
'linear'  Model contains an intercept and linear terms for each predictor. 
'interactions'  Model contains an intercept, linear terms, and all products of pairs of distinct predictors (no squared terms). 
'purequadratic'  Model contains an intercept, linear terms, and squared terms. 
'quadratic'  Model contains an intercept, linear terms, interactions, and squared terms. 
'poly  Model is a polynomial with all terms up to degree i in
the first predictor, degree j in the second
predictor, etc. Use numerals 0 through 9 .
For example, 'poly2111' has a constant plus all
linear and product terms, and also contains terms with predictor 1
squared. 
tby(p + 1) matrix, namely terms matrix, specifying terms to include in the model, where t is the number of terms and p is the number of predictor variables, and plus 1 is for the response variable.
A string representing a formula in the form
'Y
~ terms'
,
where the terms
are
in Wilkinson Notation.
Example: 'quadratic'
Example: 'y ~ X1 + X2^2 + X1:X2'
Specify optional commaseparated pairs of Name,Value
arguments.
Name
is the argument
name and Value
is the corresponding
value. Name
must appear
inside single quotes (' '
).
You can specify several name and value pair
arguments in any order as Name1,Value1,...,NameN,ValueN
.
'Intercept',false,'PredictorVars',[1,3],'ResponseVar',5,'RobustOpts','logistic'
specifies
a robust regression model with no constant term, where the algorithm
uses the logistic weighting function with the default tuning constant,
first and third variables are the predictor variables, and fifth variable
is the response variable.'CategoricalVars'
— Categorical variablescell array of strings  logical or numeric index vectorCategorical variables in the fit, specified as the commaseparated
pair consisting of 'CategoricalVars'
and either
a cell array of strings of the names of the categorical variables
in the table or dataset array tbl
, or a logical
or numeric index vector indicating which columns are categorical.
If data is in a table or dataset array tbl
,
then the default is to treat all categorical or logical variables,
character arrays, or cell arrays of strings as categorical variables.
If data is in matrix X
, then the
default value of this namevalue pair argument is an empty matrix []
.
That is, no variable is categorical unless you specify it.
For example, you can specify the observations 2 and 3 out of 6 as categorical using either of the following examples.
Example: 'CategoricalVars',[2,3]
Example: 'CategoricalVars',logical([0 1 1 0 0 0])
Data Types: single
 double
 logical
'Exclude'
— Observations to excludelogical or numeric index vectorObservations to exclude from the fit, specified as the commaseparated
pair consisting of 'Exclude'
and a logical or numeric
index vector indicating which observations to exclude from the fit.
For example, you can exclude observations 2 and 3 out of 6 using either of the following examples.
Example: 'Exclude',[2,3]
Example: 'Exclude',logical([0 1 1 0 0 0])
Data Types: single
 double
 logical
'Intercept'
— Indicator for constant termtrue
(default)  false
Indicator the for constant term (intercept) in the fit, specified
as the commaseparated pair consisting of 'Intercept'
and
either true
to include or false
to
remove the constant term from the model.
Use 'Intercept'
only when specifying the
model using a string, not a formula or matrix.
Example: 'Intercept',false
'PredictorVars'
— Predictor variablescell array of strings  logical or numeric index vectorPredictor variables to use in the fit, specified as the commaseparated
pair consisting of 'PredictorVars'
and either a
cell array of strings of the variable names in the table or dataset
array tbl
, or a logical or numeric index vector
indicating which columns are predictor variables.
The strings should be among the names in tbl
,
or the names you specify using the 'VarNames'
namevalue
pair argument.
The default is all variables in X
, or all
variables in tbl
except for ResponseVar
.
For example, you can specify the second and third variables as the predictor variables using either of the following examples.
Example: 'PredictorVars',[2,3]
Example: 'PredictorVars',logical([0 1 1 0 0 0])
Data Types: single
 double
 logical
 cell
'ResponseVar'
— Response variablelast column in tbl
(default)  string for variable name  logical or numeric index vectorResponse variable to use in the fit, specified as the commaseparated
pair consisting of 'ResponseVar'
and either a string
of the variable name in the table or dataset array tbl
,
or a logical or numeric index vector indicating which column is the
response variable. You typically need to use 'ResponseVar'
when
fitting a table or dataset array tbl
.
For example, you can specify the fourth variable, say yield
,
as the response out of six variables, in one of the following ways.
Example: 'ResponseVar','yield'
Example: 'ResponseVar',[4]
Example: 'ResponseVar',logical([0 0 0 1 0 0])
Data Types: single
 double
 logical
 char
'RobustOpts'
— Indicator of robust fitting type'off'
(default)  'on'
 string  structure with string or function handleIndicator of the robust fitting type to use, specified as the
commaseparated pair consisting of 'RobustOpts'
and
one of the following.
'off'
— No robust fitting. fitlm
uses
ordinary least squares.
'on'
— Robust fitting. When
you use robust fitting, 'bisquare'
weight function
is the default.
String — Name of the robust fitting weight
function from the following table. fitlm
uses the
corresponding default tuning constant in the table.
Structure with the string RobustWgtFun
containing
the name of the robust fitting weight function from the following
table and optional scalar Tune
fields — fitlm
uses
the RobustWgtFun
weight function and Tune
tuning
constant from the structure. You can choose the name of the robust
fitting weight function from this table. If you do not supply a Tune
field,
the fitting function uses the corresponding default tuning constant.
Weight Function  Equation  Default Tuning Constant 

'andrews'  w = (abs(r)<pi) .* sin(r) ./ r  1.339 
'bisquare' (default)  w = (abs(r)<1) .* (1  r.^2).^2  4.685 
'cauchy'  w = 1 ./ (1 + r.^2)  2.385 
'fair'  w = 1 ./ (1 + abs(r))  1.400 
'huber'  w = 1 ./ max(1, abs(r))  1.345 
'logistic'  w = tanh(r) ./ r  1.205 
'ols'  Ordinary least squares (no weighting function)  None 
'talwar'  w = 1 * (abs(r)<1)  2.795 
'welsch'  w = exp((r.^2))  2.985 
The value r in the weight functions is
r = resid/(tune*s*sqrt(1h))
,
where resid
is the vector of residuals from
the previous iteration, h
is the vector of leverage
values from a leastsquares fit, and s
is an estimate
of the standard deviation of the error term given by
s = MAD/0.6745
.
MAD is the median absolute deviation of the residuals from their
median. The constant 0.6745 makes the estimate unbiased for the normal
distribution. If there are p columns in X
,
the smallest p absolute deviations are excluded
when computing the median.
Default tuning constants give coefficient estimates that are approximately 95% as statistically efficient as the ordinary leastsquares estimates, provided the response has a normal distribution with no outliers. Decreasing the tuning constant increases the downweight assigned to large residuals; increasing the tuning constant decreases the downweight assigned to large residuals.
Structure with the function handle RobustWgtFun
and
optional scalar Tune
fields — You can specify
a custom weight function. fitlm
uses the RobustWgtFun
weight
function and Tune
tuning constant from the structure.
Specify RobustWgtFun
as a function handle that
accepts a vector of residuals, and returns a vector of weights the
same size. The fitting function scales the residuals, dividing by
the tuning constant (default 1
) and by an estimate
of the error standard deviation before it calls the weight function.
Example: 'RobustOpts','andrews'
'VarNames'
— Names of variables in fit{'x1','x2',...,'xn','y'}
(default)  cell array of stringsNames of variables in fit, specified as the commaseparated
pair consisting of 'VarNames'
and a cell array
of strings including the names for the columns of X
first,
and the name for the response variable y
last.
'VarNames'
is not applicable to variables
in a table or dataset array, because those variables already have
names.
For example, if in your data, horsepower, acceleration, and model year of the cars are the predictor variables, and miles per gallon (MPG) is the response variable, then you can name the variables as follows.
Example: 'VarNames',{'Horsepower','Acceleration','Model_Year','MPG'}
Data Types: cell
'Weights'
— Observation weightsones(n,1)
(default)  nby1 vector of nonnegative scalar valuesObservation weights, specified as the commaseparated pair consisting
of 'Weights'
and an nby1 vector
of nonnegative scalar values, where n is the number
of observations.
Data Types: single
 double
mdl
— Linear modelLinearModel
objectLinear model representing a leastsquares fit of the response
to the data, returned as a LinearModel
object.
If the value of the 'RobustOpts'
namevalue
pair is not []
or 'ols'
, the
model is not a leastsquares fit, but uses the robust fitting function.
For properties and methods of the linear model object, mdl
,
see the LinearModel
class
page.
A terms matrix is a tby(p + 1) matrix specifying terms in a model, where t is the number of terms, p is the number of predictor variables, and plus one is for the response variable.
The value of T(i,j)
is the exponent of variable j
in
term i
. Suppose there are three predictor variables A
, B
,
and C
:
[0 0 0 0] % Constant term or intercept [0 1 0 0] % B; equivalently, A^0 * B^1 * C^0 [1 0 1 0] % A*C [2 0 0 0] % A^2 [0 1 2 0] % B*(C^2)
0
at the
end of each term represents the response variable. In general,If you have the variables in a table or dataset array,
then 0
must represent the response variable depending
on the position of the response variable. The following example illustrates
this.
Load the sample data and define the dataset array.
load hospital ds = dataset(hospital.Sex,hospital.BloodPressure(:,1),hospital.Age,... hospital.Smoker,'VarNames',{'Sex','BloodPressure','Age','Smoker'});
Represent the linear model 'BloodPressure ~ 1 + Sex
+ Age + Smoker'
in a terms matrix. The response variable
is in the second column of the dataset array, so there must be a column
of 0s for the response variable in the second column of the terms
matrix.
T = [0 0 0 0;1 0 0 0;0 0 1 0;0 0 0 1]
T = 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1
Redefine the dataset array.
ds = dataset(hospital.BloodPressure(:,1),hospital.Sex,hospital.Age,... hospital.Smoker,'VarNames',{'BloodPressure','Sex','Age','Smoker'});
Now, the response variable is the first term in the dataset
array. Specify the same linear model, 'BloodPressure ~ 1
+ Sex + Age + Smoker'
, using a terms matrix.
T = [0 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1]
T = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
If you have the predictor and response variables in
a matrix and column vector, then you must include 0
for
the response variable at the end of each term. The following example
illustrates this.
Load the sample data and define the matrix of predictors.
load carsmall
X = [Acceleration,Weight];
Specify the model 'MPG ~ Acceleration + Weight + Acceleration:Weight
+ Weight^2'
using a term matrix and fit the model to the
data. This model includes the main effect and twoway interaction
terms for the variables, Acceleration
and Weight
,
and a secondorder term for the variable, Weight
.
T = [0 0 0;1 0 0;0 1 0;1 1 0;0 2 0]
T = 0 0 0 1 0 0 0 1 0 1 1 0 0 2 0
Fit a linear model.
mdl = fitlm(X,MPG,T)
mdl = Linear regression model: y ~ 1 + x1*x2 + x2^2 Estimated Coefficients: Estimate SE tStat pValue (Intercept) 48.906 12.589 3.8847 0.00019665 x1 0.54418 0.57125 0.95261 0.34337 x2 0.012781 0.0060312 2.1192 0.036857 x1:x2 0.00010892 0.00017925 0.6076 0.545 x2^2 9.7518e07 7.5389e07 1.2935 0.19917 Number of observations: 94, Error degrees of freedom: 89 Root Mean Squared Error: 4.1 Rsquared: 0.751, Adjusted RSquared 0.739 Fstatistic vs. constant model: 67, pvalue = 4.99e26
Only the intercept and x2
term, which correspond
to the Weight
variable, are significant at the
5% significance level.
Now, perform a stepwise regression with a constant model as the starting model and a linear model with interactions as the upper model.
T = [0 0 0;1 0 0;0 1 0;1 1 0];
mdl = stepwiselm(X,MPG,[0 0 0],'upper',T)
1. Adding x2, FStat = 259.3087, pValue = 1.643351e28 mdl = Linear regression model: y ~ 1 + x2 Estimated Coefficients: Estimate SE tStat pValue (Intercept) 49.238 1.6411 30.002 2.7015e49 x2 0.0086119 0.0005348 16.103 1.6434e28 Number of observations: 94, Error degrees of freedom: 92 Root Mean Squared Error: 4.13 Rsquared: 0.738, Adjusted RSquared 0.735 Fstatistic vs. constant model: 259, pvalue = 1.64e28
The results of the stepwise regression are consistent with the
results of fitlm
in the previous step.
A formula for model specification is a string
of the form '
Y
~ terms
'
where
Y
is the response name.
terms
contains
Variable names
+
means include the next variable

means do not include the next
variable
:
defines an interaction, a product
of terms
*
defines an interaction and all lowerorder terms
^
raises the predictor to a power,
exactly as in *
repeated, so ^
includes
lower order terms as well
()
groups terms
Note:
Formulas include a constant (intercept) term by default. To
exclude a constant term from the model, include 
For example,
'Y ~ A + B + C'
means a threevariable
linear model with intercept.'Y ~ A + B +
C  1'
is a threevariable linear model without intercept.'Y ~ A + B + C + B^2'
is a threevariable
model with intercept and a B^2
term.'Y
~ A + B^2 + C'
is the same as the previous example because B^2
includes
a B
term.'Y ~ A + B +
C + A:B'
includes an A*B
term.'Y
~ A*B + C'
is the same as the previous example because A*B
= A + B + A:B
.'Y ~ A*B*C  A:B:C'
has
all interactions among A
, B
,
and C
, except the threeway interaction.'Y
~ A*(B + C + D)'
has all linear terms, plus products of A
with
each of the other variables.
Wilkinson notation describes the factors present in models. The notation relates to factors present in models, not to the multipliers (coefficients) of those factors.
Wilkinson Notation  Factors in Standard Notation 

1  Constant (intercept) term 
A^k , where k is a positive
integer  A , A^{2} ,
..., A^{k} 
A + B  A , B 
A*B  A , B , A*B 
A:B  A*B only 
B  Do not include B 
A*B + C  A , B , C , A*B 
A + B + C + A:B  A , B , C , A*B 
A*B*C  A:B:C  A , B , C , A*B , A*C , B*C 
A*(B + C)  A , B , C , A*B , A*C 
Statistics and Machine Learning Toolbox™ notation always includes a constant term
unless you explicitly remove the term using 1
.