|
5#

楼主 |
发表于 2011-9-2 14:42:43
|
只看该作者
Re: 请问一个循环进行回归的问题,谢谢!
速度超快!
不过计算结果有点小问题,第一个估计的结果是对的,第二个开始就不对了。每一行的估计结果都是基于前面10条记录估计得到的。
我改了一下,如下:
%let n=10;
sasfile a load;
data rest0;
set a;
by id;
array _x{4} _temporary_;
array _a{2,&n} _temporary_;
if first.id then do;
do i=1 to dim(_x); _x[i]=0; end;
do i=1 to dim(_a, 1);
do j=1 to dim(_a, 2);
_a[i, j]=0;
end;
end;
end;
m=mod(num-1, &n)+1;
if num<=&n then do;
_a[1, m]=x; _a[2,m]=y;
link filler;
end;
if num>=&n then do;
if num>&n then do;
link deduct;
_a[1, m]=x; _a[2,m]=y;
link filler;
end;
beta=(_x[2]-_x[1]*_x[4]/&n)/(_x[3]-_x[1]**2/&n);
intercept=_x[4]/&n - beta*_x[1]/&n;
keep id num intercept beta ;
output;
end;
return;
filler:
_x[1]+x;
_x[2]+x*y;
_x[3]+x**2;
_x[4]+y;
return;
deduct:
_x[1]=_x[1]-_a[1,m];
_x[2]=_x[2]-_a[1,m]*_a[2,m];
_x[3]=_x[3]-_a[1,m]**2;
_x[4]=_x[4]-_a[2,m];
return;
run;
sasfile a close; |
|