Re: rolling regression (但也不完全算是=.=)
以下程式是根據liton的提示所寫的
不過liton版友的提示應該是屬於moving window的做法
我將他改為rolling regression method
附上我說的虛擬變數跑法 以及正規邏輯上的跑法
你可以驗證一下設虛擬變數跟代入的結果
/*以下是代入虛擬變數的做法*/
data original;
do year=1995 to 2009;
x=rannor(0);
y=3+2*x+rannor(0);
/*這邊只是要弄個模型出來跑迴歸*/
output;
end;
run;
data subsample;
set original;
do i=2000 to 2008;
if year<=i +1
/* 因為下一年是要用來做預測的 所以才有+1 */
then output;
end;
run;
proc sort ;by i;
run;
data subsample;
set subsample;by i;
if last.i then res=1;else res=0;
/*此處產生虛擬變數 res */
run;
proc reg data=subsample noprint outest=beta;
model y=x res;
by i;
run;
quit;
proc print data=beta(keep=res);
run;
/*以下是正規做法 跑出迴歸係數值帶入最後一年資料*/
data subsample;
set original;
do i=2000 to 2008;
if year<=i
/*這邊只是拿出來跑迴歸的數據*/
then output;
end;
run;
proc sort ;by i;
run;
proc reg data=subsample noprint outest=beta (keep=i intercept x);
model y=x ;
by i;
run;
quit;
data subsample;
set original;
rename x=xx;
/*由於迴歸係數裡有x合併會出錯 所以改名*/
do i=2000 to 2008;
if year=i +1
/*使用下一年的資料來做預測*/
then output;
end;
run;
proc sort ;by i;
run;
data beta2;
merge beta subsample;by i;
res=y-intercept-x*xx;
/*殘差就是這樣算*/
keep res;
run;
proc print data=beta2;
run;
/*接下來就可以比較結果是否一樣了*/
※ 引述《liton (歐吉桑留學生)》之銘言:
: ※ 引述《stephanieyou ()》之銘言:
: : 然後再用以上的資料跑regression 同樣的只要第七年的predicted value residual
: : 然後沿路放新的年進來,但不刪除之前的data..所以regression用的data period會增加
: : 我不會寫 >"<
: : 目前唯一想到的辦法是一行一行寫subsample orz
: 我時間不多
: 講個大概的 其他你自己想辦法
: 如果你要寫subsample
: 那麼Macro是跑不了的
: data subsample;
: set original;
: do i=1995 to 2009;
: if (year ge i) and (year le i+10)
: then output;
: end;
: run;
: 將這個subsample程式和regression還有predicted value residual塞在一起
: 寫段macro
: 然後用ods取出值
: 不過..
: 我想rolling windows是計量上很普遍作法(另一種是recursive)
: 你自己在sas裡面搜尋rolling
: 應該會有你要的
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.225.32.247
※ 編輯: tew 來自: 125.225.32.247 (04/13 11:22)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 6 篇):