Running a validation model using Stepwisefit

조회 수: 4 (최근 30일)
Robert
Robert 2015년 7월 18일
답변: Sruthi Ayloo 2015년 7월 21일
I have produced the following code. It does 1505 stepwise regression with 8 predictor variables (a regression for every pixel of a map). As you can see it runs 2 regressions, 1 using all of the predictor variables and one using only predictor variables deemed to be in the model (INMODEL output from regression).
The regressions use data 1:127 from the input data.
Now I want to produce a validation set. Basically I want to run the same regression but using new data (128:144). It should only use variables deemed to be in the model in the first stepwise regression.
I am unsure of how to proceed. Any advice would be greatly appreciated
%Sort the Data
[rows, cols]=size(A11);
% initialize matrix to store the data
pvalues=zeros(rows, cols)-1;
coefficients=zeros(rows,cols);
for i = 1:rows
for j = 1:cols
y=zeros(8,1)
x=zeros(8,8)
for m=1:127 %number of months
y(m)=All((m-1)*rows+i,j);
x(m,1)=Approx1((m-1)*rows+i,j);
x(m,2)=Approx2((m-1)*rows+i,j);
x(m,3)=Approx3((m-1)*rows+i,j);
x(m,4)=Approx4((m-1)*rows+i,j);
x(m,5)=Dpprox1((m-1)*rows+i,j);
x(m,6)=Dpprox2((m-1)*rows+i,j);
x(m,7)=Dpprox3((m-1)*rows+i,j);
x(m,8)=Dpprox4((m-1)*rows+i,j);
x(m,9)=1;
end
%Run the stepwise Regression
if sum(sum(isnan(x)),2)==0 && sum(isnan(y))==0
xx=x(:,1:9); yy=y(:,1);
[B,SE,PVAL,INMODEL,STATS,NEXTSTEP,HISTORY]= ...
stepwisefit(xx,yy,'penter',.05);
inApprox1(i,j)=INMODEL(1); %Find variables that are in the model
inApprox2(i,j)=INMODEL(2);
inApprox3(i,j)=INMODEL(3);
inApprox4(i,j)=INMODEL(4);
inDpprox7(i,j)=INMODEL(5);
inDpprox8(i,j)=INMODEL(6);
inDpprox9(i,j)=INMODEL(7);
inDpprox10(i,j)=INMODEL(8);
sstotApprox1(i,j)=STATS.SStotal; %calculate R^2
ssresidApprox1(i,j)=STATS.SSresid;
rsq = 1- ssresidApprox1./sstotApprox1
rsq(rsq==Inf) = NaN %Set Inf to NaN
rmse(i,j)=STATS.rmse; %Extract rmse
rmse(rmse==Inf) = NaN %Set Inf to NaN
% repeat regresson only on the sigificant variables
xip=0;
for k=1:9 %9 refers to previous 9 variables including intecept
if INMODEL(1,k)==1
xip=xip+1;
xxn(:,xip)=xx(:,k);
end
end
[Bn,SEn,PVALn,INMODELn,STATSn,NEXTSTEPn,HISTORYn]= ...
stepwisefit(xxn,yy,'penter',.05);
rmsen(i,j)=STATSn.rmse; %Extract rmse
rmsen(rmse==Inf) = NaN %Set Inf to NaN
end
end
end

답변 (1개)

Sruthi Ayloo
Sruthi Ayloo 2015년 7월 21일
Hi Robert,
I understand that you want to run the stepwise regression on your validation set using a different dataset (128:144). I see that your script has the following line where it takes in the input data:
for m =1:127 % number of months
One way to do is you can create a function which takes in an input and performs stepwise regression. This input can either be the train data or the validation data or the test data and this function can be called in your script. You can either declare the variables deemed to be in the model in the first stepwise regression as global so that they can be used in the function or you can return them as function output values.
Scripts Vs Functions discusses the differences between scripts and functions and shows how to convert a script to a function.
Functions - has more information on how to declare functions and call them.
Hope this helps.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by