Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to calculate the residual of last 3 months using the regression coefficient of this month

    Hello everyone!

    I have a problem with calculating the residual, the calculation following the steps below:
    In each month t,I should:
    1. firstly, use daily data from month t-2 through month t to run a FamaFrench 3 factor regression, with formula: excessreturn = b_cons + b_hml*hml + b_smb*smb + b_rmrf*rmrf,
    2. define the residual of month t-2 to month t using the regression coefficients that are estimated in step 1,
    3. calculate the sd and skewness of month t using the residual defined in step 2, only one sd and one skewness should be computedf or each month,
    4. following the same steps for next month t+1.
    For example, in jan1989, I run a FF regression using daily data for days from the first day of nov1988 through the end of jan1989, the regression coefficient are estimated, and the daily residuals are calculated using the regression coefficient, the sd and the skewness of jan 1989 are computed using the daily residual from nov1988 to jan1989. In feb1989, I run a FF regression using daily data for days from the first day of dec1988 through the end of feb1989, the regression coefficient are estimated, the residuals of dec1988 to feb1989, the sd and skewness of feb1989 are computed as above.
    Many thanks for your help and time, I will be very grateful if you can give some tips!

    My panel data contains 7 variables and sorted by date id: date(01.Oct.1988 to 30.Jun.2016) ym(monthly date) id(from 1 to 5883) rmrf smb hml excessreturn.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(date ym id excessreturn rmrf smb hml)
    10503 345 1   -.01936289646965491 -.01127306  .00525148   .0006878
    10504 345 1   .004428615495805104  .00170627 -.00085576 -.00139754
    10505 345 1             -.0004251  .00996818 -.00446347  .00140606
    10506 345 1             -.0004254   .0059196 -.00249881  .00250139
    10507 345 1             -.0004254  .00319251  .00030991 -.00277563
    10510 345 1             -.0004223 -.00024731  .00422974 -.00149079
    10511 345 1             -.0004216 -.00274863  .00377518  .00549557
    10512 345 1             -.0004223 -.01161097  .00538493  .00050242
    10513 345 1             -.0004223  .00748774 -.00172834  .00512555
    10514 345 1             -.0004223  .00450467 -.00003376  .00478568
    10517 345 1   .023616446896690534  .01022073 -.00470417  .00443819
    10518 345 1   .013620751277013725  -.0011541  .00067627 -.00090353
    10519 345 1   .013474597771965517  .00363072  .00327984 -.00298672
    10520 345 1             -.0004251  .00043836 -.00042181  .00095446
    10521 345 1   .004160660287584015 -.00152961   .0025553   .0011501
    10524 345 1   .004094270973417762 -.00565686  .00289626 -.00235665
    10525 345 1   -.00491995430789627 -.00111718 -.00253788  .00240047
    10526 345 1             -.0004223  .00032483  -.0014962  .00228125
    10527 345 1   .017744790651005753  .00044753  .00076314  .00001995
    10528 345 1   .008496248264910458  .00309713  .00082262  .00215505
    10531 345 1             -.0004216 -.00123408  .00574452  .00172679
    10532 346 1             -.0004251  .00287933 -.00061362     .00232
    10533 346 1             -.0004233 -.00697585  .00488589  .00050676
    10534 346 1             -.0004244 -.00285437  .00281202   .0029033
    10535 346 1   .004018632959910915 -.00192175   .0009521  .00358065
    10538 346 1             -.0004254 -.00776155   .0027741  .00311697
    10539 346 1             -.0004264  .00952688 -.00659687  .00322087
    10540 346 1             -.0004254 -.00755174  .00424434  .00203433
    10541 346 1             -.0004244   .0000658 -.00040594  .00213749
    10542 346 1             -.0004227 -.01219847  .00586826  .00206838
    10545 346 1  -.013649725532307035 -.00454815 -.00256307  .00250378
    10546 346 1             -.0004233  .00395039  .00248942  .00142837
    10547 346 1             -.0004213  .00237563 -.00113014  .00247123
    10548 346 1             -.0004223  .00695646 -.00743721 -.00091841
    10549 346 1             -.0004244 -.00040684  .00169483  .00343632
    10552 346 1   .004060976614823962 -.00631036  .00254483 -.00003972
    10553 346 1  -.004886662993443917  .00421284 -.00169779  .00349698
    10554 346 1              -.000424  .00715559 -.00209694  .00426515
    10555 346 1  -.004908076614824184 -.00287966   .0015931  .00042118
    10556 346 1  -.013841080905465748 -.02226113  .00126729 -.00038323
    10559 346 1             -.0004548 -.00686149  .00134545  .00068337
    10560 346 1 -.0050233243520851146  .00304422 -.00087426 -.00150591
    10561 346 1             -.0004606  .00311911  .00006603  .00334213
    10562 347 1  -.005045560287584237 -.00880611 -.00284232  .00227736
    10563 347 1  -.005018397581225804 -.00939757 -.00306623 -.00021765
    10566 347 1             -.0004585 -.00323499 -.00183136  .00373087
    10567 347 1   -.00966786949185222  .00212448 -.00235887  .00124775
    10568 347 1  -.005130805532577441  .00061133 -.00497766  -.0044441
    10569 347 1  -.005102141236861527 -.00950524 -.00064092  .00103131
    10570 347 1  -.005172627701375276 -.00722724 -.00339905  .00152378
    10573 347 1  -.005192565455980971  .00015278 -.00530367 -.00236584
    10574 347 1             -.0004548  .00223309  .00088146  .00437897
    10575 347 1  -.005164832368107975 -.00032592 -.00276482  .00010165
    10576 347 1             -.0004575  .00387507 -.00047641 -.00151807
    10577 347 1    .00905806926717472  .00552013 -.00129807  .00101304
    10580 347 1  -.005191265455980971 -.00128961 -7.196e-06  .00584483
    10581 347 1             -.0004544  .00347369  -.0033825 -.00407795
    10582 347 1             -.0004565 -.00266647 -.00067593 -.00204654
    10583 347 1             -.0004551 -.00176227   .0036615 -.00504651
    10584 347 1             -.0004517  .00185695 -.00313413  .00019805
    10589 347 1             -.0004521  .00926918 -.00593432 -.00186907
    10590 347 1             -.0004534  .00813243 -.00295587 -.00200299
    10591 347 1             -.0004575 -.00457141  .00446324  .00128521
    10595 348 1             -.0004603 -.00625428  .00348006  .00297693
    10596 348 1  -.009930748234827456  .00525955  -.0027392 -.00272645
    10597 348 1             -.0004603  .00329548 -.00122743 -.00069683
    10598 348 1   .009101095144415997  .00577624 -.00208543 -.00008884
    10601 348 1             -.0004593   .0099526 -.00551761 -.00026279
    10602 348 1   .009059031733438961  .00179973  .00354946  .00461995
    10603 348 1             -.0004603 -.00075754  .00440392  .00339012
    10604 348 1             -.0004593  .00819852 -.00007103  .00246756
    10605 348 1             -.0004593   .0060607   .0023039 -.00229719
    10608 348 1   .004205411787819446  .00513847 -.00015631  .00334099
    10609 348 1             -.0004613 -.00229445  .00057371 -.00277521
    10610 348 1             -.0004613  .01093528 -.00588377 -.00158704
    10611 348 1   .027600298631141437  .00921167 -.00284583 -.00459487
    10612 348 1             -.0004613  .00313551 -.00084835 -.00063124
    10615 348 1   .013138266298920526  .00374158  .00260237 -.00339525
    10616 348 1             -.0004623  .00752605 -.00519193  .00170633
    10617 348 1             -.0004617 -.00090837  .00317521  .00025433
    10618 348 1             -.0004613  .00960258 -.00631583   .0045522
    10619 348 1   .008546441027445393  .02269427 -.00559572 -.00016106
    10622 348 1    .07504929897242767  .01651668 -.00309322 -.00189771
    10623 348 1             -.0004613  .00320827  -.0016308  .00090841
    10624 349 1             -.0004603  -.0049795  .00361908 -.00676923
    10625 349 1  -.004567342497081826  .00225093  .00262478  .00050828
    10626 349 1             -.0004599  .01362544  -.0009618  .00026414
    10629 349 1             -.0004603 -.01070708  .00748479 -.00392056
    10630 349 1             -.0004613  .01264815 -.00663474 -.00156815
    10631 349 1   .003662679857614035   .0107934 -.00043577  .00168113
    10632 349 1             -.0004613 -.00688215  .00523801  .00368968
    10633 349 1  -.004567342497081826 -.01046152  .00620448  .00326649
    10636 349 1  -.004626690171904928 -.01071695  .00314722 -.00381311
    10637 349 1             -.0004593  .00668561 -.00352879  .00087794
    10638 349 1             -.0004596 -.00106768  .00325114   .0002309
    10639 349 1  .0037255299912816687  -.0052176  .00625345 -.00239602
    10640 349 1  -.008750670029519073  .00303642 -.00170061 -.00133237
    10643 349 1             -.0004579  .00946149 -.00533087  .00049292
    10644 349 1             -.0004572 -.00169463  .00397498  .00187004
    10645 349 1  -.004658732435981501 -.01182387  .00857389  .00020951
    end
    format %td date
    format %tm ym

  • #2
    I don't know what a Fama-Macbeth regression is, so the code below just does an ordinary regression of excessreturn on hml smb and rmrf: change the code accordingly. I assume that after whatever program does Fama-Macbeth regression, the -predict- command is available just as it is after -regress- and can calculate the residual. If not, you will have to modify the code accordingly.

    Code:
    capture program drop one_regression
    program define one_regression
        regress excessreturn hml smb rmrf
        predict resid, resid
        summ resid, detail
        gen sd = r(sd)
        gen skewness = r(sd)
        exit
    end
    
    by id ym (date),sort: gen lb = ym -2 if _n == 1
    by id ym (date): replace lb = ym + 1 if _n > 1
    by id ym (date): gen ub = ym
    
    rangerun one_regression, by(id) interval(ym lb ub)
    keep if !missing(sd)
    keep id ym sd skewness
    Notes: You don't say as much, but I assume that you want to do this separately for each id. If that is not the case, remove the -by(id)- option from the -rangerun- command. I also assume that you want only a single observation for each id-ym pair, as the results would just be the same in any other such observation.

    -rangerun- is by Robert Picard and is available from SSC. In order to use it, you also need -rangestat-, by Robert Picard, Nick Cox, and Roberto Ferrer, also available from SSC.



    Comment


    • #3
      Code:
       
       gen skewness = r(sd)
      is a typo for

      Code:
       
       gen skewness = r(skewness)

      Comment


      • #4
        Hello Clyde, thank you very much for your reply and for writing the codes for me! It is exactly what I'm looking for to my issue, thank you for your help!

        Comment


        • #5
          Hello Nick, thank you very mcuh for catching that typo.

          Comment


          • #6
            See this recent post for a more efficient tweak on Clyde's code based on the fact that you only need to make these calculations once per month.

            Comment


            • #7
              Hello Robert, thanks for remind me.

              Comment

              Working...
              X