Fit linear regression model

`mdl = fitlm(tbl)`

`mdl = fitlm(X,y)`

`mdl = fitlm(___,modelspec)`

`mdl = fitlm(___,Name,Value)`

specifies additional options using one or more name-value pair arguments. For
example, you can specify which variables are categorical, perform robust
regression, or use observation weights.`mdl`

= fitlm(___,`Name,Value`

)

Fit a linear regression model using a matrix input data set.

Load the `carsmall`

data set, a matrix input data set.

```
load carsmall
X = [Weight,Horsepower,Acceleration];
```

Fit a linear regression model by using `fitlm`

.

mdl = fitlm(X,MPG)

mdl = Linear regression model: y ~ 1 + x1 + x2 + x3 Estimated Coefficients: Estimate SE tStat pValue __________ _________ _________ __________ (Intercept) 47.977 3.8785 12.37 4.8957e-21 x1 -0.0065416 0.0011274 -5.8023 9.8742e-08 x2 -0.042943 0.024313 -1.7663 0.08078 x3 -0.011583 0.19333 -0.059913 0.95236 Number of observations: 93, Error degrees of freedom: 89 Root Mean Squared Error: 4.09 R-squared: 0.752, Adjusted R-Squared: 0.744 F-statistic vs. constant model: 90, p-value = 7.38e-27

The model display includes the model formula, estimated coefficients, and model summary statistics.

The model formula in the display, `y ~ 1 + x1 + x2 + x3`

, corresponds to $\mathit{y}={\beta}_{0}+{\beta}_{1}{\mathit{X}}_{1}+{\beta}_{2}{\mathit{X}}_{2}+{\beta}_{3}{\mathit{X}}_{3}+\u03f5$.

The model display also shows the estimated coefficient information, which is stored in the `Coefficients`

property. Display the `Coefficients`

property.

mdl.Coefficients

`ans=`*4×4 table*
Estimate SE tStat pValue
__________ _________ _________ __________
(Intercept) 47.977 3.8785 12.37 4.8957e-21
x1 -0.0065416 0.0011274 -5.8023 9.8742e-08
x2 -0.042943 0.024313 -1.7663 0.08078
x3 -0.011583 0.19333 -0.059913 0.95236

The `Coefficient`

property includes these columns:

`Estimate`

— Coefficient estimates for each corresponding term in the model. For example, the estimate for the constant term (`intercept`

) is 47.977.`SE`

— Standard error of the coefficients.`tStat`

—*t*-statistic for each coefficient to test the null hypothesis that the corresponding coefficient is zero against the alternative that it is different from zero, given the other predictors in the model. Note that`tStat = Estimate/SE`

. For example, the*t*-statistic for the intercept is 47.977/3.8785 = 12.37.`pValue`

—*p*-value for the*t*-statistic of the hypothesis test that the corresponding coefficient is equal to zero or not. For example, the*p*-value of the*t*-statistic for`x2`

is greater than 0.05, so this term is not significant at the 5% significance level given the other terms in the model.

The summary statistics of the model are:

`Number of observations`

— Number of rows without any`NaN`

values. For example,`Number of observations`

is 93 because the`MPG`

data vector has six`NaN`

values and the`Horsepower`

data vector has one`NaN`

value for a different observation, where the number of rows in`X`

and`MPG`

is 100.`Error degrees of freedom`

—*n*–*p*, where*n*is the number of observations, and*p*is the number of coefficients in the model, including the intercept. For example, the model has four predictors, so the`Error degrees of freedom`

is 93 – 4 = 89.`Root mean squared error`

— Square root of the mean squared error, which estimates the standard deviation of the error distribution.`R-squared`

and`Adjusted R-squared`

— Coefficient of determination and adjusted coefficient of determination, respectively. For example, the`R-squared`

value suggests that the model explains approximately 75% of the variability in the response variable`MPG`

.`F-statistic vs. constant model`

— Test statistic for the*F*-test on the regression model, which tests whether the model fits significantly better than a degenerate model consisting of only a constant term.`p-value`

—*p*-value for the*F*-test on the model. For example, the model is significant with a*p*-value of 7.3816e-27.

You can find these statistics in the model properties (`NumObservations`

, `DFE`

, `RMSE`

, and `Rsquared`

) and by using the `anova`

function.

`anova(mdl,'summary')`

`ans=`*3×5 table*
SumSq DF MeanSq F pValue
______ __ ______ ______ __________
Total 6004.8 92 65.269
Model 4516 3 1505.3 89.987 7.3816e-27
Residual 1488.8 89 16.728

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=`*5×3 table*
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). Specify the model formula by using Wilkinson notation.

`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.6266e-22 Weight -0.0082475 0.00059836 -13.783 5.3165e-24 Acceleration 0.19694 0.14743 1.3359 0.18493 Number of observations: 94, Error degrees of freedom: 91 Root Mean Squared Error: 4.12 R-squared: 0.743, Adjusted R-Squared: 0.738 F-statistic vs. constant model: 132, p-value = 1.38e-27

The model `'MPG~Weight+Acceleration'`

in this example is equivalent to set the model specification as `'linear'`

. For example,

`lm2 = fitlm(tbl,'linear');`

If you use a character vector for model specification and you do not specify the response variable, then `fitlm`

accepts the last variable in `tbl`

as the response variable and the other variables as the predictor variables.

Fit a linear regression model using a model formula specified by Wilkinson notation.

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.6266e-22 Weight -0.0082475 0.00059836 -13.783 5.3165e-24 Acceleration 0.19694 0.14743 1.3359 0.18493 Number of observations: 94, Error degrees of freedom: 91 Root Mean Squared Error: 4.12 R-squared: 0.743, Adjusted R-Squared: 0.738 F-statistic vs. constant model: 132, p-value = 1.38e-27

The *p*-value 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 categorical 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.2024e-43 Weight -0.0066475 0.00042802 -15.531 3.3639e-27 Model_Year_76 1.9291 0.74761 2.5804 0.011488 Model_Year_82 7.9093 0.84975 9.3078 7.8681e-15 Number of observations: 94, Error degrees of freedom: 90 Root Mean Squared Error: 2.92 R-squared: 0.873, Adjusted R-Squared: 0.868 F-statistic vs. constant model: 206, p-value = 3.83e-40

Specifying `modelspec`

using Wilkinson notation 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 linear regression model using a terms matrix.

**Terms Matrix for Table Input**

If the model variables are in a table, then a column of `0`

s in a terms matrix represents the position of the response variable.

Load the `hospital`

data set.

`load hospital`

Store the variables in a table.

t = table(hospital.Sex,hospital.BloodPressure(:,1),hospital.Age,hospital.Smoker, ... 'VariableNames',{'Sex','BloodPressure','Age','Smoker'});

Represent the linear model `'BloodPressure ~ 1 + Sex + Age + Smoker'`

using a terms matrix. The response variable is in the second column of the table, so the second column of the terms matrix must be a column of `0`

s for the response variable.

T = [0 0 0 0;1 0 0 0;0 0 1 0;0 0 0 1]

`T = `*4×4*
0 0 0 0
1 0 0 0
0 0 1 0
0 0 0 1

Fit a linear model.

mdl1 = fitlm(t,T)

mdl1 = Linear regression model: BloodPressure ~ 1 + Sex + Age + Smoker Estimated Coefficients: Estimate SE tStat pValue ________ ________ ________ __________ (Intercept) 116.14 2.6107 44.485 7.1287e-66 Sex_Male 0.050106 0.98364 0.050939 0.95948 Age 0.085276 0.066945 1.2738 0.2058 Smoker_1 9.87 1.0346 9.5395 1.4516e-15 Number of observations: 100, Error degrees of freedom: 96 Root Mean Squared Error: 4.78 R-squared: 0.507, Adjusted R-Squared: 0.492 F-statistic vs. constant model: 33, p-value = 9.91e-15

**Terms Matrix for Matrix Input**

If the predictor and response variables are in a matrix and column vector, then you must include `0`

for the response variable at the end of each row in a terms matrix.

Load the `carsmall`

data set and define the matrix of predictors.

```
load carsmall
X = [Acceleration,Weight];
```

Specify the model `'MPG ~ Acceleration + Weight + Acceleration:Weight + Weight^2'`

using a terms matrix. This model includes the main effect and two-way interaction terms for the variables `Acceleration`

and `Weight`

, and a second-order term for the variable `Weight`

.

T = [0 0 0;1 0 0;0 1 0;1 1 0;0 2 0]

`T = `*5×3*
0 0 0
1 0 0
0 1 0
1 1 0
0 2 0

Fit a linear model.

mdl2 = fitlm(X,MPG,T)

mdl2 = 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.7518e-07 7.5389e-07 1.2935 0.19917 Number of observations: 94, Error degrees of freedom: 89 Root Mean Squared Error: 4.1 R-squared: 0.751, Adjusted R-Squared: 0.739 F-statistic vs. constant model: 67, p-value = 4.99e-26

Only the intercept and `x2`

term, which corresponds to the `Weight`

variable, are significant at the 5% significance level.

Fit a linear regression model that contains a categorical predictor. Reorder the categories of the categorical predictor to control the reference level in the model. Then, use `anova`

to test the significance of the categorical variable.

**Model with Categorical Predictor**

Load the `carsmall`

data set and create a linear regression model of `MPG`

as a function of `Model_Year`

. To treat the numeric vector `Model_Year`

as a categorical variable, identify the predictor using the `'CategoricalVars'`

name-value pair argument.

load carsmall mdl = fitlm(Model_Year,MPG,'CategoricalVars',1,'VarNames',{'Model_Year','MPG'})

mdl = Linear regression model: MPG ~ 1 + Model_Year Estimated Coefficients: Estimate SE tStat pValue ________ ______ ______ __________ (Intercept) 17.69 1.0328 17.127 3.2371e-30 Model_Year_76 3.8839 1.4059 2.7625 0.0069402 Model_Year_82 14.02 1.4369 9.7571 8.2164e-16 Number of observations: 94, Error degrees of freedom: 91 Root Mean Squared Error: 5.56 R-squared: 0.531, Adjusted R-Squared: 0.521 F-statistic vs. constant model: 51.6, p-value = 1.07e-15

The model formula in the display, `MPG ~ 1 + Model_Year`

, corresponds to

$\mathrm{MPG}={\beta}_{0}+{\beta}_{1}{{\rm I}}_{\mathrm{Year}=76}+{\beta}_{2}{{\rm I}}_{\mathrm{Year}=82}+\u03f5$,

where ${{\rm I}}_{\mathrm{Year}=76}$ and ${{\rm I}}_{\mathrm{Year}=82}$ are indicator variables whose value is one if the value of `Model_Year`

is 76 and 82, respectively. The `Model_Year`

variable includes three distinct values, which you can check by using the `unique`

function.

unique(Model_Year)

`ans = `*3×1*
70
76
82

`fitlm`

chooses the smallest value in `Model_Year`

as a reference level (`'70'`

) and creates two indicator variables ${{\rm I}}_{\mathrm{Year}=76}$ and ${{\rm I}}_{\mathrm{Year}=82}$. The model includes only two indicator variables because the design matrix becomes rank deficient if the model includes three indicator variables (one for each level) and an intercept term.

**Model with Full Indicator Variables**

You can interpret the model formula of `mdl`

as a model that has three indicator variables without an intercept term:

$\mathit{y}={\beta}_{0}{{\rm I}}_{{\mathit{x}}_{1}=70}+\left({\beta}_{0}+{\beta}_{1}\right){{\rm I}}_{{\mathit{x}}_{1}=76}+\left({{\beta}_{0}+\beta}_{2}\right){{\rm I}}_{{\mathit{x}}_{2}=82}+\u03f5$.

Alternatively, you can create a model that has three indicator variables without an intercept term by manually creating indicator variables and specifying the model formula.

```
temp_Year = dummyvar(categorical(Model_Year));
Model_Year_70 = temp_Year(:,1);
Model_Year_76 = temp_Year(:,2);
Model_Year_82 = temp_Year(:,3);
tbl = table(Model_Year_70,Model_Year_76,Model_Year_82,MPG);
mdl = fitlm(tbl,'MPG ~ Model_Year_70 + Model_Year_76 + Model_Year_82 - 1')
```

mdl = Linear regression model: MPG ~ Model_Year_70 + Model_Year_76 + Model_Year_82 Estimated Coefficients: Estimate SE tStat pValue ________ _______ ______ __________ Model_Year_70 17.69 1.0328 17.127 3.2371e-30 Model_Year_76 21.574 0.95387 22.617 4.0156e-39 Model_Year_82 31.71 0.99896 31.743 5.2234e-51 Number of observations: 94, Error degrees of freedom: 91 Root Mean Squared Error: 5.56

**Choose Reference Level in Model**

You can choose a reference level by modifying the order of categories in a categorical variable. First, create a categorical variable `Year`

.

Year = categorical(Model_Year);

Check the order of categories by using the `categories`

function.

categories(Year)

`ans = `*3x1 cell array*
{'70'}
{'76'}
{'82'}

If you use `Year`

as a predictor variable, then `fitlm`

chooses the first category `'70'`

as a reference level. Reorder `Year`

by using the `reordercats`

function.

Year_reordered = reordercats(Year,{'76','70','82'}); categories(Year_reordered)

`ans = `*3x1 cell array*
{'76'}
{'70'}
{'82'}

The first category of `Year_reordered`

is `'76'`

. Create a linear regression model of `MPG`

as a function of `Year_reordered`

.

mdl2 = fitlm(Year_reordered,MPG,'VarNames',{'Model_Year','MPG'})

mdl2 = Linear regression model: MPG ~ 1 + Model_Year Estimated Coefficients: Estimate SE tStat pValue ________ _______ _______ __________ (Intercept) 21.574 0.95387 22.617 4.0156e-39 Model_Year_70 -3.8839 1.4059 -2.7625 0.0069402 Model_Year_82 10.136 1.3812 7.3385 8.7634e-11 Number of observations: 94, Error degrees of freedom: 91 Root Mean Squared Error: 5.56 R-squared: 0.531, Adjusted R-Squared: 0.521 F-statistic vs. constant model: 51.6, p-value = 1.07e-15

`mdl2`

uses `'76'`

as a reference level and includes two indicator variables ${{\rm I}}_{\mathrm{Year}=70}$ and ${{\rm I}}_{\mathrm{Year}=82}$.

**Evaluate Categorical Predictor**

The model display of `mdl2`

includes a *p*-value of each term to test whether or not the corresponding coefficient is equal to zero. Each *p*-value examines each indicator variable. To examine the categorical variable `Model_Year`

as a group of indicator variables, use `anova`

. Specify `'components'`

to return a component ANOVA table that includes ANOVA statistics for each variable in the model except the constant term.

`anova(mdl2,'components')`

`ans=`*2×5 table*
SumSq DF MeanSq F pValue
______ __ ______ _____ __________
Model_Year 3190.1 2 1595.1 51.56 1.0694e-15
Error 2815.2 91 30.936

The component ANOVA table includes the *p*-value of the `Model_Year`

variable, which is smaller than the *p*-values of the indicator variables.

Fit a linear regression model to sample data. Specify the response and predictor variables, and include only pairwise interaction terms in the model.

Load sample data.

`load hospital`

Fit a linear model with interaction terms to the data. Specify weight as the response variable, and sex, age, and smoking status as the predictor variables. Also, specify that sex and smoking status are categorical variables.

mdl = fitlm(hospital,'interactions','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.821e-30 Sex_Male 68.336 9.7153 7.0339 3.3386e-10 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 R-squared: 0.898, Adjusted R-Squared: 0.892 F-statistic vs. constant model: 137, p-value = 6.91e-44

The weight of the patients do not seem to differ significantly according to age, or the status of smoking, or interaction of these factors with patient sex at the 5% significance level.

Load the `hald`

data set, which measures the effect of cement composition on its hardening heat.

`load hald`

This data set includes the variables `ingredients`

and `heat`

. The matrix `ingredients`

contains the percent composition of four chemicals present in the cement. The vector `heat`

contains the values for the heat hardening after 180 days for each cement sample.

Fit a robust linear regression model to the data.

mdl = fitlm(ingredients,heat,'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 R-squared: 0.979, Adjusted R-Squared: 0.969 F-statistic vs. constant model: 94.6, p-value = 9.03e-07

For more details, see the topic Robust Regression — Reduce Outlier Effects, which compares the results of a robust fit to a standard least-squares fit.

`tbl`

— Input datatable | dataset array

Input data, specified as a table or dataset array. When `modelspec`

is a
`formula`

, the formula specifies the predictor and response
variables. Otherwise, if you do not specify the predictor and response variables, the
last variable in `tbl`

is the response variable and the others are the
predictor variables by default.

The predictor variables can be numeric, logical, categorical, character, or string. The response variable must be numeric or logical.

To set a different column as the response variable, use the `ResponseVar`

name-value
pair argument. To use a subset of the columns as predictors, use the `PredictorVars`

name-value
pair argument.

`X`

— Predictor variablesmatrix

Predictor variables, specified as an *n*-by-*p* 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`

`y`

— Response variablevector

Response variable, specified as an *n*-by-1
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`

| `logical`

`modelspec`

— Model specification`'linear'`

(default) | `'constant'`

| `'interactions'`

| ```
'Y ~
terms'
```

| ...Model specification, specified as one of these values.

A character vector or string scalar naming the model.

Value Model Type `'constant'`

Model contains only a constant (intercept) term. `'linear'`

Model contains an intercept and linear term for each predictor. `'interactions'`

Model contains an intercept, linear term for each predictor, and all products of pairs of distinct predictors (no squared terms). `'purequadratic'`

Model contains an intercept term and linear and squared terms for each predictor. `'quadratic'`

Model contains an intercept term, linear and squared terms for each predictor, and all products of pairs of distinct predictors. `'poly`

'`ijk`

Model is a polynomial with all terms up to degree in the first predictor, degree`i`

in the second predictor, and so on. Specify the maximum degree for each predictor by using numerals 0 though 9. The model contains interaction terms, but the degree of each interaction term does not exceed the maximum value of the specified degrees. For example,`j`

`'poly13'`

has an intercept and*x*_{1},*x*_{2},*x*_{2}^{2},*x*_{2}^{3},*x*_{1}**x*_{2}, and*x*_{1}**x*_{2}^{2}terms, where*x*_{1}and*x*_{2}are the first and second predictors, respectively.A

*t*-by-(*p*+ 1) matrix, or a Terms Matrix, specifying terms in the model, where*t*is the number of terms and*p*is the number of predictor variables, and +1 accounts for the response variable. A terms matrix is convenient when the number of predictors is large and you want to generate the terms programmatically.A character vector or string scalar representing a Formula in the form

`'Y ~ terms'`

,where the

`terms`

are in Wilkinson Notation.

**Example: **`'quadratic'`

**Example: **`'y ~ X1 + X2^2 + X1:X2'`

**Data Types: **`single`

| `double`

| `char`

| `string`

Specify optional
comma-separated pairs of `Name,Value`

arguments. `Name`

is
the argument name and `Value`

is the corresponding value.
`Name`

must appear inside 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 variable liststring array | cell array of character vectors | logical or numeric index vector

Categorical variable list, specified as the comma-separated pair consisting of
`'CategoricalVars'`

and either a string array or cell array of
character vectors containing categorical variable names 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, by default,`fitlm`

treats all categorical values, logical values, character arrays, string arrays, and cell arrays of character vectors as categorical variables.If data is in matrix

`X`

, then the default value of`'CategoricalVars'`

is an empty matrix`[]`

. That is, no variable is categorical unless you specify it as categorical.

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`

| `string`

| `cell`

`'Exclude'`

— Observations to excludelogical or numeric index vector

Observations to exclude from the fit, specified as the comma-separated
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 term`true`

(default) | `false`

Indicator for the constant term (intercept) in the fit, specified as the comma-separated 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 character vector or
string scalar, not a formula or matrix.

**Example: **`'Intercept',false`

`'PredictorVars'`

— Predictor variablesstring array | cell array of character vectors | logical or numeric index vector

Predictor variables to use in the fit, specified as the comma-separated pair consisting of
`'PredictorVars'`

and either a string array or cell array of
character vectors 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 string values or character vectors should be among the names in `tbl`

, or
the names you specify using the `'VarNames'`

name-value 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`

| `string`

| `cell`

`'ResponseVar'`

— Response variablelast column in

`tbl`

(default) | character vector or string scalar containing variable name | logical or numeric index vectorResponse variable to use in the fit, specified as the comma-separated pair consisting of
`'ResponseVar'`

and either a character vector or string scalar
containing 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`

| `string`

`'RobustOpts'`

— Indicator of robust fitting type`'off'`

(default) | `'on'`

| character vector | string scalar | structureIndicator of the robust fitting type to use, specified as the comma-separated pair consisting
of `'RobustOpts'`

and one of these values.

`'off'`

— No robust fitting.`fitlm`

uses ordinary least squares.`'on'`

— Robust fitting using the`'bisquare'`

weight function with the default tuning constant.Character vector or string scalar — Name of a robust fitting weight function from the following table.

`fitlm`

uses the corresponding default tuning constant specified in the table.Structure with the two fields

`RobustWgtFun`

and`Tune`

.The

`RobustWgtFun`

field contains the name of a robust fitting weight function from the following table or a function handle of a custom weight function.The

`Tune`

field contains a tuning constant. If you do not set the`Tune`

field,`fitlm`

uses the corresponding default tuning constant.

Weight Function Description Default Tuning Constant `'andrews'`

`w = (abs(r)<pi) .* sin(r) ./ r`

1.339 `'bisquare'`

`w = (abs(r)<1) .* (1 - r.^2).^2`

(also called biweight)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 function handle Custom weight function that accepts a vector `r`

of scaled residuals, and returns a vector of weights the same size as`r`

1 The default tuning constants of built-in weight functions give coefficient estimates that are approximately 95% as statistically efficient as the ordinary least-squares 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.

The value

*r*in the weight functions is`r = resid/(tune*s*sqrt(1–h))`

,where

`resid`

is the vector of residuals from the previous iteration,`tune`

is the tuning constant,`h`

is the vector of leverage values from a least-squares 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`X`

has*p*columns, the software excludes the smallest*p*absolute deviations when computing the median.

For robust fitting, `fitlm`

uses
M-estimation to formulate estimating equations and solves them using the method of iterative
reweighted least squares (IRLS).

**Example: **`'RobustOpts','andrews'`

`'VarNames'`

— Names of variables`{'x1','x2',...,'xn','y'}`

(default) | string array | cell array of character vectorsNames of variables, specified as the comma-separated pair consisting of
`'VarNames'`

and a string array or cell array of character vectors
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: **`string`

| `cell`

`'Weights'`

— Observation weights`ones(n,1)`

(default) | Observation weights, specified as the comma-separated pair consisting
of `'Weights'`

and an *n*-by-1 vector
of nonnegative scalar values, where *n* is the number
of observations.

**Data Types: **`single`

| `double`

`mdl`

— Linear model`LinearModel`

objectLinear model representing a least-squares fit of the response to the data, returned as a
`LinearModel`

object.

If the value of the `'RobustOpts'`

name-value
pair is not `[]`

or `'ols'`

, the
model is not a least-squares fit, but uses the robust fitting function.

A terms matrix `T`

is a
*t*-by-(*p* + 1) matrix specifying terms in a model,
where *t* is the number of terms, *p* is the number of
predictor variables, and +1 accounts for the response variable. The value of
`T(i,j)`

is the exponent of variable `j`

in term
`i`

.

For example, suppose that an input includes three predictor variables `A`

,
`B`

, and `C`

and the response variable
`Y`

in the order `A`

, `B`

,
`C`

, and `Y`

. Each row of `T`

represents one term:

`[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)`

The `0`

at the end of each term represents the response variable. In
general, a column vector of zeros in a terms matrix represents the position of the response
variable. If you have the predictor and response variables in a matrix and column vector,
then you must include `0`

for the response variable in the last column of
each row.

A formula for model specification is a character vector or string scalar of
the form `'`

.* Y* ~

`terms`

is the response name.`Y`

represents the predictor terms in a model using Wilkinson notation.`terms`

For example:

`'Y ~ A + B + C'`

specifies a three-variable linear model with intercept.`'Y ~ A + B + C – 1'`

specifies a three-variable linear model without intercept. Note that formulas include a constant (intercept) term by default. To exclude a constant term from the model, you must include`–1`

in the formula.

Wilkinson notation describes the terms present in a model. The notation relates to the terms present in a model, not to the multipliers (coefficients) of those terms.

Wilkinson notation uses these symbols:

`+`

means include the next variable.`–`

means do not include the next variable.`:`

defines an interaction, which is a product of terms.`*`

defines an interaction and all lower-order terms.`^`

raises the predictor to a power, exactly as in`*`

repeated, so`^`

includes lower-order terms as well.`()`

groups terms.

This table shows typical examples of Wilkinson notation.

Wilkinson Notation | Term in Standard Notation |
---|---|

`1` | Constant (intercept) term |

`A^k` , where `k` is a positive
integer | `A` ,
`A` , ...,
`A` |

`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`

.

For more details, see Wilkinson Notation.

To access the model properties of the

`LinearModel`

object`mdl`

, you can use dot notation. For example,`mdl.Residuals`

returns a table of the raw, Pearson, Studentized, and standardized residual values for the model.After training a model, you can generate C/C++ code that predicts responses for new data. Generating C/C++ code requires MATLAB

^{®}Coder™. For details, see Introduction to Code Generation.

The main fitting algorithm is QR decomposition. For robust fitting,

`fitlm`

uses M-estimation to formulate estimating equations and solves them using the method of iterative reweighted least squares (IRLS).`fitlm`

treats a categorical predictor as follows:A model with a categorical predictor that has

*L*levels (categories) includes*L*– 1 indicator variables. The model uses the first category as a reference level, so it does not include the indicator variable for the reference level. If the data type of the categorical predictor is`categorical`

, then you can check the order of categories by using`categories`

and reorder the categories by using`reordercats`

to customize the reference level.`fitlm`

treats the group of*L*– 1 indicator variables as a single variable. If you want to treat the indicator variables as distinct predictor variables, create indicator variables manually by using`dummyvar`

. Then use the indicator variables, except the one corresponding to the reference level of the categorical variable, when you fit a model. For the categorical predictor`X`

, if you specify all columns of`dummyvar(X)`

and an intercept term as predictors, then the design matrix becomes rank deficient.Interaction terms between a continuous predictor and a categorical predictor with

*L*levels consist of the element-wise product of the*L*– 1 indicator variables with the continuous predictor.Interaction terms between two categorical predictors with

*L*and*M*levels consist of the (*L*– 1)*(*M*– 1) indicator variables to include all possible combinations of the two categorical predictor levels.You cannot specify higher-order terms for a categorical predictor because the square of an indicator is equal to itself.

`fitlm`

considers`NaN`

,`''`

(empty character vector),`""`

(empty string),`<missing>`

, and`<undefined>`

values in`tbl`

,`X`

, and`Y`

to be missing values.`fitlm`

does not use observations with missing values in the fit. The`ObservationInfo`

property of a fitted model indicates whether or not`fitlm`

uses each observation in the fit.

For reduced computation time on high-dimensional data sets, fit a linear regression model using the

`fitrlinear`

function.To regularize a regression, use

`fitrlinear`

,`lasso`

,`ridge`

, or`plsregress`

.`fitrlinear`

regularizes a regression for high-dimensional data sets using lasso or ridge regression.`lasso`

removes redundant predictors in linear regression using lasso or elastic net.`ridge`

regularizes a regression with correlated terms using ridge regression.`plsregress`

regularizes a regression with correlated terms using partial least squares.

[1] DuMouchel, W. H., and F. L.
O'Brien. “Integrating a Robust Option into a Multiple Regression Computing
Environment.” *Computer Science and Statistics*:*
Proceedings of the 21st Symposium on the Interface*. Alexandria, VA:
American Statistical Association, 1989.

[2] Holland, P. W., and R. E.
Welsch. “Robust Regression Using Iteratively Reweighted Least-Squares.”
*Communications in Statistics: Theory and Methods*,
*A6*, 1977, pp. 813–827.

[3] Huber, P. J. *Robust
Statistics*. Hoboken, NJ: John Wiley & Sons, Inc.,
1981.

[4] Street, J. O., R. J. Carroll,
and D. Ruppert. “A Note on Computing Robust Regression Estimates via Iteratively
Reweighted Least Squares.” *The American Statistician*. Vol.
42, 1988, pp. 152–154.

Calculate with arrays that have more rows than fit in memory.

This function supports tall arrays for out-of-memory data with some limitations.

If any input argument to

`fitlm`

is a tall array, then all of the other inputs must be tall arrays as well. This includes nonempty variables supplied with the`'Weights'`

and`'Exclude'`

name-value pairs.The

`'RobustOpts'`

name-value pair is not supported with tall arrays.For tall data,

`fitlm`

returns a`CompactLinearModel`

object that contains most of the same properties as a`LinearModel`

object. The main difference is that the compact object is sensitive to memory requirements. The compact object does not include properties that include the data, or that include an array of the same size as the data. The compact object does not contain these`LinearModel`

properties:`Diagnostics`

`Fitted`

`ObservationInfo`

`ObservationNames`

`Residuals`

`Steps`

`Variables`

You can compute the residuals directly from the compact object returned by

`LM = fitlm(X,Y)`

usingRES = Y - predict(LM,X); S = LM.RMSE; histogram(RES,linspace(-3*S,3*S,51))

If the

`CompactLinearModel`

object is missing lower order terms that include categorical factors:The

`plotEffects`

and`plotInteraction`

methods are not supported.The

`anova`

method with the`'components'`

option is not supported.

For more information, see Tall Arrays (MATLAB).

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)