我有一些运行良好的代码,它将估计在matlab中的滚动窗口回归,请参见下文:
我遇到的问题是我想在回归中添加另一个for循环,以估计不同的回归。
例如,
Y_{t}i = \alpha + \beta_1i Y_{t-1}i + \beta_2j Yj
for i = 1:6
for j = 1:6
if i does = j.
我可以添加滞后变量并运行所需的回归。
但是如何存储上面的for循环的结果?
代码1:
Y=rand(211,10); %replace with dependant variable values, NOTE: Y cannot be a matrix; I have worked around it by creating a higher loop which goes through each col of Y and repeats the analysis
X=rand(211,1); %replace with independant variable values,X can be a vector or matrix, if latter then update regress as well!
steps=size(Y,1); % this function gives you the size of the colume vector
d=24;%this is discrete window length you wish to regress over - Change as per requirements
p=1; %number of regressors - Change as per requirements
adjustment=(d-1)/(d-p-1); % regressor adjustment for multiple regression
j=1; %increment level so in this case we are running a rolling regression with a fixed window length of 24 data points that updates with 1 point increment. The loop will work for j>1 as well.
for k=1; % this is for the dependant variable so 1=col 1 and so on..increase k as per requirements
for i=1:j:steps-d;
[b bint r rint stats]=regress(Y(i:23+i,k),[ones(24,1),X(i:23+i,1)]);
Alpha(i,k)=b(1);
Beta(i,k)=b(2);
R_Squared(i,k)=stats(1); %stats provides other information as well such as F-stat;p-value etc that can be added to this easily
%Adj_R_Squared(i)=1-(1-R_Squared(i))*adjustment; %Use this when the number of regressors is greater than 1
error(:,i,k)=r(:,1); %if k>1 then the residulas will be 3D but are easily understood based on the matrix structure
CI_Alpha(i,:,k)=bint(1,:);
CI_Beta(i,:,k)=bint(2,:);
if CI_Alpha(i,1,k)*CI_Alpha(i,2,k)<0;
Significance_Alpha(i,k)=0;
else
Significance_Alpha(i,k)=1;
end
if CI_Beta(i,1,k)*CI_Beta(i,2,k)<0;
Significance_Beta(i,k)=0;
else
Significance_Beta(i,k)=1;
end
end
end
%Graphs
subplot(3,2,1),plot(Alpha); legend('Rolling_Alpha')
subplot(3,2,2),plot(Beta); legend('Rolling_Beta')