Documentation

stepwiselm

Fit linear regression model using stepwise regression

Description

example

mdl = stepwiselm(tbl) creates a linear model for the variables in the table or dataset array 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.

example

mdl = stepwiselm(X,y) creates a linear model of the responses y to the predictor variables in the data matrix X.

example

mdl = stepwiselm(___,modelspec) specifies the starting model modelspec using any of the input argument combinations in previous syntaxes.

example

mdl = stepwiselm(___,Name,Value) 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 stepwiselm uses to add or remove terms.

Examples

collapse all

Load the hald data set, which measures the effect of cement composition on its hardening heat.

This data set includes the variables ingredients and heat. The matrix ingredients contains the percent composition of four chemicals present in the cement. The vector heat contains the values for the heat hardening after 180 days for each cement sample.

Fit a stepwise linear regression model to the data. Specify 0.06 as the threshold for the criterion to add a term to the model.

mdl = stepwiselm(ingredients,heat,'PEnter',0.06)
1. Adding x4, FStat = 22.7985, pValue = 0.000576232
2. Adding x1, FStat = 108.2239, pValue = 1.105281e-06
3. Adding x2, FStat = 5.0259, pValue = 0.051687
4. Removing x4, FStat = 1.8633, pValue = 0.2054
mdl =
Linear regression model:
y ~ 1 + x1 + x2

Estimated Coefficients:
Estimate       SE       tStat       pValue
________    ________    ______    __________

(Intercept)     52.577       2.2862    22.998    5.4566e-10
x1              1.4683       0.1213    12.105    2.6922e-07
x2             0.66225     0.045855    14.442     5.029e-08

Number of observations: 13, Error degrees of freedom: 10
Root Mean Squared Error: 2.41
R-squared: 0.979,  Adjusted R-Squared: 0.974
F-statistic vs. constant model: 230, p-value = 4.41e-09

By default, the starting model is a constant model. stepwiselm performs forward selection and adds the x4, x1, and x2 terms (in that order), because the corresponding p-values are less than the PEnter value of 0.06. stepwiselm then uses backward elimination and removes x4 from the model because, once x2 is in the model, the p-value of x4 is greater than the default value of PRemove, 0.1.

Perform stepwise regression using variables stored in a dataset array. Specify the starting model using Wilkinson notation, and identify the response and predictor variables using optional arguments.

Load the sample data.

The hospital dataset array includes the gender, age, weight, and smoking status of patients.

Fit a linear model with a starting model of a constant term and Smoker as the predictor variable. Specify the response variable, Weight, and categorical predictor variables, Sex, Age, and Smoker.

mdl = stepwiselm(hospital,'Weight~1+Smoker',...
'ResponseVar','Weight','PredictorVars',{'Sex','Age','Smoker'},...
'CategoricalVar',{'Sex','Smoker'})
1. Adding Sex, FStat = 770.0158, pValue = 6.262758e-48
2. Removing Smoker, FStat = 0.21224, pValue = 0.64605
mdl =
Linear regression model:
Weight ~ 1 + Sex

Estimated Coefficients:
Estimate      SE      tStat       pValue
________    ______    ______    ___________

(Intercept)     130.47     1.1995    108.77    5.2762e-104
Sex_Male         50.06     1.7496    28.612     2.2464e-49

Number of observations: 100, Error degrees of freedom: 98
Root Mean Squared Error: 8.73
R-squared: 0.893,  Adjusted R-Squared: 0.892
F-statistic vs. constant model: 819, p-value = 2.25e-49

At each step, stepwiselm searches for terms to add and remove. At first step, stepwise algorithm adds Sex to the model with a $p$-value of 6.26e-48. Then, removes Smoker from the model, since given Sex in the model, the variable Smoker becomes redundant. stepwiselm only includes Sex in the final linear model. The weight of the patients do not seem to differ significantly according to age or the status of smoking.

Load a sample data set and define the matrix of predictors.

X = [Acceleration,Weight];

Define the starting model and the upper model using terms matrices.

T_starting = [0 0 0] % a constant model
T_starting = 1×3

0     0     0

T_upper = [0 0 0;1 0 0;0 1 0;1 1 0] % a linear model with interactions
T_upper = 4×3

0     0     0
1     0     0
0     1     0
1     1     0

Create a linear regression model using stepwise regression. Specify the starting model and the upper bound of the model using the terms matrices, and specify 'Verbose' as 2 to display the evaluation process and the decision taken at each step.

mdl = stepwiselm(X,MPG,T_starting,'upper',T_upper,'Verbose',2)
pValue for adding x1 is 4.0973e-06
pValue for adding x2 is 1.6434e-28
1. Adding x2, FStat = 259.3087, pValue = 1.643351e-28
pValue for adding x1 is 0.18493
No candidate terms to remove
mdl =
Linear regression model:
y ~ 1 + x2

Estimated Coefficients:
Estimate        SE         tStat       pValue
__________    _________    _______    __________

(Intercept)        49.238       1.6411     30.002    2.7015e-49
x2             -0.0086119    0.0005348    -16.103    1.6434e-28

Number of observations: 94, Error degrees of freedom: 92
Root Mean Squared Error: 4.13
R-squared: 0.738,  Adjusted R-Squared: 0.735
F-statistic vs. constant model: 259, p-value = 1.64e-28

Fit a linear regression model with a categorical predictor using stepwise regression. stepwiselm adds or removes a group of indicator variables in one step to add or removes a categorical predictor. This example also shows how to create indicator variables manually and pass them to stepwiselm so that stepwiselm treats each indicator variable as a separate predictor.

Load the carsmall data set, and create a table using the Weight, Model_Year, and MPG variables.

Year = categorical(Model_Year);
tbl1 = table(MPG,Weight,Year);

Fit a linear regression model of MPG using stepwise regression. Specify the starting model as a function of Weight. Set the upper bound of the model to 'poly21', meaning the model can include (at most) a constant and the terms Weight, Weight^2, Year, and Weight*Year. Specify 'Verbose' as 2 to display the evaluation process and the decision taken at each step.

mdl1 = stepwiselm(tbl1,'MPG ~ Weight','Upper','poly21','Verbose',2)
pValue for adding Year is 8.2284e-15
pValue for adding Weight^2 is 0.15454
1. Adding Year, FStat = 47.5136, pValue = 8.22836e-15
pValue for adding Weight^2 is 0.0022303
pValue for adding Weight:Year is 0.0071637
2. Adding Weight^2, FStat = 9.9164, pValue = 0.0022303
pValue for adding Weight:Year is 0.19519
pValue for removing Year is 2.9042e-16
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

stepwiselm creates two indicator variables, Year_76 and Year_82, because Year includes three distinct values.

Because 'Verbose' is 2, stepwiselm displays the evaluation process:

• stepwiselm creates a model as a function of Weight.

• stepwiselm computes the p-values for adding Year or Weight^2. The p-value for Year is less than both the p-value for Weight^2 and the default threshold value of 0.05; therefore, stepwiselm adds Year to the model.

• stepwiselm computes the p-values for adding Weight:Year or Weight^2. Because the p-value for Weight^2 is less than the p-value for Weight:Year, the stepwiselm function adds Weight^2 to the model.

• After adding the quadratic term, stepwiselm computes the p-value for adding Weight:Year again, but the p-value is greater than the threshold value. Therefore, stepwiselm does not add the term to the model. stepwiselm does not examine adding Weight^3 because of the upper bound specified by the 'Upper' name-value pair argument.

• stepwiselm looks for terms to remove. stepwiselm already examined Weight^2, so it computes only the p-value for removing Year. Because the p-value is less than the default threshold value of 0.10, stepwiselm does not remove the term.

• Although the maximum allowed number of steps is 5, stepwiselm terminates the process after two steps because the model does not improve by adding or removing a term.

stepwiselm treats the two indicator variables as one predictor variable and adds Year in one step. To treat the two indicator variables as two distinct predictor variables, use dummyvar to create separate categorical variables.

temp_Year = dummyvar(Year);
Year_76 = logical(temp_Year(:,2));
Year_82 = logical(temp_Year(:,3));

Create a table containing MPG, Weight, Year_76, and Year_82.

tbl2 = table(MPG,Weight,Year_76,Year_82);

Create a stepwise linear regression model from the same starting model used for mdl1.

mdl2 = stepwiselm(tbl2,'MPG ~ Weight','Upper','poly211')
1. Adding Year_82, FStat = 83.1956, pValue = 1.76163e-14
2. Adding Weight:Year_82, FStat = 8.0641, pValue = 0.0055818
3. Adding Year_76, FStat = 8.1284, pValue = 0.0054157
mdl2 =
Linear regression model:
MPG ~ 1 + Year_76 + Weight*Year_82

Estimated Coefficients:
Estimate         SE         tStat       pValue
__________    __________    _______    __________

(Intercept)             38.844        1.5294     25.397     1.503e-42
Weight               -0.006272    0.00042673    -14.698    1.5622e-25
Year_76_1               2.0395       0.71537      2.851     0.0054157
Year_82_1               19.607        3.8731     5.0623    2.2163e-06
Weight:Year_82_1    -0.0046268     0.0014979    -3.0888     0.0026806

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

The model mdl2 includes the interaction term Weight:Year_82_1 instead of Weight^2, the term included in mdl1.

Input Arguments

collapse all

Input data including predictor and response variables, specified as a table or dataset array. The predictor variables can be numeric, logical, categorical, character, or string. The response variable must be numeric or logical.

• By default, stepwiselm takes the last variable as the response variable and the others as the predictor variables.

• To set a different column as the response variable, use the ResponseVar name-value pair argument.

• To use a subset of the columns as predictors, use the PredictorVars name-value pair argument.

• To define a model specification, set the modelspec argument using a formula or terms matrix. The formula or terms matrix specifies which columns to use as the predictor or response variables.

The variable names in a table do not have to be valid MATLAB® identifiers. However, if the names are not valid, you cannot use a formula when you fit or adjust a model; for example:

• You cannot specify modelspec using a formula.

• You cannot use a formula to specify the terms to add or remove when you use the addTerms function or the removeTerms function, respectively.

• You cannot use a formula to specify the lower and upper bounds of the model when you use the step or stepwiselm function with the name-value pair arguments 'Lower' and 'Upper', respectively.

You can verify the variable names in tbl by using the isvarname function. The following code returns logical 1 (true) for each variable that has a valid variable name.

cellfun(@isvarname,tbl.Properties.VariableNames)
If the variable names in tbl are not valid, then convert them by using the matlab.lang.makeValidName function.
tbl.Properties.VariableNames = matlab.lang.makeValidName(tbl.Properties.VariableNames);

Predictor variables, specified as an n-by-p matrix, where n is the number of observations and p is the number of predictor variables. Each column of X represents one variable, and each row represents one observation.

By default, there is a constant term in the model, unless you explicitly remove it, so do not include a column of 1s in X.

Data Types: single | double

Response variable, specified as an n-by-1 vector, where n is the number of observations. Each entry in y is the response for the corresponding row of X.

Data Types: single | double | logical

Starting model for the stepwise regression, specified as one of the following:

• A character vector or string scalar naming the model.

ValueModel Type
'constant'Model contains only a constant (intercept) term.
'linear'Model contains an intercept and linear term for each predictor.
'interactions'Model contains an intercept, linear term for each predictor, and all products of pairs of distinct predictors (no squared terms).
'purequadratic'Model contains an intercept term and linear and squared terms for each predictor.
'quadratic'Model contains an intercept term, linear and squared terms for each predictor, and all products of pairs of distinct predictors.
'polyijk'Model is a polynomial with all terms up to degree i in the first predictor, degree j in the second predictor, and so on. Specify the maximum degree for each predictor by using numerals 0 though 9. The model contains interaction terms, but the degree of each interaction term does not exceed the maximum value of the specified degrees. For example, 'poly13' has an intercept and x1, x2, x22, x23, x1*x2, and x1*x22 terms, where x1 and x2 are the first and second predictors, respectively.
• A t-by-(p + 1) matrix, or a Terms Matrix, specifying terms in the model, where t is the number of terms and p is the number of predictor variables, and +1 accounts for the response variable. A terms matrix is convenient when the number of predictors is large and you want to generate the terms programmatically.

• A character vector or string scalar representing a Formula in the form

'Y ~ terms',

where the terms are in Wilkinson Notation. The variable names in the formula must be valid MATLAB identifiers.

If you want to specify the smallest or largest set of terms in the model that stepwiselm fits, use the Lower and Upper name-value pair arguments.

Data Types: char | string | single | double

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 quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Criterion','aic','Upper','interactions','Verbose',1 instructs stepwiselm to use the Akaike information criterion, display the action it takes at each step, and include at most the interaction terms in the model.

Categorical variable list, specified as the comma-separated pair consisting of 'CategoricalVars' and either a string array or cell array of character vectors containing categorical variable names in the table or dataset array tbl, or a logical or numeric index vector indicating which columns are categorical.

• If data is in a table or dataset array tbl, then, by default, stepwiselm treats all categorical values, logical values, character arrays, string arrays, and cell arrays of character vectors as categorical variables.

• If data is in matrix X, then the default value of 'CategoricalVars' is an empty matrix []. That is, no variable is categorical unless you specify it as categorical.

For example, you can specify the observations 2 and 3 out of 6 as categorical using either of the following examples.

Example: 'CategoricalVars',[2,3]

Example: 'CategoricalVars',logical([0 1 1 0 0 0])

Data Types: single | double | logical | string | cell

Criterion to add or remove terms, specified as the comma-separated pair consisting of 'Criterion' and one of these values:

• 'sse'p-value for an F-test of the change in the sum of squared error that results from adding or removing the term

• 'aic' — Change in the value of Akaike information criterion (AIC)

• 'bic' — Change in the value of Bayesian information criterion (BIC)

• 'rsquared' — Increase in the value of R2

• 'adjrsquared' — Increase in the value of adjusted R2

Example: 'Criterion','bic'

Observations to exclude from the fit, specified as the comma-separated pair consisting of 'Exclude' and a logical or numeric index vector indicating which observations to exclude from the fit.

For example, you can exclude observations 2 and 3 out of 6 using either of the following examples.

Example: 'Exclude',[2,3]

Example: 'Exclude',logical([0 1 1 0 0 0])

Data Types: single | double | logical

Indicator for the constant term (intercept) in the fit, specified as the comma-separated pair consisting of 'Intercept' and either true to include or false to remove the constant term from the model.

Use 'Intercept' only when specifying the model using a character vector or string scalar, not a formula or matrix.

Example: 'Intercept',false

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'

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

Example: 'NSteps',5

Data Types: single | double

Threshold for the criterion to add a term, specified as the comma-separated pair consisting of 'PEnter' and a scalar value, as described in this table.

CriterionDefault ValueDecision
'SSE'0.05If the p-value of the F-statistic is less than PEnter (p-value to enter), add the term to the model.
'AIC'0If the change in the AIC of the model is less than PEnter, add the term to the model.
'BIC'0If the change in the BIC of the model is less than PEnter, add the term to the model.
'Rsquared'0.1If the increase in the R-squared value of the model is greater than PEnter, add the term to the model.
'AdjRsquared'0If the increase in the adjusted R-squared value of the model is greater than PEnter, add the term to the model.

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

Example: 'PEnter',0.075

Predictor variables to use in the fit, specified as the comma-separated pair consisting of 'PredictorVars' and either a string array or cell array of character vectors of the variable names in the table or dataset array tbl, or a logical or numeric index vector indicating which columns are predictor variables.

The string values or character vectors should be among the names in tbl, or the names you specify using the 'VarNames' name-value pair argument.

The default is all variables in X, or all variables in tbl except for ResponseVar.

For example, you can specify the second and third variables as the predictor variables using either of the following examples.

Example: 'PredictorVars',[2,3]

Example: 'PredictorVars',logical([0 1 1 0 0 0])

Data Types: single | double | logical | string | cell

Threshold for the criterion to remove a term, specified as the comma-separated pair consisting of 'PRemove' and a scalar value, as described in this table.

CriterionDefault ValueDecision
'SSE'0.10If the p-value of the F-statistic is greater than PRemove (p-value to remove), remove the term from the model.
'AIC'0.01If the change in the AIC of the model is greater than PRemove, remove the term from the model.
'BIC'0.01If the change in the BIC of the model is greater than PRemove, remove the term from the model.
'Rsquared'0.05If the increase in the R-squared value of the model is less than PRemove, remove the term from the model.
'AdjRsquared'-0.05If the increase in the adjusted R-squared value of the model is less than PRemove, remove the term from the model.

At each step, the stepwiselm function also checks whether a 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, the stepwiselm function removes the redundant term, regardless of the criterion value.

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

Example: 'PRemove',0.05

Response variable to use in the fit, specified as the comma-separated pair consisting of 'ResponseVar' and either a character vector or string scalar containing the variable name in the table or dataset array tbl, or a logical or numeric index vector indicating which column is the response variable. You typically need to use 'ResponseVar' when fitting a table or dataset array tbl.

For example, you can specify the fourth variable, say yield, as the response out of six variables, in one of the following ways.

Example: 'ResponseVar','yield'

Example: 'ResponseVar',

Example: 'ResponseVar',logical([0 0 0 1 0 0])

Data Types: single | double | logical | char | string

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

Names of variables, specified as the comma-separated pair consisting of 'VarNames' and a string array or cell array of character vectors including the names for the columns of X first, and the name for the response variable y last.

'VarNames' is not applicable to variables in a table or dataset array, because those variables already have names.

The variable names do not have to be valid MATLAB identifiers. However, if the names are not valid, you cannot use a formula when you fit or adjust a model; for example:

• You cannot use a formula to specify the terms to add or remove when you use the addTerms function or the removeTerms function, respectively.

• You cannot use a formula to specify the lower and upper bounds of the model when you use the step or stepwiselm function with the name-value pair arguments 'Lower' and 'Upper', respectively.

Before specifying 'VarNames',varNames, you can verify the variable names in varNames by using the isvarname function. The following code returns logical 1 (true) for each variable that has a valid variable name.

cellfun(@isvarname,varNames)
If the variable names in varNames are not valid, then convert them by using the matlab.lang.makeValidName function.
varNames = matlab.lang.makeValidName(varNames);

Example: 'VarNames',{'Horsepower','Acceleration','Model_Year','MPG'}

Data Types: string | cell

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

• 0 — Suppress all display.

• 1 — Display the action taken at each step.

• 2 — Display the evaluation process and the action taken at each step.

Example: 'Verbose',2

Observation weights, specified as the comma-separated pair consisting of 'Weights' and an n-by-1 vector of nonnegative scalar values, where n is the number of observations.

Data Types: single | double

Output Arguments

collapse all

Linear model representing a least-squares fit of the response to the data, returned as a LinearModel object.

For the properties and methods of the linear model object, mdl, see the LinearModel class page.

collapse all

Terms Matrix

A terms matrix T is a t-by-(p + 1) matrix specifying terms in a model, where t is the number of terms, p is the number of predictor variables, and  +1 accounts for the response variable. The value of T(i,j) is the exponent of variable j in term i.

For example, suppose that an input includes three predictor variables A, B, and C and the response variable Y in the order A, B, C, and Y. Each row of T represents one term:

• [0 0 0 0] — Constant term or intercept

• [0 1 0 0]B; equivalently, A^0 * B^1 * C^0

• [1 0 1 0]A*C

• [2 0 0 0]A^2

• [0 1 2 0]B*(C^2)

The 0 at the end of each term represents the response variable. In general, a column vector of zeros in a terms matrix represents the position of the response variable. If you have the predictor and response variables in a matrix and column vector, then you must include 0 for the response variable in the last column of each row.

Formula

A formula for model specification is a character vector or string scalar of the form 'Y ~ terms'.

• Y is the response name.

• terms represents the predictor terms in a model using Wilkinson notation.

For example:

• 'Y ~ A + B + C' specifies a three-variable linear model with intercept.

• 'Y ~ A + B + C – 1' specifies a three-variable linear model without intercept. Note that formulas include a constant (intercept) term by default. To exclude a constant term from the model, you must include –1 in the formula.

Wilkinson Notation

Wilkinson notation describes the terms present in a model. The notation relates to the terms present in a model, not to the multipliers (coefficients) of those terms.

Wilkinson notation uses these symbols:

• + means include the next variable.

• means do not include the next variable.

• : defines an interaction, which is a product of terms.

• * defines an interaction and all lower-order terms.

• ^ raises the predictor to a power, exactly as in * repeated, so ^ includes lower-order terms as well.

• () groups terms.

This table shows typical examples of Wilkinson notation.

Wilkinson NotationTerm in Standard Notation
1Constant (intercept) term
A^k, where k is a positive integerA, A2, ..., Ak
A + BA, B
A*BA, B, A*B
A:BA*B only
–BDo not include B
A*B + CA, B, C, A*B
A + B + C + A:BA, B, C, A*B
A*B*C – A:B:CA, B, C, A*B, A*C, B*C
A*(B + C)A, B, C, A*B, A*C

Statistics and Machine Learning Toolbox™ notation always includes a constant term unless you explicitly remove the term using –1.

For more details, see Wilkinson Notation.

Tips

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

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

1. Fit the initial model.

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

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.