From: Anthony on
Hey folks,

I am trying to create a structure with a loop so I can use each element of the structure for an OLS regression to calculate the parameters of an AR(p):


p = 2; % # of lags
y = FX(p+1:end,:); % 334x4
x = ones(size(y));
for i =1:size(y,2)
a.i = [x(:,i), y(p+1-i:end-i,i)];
end
(THIS DOESN'T WORK)

The above doesn't work. I get a horzcat error. I don't necessarily want a structure output, it just seemed like the easiest way to go about it.
Each element of the structure has to have 3 columns. The first being the columns of ones from x. The second column is the 1st column of y lagged by 1. And the third column is the 1st column of y, lagged by 2.

And so the 2nd structure element, has a column of ones followed by the second column of y lagged by 1 and the second column of y lagged by 2.

And so on, until 4. I hope this is clear.

I am able to do this for one time series by itself, thanks to Nick, but I don't want to just copy and paste it 4 times for each series:

yCAD = FX(p+1:end,1);
xCAD = ones(size(FX,1)-p,1);
for i = 1:p
xCAD = [xCAD, FX(p+1-i:end-i,1)];
end
coeffsCAD = inv(xCAD'*xCAD)*(xCAD'*yCAD);
(THIS WORKS)

Thanks for your help.

Anthony
From: us on
"Anthony " <antfarinaccio(a)gmail.comremove.spam> wrote in message <htp4ve$hr5$1(a)fred.mathworks.com>...
> Hey folks,
>
> I am trying to create a structure with a loop so I can use each element of the structure for an OLS regression to calculate the parameters of an AR(p):
>
>
> p = 2; % # of lags
> y = FX(p+1:end,:); % 334x4
> x = ones(size(y));
> for i =1:size(y,2)
> a.i = [x(:,i), y(p+1-i:end-i,i)];
> end
> (THIS DOESN'T WORK)

no - it does not...

one of the many solutions

clear s; % <- save old stuff
for i=1:3
fn=sprintf('r_%2.2d',i);
s.(fn)=magic(i); % <- use your filler...
end
disp(s);
%{
% s.
r_01: 1
r_02: [2x2 double]
r_03: [3x3 double]
%}

us