Solved
New Contributor
Posts: 4

# Moving window for AR(1)

[ Edited ]

Hello !

I have to create a moving window for an AR(1) in SAS/IML . I don't know how to create my loop.

I wrote an AR(1) (And it is ok) :

``````Ya=lag(Xa);
Y=Ya[2:nrow(Ya)];
X=Xa[2:nrow(Xa)];
B=inv(t(X)*X)*t(X)*Y;
end;``````

Xa is my vector data.

So how to do a moving window in half of the data? I'm a beginer in SAS IML and I have not achieved the loop.

I tried like that :

``````Ya=lag(Xa);
Roll=(nrow(Ya)-1)/2;
do i=1 to Roll;
Y[i]=Ya[i+1:i+Roll];
X[i]=Xa[i+1:i+Roll];
B[i]=inv(t(X[i])*X[i])*t(X[i])*Y[i];
end;``````

But it doesnt work and I feel like it's completely wrong and stupid ^^.

Could you help me please ?

Thank you very much !

Accepted Solutions
Solution
‎01-21-2016 11:39 AM
SAS Super FREQ
Posts: 4,237

## Re: Moving window for AR(1)

I now see that your ORIGINAL variables are called Xa and Ya, and that you are using X and Y for the local (moving) regression. I'm sorry for the confusion. Try this:

``````Proc iml;
Xa = {1, 2, 3, 4, 5, 6};
Ya = {2, 3, 4 ,5, 6, 7};

k = 3;
B = j(nrow(Xa)-k, 1);   /* allocate vector for results */
do i = k to nrow(Xa);
j = i-k+1;    /* first element */
idx = j:i;
X = Xa[idx];
Y = Ya[idx];
B[j]=inv(t(X)*X)*t(X)*Y;
end;
print B;

``````

Since you are new to SAS/IML programming, here are ten tips for learning the SAS/IML language. You might also want to read the paper "Getting started with the SAS/IML language."  Good luck!

All Replies
SAS Super FREQ
Posts: 4,237

## Re: Moving window for AR(1)

I'm sorry but I do not understand your question well enough to respond.  Let's use some example data.

``````proc iml;
X = { 0, 1, 2, 3, 4, 5};
Y = {11,12,13,12,11,10};``````

For this example N=6, so you want a rolling window of length 3.  Is this a backward window? Centered? Forward?

One way to interpret your question is to use backward windows and say that B will have 4 elements and that

B[1] is the coefficient of the linear regression that uses data points 1:3

B[2] is the coefficient of the linear regression that uses data points 2:4

B[3] is the coefficient of the linear regression that uses data points 3:5

B[4] is the coefficient of the linear regression that uses data points 4:6

A loop that implements that condition would look like the following:

``````k = 3;
do i = k to nrow(X);
idx = (i-k+1):i;
Xa = X[idx];
Ya = Y[idx];
/* compute and store B here */
end;``````
New Contributor
Posts: 4

## Re: Moving window for AR(1)

[ Edited ]

Thank you very much for your answer ! My english isn't really good and I'm a bit lost so I'm happy you understood my pb .

It was exactly what I expected : I need a backward windows.

Unfortunately I didn't solve my problem yet :

M data is like :

``````Proc iml;
X = {1, 2, 3, 4, 5, 6];
Y = {2, 3, 4 ,5, 6, 7};``````

I'm using you're SAS code :

``````k = 3;
do i = k to nrow(X);
idx = (i-k+1):i;
Xa = X[idx];
Ya = Y[idx];``````

That souds perfect.

But when I try to calculate my 4 differents Beta I have a new pb :

The B without any loop is like that :

``B=inv(t(X)*X)*t(X)*Y;``

But in my case I need to use Xa and Ya and I need to have a vector of 4 different beta.

Do you know how to do that ?

Thank you so much for your help

Solution
‎01-21-2016 11:39 AM
SAS Super FREQ
Posts: 4,237

## Re: Moving window for AR(1)

I now see that your ORIGINAL variables are called Xa and Ya, and that you are using X and Y for the local (moving) regression. I'm sorry for the confusion. Try this:

``````Proc iml;
Xa = {1, 2, 3, 4, 5, 6};
Ya = {2, 3, 4 ,5, 6, 7};

k = 3;
B = j(nrow(Xa)-k, 1);   /* allocate vector for results */
do i = k to nrow(Xa);
j = i-k+1;    /* first element */
idx = j:i;
X = Xa[idx];
Y = Ya[idx];
B[j]=inv(t(X)*X)*t(X)*Y;
end;
print B;

``````

Since you are new to SAS/IML programming, here are ten tips for learning the SAS/IML language. You might also want to read the paper "Getting started with the SAS/IML language."  Good luck!

New Contributor
Posts: 4

## Re: Moving window for AR(1)

It's working now ! Thank you !

SAS Super FREQ
Posts: 4,237

## Re: Moving window for AR(1)

Great.  But I think you meant to select my answer as the sollution, rather than your reply. :-)

New Contributor
Posts: 4

## Re: Moving window for AR(1)

I'm getting tired I guess :')
🔒 This topic is solved and locked.