Create Linear Regression Model Using Stepwise Regression - MATLAB Stepwiselm - MathWorks India
Create Linear Regression Model Using Stepwise Regression - MATLAB Stepwiselm - MathWorks India
stepwiselm
Create linear regression model using stepwise regression
Syntax
Description
mdl = stepwiselm(tbl,modelspec)returns a linear model for the variables in the table or dataset example
array tblusing stepwise regression to add or remove predictors. stepwiselmuses 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.
modelspecis the starting model for the stepwise procedure.
example
mdl = stepwiselm(X,y,modelspec)creates a linear model of the responses yto the predictor
variables in the data matrix X, using stepwise regression to add or remove predictors. modelspecis
the starting model for the stepwise procedure.
example
mdl = stepwiselm(___,Name,Value)creates a linear model for any of the inputs in the previous
syntaxes, with additional options specified by one or more Name,Valuepair 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 stepwiselmuses to add or
remove terms.
load hald
haldcontains hardening data for 13 different concrete compositions. heatis the heat of hardening after 180
days. ingredientsis the percentage of each different ingredient in the cement sample.
Fit a linear model to the data. Set the criterion value to enter the model as 0.06.
mdl = stepwiselm(ingredients,heat,'PEnter',0.06)
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 1/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
mdl =
Estimated Coefficients:
Estimate SE tStat pValue
________ ________ ______ __________
load hospital
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 Smokeras 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'})
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 2/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
mdl =
Estimated Coefficients:
Estimate SE tStat pValue
________ ______ ______ ___________
Related Examples
Compare large and small stepwise models
Linear Regression
Input data, specified as a table or dataset array. When modelspecis a formula, it specifies the variables to
be used as the predictors and response. Otherwise, if you do not specify the predictor and response
variables, the last variable is the response variable and the others are the predictor variables by default.
Predictor variables can be numeric, or any grouping variable type, such as logical or categorical (see
Grouping Variables). The response must be numeric or logical.
To set a different column as the response variable, use the ResponseVarname-value pair argument. To use a
subset of the columns as predictors, use the PredictorVarsname-value pair argument.
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 3/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
X Predictor variables
matrix
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 Xrepresents 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.
y Response variable
vector
Response variable, specified as an n-by-1 vector, where n is the number of observations. Each entry in yis
the response for the corresponding row of X.
Starting model for the stepwise regression, specified as one of the following:
'linear' Model contains an intercept and linear terms for each predictor.
'interactions' Model contains an intercept, linear terms for each predictor, and all
products of pairs of distinct predictors (no squared terms).
'purequadratic' Model contains an intercept, linear terms, and squared terms for each
predictor.
'polyijk' Model is a polynomial with all terms up to degree iin the first predictor,
degree jin the second predictor, etc. Use numerals 0through 9. For
example, 'poly2111'has a constant plus all linear* and product
terms, and also contains terms with predictor 1 squared.
If you want to specify the smallest or largest set of terms in the model that stepwiselmfits, use the
Lowerand Uppername-value pair arguments.
t-by-(p+1) matrix, namely a terms matrix, specifying terms to include in model, where t is the number of
terms and p is the number of predictor variables, and plus one is for the response variable.
Character vector representing a formula in the form
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 4/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
'Y ~ terms',
where the termsare in Wilkinson Notation.
Categorical variables in the fit, specified as the comma-separated pair consisting of 'CategoricalVars'and
either a cell array of character vectors of the names of the categorical variables 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 the default is to treat all categorical or logical variables,
character arrays, or cell arrays of character vectors as categorical variables.
If data is in matrix X, then the default value of this name-value pair argument is an empty matrix []. That
is, no variable is categorical unless you specify it.
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]
Criterion to add or remove terms, specified as the comma-separated pair consisting of 'Criterion'and one
of the following:
'sse' Default for stepwiselm. p-value for an F-test of the change in the sum of squared error by 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'
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 5/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
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]
Indicator the for constant term (intercept) in the fit, specified as the comma-separated pair consisting of
'Intercept'and either trueto include or falseto remove the constant term from the model.
Use 'Intercept'only when specifying the model using a character vector, not a formula or matrix.
Example: 'Intercept',false
'Lower' Model specification describing terms that cannot be removed from model
'constant'(default)
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 modelspecnaming the model.
Example: 'Lower','linear'
Number of steps to take, specified as the comma-separated pair consisting of 'NSteps'and a positive
integer.
Improvement measure for adding a term, specified as the comma-separated pair consisting of 'PEnter'and
a scalar value. The default values are below.
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 6/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
Example: 'PEnter',0.075
Predictor variables to use in the fit, specified as the comma-separated pair consisting of 'PredictorVars'
and either a 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 character vectors should be among the names in tbl, or the names you specify using the 'VarNames'
name-value pair argument.
For example, you can specify the second and third variables as the predictor variables using either of the
following examples.
Example: 'PredictorVars',[2,3]
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 7/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
Improvement measure for removing a term, specified as the comma-separated pair consisting of 'PRemove'
and a scalar value.
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.
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 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',[4]
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 8/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
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 modelspecnaming the model.
Example: 'Upper','quadratic'
Names of variables in fit, specified as the comma-separated pair consisting of 'VarNames'and a cell array of
character vectors including the names for the columns of Xfirst, 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.
For example, if in your data, horsepower, acceleration, and model year of the cars are the predictor variables,
and miles per gallon (MPG) is the response variable, then you can name the variables as follows.
Example: 'VarNames',{'Horsepower','Acceleration','Model_Year','MPG'}
Control for display of information, specified as the comma-separated pair consisting of 'Verbose'and one of
the following:
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.
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 9/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
Linear model representing a least-squares fit of the response to the data, returned as a LinearModelobject.
For the properties and methods of the linear model object, mdl, see the LinearModelclass page.
Alternative Functionality
You can construct a model using fitlm, and then manually adjust the model using step, addTerms, or
removeTerms.
Terms Matrix
A terms matrix 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 plus one is for the response variable.
The value of T(i,j)is the exponent of variable jin term i. Suppose there are three predictor variables A, B,
and C:
The 0at the end of each term represents the response variable. In general,
If you have the variables in a table or dataset array, then 0must represent the response variable
depending on the position of the response variable. The following example illustrates this.
Load the sample data and define the dataset array.
load hospital
dsa = dataset(hospital.Sex,hospital.BloodPressure(:,1),hospital.Age,...
hospital.Smoker,'VarNames',{'Sex','BloodPressure','Age','Smoker'});
Represent the linear model 'BloodPressure ~ 1 + Sex + Age + Smoker'in a terms matrix. The
response variable is in the second column of the dataset array, so there must be a column of 0s for the
response variable in the second column of the terms matrix.
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 10/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
T=
0 0 0 0
1 0 0 0
0 0 1 0
0 0 0 1
Redefine the dataset array.
dsa = dataset(hospital.BloodPressure(:,1),hospital.Sex,hospital.Age,...
hospital.Smoker,'VarNames',{'BloodPressure','Sex','Age','Smoker'});
Now, the response variable is the first term in the dataset array. Specify the same linear model,
'BloodPressure ~ 1 + Sex + Age + Smoker', using a terms matrix.
T=
0 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
If you have the predictor and response variables in a matrix and column vector, then you must include 0for
the response variable at the end of each term. The following example illustrates this.
Load the sample data and define the matrix of predictors.
load carsmall
X = [Acceleration,Weight];
T=
0 0 0
1 0 0
0 1 0
1 1 0
0 2 0
Fit a linear model.
mdl = fitlm(X,MPG,T)
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 11/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
mdl =
Estimated Coefficients:
Estimate SE tStat pValue
(Intercept) 48.906 12.589 3.8847 0.00019665
x1 0.54418 0.57125 0.95261 0.34337
x2 -0.012781 0.0060312 -2.1192 0.036857
x1:x2 -0.00010892 0.00017925 -0.6076 0.545
x2^2 9.7518e-07 7.5389e-07 1.2935 0.19917
mdl =
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
Formula
A formula for model specification is a character vector of the form 'Y ~ terms'
where
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 12/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
Variable names
+means include the next variable
-means do not include the next variable
:defines an interaction, 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
Note: Formulas include a constant (intercept) term by default. To exclude a constant term from the model,
include -1in the formula.
For example,
Wilkinson Notation
Wilkinson notation describes the factors present in models. The notation relates to factors present in models,
not to the multipliers (coefficients) of those factors.
A+B A, B
A*B A, B, A*B
A:B A*Bonly
-B Do not include B
A*B + C A, B, C, A*B
A + B + C + A:B A, B, C, A*B
Statistics and Machine Learning Toolbox notation always includes a constant term unless you explicitly
remove the term using -1.
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 LinearModelobject, see LinearModel.
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 13/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
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.
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, stepwiselmuses 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.
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^2unless both X1and X2^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, it will not examine to remove X1or X2^2if X1:X2^2stays in the
model.
The default for stepwiseglmis '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.
Stepwise Regression
See Also
fitlm| LinearModel| step
Introduced in R2013b
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 14/15
2/3/2017 Create linear regression model using stepwise regression - MATLAB stepwiselm - MathWorks India
https://in.mathworks.com/help/stats/stepwiselm.html?requestedDomain=www.mathworks.com 15/15