How can I specify "forward" or "backward" model selection in stepwiselm function?

7 views (last 30 days)
Hi,all,
I am using *stepwiselm* function in Matlab statistics and machine learning toolbox. The documentation says "stepwiselm uses forward and backward stepwise regression to determine a final model". But how can I explicitly inform forward or backward direction?

Answers (1)

Cindy Solomon
Cindy Solomon on 27 Jul 2015
Hi Boyi,
To do a forward selection, you can start with a model with no variables.
For example:
>> rng('default')
>> X = rand(20,7);
>> y = [ones(20,1),X(:,1),X(:,3),X(:,5)]*rand(4,1) + 0.1*randn(20,1);
>> stepwiselm(X,y,'Constant')
If there is a set of predictors that must be included in the model, you can use the 'Lower' name/value pair to specify it, and "stepwiselm" will add terms in addition to that, such as starting with the form:
>> lowerMdl = 'y ~ 1 + x1 + x2';
>> stepwiselm(X,y,lowerMdl,'lower',lowerMdl)
As an additional note, if you want to prevent "stepwiselm" from going backwards, you can set the 'PRemove' value to 1 in the Name/ Value pair arguments for "stepwiselm". Generally both forward and backward stepwise regression are both used to determine a final model- it terminates when no single step improves the model according to your criterion.
Hope this helps! -Cindy
  3 Comments
Carly Cocke
Carly Cocke on 11 Apr 2020
Hello Yajei, I am not 100% sure but it seems that the best way to allow only backwards movement in the model is to set the PEnter value to 0. By setting the PEnter value to zero, only p-values less than 0 can be added therefore no new predictors can be added. This should do the converse of Cindy's solution. I haven't tested it, so please take this advice as an idea instead of a solution.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!