作者tew (咖啡王子)
站内Statistics
标题Re: rolling regression (但也不完全算是=.=)
时间Mon Apr 13 01:47:51 2009
以下程式是根据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)