SAS中文论坛

标题: 请问一个循环进行回归的问题,谢谢! [打印本页]

作者: shiyiming    时间: 2011-8-30 14:35
标题: 请问一个循环进行回归的问题,谢谢!
数据样表如下(实际数据是很大的):
对于表中的数据,在每个ID内,用X对Y进行循环回归。具体要求是:Num = i这条记录回归的结果是用Num = i-9,i-8,…, i 这10条记录的数据回归得到的。简单地说对于ID=001,我要得到Num=10, 11, …, 14的回归参数;对于ID = 002,要得到Num=10, 11, …, 17的回归参数。

不知道说清楚了没有,谢谢 <!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D -->  <!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D -->
data A;
   input ID Y X Num;
cards;
001 6 3 1
001 4 2 2
001 8 4 3
001 9 5 4
001 10 5 5
001 12 6 6
001 15 8 7
001 20 10 8
001 30 14 9
001 32 17 10
001 42 20 11
001 26 16 12
001 19 9 13
001 7 3 14
002 16 3 1
002 14 2 2
002 18 4 3
002 19 5 4
002 20 5 5
002 22 6 6
002 25 8 7
002 30 10 8
002 40 14 9
002 42 17 10
002 52 20 11
002 36 16 12
002 29 9 13
002 17 3 14
002 36 16 15
002 29 9 16
002 17 3 17;
作者: shiyiming    时间: 2011-8-30 21:56
标题: Re: 请问一个循环进行回归的问题,谢谢!
没有考虑数据很大。京剧

以前的帖子好像有这一类的东西。现在好像不是很有兴趣做这些
作者: shiyiming    时间: 2011-8-31 19:42
标题: Re: 请问一个循环进行回归的问题,谢谢!
我的数据有几百万条,想把这个程序跑完是不可能完成的任务呵 <!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D -->

有没有效率更高的办法呢?
作者: shiyiming    时间: 2011-9-1 22:30
标题: Re: 请问一个循环进行回归的问题,谢谢!
try this one
[code:44khth0f]
data A;
input ID Y X Num;
cards;
001 6 3 1
001 4 2 2
001 8 4 3
001 9 5 4
001 10 5 5
001 12 6 6
001 15 8 7
001 20 10 8
001 30 14 9
001 32 17 10
001 42 20 11
001 26 16 12
001 19 9 13
001 7 3 14
002 16 3 1
002 14 2 2
002 18 4 3
002 19 5 4
002 20 5 5
002 22 6 6
002 25 8 7
002 30 10 8
002 40 14 9
002 42 17 10
002 52 20 11
002 36 16 12
002 29 9 13
002 17 3 14
002 36 16 15
002 29 9 16
002 17 3 17
;
run;

%let n=9;
sasfile a load;
data rest0;
        set a;
                by id;
                array _x{4} _temporary_;
                array _a{2,&amp;n}  _temporary_;
                if first&#46;id then do;
                   do i=1 to dim(_x); _x&#91;i&#93;=0; end;
                   do i=1 to dim(_a, 1);
                      do j=1 to dim(_a, 2);
                             _a&#91;i, j&#93;=0;
                          end;
                   end;
                end;
                m=mod(num-1, &amp;n)+1;
                _a&#91;1, m&#93;=x; _a&#91;2,m&#93;=y;
                link filler;

                m2=mod(num-&amp;n, &amp;n)+1;
                if num&gt;=&amp;n then do;
                  if num&gt;&amp;n then do;
              link deduct;
                  end;
                  beta=(_x&#91;2&#93;-_x&#91;1&#93;*_x&#91;4&#93;/&amp;n)/(_x&#91;3&#93;-_x&#91;1&#93;**2/&amp;n);
                  intercept=_x&#91;4&#93;/&amp;n - beta*_x&#91;1&#93;/&amp;n;
                  keep  id num   intercept  beta  ;
                  output;
                end;
                return;      
filler&#58;
     _x&#91;1&#93;+x;
         _x&#91;2&#93;+x*y;
         _x&#91;3&#93;+x**2;
         _x&#91;4&#93;+y;
return;
deduct&#58;
     _x&#91;1&#93;=_x&#91;1&#93;-_a&#91;1,m2&#93;;
         _x&#91;2&#93;=_x&#91;2&#93;-_a&#91;1,m2&#93;*_a&#91;2,m2&#93;;
         _x&#91;3&#93;=_x&#91;3&#93;-_a&#91;1,m2&#93;**2;
         _x&#91;4&#93;=_x&#91;4&#93;-_a&#91;2,m2&#93;;
return;
run;
sasfile a close;

/* double check with standard approach */
proc reg data=a;
     where id=001 and num&lt;10;
         model y =x;
run;quit;
[/code:44khth0f]
作者: shiyiming    时间: 2011-9-2 14:42
标题: Re: 请问一个循环进行回归的问题,谢谢!
速度超快!

不过计算结果有点小问题,第一个估计的结果是对的,第二个开始就不对了。每一行的估计结果都是基于前面10条记录估计得到的。
我改了一下,如下:

%let n=10;
sasfile a load;
data rest0;
        set a;
      by id;
      array _x{4} _temporary_;
      array _a{2,&amp;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, &amp;n)+1;
      if num&lt;=&amp;n then do;
              _a[1, m]=x; _a[2,m]=y;
              link filler;
      end;

      if num&gt;=&amp;n then do;
        if num&gt;&amp;n then do;
                link deduct;
                        _a[1, m]=x; _a[2,m]=y;
                        link filler;
        end;
        beta=(_x[2]-_x[1]*_x[4]/&amp;n)/(_x[3]-_x[1]**2/&amp;n);
        intercept=_x[4]/&amp;n - beta*_x[1]/&amp;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;




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2