[ Retour aux Travaux Dirigés de SAS ]
/* Séries temporelles avec SAS, Corrigé du TD 2 */
/* Jean-Sebastien Roy (js@jeannot.org), 2000-2001 */
/* Premier jeu de données */
data serie;
do t=1 to 10*12;
x=sin(t*(2*3.14)/24)+sin(t*(2*3.14)/(24*5))*3;
xb=x+sin(t*(2*3.14)/3.3)*5;
output;
end;
run;
symbol1 join black none;
symbol2 join red none;
symbol3 join green none;
symbol4 join blue none;
symbol5 join orange none;
symbol6 join pink none;
proc gplot serie;
plot x*t xb*t / overlay;
run;
quit;
/* Moyenne mobile */
/* Avec des lags */
data mlag;
set serie;
a3=(xb+lag(xb)+lag2(xb))/3;
xb=lag(xb);
x=lag(x);
t=lag(t);
run;
proc gplot mlag;
plot (x xb a3)*t/ overlay legend;
run;
quit;
/* Avec expand */
proc expand serie xlag;
id t;
convert xb=a3 / (cmovave 3);
convert xb=a5 / (cmovave 5);
convert xb=a13 / (cmovave 13);
convert xb=a23 / (cmovave 23);
run;
proc gplot xlag;
plot (x xb a3 a5 a13 a23)*t / overlay legend;
run;
quit;
/* Moyenne mobile de henderson */
data xlagh;
set serie;
h13=(
-325*xb
-468*lag1(xb)
+1100*lag3(xb)
+2475*lag4(xb)
+3600*lag5(xb)
+4032*lag6(xb)
+3600*lag7(xb)
+2475*lag8(xb)
+1100*lag9(xb)
-468*lag11(xb)
-325*lag12(xb))/16796;
t=lag6(t);
x=lag6(x);
xb=lag6(xb);
if h13 ne .;
run;
proc gplot xlagh;
plot (x xb h13)*t / overlay legend;
run;
quit;
/* Moyenne mobile de Henderson avec expand */
proc expand serie xlag2;
id t;
convert xb=a5_5_5 / (cmovave 5 cmovave 5 cmovave 5);
convert xb=h13_p / (cmovave (0 0 0 1100 2475 3600 4032 3600 2475 1100 0 0 0));
convert xb=h13_n / (cmovave (325 468 0 0 0 0 0 0 0 0 0 468 325));
run;
data xlag2;
set xlag2;
h13=(h13_p*(+1100 +2475 +3600 +4032 +3600 +2475 +1100)
-h13_n*(+325 +468 +468 +325))/16796;
run;
proc gplot xlag2;
plot (x xb a5_5_5 h13)*t / overlay legend;
run;
quit;
/* Annulation d'une composante */
data serie2;
do t=1 to 10*12;
x=sin(t*(2*3.14)/(17));
xb=x+sin(t*(2*3.14)/7)*2;
output;
end;
run;
proc expand serie2 xlag3;
id t;
convert xb=a3 / (cmovave 3);
convert xb=a5 / (cmovave 5);
convert xb=a7 / (cmovave 7);
convert xb=a13 / (cmovave 13);
run;
proc gplot xlag3;
plot (x xb a3 a5 a7 a13)*t / overlay legend;
run;
quit;
/* Analyse d'une moyenne mobile */
%let 30;
data one;
x=0;
do t=-&width to -1;
output;
end;
t=0;x=1;output;
x=0;
do t=1 to &width;
output;
end;
run;
proc expand one weights;
id t;
convert x=a5_7_13 / (cmovave 5 cmovave 7 cmovave 13);
convert x=s / (cmovave (1 2 2 2 1));
convert x=a17 / (cmovave 17);
convert x=h13_p / (cmovave (0 0 0 1100 2475 3600 4032 3600 2475 1100 0 0 0));
convert x=h13_n / (cmovave (325 468 0 0 0 0 0 0 0 0 0 468 325));
run;
data weights;
set weights;
h13=(h13_p*(+1100 +2475 +3600 +4032 +3600 +2475 +1100)
-h13_n*(+325 +468 +468 +325))/
(-325 -468 +1100 +2475 +3600 +4032 +3600 +2475 +1100 -468 -325);
drop h13_p h13_n;
run;
proc gplot weights;
plot (a5_7_13 s a17 h13)*t / overlay legend;
run;
quit;
/* Reduction d'un bruit blanc */
data bb;
do t=1 to 100;
x=rannor(0);output;
end;
run;
proc expand bb xlag4;
id t;
convert x=a3 / (cmovave 3);
convert x=a44 / (cmovave 4 cmovave 4);
convert x=a5 / (cmovave 5);
convert x=a7 / (cmovave 7);
convert x=a17 / (cmovave 17);
convert x=m5 / (cmovmed 5);
convert x=s / (cmovave (1 2 2 2 1));
run;
proc gplot xlag4;
plot (x a3 a44 a5 a7 m5)*t / overlay vref=0 legend;
plot (x s a17)*t / overlay vref=0 legend;
run;
quit;
proc summary xlag4 print var;
var x a3 a44 a5 a7 m5 s;
run;
/* Tau (Slutsky-Yule) */
proc transpose weights tweights
( (_name_=nom col1=w));
by t;
run;
proc sort tweights;
by nom t;
run;
data tau;
set tweights;
ttm1 = w*lag(w);
t2 = w*w;
run;
proc sql;
select nom,2*3.1415/arcos(sum(ttm1)/sum(t2))
from tau
group by nom;
quit;