Announcement

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

  • #16
    I would like to run this in a separate code, as I would like to use another time period and use the code at a later point. I have made these changes but it doesn´t seem to work out.
    Originally posted by Clyde Schechter View Post
    Code:
    capture program drop one_event
    program define one_event
    capture regress return var1 var2 if inrange(eventwindow, -20, 0) // new period
    if inlist(c(rc), 2000, 2001) { // ID WITH NO WINDOW
    exit 0
    }
    else if c(rc) != 0 {
    gen error_code = c(rc)
    exit c(rc)
    }
    else {
    predict fit if e(sample), xb
    summ fit
    gen fit_value= r(Var) if e(sample)
    }
    exit
    end
    
    runby one_event, by(ID event_num)
    From this point (if i would be able to obtain the fitted values), I could calculate the R2 manually with the formula.
    Last edited by David Moeller; 14 Dec 2024, 08:04.

    Comment


    • #17
      Clyde Schechter I am sorry. I think I have just resolved my problem. I was a little bit confused by the formula.
      I have done the following:

      Code:
      bysort ID event_num (Date): egen sd_r=sd(return) if eventwindow>=-10 &eventwindow<=1
      replace sd_r=sd_r^2
      rename sd_r var_r
      gen r_squared=1-((resid_variance)/(var_r))
      This should be correct, right?

      Comment


      • #18
        Creating a variable for R2 just requires a simple modification of program one_event. Add in the command -gen rsq = e(r2)- inside the -else { }- block immediately before the -predict- command.

        Comment


        • #19
          Without commenting on whether this is quite right, four lines can be condensed to two:

          Code:
          bysort ID event_num (Date): egen sd_r=sd(return) if inrange(eventwindow, -10, 1)
          
          gen r_squared = 1 - resid_variance/(sd_r^2)
          Last edited by Nick Cox; 14 Dec 2024, 10:22.

          Comment


          • #20
            Originally posted by Clyde Schechter View Post
            Creating a variable for R2 just requires a simple modification of program one_event. Add in the command -gen rsq = e(r2)- inside the -else { }- block immediately before the -predict- command.
            Clyde Schechter Thank you. I might have one additional question regarding the generation of event windows. I am currently working with an updated dataset, where I want to calculate variables (such as the standard deviation of returns) in eventwindows (-40,+1) starting 40 days before the event and ending 1 day after the event. As before, each firm can have several events. The problem is, that some events are less than 40 days apart from each other. Therefore, the previous procedure doesn´t work. How would you, solve such a problem? Is there a way to calculate variables (within a range around the events) without the generation of an eventwindow variable (which essentially functions as a counter variable)? And would that make sense?

            Comment


            • #21
              You can do something like this:
              Code:
              recode event (0 = 1) (. = 0)
              
              gen low = cond(event, Date-40, 1)
              gen high = cond(event, Date+1, 0)
              
              frame create event_stats int event_date float(mean_price sd_price)
              
              capture program drop one_event
              program define one_event
                  summ Date if event, meanonly
                  local event_date `r(max)'
                  summ price
                  local mean_price `r(mean)'
                  local sd_price `r(sd)'
                  frame post event_stats (`event_date') (`mean_price') (`sd_price')
                  exit
              end
              
              rangerun one_event, interval(Date low high)
              frame change event_stats
              -rangerun -is wrtten by Robert Picard and is available from SSC. To use it, you must also install -rangestat-, by Robert Picard, Nick Cox, and Roberto Ferrer, also from SSC.

              Basically program one_event calculates the desired statistics (of any kind you like--do regressions, whatever) and then posts the results in frame event_stats. To customize this code to your actual intended calculations, you must flesh out program one_event, and you must also change the creation of frame event_stats to receive those actual results.

              When done, frame event_stats will contain one observation per event, with the -40 to +1 window statistics you want. Be cautious, however, in performing subsequent statistical analyses with these results, because whenever two windows overlap, that makes the observations no longer independent, so many types of statistical inference will not be correct.

              Comment


              • #22
                Originally posted by Clyde Schechter View Post

                Be cautious, however, in performing subsequent statistical analyses with these results, because whenever two windows overlap, that makes the observations no longer independent, so many types of statistical inference will not be correct.
                Clyde Schechter Thank you for the code and the additional remark. I would like to ask an additional question regarding the computation of autocorrelations in Stata. What I would like to do, is to calculate the first order autocorrelation of daily returns over a period from -10 days before the event till 1 day after the event (+1). Once again I have several firms with several events. I have already created the eventwindows around the event.

                I have tried to come up with a Stata code, but I am struggling a little bit. This is the code, that I have tried to use:

                Code:
                rangestat (corr) Return Return_LAG,  by(ID event_num) interval(eventwindow -10 1) // Return_LAG is the 1 day lag return
                Is the rangestat command an appropriate command, to calculate autocorrelation? My code somehow doesn´t seem to work properly. I´d appreciate any comment/hint. Once again thank you for your help!

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input float(ID Date) double(Return Return_LAG) float(eventwindow event_num)
                1 21728  -1.06528271   .301369863   .  .
                1 21731   .800662617  -1.06528271   .  .
                1 21732  -.082169269   .800662617 -10 11
                1 21733  1.288377193  -.082169269  -9 11
                1 21734 -1.055480379  1.288377193  -8 11
                1 21735  -.273522976 -1.055480379  -7 11
                1 21738  -.191991223  -.273522976  -6 11
                1 21739  1.291563616  -.191991223  -5 11
                1 21740 -1.275094954  1.291563616  -4 11
                1 21741   .109920308 -1.275094954  -3 11
                1 21742  -.548998079   .109920308  -2 11
                1 21745   .552028705  -.548998079  -1 11
                1 21746 -4.254735108   .552028705   0 11
                1 21747   .315366972 -4.254735108   1 11
                1 21748  1.428979708   .315366972   .  .
                1 21749  -.760777684  1.428979708   .  .
                1 21752  -.908574673  -.760777684   .  .
                1 21753  1.461318052  -.908574673   .  .
                1 21754  -1.24258684  1.461318052   .  .
                1 21755   .657706606  -1.24258684   .  .
                1 21756  1.335227273   .657706606   .  .
                1 21759    .44855621  1.335227273   .  .
                1 21760  -.530281887    .44855621   .  .
                1 21761  1.066217733  -.530281887   .  .
                1 21762  1.027207107  1.066217733   .  .
                1 21763   .961802693  1.027207107   .  .
                1 21766  -.870985302   .961802693   .  .
                1 21767  1.208127403  -.870985302   .  .
                1 21768 -1.383613673  1.208127403   .  .
                1 21769  1.182943604 -1.383613673   .  .
                1 21770  -.897226754  1.182943604   .  .
                1 21773   .356652949  -.897226754   .  .
                1 21774  -.519409513   .356652949   .  .
                1 21775  -.302280846  -.519409513   .  .
                1 21776   .606394708  -.302280846   .  .
                1 21777   .465753425   .606394708   .  .
                1 21780   .790837197   .465753425   .  .
                1 21781  -.703463203   .790837197   .  .
                1 21782   .272479564  -.703463203   .  .
                1 21783  -.570652174   .272479564   .  .
                1 21784   .409948073  -.570652174   .  .
                1 21787  -.108873163   .409948073   .  .
                1 21788  1.171662125  -.108873163   .  .
                1 21789   .565580393  1.171662125   .  .
                1 21790   .160685592   .565580393   .  .
                1 21791   .053475936   .160685592   .  .
                1 21794   .801710315   .053475936   .  .
                1 21795  -.318133616   .801710315   .  .
                1 21796  -.053191489  -.318133616   .  .
                1 21797 -1.330494944  -.053191489   .  .
                1 21798 -3.425026969 -1.330494944   .  .
                1 21801 -1.563808992 -3.425026969   .  .
                1 21802  -.255319149 -1.563808992   .  .
                1 21803  1.706484642  -.255319149   .  .
                1 21804   .699105145  1.706484642   .  .
                1 21805  -.249930575   .699105145   .  .
                1 21808  1.419821826  -.249930575   .  .
                1 21809    .13724952  1.419821826   .  .
                1 21810  1.973684211    .13724952   .  .
                1 21811 -1.075268817  1.973684211   .  .
                1 21812   .108695652 -1.075268817   .  .
                1 21815  -.732899023   .108695652   .  .
                1 21816   .246103363  -.732899023   .  .
                1 21817  -.354609929   .246103363   .  .
                1 21818   .273747605  -.354609929   .  .
                1 21819  -.382200382   .273747605   .  .
                1 21822   .054809537  -.382200382   .  .
                1 21823   .493015612   .054809537   .  .
                1 21824 -2.180430635   .493015612   .  .
                1 21825   .640847033 -2.180430635   .  .
                1 21826   .442967885   .640847033   .  .
                1 21829  1.405733186   .442967885   .  .
                1 21830  -.842620277  1.405733186   .  .
                1 21831  1.151315789  -.842620277 -10 12
                1 21832  -.162601626  1.151315789  -9 12
                1 21833   .499445061  -.162601626  -8 12
                1 21836  -.414135837   .499445061  -7 12
                1 21837  -.471305794  -.414135837  -6 12
                1 21838   .584958217  -.471305794  -5 12
                1 21839   .138465799   .584958217  -4 12
                1 21840  -.193584071   .138465799  -3 12
                1 21843   .692712663  -.193584071  -2 12
                1 21844  1.265822785   .692712663  -1 12
                1 21845 -1.358695652  1.265822785   0 12
                1 21846  -3.47107438 -1.358695652   1 12
                1 21847 -1.084474886  -3.47107438   .  .
                1 21850  -.490478938 -1.084474886   .  .
                end
                format %tdnn/dd/CCYY Date






                Comment


                • #23
                  That code is correct for calculating the correlation between the lag of the return and the return in the window, yes. You appropriately did this by creating a separate variable for the lagged return so that you do not lose a final observation in -rangestat-.

                  Comment


                  • #24
                    Originally posted by Clyde Schechter View Post
                    That code is correct for calculating the correlation between the lag of the return and the return in the window, yes. You appropriately did this by creating a separate variable for the lagged return so that you do not lose a final observation in -rangestat-.
                    Thank you for your response. How would I have to adjust the code, to get one value for the first order autocorrelation in each eventwindow (for each firm)? When I use my code, each day whithin a window has a different value.

                    Comment


                    • #25
                      Your code is computing that already. It's just that it's also computing a value of the autocorrelation for other observations as well. There is no way to stop -rangestat- from doing that, because -rangestat- does not support -if- or -in- clauses. But you can just get rid of the excess results after it's done by coding -replace corr_x = . if eventwindow == 0-. That will leave the autocorrelation only in the observation that defines the zero-point of the window.

                      Comment


                      • #26
                        Originally posted by Clyde Schechter View Post
                        Your code is computing that already. It's just that it's also computing a value of the autocorrelation for other observations as well. There is no way to stop -rangestat- from doing that, because -rangestat- does not support -if- or -in- clauses. But you can just get rid of the excess results after it's done by coding -replace corr_x = . if eventwindow == 0-. That will leave the autocorrelation only in the observation that defines the zero-point of the window.
                        I somehow struggle to understand, how
                        Code:
                        replace corr_x = . if eventwindow == 0
                        is going to resolve the issue. Wouldn´t it have to be
                        Code:
                        replace corr_x = . if eventwindow != 1
                        ? Is my understanding of the rangestat command correct, that the last value of corr_x (e.g. -.37939626 for event_num 11 and ID 1 on the last eventwindow day 1) corresponds to the first order autocorrelation over the entire event period? So If I am interested in the autocorrelation over the entire window (-10,1), the value of corr_x on day 1 of each event is the relevant one, such that this the autocorrelation from day -10 to day 1? If this is the case, why doesn´t the code correspond to the results, when I use the corrgram command ?
                        Code:
                        xtset ID Date
                        Code:
                        corrgram Return if ID==1 & event_num==12
                        When I compare the results of corr_x on day 1 of event 12 with the corrgram results, they differ significantly.
                        Thanks again for your help!

                        Comment


                        • #27
                          Sorry, I meant -replace corr_x = . if eventwindow != 0-. That will leave the autocorrelation for the full -10 to + 1 window in the observation that generated it, namely the one with eventwindow == 0.

                          Is my understanding of the rangestat command correct, that the last value of corr_x (e.g. -.37939626 for event_num 11 and ID 1 on the last eventwindow day 1) corresponds to the first order autocorrelation over the entire event period?
                          No. That's not correct. In any observation, the value of corr_x is the correlation calculated using all observations with the same event_num and whose values of eventwindow lie between 10 below and 1 above (inclusive) the value of eventwindow in that same observation. So, for example, in an observation with eventwindow = -5 it includes the observations whose values of eventwindow are between -15 and -4. Since there are no observations with eventwindow less than -10, this reduces to observations whose values of eventwindow are between -10 and -4, inclusive. Similarly, in the observation with eventwindow = 1, the value of corr_x is calculated for values of eventwindow between 1 - 10 = -9, and 1 +1 = 2. Again, as eventwindow is never 2, this boils down to a range from -9 to 2. The only value of eventnum that gives the full window is eventnum == 0.

                          Comment

                          Working...
                          X