Categories &

Functions List

Function Reference: stepwiseglm

statistics: mdl = stepwiseglm (X, y)
statistics: mdl = stepwiseglm (X, y, modelspec)
statistics: mdl = stepwiseglm (tbl)
statistics: mdl = stepwiseglm (tbl, modelspec)
statistics: mdl = stepwiseglm (…, Name, Value)

Fit a generalized linear regression model by stepwise term selection.

mdl = stepwiseglm (X, y) starts from a model given by modelspec and repeatedly adds or removes terms, one at a time, until no further move improves the selection criterion. It returns the fitted GeneralizedLinearModel object mdl, whose Steps property records the term-selection trace. X is an n-by-p numeric predictor matrix and y the response; mdl = stepwiseglm (tbl) instead takes the predictors and response from the table tbl (the last column is the response unless overridden).

modelspec is the starting model. It is a Wilkinson formula string (e.g. 'y ~ x1 + x2'), a keyword ('constant' (default), 'linear', 'interactions', 'purequadratic', 'quadratic', or 'full'), or a terms matrix. The candidate terms available to the search are bounded below by 'Lower' and above by 'Upper'.

The following Name/Value pairs control the stepwise search:

NameValue
'Lower'the smallest model considered (terms in it are never removed). Defaults to 'constant'.
'Upper'the largest model considered (the candidate term universe). Defaults to 'interactions'.
'Criterion'the selection criterion: 'Deviance' (default), 'sse', 'aic', or 'bic'. Under 'Deviance' and 'sse', terms enter or leave by a chi-squared or F test on the change in deviance; under 'aic'/'bic' the move that most reduces the information criterion is taken.
'PEnter'the p-value (or criterion margin) below which a term is added. Defaults to 0.05 for 'Deviance' and 'sse', and 0 for 'aic'/'bic'.
'PRemove'the p-value (or criterion margin) above which a term is removed. Defaults to 0.10 for 'Deviance' and 'sse', and 0 for 'aic'/'bic'.
'NSteps'the maximum number of steps. Defaults to Inf (run to convergence).
'Verbose'0 to run silently, or 1 (default) to print each accepted step.

Source Code: stepwiseglm

As in fitglm, a 'binomial' response is the number of successes, and the trials come either from 'BinomialSize' or from passing y as an n-by-2 matrix of successes and trials. Changed in 1.9.0: y was previously read as the proportion.

All Name/Value pairs accepted by fitglm (such as 'Distribution', 'Link', 'Weights', 'Offset', 'BinomialSize', 'Intercept', 'DispersionFlag', 'CategoricalVars', and 'Exclude') are also accepted and forwarded to the fit.

The Steps property

The returned model’s Steps property records the trace, as a structure with seven fields. Start, Lower, and Upper are LinearFormula objects for the starting model and the two bounds; Criterion is the criterion as it was asked for; PEnter and PRemove are the thresholds it ran under; and History is a table with one row per step.

History always carries Action ('Start', 'Add', or 'Remove'), TermName, Terms (the terms matrix after the step, over the model’s variables), DF (the coefficient count after the step), and delDF (the change in it, negative for a removal). The remaining columns follow the criterion, which is why a history is read by name and not by position:

CriterionFurther columns
'Deviance'Deviance, then Chi2Stat or FStat as the dispersion is fixed or estimated, then PValue.
'sse'FStat and pValue.
'aic', 'bic'one column, AIC or BIC, holding the criterion’s value after the step, the starting model included.

Source Code: stepwiseglm

The first row is the starting model, named by its right-hand side.

Categorical predictors

A categorical predictor with L levels contributes L - 1 indicator columns, the first level being the omitted reference, and the search treats that whole group as a single term: it is added or removed in one step, worth L - 1 degrees of freedom, and never one indicator at a time. An interaction naming a categorical predictor behaves the same way, contributing one column per indicator and entering as one term. Steps.History names such a term by the predictor ('g', or 'x1:g' for the interaction) rather than by its indicators, while CoefficientNames names the indicators ('g_B', 'x1:g_C').

In a table, every column that groups its observations is taken as categorical: a cell array of character vectors, a categorical array, a string array, or a logical column. 'CategoricalVars' adds to these, and is the only way to mark a column of a predictor matrix; it takes predictor names, column indices, or a logical vector.

See also: GeneralizedLinearModel, fitglm, stepwisefit, glmfit, glmval

Source Code: stepwiseglm

Stepwise Poisson regression: start from a constant model and let the search add the predictors that matter.

 X = [0.83, -0.68; 0.22, 0.93; -0.12, 0.72; 0.55, -2.55; 1.89, 1.39; ...
      -1.46, -1.18; 1.06, -0.75; -0.89, 0.85; 0.19, -0.71; -0.43, -0.57];
 y = [2; 2; 2; 1; 9; 0; 2; 1; 1; 1];
 mdl = stepwiseglm (X, y, 'constant', 'Distribution', 'poisson', ...
                    'Upper', 'linear')
1. Adding x1, Deviance = 3.73309, Chi2Stat = 15.7459, PValue = 7.24432e-05
mdl =

  Generalized linear regression model:
      log(y) ~ 1 + x1
      Distribution = Poisson,  Link = log

  Coefficients:

  2x4 table

                   Estimate       SE        tStat        pValue       
                   ________    ________    ________    ___________    

    (Intercept)    0.159051    0.335298    0.474358       0.635245    
    x1             0.985476    0.261929     3.76238    0.000168306    


Number of observations: 10, Error degrees of freedom: 8
Dispersion: 1
Deviance: 3.73309
Chi^2-statistic vs. constant model: 15.7459, p-value = 7.24432e-05

Stepwise logistic regression selected by AIC, reported from a table.

 X = [0.83, 1.02; 0.22, 0.29; -0.12, 0.09; 0.55, 0.56; 1.89, 1.96; ...
      -1.46, -0.46; 1.06, -0.87; -0.89, 1.18; 0.19, -1.00; -0.43, -0.04];
 y = [1; 1; 0; 1; 1; 0; 1; 0; 1; 0];
 tbl = array2table ([X, y], 'VariableNames', {'x1', 'x2', 'y'});
 mdl = stepwiseglm (tbl, 'constant', 'Distribution', 'binomial', ...
                    'Criterion', 'aic', 'Verbose', 0)
warning: glmfit: maximum number of iterations has been reached.
warning: called from
    glmfit at line 386 column 5
    GeneralizedLinearModel at line 1072 column 8
    stepwiseglm at line 416 column 3
    __eval_demo__ at line 84 column 9
    __demo_notebook__ at line 43 column 3
    __build_demos__ at line 79 column 7
    function_texi2html at line 135 column 5
    package_texi2html at line 336 column 9

warning: matrix singular to machine precision
warning: called from
    GeneralizedLinearModel at line 1233 column 7
    stepwiseglm at line 416 column 3
    __eval_demo__ at line 84 column 9
    __demo_notebook__ at line 43 column 3
    __build_demos__ at line 79 column 7
    function_texi2html at line 135 column 5
    package_texi2html at line 336 column 9

mdl =

  Generalized linear regression model:
      logit(y) ~ 1 + x1
      Distribution = Binomial,  Link = logit

  Coefficients:

  2x4 table

                   Estimate        SE            tStat         pValue     
                   ________    ___________    ____________    ________    

    (Intercept)    -6.17162    2.16375e+07    -2.85228e-07           1    
    x1              267.095    2.29426e+07     1.16418e-05    0.999991    


Number of observations: 10, Error degrees of freedom: 8
Dispersion: 1
Deviance: 4.44089e-15
Chi^2-statistic vs. constant model: 13.4602, p-value = 0.000243673