Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Fit linear regression model using stepwise regression

`mdl = stepwiselm(tbl)`

`mdl = stepwiselm(X,y)`

`mdl = stepwiselm(___,modelspec)`

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

creates a linear model for the variables in the table or dataset array
`mdl`

= stepwiselm(`tbl`

)`tbl`

using stepwise regression to add or remove predictors,
starting from a constant model. `stepwiselm`

uses the last
variable of `tbl`

as the response variable.
`stepwiselm`

uses forward and backward stepwise regression to
determine a final model. At each step, the function searches for terms to add the
model to or remove from the model, based on the value of the
`'Criterion'`

argument.

specifies additional options using one or more name-value pair arguments. For
example, you can specify the categorical variables, the smallest or largest set of
terms to use in the model, the maximum number of steps to take, or the criterion
that `mdl`

= stepwiselm(___,`Name,Value`

)`stepwiselm`

uses to add or remove terms.

You cannot use robust regression with stepwise regression. Check your data for outliers before using

`stepwiselm`

.For other methods such as

`anova`

, or properties of the`LinearModel`

object, see`LinearModel`

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

*Stepwise regression*is a systematic method for adding and removing terms from a linear or generalized linear model based on their statistical significance in explaining the response variable. The method begins with an initial model, specified using`modelspec`

, and then compares the explanatory power of incrementally larger and smaller models.The

`stepwiselm`

function uses forward and backward stepwise regression to determine a final model. At each step, the function searches for terms to add to the model or remove from the model based on the value of the`'Criterion'`

name-value pair argument.The default value of

`'Criterion'`

for a linear regression model is`'sse'`

. In this case,`stepwiselm`

and`step`

of`LinearModel`

use the*p*-value of an*F*-statistic to test models with and without a potential term at each step. If a term is not currently in the model, the null hypothesis is that the term would have a zero coefficient if added to the model. If there is sufficient evidence to reject the null hypothesis, the function adds the term to the model. Conversely, if a term is currently in the model, the null hypothesis is that the term has a zero coefficient. If there is insufficient evidence to reject the null hypothesis, the function removes the term from the model.Stepwise regression takes these steps when

`'Criterion'`

is`'sse'`

:Fit the initial model.

Examine a set of available terms not in the model. If any of the terms have

*p*-values less than an entrance tolerance (that is, if it is unlikely a term would have a zero coefficient if added to the model), add the term with the smallest*p*-value and repeat this step; otherwise, go to step 3.If any of the available terms in the model have

*p*-values greater than an exit tolerance (that is, the hypothesis of a zero coefficient cannot be rejected), remove the term with the largest*p*-value and return to step 2; otherwise, end the process.

At any stage, the function will not add a higher-order term if the model does not also include all lower-order terms that are subsets of the higher-order term. For example, the function will not try to add the term

`X1:X2^2`

unless both`X1`

and`X2^2`

are already in the model. Similarly, the function will not remove lower-order terms that are subsets of higher-order terms that remain in the model. For example, the function will not try to remove`X1`

or`X2^2`

if`X1:X2^2`

remains in the model.The default value of

`'Criterion'`

for a generalized linear model is`'Deviance'`

.`stepwiseglm`

and`step`

of`GeneralizedLinearModel`

follow a similar procedure for adding or removing terms.You can specify other criteria by using the

`'Criterion'`

name-value pair argument. For example, you can specify the change in the value of the Akaike information criterion, Bayesian information criterion, R-squared, or adjusted R-squared as the criterion to add or remove terms.Depending on the terms included in the initial model, and the order in which the function adds and removes terms, the function might build different models from the same set of potential terms. The function terminates when no single step improves the model. However, a different initial model or a different sequence of steps does not guarantee a better fit. In this sense, stepwise models are locally optimal, but might not be globally optimal.

`stepwiselm`

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

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.

Therefore, if

`stepwiselm`

adds or removes a categorical predictor, the function actually adds or removes the group of indicator variables in one step. Similarly, if`stepwiselm`

adds or removes an interaction term with a categorical predictor, the function actually adds or removes the group of interaction terms including the categorical predictor.`stepwiselm`

considers`NaN`

,`''`

(empty character vector),`""`

(empty string),`<missing>`

, and`<undefined>`

values in`tbl`

,`X`

, and`Y`

to be missing values.`stepwiselm`

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

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

uses each observation in the fit.

You can construct a model using

`fitlm`

, and then manually adjust the model using`step`

,`addTerms`

, or`removeTerms`

.

`LinearModel`

| `fitlm`

| `step`