Announcement

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

  • Obtain coefficients for cross-sectional regression by id monthly

    Dear Statalists,

    I like to run this cross-sectional regression for my panel data and obtain only the coefficient of gamma as a variable.

    retex(i, d+1, t) = alpha + beta* ret(i, d, t) + gamma * sign(retex(i, d, t))*vol(i, d, t)

    where
    retex(id, d+1, t): excess return for stock i on day d+1 in month t

    ret(i, d, t): return for stock i on day d in month t

    sign(retex(i, d, t)): the sign(positive or negative sign) for excess return of stock i on day d in month t

    vol(i, d, t): trading volume for stock i on day d in month t

    And in a given month, the number of days is not below 15 days.

    Code:
    gen month=month(date)
    egen group=group(id year)
    
    gen sign_positive=1 if retex>0
    gen sign_negative=1 if retex<0
    
    
    sort id date
    bys id: gen retex_lead = retex[_n+1]
    
    capture program drop one_regression
    program define one_regression
            regress retex_lead ret  vol*sign_positive  vol*sign_negative
            if e(N) > 15 {
                gen nobs = e(N)
                forvalues i = 1/3 {
                    gen b_x`i' = _b[x`i']
                    gen se_x`i' = _se[x`i']
                }
            }
            exit
    end
    
    runby one_regression, by(group month) status
    I do not know how to switch between sign_positive and sign_negative, instead of putting them in the same equation. Moreover, running by group month leads to errors. Can anyone help me with this please?

    I really appreciate your help.

    Kind regards,
    Ken
    Last edited by Ken Yang; 26 Mar 2019, 21:41.

  • #2
    You can use asreg for rolling window regressions or cross-sectional regressions. asreg can be downloaded from SSC. Here is a general syntax or asreg. If you need more specific help, then post a sample of your data using dataex program (SSC)

    Code:
    * Install asreg
    ssc install asreg
    
    * See the help file
    help asreg
    
    * Generate the needed variables first
    gen vol_sign_positive   = vol*sign_positive
    gen vol_sign_negative = vol*sign_negative
    
    * Estimate the regression for each group and month, requiring at least 15 observations.
     bys group month: asreg retex_lead ret vol_sign_positive vol_sign_negative, min(15)
    Last edited by Attaullah Shah; 27 Mar 2019, 00:12.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      Attaullah Shah carefully explained that asreg is from SSC. SImilarly, Ken, you are asked to explain that runby is from SSC.

      Your definition of

      Code:
      gen sign_positive=1 if retex>0
      gen sign_negative=1 if retex<0
      seems to me to imply that you will never have any observations to work with. Each of those predictors is missing when the other is, a fact inherited by any interactions in which they appear and regress will decline to perform. There are several small puzzles about your code, but I wonder whether something like this is what you seek:

      Code:
      gen mdate = mofd(date)
      egen group = group(id)
      gen interaction = volume * (retex > 0)
      bysort id (date): gen retex_lead = retex[_n+1]
      rangestat (reg) retex_lead retex interaction, interval(mdate 0 0) by(group)
      I am always puzzled by these magic thresholds, that a sample of 15 is fine but 14 too defective to deal with -- while naturally not disputing that small sample sizes are problematic, but if so why constrain yourself to months and not use longer rolling windows? rangestat (SSC)approaches this issue differently by emitting sample size used and letting the researcher decide what to ignore.

      Comment


      • #4
        Should be

        Each of those predictors is missing when the other is not missing

        Comment


        • #5
          Thank you so much for your help Attaullah Shah and Nick Cox.

          I believe generating an interaction variable as Nick can help to switch the signs of retex. Can anyone help me how to obtain the coefficients of gamma only (the coefficient of the interaction variable)?

          For the thresholds of 15 days, I follow prior research, in which they try to see the liquidity of the stock. The greater expected reversal for a given dollar volume (signed by the contemporaneous return on the stock in excess of the market), the lower is the stock liquidity. From my understanding, constraining to months to see how quickly the investors react.

          I attach a sample of my data here in case you like to check.

          Thank you.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str5 id long date double(ret vol) float retex
          "51131" 14915                     0   117300   .004403302
          "51131" 14916   .008219177834689617   230400 -.0017433872
          "51131" 14917  -.005434782709926367   423500  -.005961025
          "51131" 14920   .005464480724185705    23600   .004219351
          "51131" 14921 -.0027173913549631834    41500 -.0017104833
          "51131" 14922                     0    44500     .0189999
          "53132" 14945                     0   411384   -.00620922
          "53132" 14948                     0    87911  -.003680979
          "53132" 14949                     0   291200   -.04535352
          "53132" 14950  -.001700680237263441   510652    .01493882
          "53132" 14951  -.001703577465377748    58448  .0042199045
          "53132" 14952  .0034129691775888205   618432   -.02506799
          "53132" 14955   .003401360474526882   374996   -.00858508
          "53132" 14956   .003389830468222499   118826    .01428942
          "53132" 14957  -.006756756920367479   313071   .005035773
          "53132" 14958   .003401360474526882   115536    .02040517
          "53132" 14959  -.003389830468222499    69551    .01439806
          "53132" 14962                     0    53513  -.005363566
          "755F1" 14628               .296875  3874631     .2922813
          "755F1" 14994     .1388888955116272   415638    .14299874
          "755F1" 14997    .24390244483947754   521506    .24362762
          "755F1" 14998   -.03921568766236305   519120   -.05503057
          "755F1" 14999    -.0357142873108387   291379   -.03906943
          "755F1" 15000    -.0793650820851326   255828   -.07183012
          "755F1" 15001    .01149425283074379   180270   .012187393
          "755F1" 15004  -.005681818351149559    97129  -.014614332
          "755F1" 15005   .011428571306169033   123235   .004874753
          "755F1" 15006  -.028248587623238564   150665   -.02148101
          "755F1" 14731  -.032608695328235626   281635    -.0477403
          "755F1" 14732    .03932584449648857   407541     .0587023
          "755F1" 14733   -.10270269960165024   252827   -.08153183
          "755F1" 14734    .10843373835086823   202534     .1075919
          "755F1" 14735  -.010869565419852734   175598  -.026340146
          "755F1" 14738   -.04395604506134987   112954  -.033683166
          "755F1" 14739   -.02298850566148758   280141  -.011150946
          "755F1" 14740     .0235294122248888   249319    .05041323
          "755F1" 14915   -.08771929889917374   177859     -.083316
          "755F1" 14916    .07692307978868484   151244    .06696051
          "755F1" 14917    -.0491071417927742   131941   -.04963338
          "755F1" 14920   .023474179208278656   136022    .02222905
          "755F1" 14921   .027522936463356018   133719   .028529845
          "755F1" 14922    -.0714285746216774   113645   -.05242867
          "755F1" 14923   -.06730769574642181   127444   -.05737165
          "755F1" 14924  -.010309278033673763   107748   .016097022
          "755F1" 14927                 -.125   254781   -.11153762
          "755F1" 14928     .0595238097012043   230751    .03350052
          "755F1" 14929    .03932584449648857   160648   .034010198
          "755F1" 14930    -.0486486479640007   116676  -.032200437
          "755F1" 14931   -.04545454680919647   249554   -.04092582
          "91041" 14641  .0019157087663188577   108174   -.01039113
          "91041" 14642                     0    98750  -.002988925
          "91041" 14643    .00382409174926579   240536  -.011699928
          "91041" 14644 -.0028571428265422583   405694 -.0038550405
          "91041" 14647   .008595988154411316   193360   .005540974
          "91041" 14648 -.0018939394503831863    71257    -.0145598
          "91041" 14649                     0   301802     .0173541
          "91041" 14650                     0   138226  -.006477069
          "91041" 14651 -.0018975331913679838    20062   .015535037
          "91041" 14654                     0    16759  -.002641042
          "07895" 14885    .01621621660888195   848236    .02219331
          "07895" 14886  -.021276595070958138   428540  -.009153325
          "07895" 14887   .010869565419852734   392681  .0045791785
          "755L1" 21313                     . 23189258            .
          "755L1" 21314  -.055732063949108124  4888114   -.06417336
          "755L1" 21315  -.015014284290373325  3056743  -.016304655
          "755L1" 21318    .02996056340634823  4899203   .029281104
          "755L1" 21319   .020668573677539825  5201553   .026477227
          "755L1" 21320   .052499961107969284  3697416    .04787252
          "755L1" 21321    .04275541752576828  2509746    .04268222
          "755L1" 21322    .02255120314657688  3853187   .024880785
          "755L1" 21325  -.008465160615742207  1122593  -.015433406
          "755L1" 21326   .004493389744311571  1073618   .007977227
          "755L1" 21327  -.003578614443540573   886005  -.005854135
          "755L1" 21328    .02199774608016014  1012450   .023868993
          "755L1" 21329   -.03382378816604614  1188736    -.0315231
          "755L1" 21333  -.002955241361632943   853978    .00704022
          "755L1" 21334 -.0018240271601825953   873318  -.014788137
          "755L1" 21335   -.01347647700458765  1115057  -.006800751
          "835L1" 21319    .17102624475955963   666140     .1768349
          "835L1" 21320   -.02577321231365204   498156   -.03040065
          "835L1" 21321     .0476190447807312   490792    .04754585
          "835L1" 21322  -.045454543083906174   306642   -.04312496
          "835L1" 21325  -.010582000017166138   232062  -.017550245
          "835L1" 21326   .005347545724362135   102163   .008831383
          "835L1" 21327  -.015957389026880264   123268   -.01823291
          "835L1" 21328   -.01621624268591404    88093  -.014344996
          "835L1" 21329  -.005494543816894293    73473  -.003193857
          "835L1" 21333    .01104971393942833   109176   .021045174
          "835L1" 21334  -.009107416495680809    99343   -.02207153
          "835L1" 21335   .022058801725506783    87844    .02873453
          "835L1" 21367   .020040158182382584   102739   .017941004
          "835L1" 21371   .026209700852632523    52968   .017849633
          "835L1" 21374   .015717076137661934   108986    .00752502
          "835L1" 21375  -.030947744846343994    78026  -.033235908
          "835L1" 21376   .013971994630992413    46749    .02139463
          "835L1" 21377  -.003937004134058952    86522  -.012095259
          "835L1" 21390    .05427979677915573   119927    .04605186
          "835L1" 21409    .04140792787075043   167644    .04610939
          "835L1" 21410    .04771367087960243   256844    .04122091
          "835L1" 21411   .030360503122210503   235526    .03956398
          "835L1" 21412    .03683246672153473   190167    .02903817
          "835L1" 21413   -.04085257649421692    66342   -.04464699
          "835L1" 21416   -.01851850003004074   123582   -.02129629
          "835L1" 21417    -.0207547415047884    50987   -.02383919
          "835L1" 21418     .0404624342918396    72959    .03975424
          "835L1" 21419   .020370393991470337    48575   .022857105
          "835L1" 21420   -.03085300698876381    63285  -.037091494
          "835L1" 21423 -.0018727020360529423    60167  -.008946028
          "835L1" 21424  -.015009366907179356    31706  -.014636414
          "835L1" 21425   .007619040552526712    86304  .0024999415
          "835L1" 21426   .037807147949934006   160250    .04242269
          "835L1" 21427   .005464519374072552   113253   .005262249
          "835L1" 21431                     0   133420   .002346193
          "835L1" 21432  -.014492739923298359   108962  -.010975432
          "835L1" 21433   -.01838233508169651    44045   -.01418984
          "835L1" 21434  -.029962606728076935    60401  -.027539384
          "835L1" 21437  .0019305461319163442    88373 -.0003353999
          "835L1" 21438  -.015414243564009666    49851  -.018599179
          "835L1" 21439 -.0019569918513298035    98129 -.0027690395
          "835L1" 21440  -.013725430704653263    47439   -.01779152
          "835L1" 21441    .00994030013680458    22160   .009164775
          "835L1" 21446    .05383027717471123    45769    .05331383
          "835L1" 21447  .0039292690344154835    56958   -.00387899
          "835L1" 21451   .018181849271059036    82144   .021930555
          "835L1" 21452    .00992067251354456    61261   .010487473
          "835L1" 21453  -.007858538068830967    28254  -.003693332
          "835L1" 21454                     0    39075  -.002159366
          "835L1" 21455   .005940541159361601    25821    .00594072
          "838W1" 14612                     0    46600   .006816899
          "838W1" 14613                     0    44500    .03968266
          "838W1" 14614                     0    35700   .001003738
          "838W1" 14615 -.0016891892300918698    28100   .005851025
          "838W1" 14616  .0008460236713290215   156500  -.031670347
          "23841" 14885   -.04712041839957237   349400   -.04114332
          "23841" 14886    .03846153989434242   408900    .05058481
          "23841" 14887  -.026455026119947433   391600  -.032745413
          "23841" 14888    -.3913043439388275  2639099    -.3899753
          "23841" 14889    -.1964285671710968  2698399    -.1748194
          "23841" 14892   .011111111380159855  1609599   .014542054
          "23841" 14893  -.021978022530674934   952000  -.007617802
          "23841" 14907     .1428571492433548  1529899    .14384952
          "33391" 14794    .07194244861602783    83100    .06196457
          "33391" 14796   -.02684563770890236   151700  -.010851628
          "33391" 14797  -.006896551698446274    64600   -.01521352
          "33391" 14798   .010416666977107525    52600 -.0039980332
          "88251" 14703  -.008823529817163944  1245000  .0083875805
          "88251" 14704   .008160237222909927  3186399    .02100542
          "88251" 14705  -.016924209892749786  1254199  -.017916843
          "88251" 14706  -.005988024175167084  2789699  -.021711314
          "88251" 14707  -.005271084140986204  1021000  -.019809484
          "88251" 14710  -.009841029532253742  1470699    .00950192
          "88251" 14711    .03516819700598717  3680500    .04544895
          "88251" 14712  -.005169867072254419  1348500     .0249122
          "88251" 14713  -.007423905190080404  4637099   .009721435
          "88251" 14714  -.024682125076651573  3563699     .0414945
          "88251" 14717   -.04831288382411003 26899488   -.07761107
          "56071" 15127  -.012738917022943497    70100  -.018117508
          "56071" 15130  -.009677410125732422    68500   -.01473837
          "56071" 15131 -.0032573258504271507    83800  -.017473936
          "56071" 15132   -.04411764442920685   135900   -.03455436
          "56071" 15133   -.11452993005514145   170100    -.1198281
          "56071" 15134  -.034749001264572144    90400   -.02521517
          "56071" 15155     .2847965657711029   804600     .2804617
          "56071" 14945    .08749999850988388    50700    .08129078
          "56071" 14948    .01149425283074379    64000   .007813274
          "56071" 14949   .034090910106897354    37000   -.01126261
          "56071" 14950  -.021978022530674934    62400  -.005338523
          "56071" 14951   -.02247191034257412    37200  -.016548429
          "56071" 14952    .01149425283074379    25500  -.016986707
          "56071" 14955                     0    28800   -.01198644
          "56071" 14956   .011363636702299118    47600   .022263227
          "56071" 14957   -.07865168899297714    61900  -.066859156
          "56071" 14959    .05128205195069313   160600   .069069944
          "56071" 14962   .015121950767934322    57000   .009758385
          "56071" 14963  -.012048192322254181   109800   .003750518
          "646L1" 14731   -.03664921596646309    45783   -.05178082
          "646L1" 14732   -.04076087102293968    65186   -.02138441
          "646L1" 14733    .00566572230309248    41853    .02683659
          "646L1" 14734   .023098591715097427     4244    .02225675
          "646L1" 14735    .09444444626569748    84717    .07897387
          "646L1" 14738  -.020304568111896515    52073  -.010031688
          "646L1" 14739  -.041450776159763336   105237  -.029613215
          "646L1" 14740  -.062162160873413086  1196061  -.035278343
          "658U1" 14885  .0008591065416112542  3878099   .006836202
          "658U1" 14886                     0  1636199    .01212327
          "658U1" 14887   .001716738217510283  1599599  -.004573649
          "75091" 14731                     0   607600    -.0151316
          "75091" 14732  -.011627906933426857   495900   .007748553
          "75091" 14733                     0   464100    .02117087
          "75091" 14734    -.0235294122248888   947800   -.02437125
          "060P1" 14945   -.01552795059978962   213200   -.02173717
          "060P1" 14948  -.020315464586019516   133900  -.023996444
          "060P1" 14949  -.002575952559709549   280600   -.04792947
          "060P1" 14950  -.005681849550455809   291400    .01095765
          "060P1" 14951  -.012987012974917889   328600  -.007063531
          "060P1" 14952   .008421044796705246   145900  -.020059915
          "060P1" 14955   -.00835072249174118   124100   -.02033716
          "060P1" 14956   -.02631578966975212    86900    -.0154162
          "060P1" 14957    .01891893893480301   120600    .03071147
          "060P1" 14958 -.0026525806169956923   120100    .01435123
          end
          format %td date

          Comment


          • #6
            Sorry, but I don't understand that this is a different question.

            Disclaimer: I don't work with finance and have zero knowledge of its special jargon.

            Comment


            • #7
              Please accept my apology if I caused any confusion. I just tried to explain why the threshold is 15 days and it should be constrained in months in this context.

              I am really grateful for your help.

              Thank you.

              Comment

              Working...
              X