How to predict output values of new data in stepwise regression?

3 views (last 30 days)
Dear Friends, I have generated a stepwise quadratic model using stepwiselm. At start of running the code, I have divided my training and tested data. After training (generating the model) I want to calculated output for tested data. I am trying to use predict command but unable to execute it. Could you please help me (maybe with an example).
  1 Comment
Ajay Goyal
Ajay Goyal on 29 Dec 2015
Thank You Proff Brendan for your help. It worked. Yesterday I was doing a silly mistake. Thanks once again for enlighten me from your knowledge

Sign in to comment.

Accepted Answer

Brendan Hamm
Brendan Hamm on 28 Dec 2015
rng('default') % Reproducibility
n = 200; % 200 samples
% Generate some data which is quadratic
x = 4*(rand(n,1)-0.5); % Samples from U([-2 2])
y = 1.2 + 7.8*x -2.6*x.^2 + 5*randn(n,1);
pt = cvpartition(n,'Holdout',0.5); % Create partition
trainX = x(training(pt)); % Get training predictor data
trainY = y(training(pt)); % Get training response data
testX = x(test(pt)); % Get validation predictor data
testY = y(test(pt)); % Get validation response data
% Plot test data:
plot(trainX,trainY,'ko')
% Fit model and predict:
mdl = stepwiselm(trainX,trainY,'y ~ 1','Criterion','BIC','Upper','y~1+x1^5');
predY = predict(mdl,testX);
% Plot predictions against known response
plot(testX,predY,'r*')
hold on
plot(testX,testY,'bo')
  2 Comments
Brendan Hamm
Brendan Hamm on 28 Dec 2015
If this is not working you may have made your own function called predict which shadows the method. What is happening when predict is "not working"? Is it an error, and if so what does it say?
Are you perhaps looking at the documentation for a different predict (there are many for different classes).
Ajay Goyal
Ajay Goyal on 29 Dec 2015
Thank You Sir for your help. It worked. Yesterday I was doing a mistake. Thanks for enlighten me from your knowledge

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!