BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lumanitoc
Fluorite | Level 6

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 !

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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!

View solution in original post

6 REPLIES 6
Rick_SAS
SAS Super FREQ

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;
Lumanitoc
Fluorite | Level 6

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

Rick_SAS
SAS Super FREQ

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!

Lumanitoc
Fluorite | Level 6

It's working now ! Thank you !

I'll read your paper 

 

 

 

 

Rick_SAS
SAS Super FREQ

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

Lumanitoc
Fluorite | Level 6
I'm getting tired I guess :')

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 6 replies
  • 1497 views
  • 2 likes
  • 2 in conversation