stepwiseglm
Create generalized linear regression model by stepwise regression
Syntax
Description
creates a generalized linear regression model for the variables in the table
mdl = stepwiseglm(tbl)tbl using stepwise regression to add or remove predictors,
starting from a constant model. stepwiseglm uses the last
variable of tbl as the response variable.
stepwiseglm uses forward and backward stepwise regression
to determine a final model. At each step, the function searches for terms to add the
to model or remove from the model, based on the value of the
'Criterion' argument.
specifies which table variable contains the response data.mdl = stepwiseglm(tbl,ResponseVarName)
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 = stepwiseglm(___,modelspec,Name,Value)stepwiseglm uses to add or remove terms.
Examples
Input Arguments
Name-Value Arguments
Output Arguments
More About
Tips
The generalized linear model
mdlis a standard linear model unless you specify otherwise with theDistributionname-value pair.For other methods such as
devianceTest, or properties of theGeneralizedLinearModelobject, seeGeneralizedLinearModel.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.
Algorithms
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
stepwiseglmfunction 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,stepwiselmandstepofLinearModeluse 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^2unless bothX1andX2^2are 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 removeX1orX2^2ifX1:X2^2remains in the model.The default value of
'Criterion'for a generalized linear model is'Deviance'.stepwiseglmandstepofGeneralizedLinearModelfollow 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.
stepwiseglmtreats 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 usingcategoriesand reorder the categories by usingreordercatsto customize the reference level. For more details about creating indicator variables, see Automatic Creation of Dummy Variables.stepwiseglmtreats 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 usingdummyvar. 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 predictorX, if you specify all columns ofdummyvar(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
stepwiseglmadds or removes a categorical predictor, the function actually adds or removes the group of indicator variables in one step. Similarly, ifstepwiseglmadds or removes an interaction term with a categorical predictor, the function actually adds or removes the group of interaction terms including the categorical predictor.stepwiseglmconsidersNaN,''(empty character vector),""(empty string),<missing>, and<undefined>values intbl,X, andYto be missing values.stepwiseglmdoes not use observations with missing values in the fit. TheObservationInfoproperty of a fitted model indicates whether or notstepwiseglmuses each observation in the fit.
Alternatives
Use
fitglmto create a model with a fixed specification. Usestep,addTerms, orremoveTermsto adjust a fitted model.
References
[1] Collett, D. Modeling Binary Data. New York: Chapman & Hall, 2002.
[2] Dobson, A. J. An Introduction to Generalized Linear Models. New York: Chapman & Hall, 1990.
[3] McCullagh, P., and J. A. Nelder. Generalized Linear Models. New York: Chapman & Hall, 1990.