Announcement

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

  • How to loop variables to run regression with one variable

    Hi all, I have sample datasets as follow:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(month BLF BMC BMP mkt mkt_lag mkt_lead)
    600          .          .           .          .          . -.08011035
    600 -.07186875 -.13838592 -.073767476 -.08011035          . -.10704234
    600 -.14840126 -.14784347  -.10364176 -.10704234 -.08011035 -.11275777
    600 -.00837866 -.14142856  -.14624734 -.11275777 -.10704234 -.13514103
    600 -.07071709    -.11154   -.1559959 -.13514103 -.11275777          .
    600          .          .           .          . -.13514103 -.14756103
    600 -.17334187  -.1534142   -.1457479 -.14756103          . -.09404086
    600 -.09202663 -.10659858   -.1224689 -.09404086 -.14756103 -.10542256
    600 -.12763597    -.11271  -.06582951 -.10542256 -.09404086 -.12629919
    600  -.1667886  -.1528849     -.11272 -.12629919 -.10542256 -.14956874
    600 -.16150147 -.16314194   -.1516216 -.14956874 -.12629919  -.0945335
    600 -.05489313 -.10300398   -.0803421  -.0945335 -.14956874          .
    600          .          .           .          .  -.0945335 -.13635546
    600  -.1302676 -.14886476   -.0977102 -.13635546          .          .
    600          .          .           .          . -.13635546 -.10551248
    600  -.1040715 -.09425955     -.11244 -.10551248          .          .
    600          .          .           .          . -.10551248 -.13636737
    600  -.1535125 -.07911568   -.1522368 -.13636737          . -.12262583
    600 -.13812657 -.16431424  -.14554626 -.12262583 -.13636737 -.11261494
    600 -.12154094    -.11303     -.11303 -.11261494 -.12262583 -.10477968
    601   -.123664    -.11508  -.06667894 -.10477968 -.11261494  -.1130095
    601 -.12367833    -.11502  -.08401432  -.1130095 -.10477968 -.09727497
    601 -.09538767 -.09524008  -.09747676 -.09727497  -.1130095 -.09626808
    601 -.10446905 -.10439398  -.10548863 -.09626808 -.09727497  -.1345686
    601 -.12143094 -.14770897   -.1432283  -.1345686 -.09626808 -.11931893
    601 -.13282233    -.11558   -.1468279 -.11931893  -.1345686          .
    601          .          .           .          . -.11931893 -.09770807
    601 -.09616494 -.09615672  -.07464554 -.09770807          .          .
    601          .          .           .          . -.09770807          .
    601          .          .           .          .          . -.10804106
    601  -.1056115 -.10532606     -.11398 -.10804106          .  -.1412695
    601 -.14788257  -.1580387  -.15229504  -.1412695 -.10804106  -.1201113
    601   -.108096 -.10770897     -.11668  -.1201113  -.1412695          .
    601          .          .           .          .  -.1201113 -.11151864
    601 -.08119761 -.10657697   -.1311398 -.11151864          . -.10317887
    602 -.07422082 -.09799007   -.1233489 -.10317887 -.11151864 -.11929395
    602 -.12338744 -.12394395  -.12332292 -.11929395 -.10317887 -.10276413
    602 -.09917016     -.1153  -.10726708 -.10276413 -.11929395          .
    602          .          .           .          . -.10276413          .
    602          .          .           .          .          . -.09910545
    602  -.0754342  -.0887317   -.0828021 -.09910545          .  -.1028476
    602 -.09210663    -.11458    -.099311  -.1028476 -.09910545  -.1184791
    602    -.11544 -.12381052    -.130709  -.1184791  -.1028476 -.12176133
    602 -.14987817     -.1198  -.12751527 -.12176133  -.1184791  -.1045903
    602 -.12526308     -.1176  -.07214075  -.1045903 -.12176133 -.11538172
    602 -.10837692 -.07488298     -.11604 -.11538172  -.1045903 -.14801851
    602  -.1647848  -.1674981  -.16335924 -.14801851 -.11538172  -.1280758
    602 -.14218338    -.11789  -.13351673  -.1280758 -.14801851 -.10276656
    602 -.08773822 -.10318842  -.10437327 -.10276656  -.1280758 -.12722206
    602 -.12796839  -.1453183  -.13562673 -.12722206 -.10276656 -.12638341
    602  -.1503241     -.1178  -.14170773 -.12638341 -.12722206 -.12992042
    602 -.11176926       -.12  -.12809797 -.12992042 -.12638341 -.10705458
    602 -.09373662 -.12661602  -.10993203 -.10705458 -.12992042 -.13630375
    602 -.15163413 -.13649993  -.15190294 -.13630375 -.10705458  -.1149129
    602 -.11046926     -.1187      -.1187  -.1149129 -.13630375          .
    602          .          .           .          .  -.1149129 -.12884723
    602 -.11049905  -.1445438  -.13581052 -.12884723          . -.12180012
    602 -.12735094 -.07664081     -.11884 -.12180012 -.12884723 -.10072123
    603 -.09315144 -.09397837  -.10166947 -.10072123 -.12180012 -.11403426
    603 -.13535762 -.10254543  -.12691091 -.11403426 -.10072123  -.1090324
    603  -.0841027 -.13344458  -.13439108  -.1090324 -.11403426 -.11777158
    603 -.14371818 -.14331163  -.10186892 -.11777158  -.1090324  -.1178696
    603 -.12725912 -.12711486  -.12725963  -.1178696 -.11777158 -.11860677
    603 -.09371343    -.11882  -.11038037 -.11860677  -.1178696          .
    603          .          .           .          . -.11860677 -.11403073
           .
    end
    format %tm month
    I want to run regression of each firm(e.g BLF,....) on excess market return (mkt mkt_lag mkt_lead) for one-month window, but i realise that I toke me a lot of time when i do as follow:
    Code:
    bys month: asreg BLF mkt mkt_lag mkt_lead
    drop _Nobs- _adjR2 _b_cons
    gen beta_BLF = _b_mkt + _b_mkt_lag + _b_mkt_lead
    drop _b_mkt - _b_mkt_lead
    
    bys month: asreg BMC mkt mkt_lag mkt_lead
    drop _Nobs- _adjR2 _b_cons
    gen beta_BMC = _b_mkt + _b_mkt_lag + _b_mkt_lead
    drop _b_mkt - _b_mkt_lead
    
    bys month: asreg BMP mkt mkt_lag mkt_lead
    drop _Nobs- _adjR2 _b_cons
    gen beta_BMP = _b_mkt + _b_mkt_lag + _b_mkt_lead
    drop _b_mkt - _b_mkt_lead
    I would appreciate any suggestions how can I approach this problem because i have more than 600 firms and do such things spend a lot of my time. Thank you very much.

  • #2
    The difficulty arises because you have the data in wide layout. Once you reshape it to long, it's just a few lines of code:

    Code:
    gen long obs_no = _n
    
    ds obs_no month mkt*, not
    local firms `r(varlist)'
    
    rename (`firms') x=
    
    reshape long x, i(obs_no) j(firm) string
    rangestat (reg) x mkt mkt_lag mkt_lead, by(firm) interval(month . .)
    gen beta = b_mkt + b_mkt_lag + b_mkt_lead
    drop reg_nobs reg_adj_r2 b_mkt* se_*

    Note: -rangestat- is by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC. I do not use -asreg-, so I cannot be sure, but I suspect you can use -asreg- instead of -rangestsat- here as well in some way if you are more comfortable with that. That would also require adjusting the subsequent two lines to reflect the variable naming conventions in -asreg-.

    Note 2: I dropped the standard errors. If you want them, you can modify the -drop- command.

    Note 3: The -reshape long- may take a while. You need to be patient with a large data set. If it really becomes unreasonable, consider getting Rafal Raciborski's -tolong- from SSC, or Mauricio Caceres Bravo's -gtools- package, which includes a -greshape- command. Both of them run considerably faster than Stata's native -reshape- in large data sets.

    Note 4: While you probably should keep the data in long layout, as that will make almost anything you want to do after this easier than with wide data, if you must return to wide layout for some reason, you can do that as follows:

    Code:
    rename (reg_r2 b_cons beta) =_
    reshape wide x reg_r2_ b_cons_ beta_, i(obs_no) j(firm) string
    Last edited by Clyde Schechter; 06 Feb 2022, 12:57.

    Comment

    Working...
    X