Rolling stepwise regression within each regression window
Show older comments
I want to run a backward stepwise regression within each regression window. Basically, I have a Matrix Y(137x84) and a Matrix X(137x612). For each of the 84 Y's I want to check which of the 612 factors are significant within the a sliding window of 24 months and after that associate to the logical Matrix obtained the corresponding Xi. I wrote the following code to do that
b=zeros(K,N);
se=zeros(K,N);
pval=zeros(K,N);
inmodel=zeros(N,K);
win=24
for i=1:N
dep_Step=Y(:,i);
indep_Step=X(:,i);
for t=win:T
x_roll_step=indep_Step(t-(win-1):t,:);
y_roll_step=dep_Step(t-(win-1):t);
[b(:,t),se(:,t),pval(:,t), inmodel(t,:), stats(t)]=stepwisefit(y_roll_step,x_roll_step);
end
i=i+1
end
cumulative=cumsum(inmodel,2);
A_i=cell(1,N);
match=zeros(T,K);
for i=1:N
for j=1:K
if inmodel(i,j)==1
match(:,j)=X(:,j);%match 1 with the correspondent factor
match( :, ~any(match,1) ) = []; %delete zero column from each matrix on each cell
end
A_i{i}=match; %store within the cell
end
match=zeros(T,K);
i=i+1
end
but the output A_i is just a structure with Matrix filled with zeros. Do you have any suggestion on what is wrong with above code?
Many thanks for your help.
1 Comment
Federico Frisaldi
on 2 Aug 2017
Answers (0)
Categories
Find more on Linear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!