Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

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.

step

Class: LinearModel

Improve linear regression model by adding or removing terms

Syntax

mdl1 = step(mdl)
mdl1 = step(mdl,Name,Value)

Description

mdl1 = step(mdl) returns an improved linear model based on mdl, with one predictor added or removed.

    Note:   You can use step only if mdl.Robust = []. This holds when you create mdl with fitlm having the RobustOpts name-value pair set to the default 'off'.

mdl1 = step(mdl,Name,Value) returns an improved linear model using additional options specified by one or more Name,Value pair arguments. For example, you can specify the criterion to use to add or remove terms.

Input Arguments

expand all

Full, fitted linear regression model, specified as a LinearModel object constructed using fitlm or stepwiselm.

Name-Value Pair Arguments

Specify optional comma-separated 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.

expand all

Criterion for selecting terms to add or remove, specified as the comma-separated pair consisting of 'Criterion' and one of the following.

CriterionPEnterPRemoveCompared Against
'SSE'0.05< 0.1p-value for F test
'AIC'0< 0.01Change in AIC
'BIC'0< 0.01Change in BIC
'Rsquared'0.1> 0.05Increase in R-squared
'AdjRsquared'0> -0.05Increase in adjusted R-squared

Example: 'Criterion','BIC'

Model specification describing terms that cannot be removed from the model, specified as the comma-separated pair consisting of 'Lower' and one of the options for modelspec naming the model.

Example: 'Lower','linear'

Number of steps to take, specified as the comma-separated pair consisting of 'NSteps' and a positive integer.

Data Types: single | double

Improvement measure for adding a term, specified as the comma-separated pair consisting of 'PEnter' and a scalar value. The default values are below.

CriterionDefault valueDecision
'Deviance'0.05If the p-value of F or chi-squared statistic is smaller than PEnter, add the term to the model.
'SSE'0.05If the SSE of the model is smaller than PEnter, add the term to the model.
'AIC'0If the change in the AIC of the model is smaller than PEnter, add the term to the model.
'BIC'0If the change in the BIC of the model is smaller than PEnter, add the term to the model.
'Rsquared'0.1If the increase in the R-squared of the model is larger than PEnter, add the term to the model.
'AdjRsquared'0If the increase in the adjusted R-squared of the model is larger than PEnter, add the term to the model.

For more information on the criteria, see Criterion name-value pair argument.

Example: 'PEnter',0.075

Improvement measure for removing a term, specified as the comma-separated pair consisting of 'PRemove' and a scalar value.

CriterionDefault valueDecision
'Deviance'0.10If the p-value of F or chi-squared statistic is larger than PRemove, remove the term from the model.
'SSE'0.10If the p-value of the F statistic is larger than PRemove, remove the term from the model.
'AIC'0.01If the change in the AIC of the model is larger than PRemove, remove the term from the model.
'BIC'0.01If the change in the BIC of the model is larger than PRemove, remove the term from the model.
'Rsquared'0.05If the increase in the R-squared value of the model is smaller than PRemove, remove the term from the model.
'AdjRsquared'-0.05If the increase in the adjusted R-squared value of the model is smaller than PRemove, remove the term from the model.

At each step, stepwise algorithm also checks whether any term is redundant (linearly dependent) with other terms in the current model. When any term is linearly dependent with other terms in the current model, it is removed, regardless of the criterion value.

For more information on the criteria, see Criterion name-value pair argument.

Example: 'PRemove',0.05

Model specification describing the largest set of terms in the fit, specified as the comma-separated pair consisting of 'Upper' and one of the character vector options for modelspec naming the model.

Example: 'Upper','quadratic'

Control for display of information, specified as the comma-separated pair consisting of 'Verbose' and one of the following:

  • 0 — Suppress all display.

  • 1 — Display the action taken at each step.

  • 2 — Also display the actions evaluated at each step.

Example: 'Verbose',2

Output Arguments

expand all

Improved linear regression model, returned as a LinearModel object. To overwrite mdl, set mdl1 equal to mdl.

Examples

expand all

Fit a linear model to car data. Use step to evaluate whether a quadratic model improves the fit quality.

Load carsmall data, and create a table using weight and model year predictors with MPG response.

load carsmall
tbl = table(MPG,Weight);
tbl.Year = categorical(Model_Year);

Make a linear model of MPG as a function of Year and Weight.

mdl = fitlm(tbl,'MPG ~ Year + Weight')
mdl = 


Linear regression model:
    MPG ~ 1 + Weight + 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
    Year_76            1.9291       0.74761     2.5804      0.011488
    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

Use step to adjust the model to potentially include full quadratic terms.

mdl1 = step(mdl,'upper','quadratic')
1. Adding Weight^2, FStat = 9.9164, pValue = 0.0022303

mdl1 = 


Linear regression model:
    MPG ~ 1 + Weight + Year + Weight^2

Estimated Coefficients:
                    Estimate         SE         tStat       pValue  
                   __________    __________    _______    __________

    (Intercept)        54.206        4.7117     11.505    2.6648e-19
    Weight          -0.016404     0.0031249    -5.2493    1.0283e-06
    Year_76            2.0887       0.71491     2.9215     0.0044137
    Year_82            8.1864       0.81531     10.041    2.6364e-16
    Weight^2       1.5573e-06    4.9454e-07      3.149     0.0022303


Number of observations: 94, Error degrees of freedom: 89
Root Mean Squared Error: 2.78
R-squared: 0.885,  Adjusted R-Squared 0.88
F-statistic vs. constant model: 172, p-value = 5.52e-41

Related Examples

Algorithms

Stepwise Regression

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':

  1. Fit the initial model.

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

  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.

Alternatives

Use stepwiselm to select a model from a starting model, continuing until no single step is beneficial.

Use addTerms or removeTerms to add or remove particular terms.

Was this topic helpful?