Note: This page has been translated by MathWorks. Please click here

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

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

Create linear regression model using stepwise regression

`mdl = stepwiselm(tbl,modelspec)`

`mdl = stepwiselm(X,y,modelspec)`

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

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

= stepwiselm(`tbl`

,`modelspec`

)`tbl`

using
stepwise regression to add or remove predictors. `stepwiselm`

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

argument. `modelspec`

is
the starting model for the stepwise procedure.

creates
a linear model for any of the inputs in the previous syntaxes, with
additional options specified by one or more `mdl`

= stepwiselm(___,`Name,Value`

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

`LinearModel`

model object by using`stepwiselm`

, use the function`predict`

and the trained model object to generate C code that predicts responses for new data. For details, see 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.

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

argument.
The default value of `'Criterion'`

is `'sse'`

,
and in this case, `stepwiselm`

uses 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 term is added 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 term is removed from the model.

Here is how stepwise proceeds when `'Criterion'`

is `'sse'`

:

Fit the initial model.

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

*p*-values less than an entrance tolerance (that is, if it is unlikely that they would have zero coefficient if added to the model), add the one 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 one with the largest*p*-value and go to step 2; otherwise, end.

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 it. For example, it 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, it will not examine to remove `X1`

or `X2^2`

if `X1:X2^2`

stays
in the model.

The default for `stepwiseglm`

is `'Deviance'`

and
it follows a similar procedure for adding or removing terms.

There are several other criteria available, which you can specify
using the `'Criterion'`

argument. You can use the
change in the value of the Akaike information criterion, Bayesian
information criterion, R-squared, adjusted R-squared as a criterion
to add or remove terms.

Depending on the terms included in the initial model and the order in which terms are moved in and out, the method might build different models from the same set of potential terms. The method terminates when no single step improves the model. There is no guarantee, however, that a different initial model or a different sequence of steps will not lead to a better fit. In this sense, stepwise models are locally optimal, but might not be globally optimal.

You can construct a model using `fitlm`

,
and then manually adjust the model using `step`

, `addTerms`

, or `removeTerms`

.

`LinearModel`

| `fitlm`

| `step`

Was this topic helpful?