www.gusucode.com > stats 源码程序 matlab案例代码 > stats/SpecifyModelUsingFormulaandSpecifyVariablesExample.m

    %% Stepwise Regression Using Specified Model Formula and Variables  
% 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. 
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 |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'}) 

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