Re: rolling regression (但也不完全算是=.=)
剛好同學有做到這個東西
以下是寫好的小程式
input 日期 個股、大盤報酬 週轉率(用不到)
假設是用前三筆資料跑回歸 算出第四筆的殘差
data one;
input date Ri Rm turnover;
cards;
200510 -13.75 0.9 -5.79
200511 4.76 1.57 7.62
200512 0.99 1.9 5.56
200601 0.39 1.4 -0.25
200602 10.14 1.39 0.45
200603 -5.49 1.04 0.8
200604 27.34 -0.23 8.43
200605 -2.94 1.91 -4.53
200606 7.88 1.3 -2.08
200607 -4.35 1.23 -3.73
200608 -9.15 0.16 2.44
200609 6.84 1.22 4.1
200610 -3.45 1.45 2.01
200611 13.27 0.75 7.78
200612 5.41 2.1 3.38
;
run;
data two;
set one;
keep date Ri Rm;
proc sort; by date;
run;
data three;
set two;
Y1=lag(Ri);
Y2=lag2(Ri);
Y3=lag3(Ri);
X1=lag(Rm);
X2=lag2(Rm);
X3=lag3(Rm);
retain count 0;
count=count+1;
run;
data four; /*expand each observation*/
set three;
do i=1 to 3;
output;
end;
run;
data five; /*replace i & j column with lag1, lag2, lag3*/
set four;
j=i;
if i=1 then i=Y1;
if i=2 then i=Y2;
if i=3 then i=Y3;
if j=1 then j=X1;
if j=2 then j=X2;
if j=3 then j=X3;
run;
這作法是擴張資料
之後 就可以跑每一期的回歸(by count) 只到資料處理的部份
缺點:資料會膨風 你需要LAG幾期 就會膨風幾倍
如果你的是年資料 那應該就還好
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 134.208.29.101
※ 編輯: flyindanger 來自: 134.208.29.101 (04/13 10:44)
※ 編輯: flyindanger 來自: 134.208.29.101 (04/13 15:08)
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 5 之 6 篇):