Announcement

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

  • Running multiple event studies using the loop command

    Dear Statalist users,

    I am new to posting in Statalist and was hoping I could find some help on loop and matrix commands.

    I am conducting an event study of the impact of central bank announcements on foreign stock market returns. I have reviewed the suggestions made previously on this site and have created a program which effectively produces the abnormal returns (ARs) and cumulative abnormal returns (CARs) for a single announcement. The event window is 40 days around the event day with an estimation window of roughly half a year (180 days) prior. Here is the code for a single event which I created following the guides from Princeton (https://dss.princeton.edu/online_hel...ventstudy.html) and Dr. Woochan Kim (http://www.sunwoohwang.com/Event_Study_STATA.pdf),

    Code:
    gen ret = 100*ln(RealPrice[_n]/RealPrice[_n-1])
    
    gen day_cnt=_n
    tsset day_cnt
    tssmooth ma mkret = ret, window(100,1)
    gen target_day=day_cnt if Change<0
    egen target_id = group(target_day)
    egen max_target_day=max(target_day)
    gen evday=day_cnt-max_target_day
    
    sort evday
    gen evt_window=1 if evday>=-20 & evday<=20
    gen est_window=1 if evday<=-21 & evday>=-200
    drop if evt_window==.&est_window==.
    
    qui reg ret mkret if est_window==1
    gen rmse=e(rmse)
    
    predict phat
    gen ar=ret-phat if evt_window==1
    drop phat
    drop if evt_window==0
    keep if evday>=-20 & evday<=+20
    egen car=sum(ar)
    gen tstat=car/(rmse*sqrt(_N))
    
    gen car_ca = sum(ar)
    list ar car_ca car tstat
    graph twoway line car_ca evday

    However I am having a difficult time with two issues in extending this program to further events,

    1. There is a total of 52 events across the time sampled, 1990m1 - 2009m12. I would like to build a loop which can store the ARs and CARs from each announcement. In this manner I can...
    2. Calculate the average ARs and CARs for the whole period.

    I am using STATA 15.1

    Here is a snapshot of my data. In this example I am looking only at negative surprises (i.e. when actual interest rate changes<expected interest rate changes), so that is why the target_id does not count positive surprises.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long Date double RealPrice str6 NewtargetFFrate str5 Change byte Surprise str3 Expected float(ret day_cnt target_day target_id mkret)
    12161988 2464.0391 "0"     "0"       0 "0"    -2.8297124  980    . .    .05851697
    12191988 2371.7617 "0"     "0"       0 "0"      -3.81689  981    . .   .014021377
    12201988 2395.5908 "0"     "0"       0 "0"      .9996869  982    . .   .008627481
    12211988 2460.5762 "0"     "0"       0 "0"      2.676567  983    . .    .03253506
    12221988 2496.4266 "0"     "0"       0 "0"       1.44648  984    . .    .06023775
    12231988 2494.6437 "0"     "0"       0 "0"   -.071443595  985    . .   .072360724
    12261988 2477.7128 "0"     "0"       0 "0"     -.6810037  986    . .     .0770777
    12271988 2466.8838 "0"     "0"       0 "0"     -.4380142  987    . .    .06843801
    12281988 2440.3787 "0"     "0"       0 "0"    -1.0802503  988    . .    .07005557
    12291988 2444.1652 "0"     "0"       0 "0"      .1550401  989    . .    .06855935
      121989 2362.7816 "0"     "0"       0 "0"    -3.3864064  990    . .    .03809728
      131989 2327.7026 "0"     "0"       0 "0"    -1.4957796  991    . .   .022140115
      141989 2317.2013 "0"     "0"       0 "0"     -.4521651  992    . .   .024277734
      151989 2342.0382 "9"     "31.25"  -7 "38"    1.0661454  993  993 3   .036884636
      161989 2346.0133 "0"     "0"       0 "0"     .16958435  994    . .    .05970508
      191989 2331.7127 "0"     "0"       0 "0"     -.6114358  995    . .    .06971015
     1101989 2295.6297 "0"     "0"       0 "0"    -1.5595877  996    . .    .05868369
     1111989 2295.3626 "0"     "0"       0 "0"    -.01163583  997    . .    .04518678
     1121989 2335.5932 "0"     "0"       0 "0"      1.737508  998    . .   .034511913
     1131989  2360.314 "0"     "0"       0 "0"     1.0528755  999    . .    .03878065
     1161989 2389.6377 "8.5"   "-25"     4 "-29"   1.2347103 1000    . .    .05540232
     1171989 2444.1239 "0"     "0"       0 "0"      2.254497 1001    . .    .08175389
     1181989 2417.8188 "0"     "0"       0 "0"    -1.0820924 1002    . .    .07725401
     1191989  2378.164 "0"     "0"       0 "0"     -1.653705 1003    . .    .06821929
     1201989 2395.4742 "8.25"  "-25"   -17 "-8"     .7252446 1004 1004 4    .07172761
     1231989 2370.7793 "0"     "0"       0 "0"    -1.0362487 1005    . .    .05284029
     1241989 2360.8008 "0"     "0"       0 "0"     -.4217836 1006    . .    .03849471
     1251989 2361.0746 "0"     "0"       0 "0"    .011597087 1007    . .   .013611864
     1261989 2389.8247 "0"     "0"       0 "0"     1.2103162 1008    . .   .003226084
     1271989 2399.5271 "0"     "0"       0 "0"       .405166 1009    . .   .007926226
     1301989 2378.5798 "0"     "0"       0 "0"     -.8768089 1010    . .  -.003822959
     1311989 2368.4818 "0"     "0"       0 "0"     -.4254428 1011    . .  -.005608775
      211989 2355.9894 "0"     "0"       0 "0"    -.52883923 1012    . .    .02691932
      221989 2365.0959 "0"     "0"       0 "0"      .3857804 1013    . .    .03531341
      231989 2395.7901 "0"     "0"       0 "0"       1.28945 1014    . .    .03545245
      261989 2400.4548 "0"     "0"       0 "0"     .19451474 1015    . .   .016686652
      271989 2399.9275 "0"     "0"       0 "0"   -.021969084 1016    . .     .0103428
      281989 2401.2843 "0"     "0"       0 "0"     .05651907 1017    . .   .018845793
      291989 2390.9525 "9.125" "12.5"    1 "11"    -.4311897 1018    . .   .016037846
     2101989 2382.9063 "0"     "0"       0 "0"     -.3370945 1019    . . -.0025060754
     2131989  2361.808 "0"     "0"       0 "0"      -.889345 1020    . .  -.011229324
    end

    Below is my code:

    Code:
    *Creating my AR matrix and running my loop. This is where I run into my problems*
    mat mat_ar = J(41,52,.)
    
    foreach x of varlist target_id{
        egen max_target_day=max(target_day) if id==`x'
        gen evday=day_cnt-max_target_day
    
        sort evday
        gen evt_window=1 if evday>=-20 & evday<=20
        gen est_window=1 if evday<=-21 & evday>=-200
        drop if evt_window==.&est_window==.
        
        qui reg ret mkret if est_window==1
        gen rmse=e(rmse)
    
        predict phat
        mat mat_ar[`a', `i'+1] = ret-phat if evt_window==1
        drop phat
        drop if evt_window==0
        keep if evday>=-20 & evday<=+20
        
        egen car=sum(ar)
        gen tstat=car/(rmse*sqrt(_N))
        gen car_ca = sum(ar)
    }
    
    *Calculating the average ARs over the sample and the overall CAR and significance*
    egen AR_mean = rowmean(mat_ar)
    egen CAR_mean = sum(AR_mean)
    gen tstat = CAR_mean/(rmse*sqrt(41))
    gen CAR_sum = sum(AR_mean)
    list AR_mean CAR_sum
    graph twoway line CAR_sum evday
    
    *Visualizing the announcement impact*
    list ar car_ca
    graph twoway line car_ca evday

    My event window is <-20,1,+20> so I create a matrix with 41 rows and 52 columns comprising the 52 events. Then I attempt to run the event study regression 52 times, storing the 41 AR values in a column of the matrix. In the end I hope to average across the rows to calculate the average AR for each evday. This way I can calculate the eventual CAR for the period.

    However I am new to using loops in STATA and was hoping someone could help advise on how to continue? What I am looking for is rather simple but I am unfamiliar with the commands. Is there a simpler approach to calculating the mean ARs and overall CAR for the sample?

    I am open to your advice, thank you.
    Last edited by James Nelson; 15 Jul 2019, 13:22.

  • #2
    Your question covers a lot more territory than can easily be answered in a single post. Here are a few suggestions to get you started:

    1) Try taking another look at -help foreach-. I say this because your foreach command only requests iteration over one variable.
    Code:
    foreach x of varlist target_id
    -foreach- is intended to iterate over some set of variables, or some list of values. Perhaps you mistakenly thought that this command would iterate over the values of -targetid-? (It won't.) What did you think this -foreach- structure would do?

    2) See -help statsby-, which I will show you a more built-in way to collect statistical results than what you are trying. This will require making your code into a program using the -program- command, and storing the results in the return list, but I think that you would find that easier to learn about than what you are trying to do. An rclass program would be fine for your purposes.
    3) Your posting requires some knowledge about event studies and the related vocabulary, yet I think your problem is actually a straightforward programming problem that would not require such knowledge. There's a larger set of people that could help you if you explain your goals without requiring knowledge in your research area.
    4) You know what your variables mean, but we (mostly) don't. That gap makes helping you harder.

    Comment

    Working...
    X