Announcement

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

  • Estimating normal performance (Event Study)

    Dear StataWizards,

    Currently I am performing an event study regarding earnings announcement. I am facing a problem estimating the normal performance because the loop I created with the help of Princeton University takes a lot of time to perform. I tried to use the following loop to estimate the normal returns:
    forvalues i=1(1)14625 { l id company_id if id==`i' & dif==0 reg ret market_return if id==`i' & estimation_window==1 predict p if id==`i' replace predicted_return = p if id==`i' & event_window==1 drop p } As you can see I have 14625 regressions to run. Today I tried to execute the loop I described above, I have waited for 2 hours and only 150 out of the 14625 regressions were finished. Does anybody has a clue how to solve this problem? Is there something wrong with my loop? or is there a command which is able to speed up the execution of my loop? (I already set more off)

    Kind Regards,

    Arnout Poelstra

  • #2
    So something like this (not tested as you provided no example data)

    Code:
    capture program drop
    program define my_predictions
        regress ret market_return
        predict predicted_return, xb
        exit
    end
    
    keep if inrange(company_id, 1, 14625) ///
        & dif == 0 & estimation_window == 1
    
    rangerun my_predictions, interval(dif . .) by(company_id) use(ret market_return)
    Note: -rangerun- is a user written program from Robert Picard and Nick Cox. You can get it from SSC. Also, in order to use it, you must also install -rangestat- from SSC. (The latter is written by the same pair plus Roberto Ferrer.)

    This will speed things up enormously. Towards that end, I have first discarded from the data set any observations whose company_id's are out of the 1 to 14625 range, or have dif != 0 or estimation_window != 1. Those observations play no role in these calculations, and applying -if- conditions to exclude them from the regressions is the biggest part of what's slowing you down. In addition, rangerun does the bookkeeping of looping over company_ids and updating the value of the predicted_return variable for you, and does it faster than you would do it with regular commands that involve using -if-.

    That said, 14,625 regressions is going to take some time.

    Comment


    • #3
      Hi Clyde,

      Thank you for your help. However, I have a new question now. After installing -rangerun- and commanding the following commands:

      capture program drop

      program define my_predictions regress ret market_return

      predict predicted_return, xb

      exit

      end

      keep if inrange(group_id, 1, 14962) & dif == 0 & estimation_window == 1

      All my observations are deleted. Can you tell me if there is something wrong with my dataset? If I use the loop i described in my first message I do obtain predicted retursn, only it takes a lot of time. Commands I used but took to much time (According to Princeton University I first created group_id's because I face multiple events per firm):

      forvalues i=1(1)14962
      l id group_id if id==`i' & dif==0 reg ret market_return if id==`i' & estimation_window==1
      predict p if id==`i'
      replace predicted_return = p if id==`i' & event_window==1
      drop p
      } I


      will try to show you a sample of my dataset:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str5 company_id float date double return float(set group_id event_date datenum td dif event_window count_event_obs estimation_window count_est_obs predicted_return id) double market_return
      "MMF" 15340                  0 17  8072 19584 1 3707 -3706 0 3 0 120 .  8040 .68
      "ZZC" 15340              -1.46  3 14919 18381 1 3679 -3678 0 3 0 120 . 14887 .68
      "BBK" 15340                  0  7  3257 20124 1 3678 -3677 0 3 0 120 .  3225 .68
      "BNP" 15340                  0 39  3628 19117 1 3680 -3679 0 3 0 120 .  3596 .68
      "SQJ" 15340                  0 28 11804 20299 1 3682 -3681 0 3 0 120 . 11772 .68
      "DBK" 15340                  0 19  4398 16924 1 3674 -3673 0 3 0 120 .  4366 .68
      "MO8" 15340                  0 30  8124 19430 1 3705 -3704 0 3 0 120 .  8092 .68
      "BLC" 15340                  0  3  3512 18127 1 2711 -2710 0 3 0 120 .  3480 .68
      "REC" 15340                  0  5 10512 18834 1 3684 -3683 0 3 0 120 . 10480 .68
      "ATC" 15340                  0 41  2997 16260 1 3674 -3673 0 3 0 120 .  2965 .68
      "TLT" 15340                  0 41 12402 16560 1 3684 -3683 0 3 0 120 . 12370 .68
      "LX2" 15340                  0  4  7773 20026 1 3698 -3697 0 3 0 120 .  7741 .68
      "KBB" 15340                  0  3  7004 19032 1 3689 -3688 0 3 0 120 .  6972 .68
      "9EZ" 15340                  0  3  1850 20220 1 3707 -3706 0 3 0 120 .  1818 .68
      "WNI" 15340                  0 45 14002 19396 1 3609 -3608 0 3 0 120 . 13970 .68
      "T1D" 15340                  0 34 12008 17374 1 3675 -3674 0 3 0 120 . 11976 .68
      "PSU" 15340                  0  9  9789 18219 1 3702 -3701 0 3 0 120 .  9757 .68
      "MUV" 15340                  0 30  8208 18661 1 3679 -3678 0 3 0 120 .  8176 .68
      "SIE" 15340                  0 15 11304 19935 1 3672 -3671 0 3 0 120 . 11272 .68
      "T1Z" 15340                  0 10 12027 19647 1 3668 -3667 0 3 0 120 . 11995 .68
      "KSK" 15340                  0 32  7261 16294 1 3678 -3677 0 3 0 120 .  7229 .68
      "SML" 15340                  0 20 11680 19220 1 3684 -3683 0 3 0 120 . 11648 .68
      "TPQ" 15340                  0  6 12548 17750 1 3677 -3676 0 3 0 120 . 12516 .68
      "2FG" 15340                  0 23   632 18595 1 3443 -3442 0 3 0 120 .   608 .68
      "Q7A" 15340                  0 30  9991 17573 1 3677 -3676 0 3 0 120 .  9959 .68
      "I8I" 15340                  0 15  6581 18107 1 3680 -3679 0 3 0 120 .  6549 .68
      "SCE" 15340                  0 17 11158 17007 1 3674 -3673 0 3 0 120 . 11126 .68
      "HTE" 15340                  0  3  6402 18325 1 3699 -3698 0 3 0 120 .  6370 .68
      "FMN" 15340                  0  9  5364 18500 1 3614 -3613 0 3 0 120 .  5332 .68
      "VB9" 15340                  0 27 13188 16826 1 3678 -3677 0 3 0 120 . 13156 .68
      "BZ3" 15340               2.67 30  3893 19655 1 3673 -3672 0 3 0 120 .  3861 .68
      "5VW" 15340                  0 18  1177 17037 1 3682 -3681 0 3 0 120 .  1153 .68
      "CB2" 15340                  0 33  3926 18678 1 3683 -3682 0 3 0 120 .  3894 .68
      "VRR" 15340                  0 24 13484 19192 1 3677 -3676 0 3 0 120 . 13452 .68
      "WXB" 15340                  0 31 14143 20381 1 3682 -3681 0 3 0 120 . 14111 .68
      "NON" 15340                  0 38  8620 17093 1 3673 -3672 0 3 0 120 .  8588 .68
      "TLB" 15340                  0  7 12326 18309 1 3679 -3678 0 3 0 120 . 12294 .68
      "DKB" 15340                  0  6  4429 19123 1 3677 -3676 0 3 0 120 .  4397 .68
      "RSL" 15340                  0 36 10767 20299 1 3694 -3693 0 3 0 120 . 10735 .68
      "SM0" 15340                  0 15 11588 16916 1 3673 -3672 0 3 0 120 . 11556 .68
      "9MB" 15340                  0  1  2017 17842 1 3693 -3692 0 3 0 120 .  1985 .68
      "BSF" 15340                  0 37  3811 20208 1 3695 -3694 0 3 0 120 .  3779 .68
      "AER" 15340                  0 32  2580 19389 1 3678 -3677 0 3 0 120 .  2548 .68
      "RDN" 15340                  0 18 10482 20299 1 3679 -3678 0 3 0 120 . 10450 .68
      "KNE" 15340                  0 32  7118 18288 1 3674 -3673 0 3 0 120 .  7086 .68
      "OTK" 15340                  0  5  9179 20207 1 3684 -3683 0 3 0 120 .  9147 .68
      "8IL" 15340                  0 40  1452 17120 1 3683 -3682 0 3 0 120 .  1420 .68
      "KBB" 15340                  0 26  7027 17479 1 3689 -3688 0 3 0 120 .  6995 .68
      "HO2" 15340                  0  2  6225 20513 1 3696 -3695 0 3 0 120 .  6193 .68
      "DNB" 15340                  0 10  4520 18452 1 3679 -3678 0 3 0 120 .  4488 .68
      "LIY" 15340                  0 12  7688 19556 1 3668 -3667 0 3 0 120 .  7656 .68
      "1FA" 15340                  0 17   384 18479 1 3708 -3707 0 3 0 120 .   360 .68
      "CMF" 15340                  0  2  4034 16588 1 2759 -2758 0 3 0 120 .  4002 .68
      "BMW" 15340                  0 42  3587 17379 1 3708 -3707 0 3 0 120 .  3555 .68
      "KYM" 15340                  0 41  7357 19842 1 3677 -3676 0 3 0 120 .  7325 .68
      "NNA" 15340                  0 14  8500 19297 1 3678 -3677 0 3 0 120 .  8468 .68
      "CON" 15340                  0 45  4147 18316 1 3699 -3698 0 3 0 120 .  4115 .68
      "PPJ" 15340                  0 18  9660 20207 1 3688 -3687 0 3 0 120 .  9628 .68
      "HUF" 15340                  0 19  6456 19669 1 3684 -3683 0 3 0 120 .  6424 .68
      "AAY" 15340                  0  1  2378 16665 1 3681 -3680 0 3 0 120 .  2346 .68
      "LUB" 15340                  0 19  7743 16384 1 3683 -3682 0 3 0 120 .  7711 .68
      "DX2" 15340                  0  9  4775 16687 1 2760 -2759 0 3 0 120 .  4743 .68
      "VLM" 15340                  0 30 13365 18472 1 3679 -3678 0 3 0 120 . 13333 .68
      "X7R" 15340                  0 36 14388 18687 1 3692 -3691 0 3 0 120 . 14356 .68
      "ARX" 15340                  0 18  2938 20299 1 3696 -3695 0 3 0 120 .  2906 .68
      "BEI" 15340                  0 36  3421 20305 1 3688 -3687 0 3 0 120 .  3389 .68
      "9KG" 15340                .78 13  1970 19584 1 3709 -3708 0 3 0 120 .  1938 .68
      "OTK" 15340                  0 14  9188 18737 1 3684 -3683 0 3 0 120 .  9156 .68
      "BA2" 15340                  0 18  3157 18844 1 3612 -3611 0 3 0 120 .  3125 .68
      "ZEK" 15340                  0 33 14731 19514 1 3698 -3697 0 3 0 120 . 14699 .68
      "AER" 15340                  0 39  2587 20201 1 3678 -3677 0 3 0 120 .  2555 .68
      "WIM" 15340                  0  5 13914 17757 1 3679 -3678 0 3 0 120 . 13882 .68
      "1NL" 15340                  0 24   447 16749 1 3712 -3711 0 3 0 120 .   423 .68
      "TTM" 15340                  0 42 12674 18737 1 3673 -3672 0 3 0 120 . 12642 .68
      "KAS" 15340                  0 24  6988 17848 1 3704 -3703 0 3 0 120 .  6956 .68
      "JMN" 15340                  0 29  6785 19472 1 3698 -3697 0 3 0 120 .  6753 .68
      "B48" 15340                  0  3  3098 18290 1 3675 -3674 0 3 0 120 .  3066 .68
      "ZCT" 15340                  0  6 14660 18197 1 3690 -3689 0 3 0 120 . 14628 .68
      "9BS" 15340                .31  1  1789 18674 1 3679 -3678 0 3 0 120 .  1757 .68
      "5HM" 15340                  0 22   882 17819 1 3680 -3679 0 3 0 120 .   858 .68
      "JMX" 15340                  0 35  6828 16736 1 3679 -3678 0 3 0 120 .  6796 .68
      "JMN" 15340                  0  3  6759 17743 1 3698 -3697 0 3 0 120 .  6727 .68
      "UI7" 15340                  0 40 12916 17664 1 3709 -3708 0 3 0 120 . 12884 .68
      "LHA" 15340                  0  9  7570 17009 1 3709 -3708 0 3 0 120 .  7538 .68
      "FLW" 15340                  0 19  5353 18220 1 3172 -3171 0 3 0 120 .  5321 .68
      "AB9" 15340                  0  3  2426 17776 1 2522 -2521 0 3 0 120 .  2394 .68
      "KNI" 15340                  0  8  7139 18735 1 3698 -3697 0 3 0 120 .  7107 .68
      "B00" 15340                  0 29  3061 17409 1 3685 -3684 0 3 0 120 .  3029 .68
      "H87" 15340                  0 26  5999 19940 1 3699 -3698 0 3 0 120 .  5967 .68
      "SMU" 15340 -.9500000000000001 34 11729 18844 1 3419 -3418 0 3 0 120 . 11697 .68
      "TTD" 15340                  0 12 12599 18379 1 3679 -3678 0 3 0 120 . 12567 .68
      "KUG" 15340                  0 17  7291 18556 1 3688 -3687 0 3 0 120 .  7259 .68
      "R2N" 15340              1.429 35 10359 18833 1 3676 -3675 0 3 0 120 . 10327 .68
      "MED" 15340                  0 11  7894 17297 1 3684 -3683 0 3 0 120 .  7862 .68
      "ORH" 15340                  0 40  9123 20207 1 3677 -3676 0 3 0 120 .  9091 .68
      "S4C" 15340                  0 25 10976 18666 1 3682 -3681 0 3 0 120 . 10944 .68
      "HHT" 15340                  0 31  6133 20019 1 3684 -3683 0 3 0 120 .  6101 .68
      "PH4" 15340               2.02 40  9552 17647 1 3684 -3683 0 3 0 120 .  9520 .68
      "LUB" 15340                  0 22  7746 17120 1 3683 -3682 0 3 0 120 .  7714 .68
      "HEX" 15340                  0  1  6059 17833 1 3680 -3679 0 3 0 120 .  6027 .68
      end
      format %tdnn/dd/CCYY date
      format %tdDD/NN/CCYY event_date
      Do you have any idea what is wrong with my dataset? Or do you know another loop how I will be able to estimate forecasted normal performance?

      Many thanks in advance,

      Arnout
      Last edited by Arnout Poelstra; 14 Jul 2017, 01:50.

      Comment


      • #4
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str5 company_id float date double return float(set group_id event_date datenum td dif event_window count_event_obs estimation_window count_est_obs predicted_return id) double market_return
        "MMF" 15340                  0 17  8072 19584 1 3707 -3706 0 3 0 120 .  8040 .68
        "ZZC" 15340              -1.46  3 14919 18381 1 3679 -3678 0 3 0 120 . 14887 .68
        "BBK" 15340                  0  7  3257 20124 1 3678 -3677 0 3 0 120 .  3225 .68
        "BNP" 15340                  0 39  3628 19117 1 3680 -3679 0 3 0 120 .  3596 .68
        "SQJ" 15340                  0 28 11804 20299 1 3682 -3681 0 3 0 120 . 11772 .68
        "DBK" 15340                  0 19  4398 16924 1 3674 -3673 0 3 0 120 .  4366 .68
        "MO8" 15340                  0 30  8124 19430 1 3705 -3704 0 3 0 120 .  8092 .68
        "BLC" 15340                  0  3  3512 18127 1 2711 -2710 0 3 0 120 .  3480 .68
        "REC" 15340                  0  5 10512 18834 1 3684 -3683 0 3 0 120 . 10480 .68
        "ATC" 15340                  0 41  2997 16260 1 3674 -3673 0 3 0 120 .  2965 .68
        "TLT" 15340                  0 41 12402 16560 1 3684 -3683 0 3 0 120 . 12370 .68
        "LX2" 15340                  0  4  7773 20026 1 3698 -3697 0 3 0 120 .  7741 .68
        "KBB" 15340                  0  3  7004 19032 1 3689 -3688 0 3 0 120 .  6972 .68
        "9EZ" 15340                  0  3  1850 20220 1 3707 -3706 0 3 0 120 .  1818 .68
        "WNI" 15340                  0 45 14002 19396 1 3609 -3608 0 3 0 120 . 13970 .68
        "T1D" 15340                  0 34 12008 17374 1 3675 -3674 0 3 0 120 . 11976 .68
        "PSU" 15340                  0  9  9789 18219 1 3702 -3701 0 3 0 120 .  9757 .68
        "MUV" 15340                  0 30  8208 18661 1 3679 -3678 0 3 0 120 .  8176 .68
        "SIE" 15340                  0 15 11304 19935 1 3672 -3671 0 3 0 120 . 11272 .68
        "T1Z" 15340                  0 10 12027 19647 1 3668 -3667 0 3 0 120 . 11995 .68
        "KSK" 15340                  0 32  7261 16294 1 3678 -3677 0 3 0 120 .  7229 .68
        "SML" 15340                  0 20 11680 19220 1 3684 -3683 0 3 0 120 . 11648 .68
        "TPQ" 15340                  0  6 12548 17750 1 3677 -3676 0 3 0 120 . 12516 .68
        "2FG" 15340                  0 23   632 18595 1 3443 -3442 0 3 0 120 .   608 .68
        "Q7A" 15340                  0 30  9991 17573 1 3677 -3676 0 3 0 120 .  9959 .68
        "I8I" 15340                  0 15  6581 18107 1 3680 -3679 0 3 0 120 .  6549 .68
        "SCE" 15340                  0 17 11158 17007 1 3674 -3673 0 3 0 120 . 11126 .68
        "HTE" 15340                  0  3  6402 18325 1 3699 -3698 0 3 0 120 .  6370 .68
        "FMN" 15340                  0  9  5364 18500 1 3614 -3613 0 3 0 120 .  5332 .68
        "VB9" 15340                  0 27 13188 16826 1 3678 -3677 0 3 0 120 . 13156 .68
        "BZ3" 15340               2.67 30  3893 19655 1 3673 -3672 0 3 0 120 .  3861 .68
        "5VW" 15340                  0 18  1177 17037 1 3682 -3681 0 3 0 120 .  1153 .68
        "CB2" 15340                  0 33  3926 18678 1 3683 -3682 0 3 0 120 .  3894 .68
        "VRR" 15340                  0 24 13484 19192 1 3677 -3676 0 3 0 120 . 13452 .68
        "WXB" 15340                  0 31 14143 20381 1 3682 -3681 0 3 0 120 . 14111 .68
        "NON" 15340                  0 38  8620 17093 1 3673 -3672 0 3 0 120 .  8588 .68
        "TLB" 15340                  0  7 12326 18309 1 3679 -3678 0 3 0 120 . 12294 .68
        "DKB" 15340                  0  6  4429 19123 1 3677 -3676 0 3 0 120 .  4397 .68
        "RSL" 15340                  0 36 10767 20299 1 3694 -3693 0 3 0 120 . 10735 .68
        "SM0" 15340                  0 15 11588 16916 1 3673 -3672 0 3 0 120 . 11556 .68
        "9MB" 15340                  0  1  2017 17842 1 3693 -3692 0 3 0 120 .  1985 .68
        "BSF" 15340                  0 37  3811 20208 1 3695 -3694 0 3 0 120 .  3779 .68
        "AER" 15340                  0 32  2580 19389 1 3678 -3677 0 3 0 120 .  2548 .68
        "RDN" 15340                  0 18 10482 20299 1 3679 -3678 0 3 0 120 . 10450 .68
        "KNE" 15340                  0 32  7118 18288 1 3674 -3673 0 3 0 120 .  7086 .68
        "OTK" 15340                  0  5  9179 20207 1 3684 -3683 0 3 0 120 .  9147 .68
        "8IL" 15340                  0 40  1452 17120 1 3683 -3682 0 3 0 120 .  1420 .68
        "KBB" 15340                  0 26  7027 17479 1 3689 -3688 0 3 0 120 .  6995 .68
        "HO2" 15340                  0  2  6225 20513 1 3696 -3695 0 3 0 120 .  6193 .68
        "DNB" 15340                  0 10  4520 18452 1 3679 -3678 0 3 0 120 .  4488 .68
        "LIY" 15340                  0 12  7688 19556 1 3668 -3667 0 3 0 120 .  7656 .68
        "1FA" 15340                  0 17   384 18479 1 3708 -3707 0 3 0 120 .   360 .68
        "CMF" 15340                  0  2  4034 16588 1 2759 -2758 0 3 0 120 .  4002 .68
        "BMW" 15340                  0 42  3587 17379 1 3708 -3707 0 3 0 120 .  3555 .68
        "KYM" 15340                  0 41  7357 19842 1 3677 -3676 0 3 0 120 .  7325 .68
        "NNA" 15340                  0 14  8500 19297 1 3678 -3677 0 3 0 120 .  8468 .68
        "CON" 15340                  0 45  4147 18316 1 3699 -3698 0 3 0 120 .  4115 .68
        "PPJ" 15340                  0 18  9660 20207 1 3688 -3687 0 3 0 120 .  9628 .68
        "HUF" 15340                  0 19  6456 19669 1 3684 -3683 0 3 0 120 .  6424 .68
        "AAY" 15340                  0  1  2378 16665 1 3681 -3680 0 3 0 120 .  2346 .68
        "LUB" 15340                  0 19  7743 16384 1 3683 -3682 0 3 0 120 .  7711 .68
        "DX2" 15340                  0  9  4775 16687 1 2760 -2759 0 3 0 120 .  4743 .68
        "VLM" 15340                  0 30 13365 18472 1 3679 -3678 0 3 0 120 . 13333 .68
        "X7R" 15340                  0 36 14388 18687 1 3692 -3691 0 3 0 120 . 14356 .68
        "ARX" 15340                  0 18  2938 20299 1 3696 -3695 0 3 0 120 .  2906 .68
        "BEI" 15340                  0 36  3421 20305 1 3688 -3687 0 3 0 120 .  3389 .68
        "9KG" 15340                .78 13  1970 19584 1 3709 -3708 0 3 0 120 .  1938 .68
        "OTK" 15340                  0 14  9188 18737 1 3684 -3683 0 3 0 120 .  9156 .68
        "BA2" 15340                  0 18  3157 18844 1 3612 -3611 0 3 0 120 .  3125 .68
        "ZEK" 15340                  0 33 14731 19514 1 3698 -3697 0 3 0 120 . 14699 .68
        "AER" 15340                  0 39  2587 20201 1 3678 -3677 0 3 0 120 .  2555 .68
        "WIM" 15340                  0  5 13914 17757 1 3679 -3678 0 3 0 120 . 13882 .68
        "1NL" 15340                  0 24   447 16749 1 3712 -3711 0 3 0 120 .   423 .68
        "TTM" 15340                  0 42 12674 18737 1 3673 -3672 0 3 0 120 . 12642 .68
        "KAS" 15340                  0 24  6988 17848 1 3704 -3703 0 3 0 120 .  6956 .68
        "JMN" 15340                  0 29  6785 19472 1 3698 -3697 0 3 0 120 .  6753 .68
        "B48" 15340                  0  3  3098 18290 1 3675 -3674 0 3 0 120 .  3066 .68
        "ZCT" 15340                  0  6 14660 18197 1 3690 -3689 0 3 0 120 . 14628 .68
        "9BS" 15340                .31  1  1789 18674 1 3679 -3678 0 3 0 120 .  1757 .68
        "5HM" 15340                  0 22   882 17819 1 3680 -3679 0 3 0 120 .   858 .68
        "JMX" 15340                  0 35  6828 16736 1 3679 -3678 0 3 0 120 .  6796 .68
        "JMN" 15340                  0  3  6759 17743 1 3698 -3697 0 3 0 120 .  6727 .68
        "UI7" 15340                  0 40 12916 17664 1 3709 -3708 0 3 0 120 . 12884 .68
        "LHA" 15340                  0  9  7570 17009 1 3709 -3708 0 3 0 120 .  7538 .68
        "FLW" 15340                  0 19  5353 18220 1 3172 -3171 0 3 0 120 .  5321 .68
        "AB9" 15340                  0  3  2426 17776 1 2522 -2521 0 3 0 120 .  2394 .68
        "KNI" 15340                  0  8  7139 18735 1 3698 -3697 0 3 0 120 .  7107 .68
        "B00" 15340                  0 29  3061 17409 1 3685 -3684 0 3 0 120 .  3029 .68
        "H87" 15340                  0 26  5999 19940 1 3699 -3698 0 3 0 120 .  5967 .68
        "SMU" 15340 -.9500000000000001 34 11729 18844 1 3419 -3418 0 3 0 120 . 11697 .68
        "TTD" 15340                  0 12 12599 18379 1 3679 -3678 0 3 0 120 . 12567 .68
        "KUG" 15340                  0 17  7291 18556 1 3688 -3687 0 3 0 120 .  7259 .68
        "R2N" 15340              1.429 35 10359 18833 1 3676 -3675 0 3 0 120 . 10327 .68
        "MED" 15340                  0 11  7894 17297 1 3684 -3683 0 3 0 120 .  7862 .68
        "ORH" 15340                  0 40  9123 20207 1 3677 -3676 0 3 0 120 .  9091 .68
        "S4C" 15340                  0 25 10976 18666 1 3682 -3681 0 3 0 120 . 10944 .68
        "HHT" 15340                  0 31  6133 20019 1 3684 -3683 0 3 0 120 .  6101 .68
        "PH4" 15340               2.02 40  9552 17647 1 3684 -3683 0 3 0 120 .  9520 .68
        "LUB" 15340                  0 22  7746 17120 1 3683 -3682 0 3 0 120 .  7714 .68
        "HEX" 15340                  0  1  6059 17833 1 3680 -3679 0 3 0 120 .  6027 .68
        end
        format %tdnn/dd/CCYY date
        format %tdDD/NN/CCYY event_date
        
        summ group_id
        tab dif
        tab estimation_window
        will show you that none of your observations satisfies the conditions for being included in the regressions. All of the group_ids are in the desired range, but dif is never 0, and estimation_window is never 1.

        So the mystery is why your original code produced any results, and I don't have any explanation for that.

        Now, scrutinizing the original code more carefully, I notice that -dif == 0- is not actually part of the condition for being in the -regress- or -predict- estimation samples, it only appears in the -list- command. So we could remove the -dif == 0- part from the -keep- command. But you will still end up with nothing because you have no observations with estimation_window = 1. I also note that in my proposed solution, I confused the variables id and group_id. So it should be:

        Code:
        capture program drop
        program define my_predictions
            regress ret market_return
            predict predicted_return, xb
            exit
        end
        
        keep if inrange(id, 1, 14625) & estimation_window == 1
        
        rangerun my_predictions, interval(dif . .) by(id) use(ret market_return)
        But because there are no observations having estimation_window = 1, you still won't get any output. So you need to find out what happened to all the observations with estimation_window = 1.

        Comment


        • #5
          Hello Clyde,

          First of all, thanks for your help. I made a mistake merging my datasets which was the cause of the fact that dif never was 0 and the estimation_window never was 1. I solved this problem but still it takes way to much time regress, also with the help of rangerun. After installing rangerun I executed the following commands:

          capture program drop
          program define my_predictions
          regress ret market_return predict
          predicted_return, xb
          exit
          end

          keep if inrange(id, 1, 14919) & estimation_window == 1

          After conducting this last command my observations decreased from 57476601 to 2983800. However, after commanding:

          rangerun my_predictions, interval(dif . .) by(id) use(ret market_return)

          I have waited for 8 hours now and Stata is still busy. The computers at the University are pretty fast so that will probably not be the problem. Could you please take another look at my dataset, after the command:

          keep if inrange(id, 1, 14919) & estimation_window == 1

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str5 company_id int date double(return market_return) float(set event_date    group_id    datenum    td    dif    event_window    count_event_obs    estimation_window    count_est_obs    predicted_return    id)
          "002W" 20221  1.12                .727 1 20515 1 304 514 -210 0 3 1 200 . 1
          "002W" 20222     0                .865 1 20515 1 305 514 -209 0 3 1 200 . 1
          "002W" 20223     0                .082 1 20515 1 306 514 -208 0 3 1 200 . 1
          "002W" 20226   .04               -.245 1 20515 1 307 514 -207 0 3 1 200 . 1
          "002W" 20227  1.59 -.47000000000000003 1 20515 1 308 514 -206 0 3 1 200 . 1
          "002W" 20228  -.17                .201 1 20515 1 309 514 -205 0 3 1 200 . 1
          "002W" 20229  4.62                .551 1 20515 1 310 514 -204 0 3 1 200 . 1
          "002W" 20230  -.96               -.926 1 20515 1 311 514 -203 0 3 1 200 . 1
          "002W" 20233     0               -.276 1 20515 1 312 514 -202 0 3 1 200 . 1
          "002W" 20234   .29              -1.917 1 20515 1 313 514 -201 0 3 1 200 . 1
          "002W" 20235  -.67               1.123 1 20515 1 314 514 -200 0 3 1 200 . 1
          "002W" 20236  -.59               -.115 1 20515 1 315 514 -199 0 3 1 200 . 1
          "002W" 20237  -.81               -1.32 1 20515 1 316 514 -198 0 3 1 200 . 1
          "002W" 20240   .86 -.47600000000000003 1 20515 1 317 514 -197 0 3 1 200 . 1
          "002W" 20241 -1.66               1.071 1 20515 1 318 514 -196 0 3 1 200 . 1
          "002W" 20242  1.25                 1.1 1 20515 1 319 514 -195 0 3 1 200 . 1
          "002W" 20243  -.09  -.6980000000000001 1 20515 1 320 514 -194 0 3 1 200 . 1
          "002W" 20244     0              -2.415 1 20515 1 321 514 -193 0 3 1 200 . 1
          "002W" 20247 -1.32                -.08 1 20515 1 322 514 -192 0 3 1 200 . 1
          "002W" 20248 -1.52               -.058 1 20515 1 323 514 -191 0 3 1 200 . 1
          "002W" 20249  1.36               2.349 1 20515 1 324 514 -190 0 3 1 200 . 1
          "002W" 20250 -1.08               -.061 1 20515 1 325 514 -189 0 3 1 200 . 1
          "002W" 20251  -.09               -.628 1 20515 1 326 514 -188 0 3 1 200 . 1
          "002W" 20254 -1.14              -1.746 1 20515 1 327 514 -187 0 3 1 200 . 1
          "002W" 20255   .04                .466 1 20515 1 328 514 -186 0 3 1 200 . 1
          "002W" 20256  -.62                -.46 1 20515 1 329 514 -185 0 3 1 200 . 1
          "002W" 20257  -.49  1.6580000000000001 1 20515 1 330 514 -184 0 3 1 200 . 1
          "002W" 20258 -1.39               -.309 1 20515 1 331 514 -183 0 3 1 200 . 1
          "002W" 20261  1.18               2.888 1 20515 1 332 514 -182 0 3 1 200 . 1
          "002W" 20262  1.44               -.752 1 20515 1 333 514 -181 0 3 1 200 . 1
          "002W" 20263  -.66               -.342 1 20515 1 334 514 -180 0 3 1 200 . 1
          "002W" 20264 -1.16               -.221 1 20515 1 335 514 -179 0 3 1 200 . 1
          "002W" 20265  -.09 -.34900000000000003 1 20515 1 336 514 -178 0 3 1 200 . 1
          "002W" 20268   .18              -2.617 1 20515 1 337 514 -177 0 3 1 200 . 1
          "002W" 20269  -.41               -1.41 1 20515 1 338 514 -176 0 3 1 200 . 1
          "002W" 20270     1               1.124 1 20515 1 339 514 -175 0 3 1 200 . 1
          "002W" 20271   .04                -.37 1 20515 1 340 514 -174 0 3 1 200 . 1
          "002W" 20272   .22  -.5680000000000001 1 20515 1 341 514 -173 0 3 1 200 . 1
          "002W" 20275  -.18              -1.476 1 20515 1 342 514 -172 0 3 1 200 . 1
          "002W" 20276  -.76              -2.818 1 20515 1 343 514 -171 0 3 1 200 . 1
          "002W" 20277  -.63                1.31 1 20515 1 344 514 -170 0 3 1 200 . 1
          "002W" 20278  1.73               2.013 1 20515 1 345 514 -169 0 3 1 200 . 1
          "002W" 20279   2.9               3.214 1 20515 1 346 514 -168 0 3 1 200 . 1
          "002W" 20282     2                .458 1 20515 1 347 514 -167 0 3 1 200 . 1
          "002W" 20283   .09  .40700000000000003 1 20515 1 348 514 -166 0 3 1 200 . 1
          "002W" 20284   .98               -.002 1 20515 1 349 514 -165 0 3 1 200 . 1
          "002W" 20285  1.85                .866 1 20515 1 350 514 -164 0 3 1 200 . 1
          "002W" 20286  -.62               -.429 1 20515 1 351 514 -163 0 3 1 200 . 1
          "002W" 20289   .79                .366 1 20515 1 352 514 -162 0 3 1 200 . 1
          "002W" 20290 -1.77               -.367 1 20229 1 353 514 -161 0 3 1 200 . 1
          "002W" 20291   -.5 -1.0150000000000001 1 20515 1 354 514 -160 0 3 1 200 . 1
          "002W" 20292  -.21                .258 1 20515 1 355 514 -159 0 3 1 200 . 1
          "002W" 20293   .47              -1.014 1 20515 1 356 514 -158 0 3 1 200 . 1
          "002W" 20296 -1.64                -.98 1 20515 1 357 514 -157 0 3 1 200 . 1
          "002W" 20297  -.17                .465 1 20515 1 358 514 -156 0 3 1 200 . 1
          "002W" 20298   .39                 .97 1 20515 1 359 514 -155 0 3 1 200 . 1
          "002W" 20299  -.43                -.36 1 20515 1 360 514 -154 0 3 1 200 . 1
          "002W" 20300   .43               1.139 1 20515 1 361 514 -153 0 3 1 200 . 1
          "002W" 20303  1.11  .08700000000000001 1 20515 1 362 514 -152 0 3 1 200 . 1
          "002W" 20304   .51               -.296 1 20515 1 363 514 -151 0 3 1 200 . 1
          "002W" 20305   .08                .466 1 20515 1 364 514 -150 0 3 1 200 . 1
          "002W" 20306  -.59               -.361 1 20515 1 365 514 -149 0 3 1 200 . 1
          "002W" 20307 -1.56               -.634 1 20515 1 366 514 -148 0 3 1 200 . 1
          "002W" 20310   .04               1.057 1 20515 1 367 514 -147 0 3 1 200 . 1
          "002W" 20311 -1.72              -1.008 1 20515 1 368 514 -146 0 3 1 200 . 1
          "002W" 20312  -2.1              -1.445 1 20515 1 369 514 -145 0 3 1 200 . 1
          "002W" 20313  1.29  .34500000000000003 1 20515 1 370 514 -144 0 3 1 200 . 1
          "002W" 20314   .84               -.314 1 20515 1 371 514 -143 0 3 1 200 . 1
          "002W" 20317  -.87               -.073 1 20515 1 372 514 -142 0 3 1 200 . 1
          "002W" 20318   .13               -.185 1 20515 1 373 514 -141 0 3 1 200 . 1
          "002W" 20319  -.92              -1.608 1 20515 1 374 514 -140 0 3 1 200 . 1
          "002W" 20320 -2.44               -.747 1 20515 1 375 514 -139 0 3 1 200 . 1
          "002W" 20321    -3               -2.27 1 20515 1 376 514 -138 0 3 1 200 . 1
          "002W" 20324 -3.38               -3.18 1 20515 1 377 514 -137 0 3 1 200 . 1
          "002W" 20325  3.55  2.6790000000000003 1 20515 1 378 514 -136 0 3 1 200 . 1
          "002W" 20326   2.3              -1.864 1 20515 1 379 514 -135 0 3 1 200 . 1
          "002W" 20327  5.91               1.803 1 20515 1 380 514 -134 0 3 1 200 . 1
          "002W" 20328  -.09                .362 1 20515 1 381 514 -133 0 3 1 200 . 1
          "002W" 20331   .48               -.291 1 20515 1 382 514 -132 0 3 1 200 . 1
          "002W" 20332 -2.03              -2.321 1 20515 1 383 514 -131 0 3 1 200 . 1
          "002W" 20333  1.63               -.004 1 20515 1 384 514 -130 0 3 1 200 . 1
          "002W" 20334 -2.34               1.149 1 20515 1 385 514 -129 0 3 1 200 . 1
          "002W" 20335   .89              -2.399 1 20515 1 386 514 -128 0 3 1 200 . 1
          "002W" 20338   .62                .873 1 20515 1 387 514 -127 0 3 1 200 . 1
          "002W" 20339   .22               1.424 1 20159 1 388 514 -126 0 3 1 200 . 1
          "002W" 20340   .22               1.185 1 20515 1 389 514 -125 0 3 1 200 . 1
          "002W" 20341  -.52               -.777 1 20515 1 390 514 -124 0 3 1 200 . 1
          "002W" 20342  -.31               -.444 1 20515 1 391 514 -123 0 3 1 200 . 1
          "002W" 20345  -.61               -.546 1 20515 1 392 514 -122 0 3 1 200 . 1
          "002W" 20346  -.97                .665 1 20515 1 393 514 -121 0 3 1 200 . 1
          "002W" 20347    .8               1.756 1 20515 1 394 514 -120 0 3 1 200 . 1
          "002W" 20348   .88               -.021 1 20515 1 395 514 -119 0 3 1 200 . 1
          "002W" 20349  1.01              -1.339 1 20515 1 396 514 -118 0 3 1 200 . 1
          "002W" 20352   .04               -.771 1 20515 1 397 514 -117 0 3 1 200 . 1
          "002W" 20353  -2.6              -3.698 1 20515 1 398 514 -116 0 3 1 200 . 1
          "002W" 20354   .62                .187 1 20515 1 399 514 -115 0 3 1 200 . 1
          "002W" 20355  -.71               -.891 1 20515 1 400 514 -114 0 3 1 200 . 1
          "002W" 20356   .31               1.816 1 20515 1 401 514 -113 0 3 1 200 . 1
          "002W" 20359  -.89              -1.965 1 20515 1 402 514 -112 0 3 1 200 . 1
          "002W" 20360  -.85               -.528 1 20515 1 403 514 -111 0 3 1 200 . 1
          end
          format %tdnn/dd/CCYY date
          format %tdDD/NN/CCYY event_date
          I really don't have a clue how I could perform this regression any faster while I have to conduct it several times. Do you have any idea which may help me? Many thanks in advance, Arnout
          Last edited by Arnout Poelstra; 15 Jul 2017, 10:48.

          Comment


          • #6
            Well, there isn't a whole lot you can do to speed it up beyond this point. -rangerun- is, of course, designed to handle a lot of different situations, so there is some overhead in running it. A different approach is to use pointers to designate ranges of observation numbers to be included in each regression and iterate that way. Your example data has only a single value of id, so it would not be possible to illustrate the process using it. I have modified your data set to have four different values of id so you can see how this code would work:

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str5 company_id int date double(return market_return) float(set event_date group_id datenum td dif event_window count_event_obs estimation_window count_est_obs predicted_return id)
            "002W" 20221  1.12                .727 1 20515 1 304 514 -210 0 3 1 200 . 1
            "002W" 20222     0                .865 1 20515 1 305 514 -209 0 3 1 200 . 1
            "002W" 20223     0                .082 1 20515 1 306 514 -208 0 3 1 200 . 1
            "002W" 20226   .04               -.245 1 20515 1 307 514 -207 0 3 1 200 . 1
            "002W" 20227  1.59 -.47000000000000003 1 20515 1 308 514 -206 0 3 1 200 . 1
            "002W" 20228  -.17                .201 1 20515 1 309 514 -205 0 3 1 200 . 1
            "002W" 20229  4.62                .551 1 20515 1 310 514 -204 0 3 1 200 . 1
            "002W" 20230  -.96               -.926 1 20515 1 311 514 -203 0 3 1 200 . 1
            "002W" 20233     0               -.276 1 20515 1 312 514 -202 0 3 1 200 . 1
            "002W" 20234   .29              -1.917 1 20515 1 313 514 -201 0 3 1 200 . 1
            "002W" 20235  -.67               1.123 1 20515 1 314 514 -200 0 3 1 200 . 1
            "002W" 20236  -.59               -.115 1 20515 1 315 514 -199 0 3 1 200 . 1
            "002W" 20237  -.81               -1.32 1 20515 1 316 514 -198 0 3 1 200 . 1
            "002W" 20240   .86 -.47600000000000003 1 20515 1 317 514 -197 0 3 1 200 . 1
            "002W" 20241 -1.66               1.071 1 20515 1 318 514 -196 0 3 1 200 . 1
            "002W" 20242  1.25                 1.1 1 20515 1 319 514 -195 0 3 1 200 . 1
            "002W" 20243  -.09  -.6980000000000001 1 20515 1 320 514 -194 0 3 1 200 . 1
            "002W" 20244     0              -2.415 1 20515 1 321 514 -193 0 3 1 200 . 1
            "002W" 20247 -1.32                -.08 1 20515 1 322 514 -192 0 3 1 200 . 1
            "002W" 20248 -1.52               -.058 1 20515 1 323 514 -191 0 3 1 200 . 1
            "002W" 20249  1.36               2.349 1 20515 1 324 514 -190 0 3 1 200 . 1
            "002W" 20250 -1.08               -.061 1 20515 1 325 514 -189 0 3 1 200 . 1
            "002W" 20251  -.09               -.628 1 20515 1 326 514 -188 0 3 1 200 . 1
            "002W" 20254 -1.14              -1.746 1 20515 1 327 514 -187 0 3 1 200 . 1
            "002W" 20255   .04                .466 1 20515 1 328 514 -186 0 3 1 200 . 1
            "002W" 20256  -.62                -.46 1 20515 1 329 514 -185 0 3 1 200 . 1
            "002W" 20257  -.49  1.6580000000000001 1 20515 1 330 514 -184 0 3 1 200 . 1
            "002W" 20258 -1.39               -.309 1 20515 1 331 514 -183 0 3 1 200 . 1
            "002W" 20261  1.18               2.888 1 20515 1 332 514 -182 0 3 1 200 . 1
            "002W" 20262  1.44               -.752 1 20515 1 333 514 -181 0 3 1 200 . 1
            "002W" 20263  -.66               -.342 1 20515 1 334 514 -180 0 3 1 200 . 2
            "002W" 20264 -1.16               -.221 1 20515 1 335 514 -179 0 3 1 200 . 2
            "002W" 20265  -.09 -.34900000000000003 1 20515 1 336 514 -178 0 3 1 200 . 2
            "002W" 20268   .18              -2.617 1 20515 1 337 514 -177 0 3 1 200 . 2
            "002W" 20269  -.41               -1.41 1 20515 1 338 514 -176 0 3 1 200 . 2
            "002W" 20270     1               1.124 1 20515 1 339 514 -175 0 3 1 200 . 2
            "002W" 20271   .04                -.37 1 20515 1 340 514 -174 0 3 1 200 . 2
            "002W" 20272   .22  -.5680000000000001 1 20515 1 341 514 -173 0 3 1 200 . 2
            "002W" 20275  -.18              -1.476 1 20515 1 342 514 -172 0 3 1 200 . 2
            "002W" 20276  -.76              -2.818 1 20515 1 343 514 -171 0 3 1 200 . 2
            "002W" 20277  -.63                1.31 1 20515 1 344 514 -170 0 3 1 200 . 2
            "002W" 20278  1.73               2.013 1 20515 1 345 514 -169 0 3 1 200 . 2
            "002W" 20279   2.9               3.214 1 20515 1 346 514 -168 0 3 1 200 . 2
            "002W" 20282     2                .458 1 20515 1 347 514 -167 0 3 1 200 . 2
            "002W" 20283   .09  .40700000000000003 1 20515 1 348 514 -166 0 3 1 200 . 2
            "002W" 20284   .98               -.002 1 20515 1 349 514 -165 0 3 1 200 . 2
            "002W" 20285  1.85                .866 1 20515 1 350 514 -164 0 3 1 200 . 2
            "002W" 20286  -.62               -.429 1 20515 1 351 514 -163 0 3 1 200 . 2
            "002W" 20289   .79                .366 1 20515 1 352 514 -162 0 3 1 200 . 2
            "002W" 20290 -1.77               -.367 1 20229 1 353 514 -161 0 3 1 200 . 2
            "002W" 20291   -.5 -1.0150000000000001 1 20515 1 354 514 -160 0 3 1 200 . 2
            "002W" 20292  -.21                .258 1 20515 1 355 514 -159 0 3 1 200 . 2
            "002W" 20293   .47              -1.014 1 20515 1 356 514 -158 0 3 1 200 . 2
            "002W" 20296 -1.64                -.98 1 20515 1 357 514 -157 0 3 1 200 . 2
            "002W" 20297  -.17                .465 1 20515 1 358 514 -156 0 3 1 200 . 2
            "002W" 20298   .39                 .97 1 20515 1 359 514 -155 0 3 1 200 . 3
            "002W" 20299  -.43                -.36 1 20515 1 360 514 -154 0 3 1 200 . 3
            "002W" 20300   .43               1.139 1 20515 1 361 514 -153 0 3 1 200 . 3
            "002W" 20303  1.11  .08700000000000001 1 20515 1 362 514 -152 0 3 1 200 . 3
            "002W" 20304   .51               -.296 1 20515 1 363 514 -151 0 3 1 200 . 3
            "002W" 20305   .08                .466 1 20515 1 364 514 -150 0 3 1 200 . 3
            "002W" 20306  -.59               -.361 1 20515 1 365 514 -149 0 3 1 200 . 3
            "002W" 20307 -1.56               -.634 1 20515 1 366 514 -148 0 3 1 200 . 3
            "002W" 20310   .04               1.057 1 20515 1 367 514 -147 0 3 1 200 . 3
            "002W" 20311 -1.72              -1.008 1 20515 1 368 514 -146 0 3 1 200 . 3
            "002W" 20312  -2.1              -1.445 1 20515 1 369 514 -145 0 3 1 200 . 3
            "002W" 20313  1.29  .34500000000000003 1 20515 1 370 514 -144 0 3 1 200 . 3
            "002W" 20314   .84               -.314 1 20515 1 371 514 -143 0 3 1 200 . 3
            "002W" 20317  -.87               -.073 1 20515 1 372 514 -142 0 3 1 200 . 3
            "002W" 20318   .13               -.185 1 20515 1 373 514 -141 0 3 1 200 . 3
            "002W" 20319  -.92              -1.608 1 20515 1 374 514 -140 0 3 1 200 . 3
            "002W" 20320 -2.44               -.747 1 20515 1 375 514 -139 0 3 1 200 . 3
            "002W" 20321    -3               -2.27 1 20515 1 376 514 -138 0 3 1 200 . 3
            "002W" 20324 -3.38               -3.18 1 20515 1 377 514 -137 0 3 1 200 . 3
            "002W" 20325  3.55  2.6790000000000003 1 20515 1 378 514 -136 0 3 1 200 . 3
            "002W" 20326   2.3              -1.864 1 20515 1 379 514 -135 0 3 1 200 . 3
            "002W" 20327  5.91               1.803 1 20515 1 380 514 -134 0 3 1 200 . 3
            "002W" 20328  -.09                .362 1 20515 1 381 514 -133 0 3 1 200 . 3
            "002W" 20331   .48               -.291 1 20515 1 382 514 -132 0 3 1 200 . 3
            "002W" 20332 -2.03              -2.321 1 20515 1 383 514 -131 0 3 1 200 . 3
            "002W" 20333  1.63               -.004 1 20515 1 384 514 -130 0 3 1 200 . 3
            "002W" 20334 -2.34               1.149 1 20515 1 385 514 -129 0 3 1 200 . 3
            "002W" 20335   .89              -2.399 1 20515 1 386 514 -128 0 3 1 200 . 3
            "002W" 20338   .62                .873 1 20515 1 387 514 -127 0 3 1 200 . 3
            "002W" 20339   .22               1.424 1 20159 1 388 514 -126 0 3 1 200 . 3
            "002W" 20340   .22               1.185 1 20515 1 389 514 -125 0 3 1 200 . 3
            "002W" 20341  -.52               -.777 1 20515 1 390 514 -124 0 3 1 200 . 3
            "002W" 20342  -.31               -.444 1 20515 1 391 514 -123 0 3 1 200 . 3
            "002W" 20345  -.61               -.546 1 20515 1 392 514 -122 0 3 1 200 . 3
            "002W" 20346  -.97                .665 1 20515 1 393 514 -121 0 3 1 200 . 3
            "002W" 20347    .8               1.756 1 20515 1 394 514 -120 0 3 1 200 . 3
            "002W" 20348   .88               -.021 1 20515 1 395 514 -119 0 3 1 200 . 4
            "002W" 20349  1.01              -1.339 1 20515 1 396 514 -118 0 3 1 200 . 4
            "002W" 20352   .04               -.771 1 20515 1 397 514 -117 0 3 1 200 . 4
            "002W" 20353  -2.6              -3.698 1 20515 1 398 514 -116 0 3 1 200 . 4
            "002W" 20354   .62                .187 1 20515 1 399 514 -115 0 3 1 200 . 4
            "002W" 20355  -.71               -.891 1 20515 1 400 514 -114 0 3 1 200 . 4
            "002W" 20356   .31               1.816 1 20515 1 401 514 -113 0 3 1 200 . 4
            "002W" 20359  -.89              -1.965 1 20515 1 402 514 -112 0 3 1 200 . 4
            "002W" 20360  -.85               -.528 1 20515 1 403 514 -111 0 3 1 200 . 4
            end
            format %tdnn/dd/CCYY date
            format %tdDD/NN/CCYY event_date
            
            
            by id, sort: gen id_count = _N
            local first = 1
            replace predicted_return = .
            
            while `first' <= _N {
                local last = `first' + id_count[`first'] - 1
                regress return market_return in `first'/`last'
                predict xb in `first'/`last', xb
                replace predicted_return = xb in `first'/`last'
                drop xb
                local first = `last' + 1
            }
            The above code has really stripped out all of the inefficiencies. The only remaining inefficiencies are in -regress- and -predict- themselves. While you could code your own versions of those in Mata to wring out a little more efficiency, both of those programs are really very tightly written and I doubt the gains would be worth the effort. So I would say that, for practical purposes, what I show you here is probably as good as you can get on what is inherently a very large problem. If this doesn't run fast enough for your purposes, I would counsel either 1) getting a faster machine, or 2) more patience.

            Added: One more thing to speed it up. Put the -regress-, -predict-, and -replace- commands in the loop inside a -quietly- block. I am pretty sure it takes longer for Stata to update the screen than to do the actual calculations for these commands. And certainly the amount of output would be far too large for you to make any use of anyway.

            Also, the above code assumes you have already eliminated from the data set any observations with out-of-range values of id or estimation_window != 1.
            Last edited by Clyde Schechter; 15 Jul 2017, 11:54.

            Comment


            • #7
              Sorry for a not so timely response (I'm traveling these days)...

              It's important to note that rangerun differs from rangestat in that it will run the user's program for each observation in the data, even when the interval does not change. This explains why rangerun was slow as used in this thread: it will run as many regressions as there are observations in the dataset. You can get around this by tagging a representative observation per id and creating a valid interval only for those tagged observations. That will run the correct number of regressions (one per id group) but the predicted results will only apply to those tagged observations.

              Stepping back a bit, all that is needed is to perform one regression per id and then calculating predicted values using the regression coefficients. The regressions can easily be done with rangestat (which is optimized to recognize that the interval does not change and will only calculate one regression per id):

              Code:
              rangestat (reg) return market_return, interval(id 0 0)
              gen preturn = b_market_return*market_return + b_cons
              If I run this code on the dataset above expanded 5000 times (20,000 id groups), the whole thing runs in less than 3 seconds on my computer.

              Comment


              • #8
                Robert Picard is right. -rangestat- is orders of magnitude faster than my "pointers" approach.

                Comment


                • #9
                  Thank you so much Clyde and Robbert. It worked out perfectly.

                  Comment

                  Working...
                  X