I would like to calculate the rolling average of the estimated beta coefficient via statsby (say "_b_LogSize") weighted by their t-statistics (say "t_LogSize"). A 5 months rolling window would be fine to start with. I am trying to get more stable coefficient, the intuitions is that we want to give more importance to less noisy beta estimates (hence higher t-stat) in the rolling window average.

Would you be able to help me code that? Thanks.


Code:
 
 * Example generated by -dataex-. To install: ssc install dataex clear input float(date _b_LogSize t_LogSize abs_t_LogSize tsum_LogSize tw_LogSize) 480   -8.266647   -4.911039   4.911039         .          . 481    2.581961    2.566609   2.566609         .          . 482    1.255753    1.632749   1.632749         .          . 483    .8442108   1.1369855  1.1369855         .          . 484  -3.3915694  -3.5551646  3.5551646 13.802547  .25757307 485  -1.0104964  -1.3168218  1.3168218  10.20833  .12899484 486   -1.903375  -2.2898364  2.2898364  9.931558  .23056166 487    .4738106    .6090372   .6090372 8.9078455  .06837087 488   -.0876909  -.11545384  .11545384  7.886314 .014639772 489   1.1190147    1.424378   1.424378  5.755527     .24748 490  -1.2875426   -1.538554   1.538554  5.977259  .25740126 491   -4.066994   -3.895815   3.895815  7.583238   .5137403 492   -.3479997    -.489352    .489352  7.463553  .06556556 493     .777527   1.1554649  1.1554649  8.503564  .13588007 494   -3.302649  -3.8024635  3.8024635  10.88165   .3494382 495  -1.8951005   -3.518439   3.518439 12.861534   .2735629 496    -3.67513   -5.062582   5.062582   14.0283   .3608835 497   .07620832   .13785669  .13785669 13.676805 .010079597 498   -.4320115   -.9034046   .9034046 13.424746  .06729398 499    2.646034    3.619975   3.619975 13.242257   .2733654 500   -6.142633   -7.235136   7.235136 16.958954   .4266263 501   -2.587543   -3.311952   3.311952 15.208324  .21777233 502  -2.1349971  -4.4263663  4.4263663 19.496834     .22703 503    .4620627     .861635    .861635 19.455065  .04428846 504   1.2540562    2.224489   2.224489 18.059578  .12317502 505   -2.555257   -5.383336   5.383336 16.207779   .3321452 506   .13377367   .23725756  .23725756 13.133084 .018065639 507    1.979651   4.3510914  4.3510914  13.05781   .3332176 508    .6884593    1.247736   1.247736  13.44391   .0928105 509    .4770622    .6950083   .6950083  11.91443  .05833333 510   -.4007422   -.7961677   .7961677  7.327261   .1086583 511   -.9754319  -1.9351906  1.9351906  9.025194  .21442093 512    .6744914    .8957349   .8957349  5.569838  .16081886 513   -5.285298   -5.344624   5.344624  9.666725   .5528888 514    .4946819    .9643504   .9643504  9.936068  .09705554 515   -.3796268   -.7855311   .7855311  9.925431  .07914327 516 -.033686493   -.0726955   .0726955  8.062936 .009016009 517  -1.7550406   -3.186371   3.186371 10.353572   .3077557 518   -.3205542   -.6093335   .6093335  5.618281   .1084555 519   -5.138365   -7.682914   7.682914 12.336844   .6227617 520   -.3349997   -.8906261   .8906261  12.44194  .07158258 521   -2.908237   -5.094873   5.094873 17.464117  .29173377 522   -.7331108   -1.782251   1.782251 16.059998  .11097455 523   -1.350431   -3.309204   3.309204 18.759869  .17639804 524  -1.0057278  -2.1181622  2.1181622 13.195116  .16052623 525  -1.0435315  -2.6591425  2.6591425 14.963633    .177707 526    .6656214   1.9491335  1.9491335 11.817893   .1649307 527  -1.0886611    -2.61192    2.61192 12.647563   .2065157 528   -.6431454  -1.8611777  1.8611777 11.199536  .16618346 529  -1.2877115   -4.164419   4.164419 13.245793   .3143956 530  -.07760277  -.18290086  .18290086  10.76955 .016983146 531   -.7118146  -1.9242047  1.9242047 10.744623  .17908536 532   -.6422755  -2.0677242  2.0677242 10.200427  .20270957 533   .19157185    .5342802   .5342802  8.873529  .06021056 534   -.2652633   -.7714291   .7714291  5.480539  .14075789 535   -1.318615  -3.7864094  3.7864094  9.084047   .4168196 536   -.8856038  -2.0179932  2.0179932  9.177836   .2198768 537  -1.7783096   -5.388499   5.388499 12.498611  .43112785 538   -.9958586   -3.248513   3.248513 15.212844   .2135375 539    .6375813   1.9973515  1.9973515 16.438766  .12150252 540    .1909554    .5236455   .5236455 13.176003  .03974237 541  -.17884855    -.613762    .613762 11.771771  .05213846 542  -.55945045  -1.5065902  1.5065902  7.889862  .19095267 543   -1.655147   -4.741406   4.741406  9.382755  .50533193 544  -1.4834945    -4.56018    4.56018 11.945584   .3817461 545   -.9255059   -2.598146   2.598146 14.020084    .185316 546 -.022075985 -.072398596 .072398596  13.47872 .005371326 547    .1892124    .5711942   .5711942 12.543324   .0455377 548   -.3425543   -.9594861   .9594861  8.761405   .1095128 549   -.9611792   -2.651947   2.651947  6.853171  .38696635 550   -.6658006  -2.2426708  2.2426708  6.497696   .3451486 551   -2.314715   -5.489883   5.489883  11.91518   .4607469 552   -.6898829  -2.0486205  2.0486205 13.392607   .1529665 553   -1.823041   -5.690817   5.690817 18.123938   .3139945 554    .9535037    3.016883   3.016883 18.488874   .1631729 555   -.1609003   -.4754425   .4754425 16.721645 .028432757 556   .18754247    .6115062   .6115062  11.84327  .05163323 557   1.0243611    2.611286   2.611286 12.405935  .21048684 558   -.7195773  -2.1321523  2.1321523   8.84727   .2409955 559   -.8113478  -2.3689744  2.3689744  8.199362   .2889218 560  -1.6538635   -5.002859   5.002859 12.726778   .3930971 561  -1.0327011  -3.5210226  3.5210226 15.636294  .22518267 562   .54082316   2.1440485  2.1440485 15.169057  .14134356 563   -1.244109  -4.0540304  4.0540304 17.090935  .23720355 564  -1.0785816   -3.872051   3.872051 18.594011  .20824185 565    .1194961    .4390831   .4390831 14.030236  .03129549 566   .20267415    .6495556   .6495556  11.15877  .05821033 567  -.23517534   -.7762432   .7762432  9.790963   .0792816 568    .1605866   .56066215  .56066215  6.297596  .08902797 569    .2715707    .7454796   .7454796  3.171024  .23509115 570   -.6019359   -1.850141   1.850141  4.582082   .4037774 571     .744113   2.2486794  2.2486794  6.181205  .36379305 572   -.7933149   -1.911649   1.911649  7.316611   .2612752 573    .7634989   1.8988053  1.8988053  8.654755  .21939446 574   -.3443597   -.9029207   .9029207  8.812196  .10246262 575    -.299389   -.6598354   .6598354   7.62189   .0865711 576   -.8753999  -2.0526454  2.0526454  7.425856  .27641872 577   .08494118   .22103527  .22103527  5.735242  .03853983 578   -.8450174  -2.0233812  2.0233812  5.859818   .3452976 579   -1.264442  -3.2563105  3.2563105  8.213208   .3964724 end format %tm date