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 !
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!
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;
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
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!
It's working now ! Thank you !
I'll read your paper
Great. But I think you meant to select my answer as the sollution, rather than your reply. 🙂
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.