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

              Working...
              X