Announcement

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

  • Generating Matrix

    Below is the example data file:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float stock_id str52 stock float(date mdate rt mcap idiovol)
    1 "3M India Ltd."                   . .   .04736608  321.22   .0916427
    1 "3M India Ltd."                   . .   .04865415  337.95  .11856794
    1 "3M India Ltd."                   . . -.007843738  355.98  .03385014
    1 "3M India Ltd."                   . . -.001308996  354.85  .05559466
    1 "3M India Ltd."                   . . -.012385068  355.98 .022072207
    1 "3M India Ltd."                   . .   -.0390713  353.05          .
    1 "3M India Ltd."                   . .  -.02738757  340.82  .04476108
    1 "3M India Ltd."                   . . -.067793116  333.05   .0641219
    1 "3M India Ltd."                   . .  -.13744837  312.04  .08455227
    1 "3M India Ltd."                   . .   .18256493  270.64  .11701576
    1 "3M India Ltd."                   . .    .1013674  321.05  .11567125
    1 "3M India Ltd."                   . .   .05692589  354.85  .07522642
    2 "A B B India Ltd."                . .  .026357943 1139.01  .09048145
    2 "A B B India Ltd."                . .  .014627993  1174.4  .06662828
    2 "A B B India Ltd."                . . -.009635856 1197.07 .032479744
    2 "A B B India Ltd."                . .   -.1510767 1191.14   .0478922
    2 "A B B India Ltd."                . . -.004365709 1016.52    .151155
    2 "A B B India Ltd."                . .  .032454778 1016.31  .08022968
    2 "A B B India Ltd."                . .   .11848308 1053.82  .03937751
    2 "A B B India Ltd."                . .   .14282623 1183.51   .0788861
    2 "A B B India Ltd."                . .  -.10635272 1358.12 .069916405
    2 "A B B India Ltd."                . .   .06663753 1220.17  .08169884
    2 "A B B India Ltd."                . .   .11098332 1305.99  .07204738
    2 "A B B India Ltd."                . .  .069325246 1456.02  .04127121
    3 "A C C Ltd."                      . .  -.16770735 2714.39   .0494189
    3 "A C C Ltd."                      . .  .032914802 2272.12  .05700322
    3 "A C C Ltd."                      . . -.006490775 2357.53   .0472087
    3 "A C C Ltd."                      . .  -.04114424  2353.5  .05463453
    3 "A C C Ltd."                      . .   .22832213 2267.27  .05449492
    3 "A C C Ltd."                      . .  .005026255 2794.56  .06807207
    3 "A C C Ltd."                      . .   -.1387426 2821.04  .04853502
    3 "A C C Ltd."                      . .   .07293778 2443.21  .07409048
    3 "A C C Ltd."                      . .  -.10571817  2633.1  .08595624
    3 "A C C Ltd."                      . .  -.05857307 2367.38 .072985575
    3 "A C C Ltd."                      . .   .10611195 2237.47  .05432591
    3 "A C C Ltd."                      . .   .14531963 2483.66  .05427632
    4 "A D C India Communications Ltd." . .  -.15515427   38.98   .0899244
    4 "A D C India Communications Ltd." . .  .018237507   33.12  .06992178
    4 "A D C India Communications Ltd." . .  -.09497112   33.88  .09055381
    4 "A D C India Communications Ltd." . .  -.09403583   30.82          .
    4 "A D C India Communications Ltd." . .   .08764605   28.06          .
    4 "A D C India Communications Ltd." . .  .009062549   30.64   .0815558
    4 "A D C India Communications Ltd." . .    -.157174   31.05   .0749493
    4 "A D C India Communications Ltd." . .  -.08687963   26.31  .08596274
    4 "A D C India Communications Ltd." . .  -.09430072   24.15  .12010587
    4 "A D C India Communications Ltd." . .    .1720329   21.99   .1493983
    4 "A D C India Communications Ltd." . .    .1829369   25.85  .16625586
    4 "A D C India Communications Ltd." . .   .10841724   30.68   .1996671
    5 "A G C Networks Ltd."             . .   -.2414415  233.32 .072601065
    5 "A G C Networks Ltd."             . .   .11042874  178.09  .14132822
    5 "A G C Networks Ltd."             . . -.065180995  198.58  .13915063
    5 "A G C Networks Ltd."             . .   -.1616635  186.56   .1046824
    5 "A G C Networks Ltd."             . .    .1778091  157.21  .08680837
    5 "A G C Networks Ltd."             . .   .12866792  185.81  .11829976
    5 "A G C Networks Ltd."             . .  -.15761645  210.58  .06747569
    5 "A G C Networks Ltd."             . .  -.07685865  178.34   .1158349
    5 "A G C Networks Ltd."             . .  -.18162194  165.46   .0434757
    5 "A G C Networks Ltd."             . .    .0617134  136.21  .06843774
    5 "A G C Networks Ltd."             . .   .19657505  145.11    .161901
    5 "A G C Networks Ltd."             . . -.005660561  174.21   .1845726
    6 "Aarti Industries Ltd."           . .  -.11743905   80.09  .07460695
    6 "Aarti Industries Ltd."           . .   .06499288   71.05  .13379171
    6 "Aarti Industries Ltd."           . .  -.06215039   76.15  .08399434
    6 "Aarti Industries Ltd."           . .   .06462212   71.72  .08620652
    6 "Aarti Industries Ltd."           . .  -.03837779   76.51  .06567193
    6 "Aarti Industries Ltd."           . .  .019171037   73.84          .
    6 "Aarti Industries Ltd."           . .  .018495552   75.66  .10668948
    6 "Aarti Industries Ltd."           . .  -.07989978   77.42  .10452055
    6 "Aarti Industries Ltd."           . .  -.03729719    71.6  .05122435
    6 "Aarti Industries Ltd."           . .   .06352744   69.17  .14523724
    6 "Aarti Industries Ltd."           . .   .07090741   74.02          .
    6 "Aarti Industries Ltd."           . .   .27436173   79.48  .05161588
    7 "Aban Offshore Ltd."              . .  -.01925893   78.61  .16154055
    7 "Aban Offshore Ltd."              . .   .11436844   77.46   .1599467
    7 "Aban Offshore Ltd."              . .   -.0765941   86.68  .15839635
    7 "Aban Offshore Ltd."              . .   .06841874   80.45 .070136435
    7 "Aban Offshore Ltd."              . .   .13943256   86.31   .0847256
    7 "Aban Offshore Ltd."              . .     .103546   98.71  .11105046
    7 "Aban Offshore Ltd."              . .    .1084034  109.37   .0941147
    7 "Aban Offshore Ltd."              . .  -.08895424  121.73  .09428739
    7 "Aban Offshore Ltd."              . .  -.15765575  111.47  .05114253
    7 "Aban Offshore Ltd."              . .    .0572381   94.43  .07025049
    7 "Aban Offshore Ltd."              . .    .4515534  100.18  .17125925
    7 "Aban Offshore Ltd."              . .    .4910405  145.81  .15821946
    8 "Abbott India Ltd."               . .   -.0927252  483.16  .03758101
    8 "Abbott India Ltd."               . .  .009107584  440.64  .03866321
    8 "Abbott India Ltd."               . . -.009212272  446.71          .
    8 "Abbott India Ltd."               . .    .0519827  444.69  .04381766
    8 "Abbott India Ltd."               . . -.014502028   469.8  .04690858
    8 "Abbott India Ltd."               . . -.029886486  464.94  .04116864
    8 "Abbott India Ltd."               . .     .091593  453.11  .04559331
    8 "Abbott India Ltd."               . .   .02138084  496.69  .07281568
    8 "Abbott India Ltd."               . .  -.14193517  509.65  .04465608
    8 "Abbott India Ltd."               . .  -.05085361  414.78  .04636159
    8 "Abbott India Ltd."               . .   .04385218  395.22  .05140735
    8 "Abbott India Ltd."               . .   .07494023  414.09  .04982206
    9 "Accelya Solutions India Ltd."    . .   -.3571708   77.16  .09019727
    9 "Accelya Solutions India Ltd."    . .    .2496361   49.97  .07974155
    9 "Accelya Solutions India Ltd."    . .  -.13954155   62.67  .12606497
    9 "Accelya Solutions India Ltd."    . .  -.14552286   54.22  .07574052
    end
    format %td date
    format %tm mdate
    On this we run the following code:

    Code:
    capture program drop one_mdate
    program define one_mdate
        gen cutoffs = .
        _pctile mcap, nq(5)
        forvalues i = 1/4 {
            replace cutoffs = r(r`i') in `i'
        }
        display `"`cutoffs'"'
        xtile mcap_quintile = mcap, cutpoints(cutoffs)
        drop cutoffs
        by mcap_quintile, sort: egen idiovol_quintile = xtile(idiovol), nq(5)
        tab *_quintile
        exit
    end
    
    
    // DOUBLE SORT EACH FISCAL YEAR
    runby one_mdate, by(mdate) status
    
    capture program drop one_weighted_return
    program define one_weighted_return
        if !missing(mcap_quintile,idiovol_quintile){
            egen numerator = total(mcap*rt)
            egen denominator = total(mcap)
            gen vw_mean_rt = numerator/denominator
        }
        exit
    end
    
    runby one_weighted_return, by(mdate mcap_quintile idiovol_quintile)
    
    gen int portfolio_num = (mcap_quintile-1)*5 + idiovol_quintile
    
    by mdate mcap_quintile idiovol_quintile, sort: egen ew_mean_rt = mean(rt)
    drop if missing(mcap_quintile,idiovol_quintile, vw_mean_rt, ew_mean_rt)
    keep mdate *_quintile *_mean_rt
    by mdate *_quintile, sort: gen stock_count = _N
    by mdate *_quintile, sort: keep if _n == 1
    
    gen str32 group = "mcap_q" + string(mcap_quintile) +"_idiovol_q" +string(idiovol_q) + "_"
    drop *_quintile
    reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
    Then we run regressions on a constant using below code:

    Code:
    gen time = _n
    tsset time
    foreach v of varlist ew_mean_mcap* {
        newey `v', lag(5)
    }
    It generates output of 25 regressions. What i require from this regression output is a 5 by 5 matrix of intercept values and another 5 by 5 matrix of associated t-values of the intercept. The row of these matrices should reflect idiovol and columns should reflect mcap. What additions to the code need to be put?


  • #2
    First, your example contains only missing values for mdate, so -newey- cannot run. Please, when asking for help with code development, please test your starting code on the example data before you post it to be sure that it actually runs and produces partial results (or, for troubleshooting questions be sure that it reproduces whatever problem you are encountering with the real data). I modified your example data by putting some arbitrary values of mdate in.

    I think all of the code up through tsset time can be used as is. Drop the -foreach v of varlist ew_mean_mcap*- loop and instead do this:
    Code:
    frame create newey_results int (idiovol_quintile mcap_quintile) float(intercept tstat)
    forvalues iv = 1/5 {
        forvalues mc = 1/5 {
            local v
            newey ew_mean_mcap_q`mc'_idiovol_q`iv'_rt, lag(5)
            matrix M = r(table)
            frame post newey_results (`iv') (`mc') (M["b", "_cons"]) (M["t", "_cons"])
        }
    }
    frame change newey_results
    rename (intercept tstat) =_mcap_
    reshape wide *_mcap_, i(idiovol_quintile) j(mcap_quintile)
    Now, this leaves you a data set arrayed more or less like the two matrices you are asking for. The observations (rows) are headed by the value of idiovol_quintile, and the intercept and tstat variables (columns) are indexed by the mcap quintile. If you literally want to make matrices, then see -help mkmat- for how to do that. I really don't recommend it, however, because unless you are going to do some real linear algebra or a few other things that are fairly uncommon in practice, Stata matrices are generally not very useful, and keeping the data in a Stata data set generally gives you greater flexibility.

    Comment


    • #3
      Thanks indeed. I could work with it.

      Now there is one more issue. If we run the code #1(1) given above on data example, it is expected to generate 25 stock portfolio return factors for vw and ew series each. From this, i need simple mean of five mcap portfolios in each idiovol decile for both vw and ew series.

      For example by definition, in case of value-weighted series, for first quintile of idiovol, it would be simple mean of:- (vw_mean_mcap_q1_idiovol_q1_rt+vw_mean_mcap_q2_idi ovol_q1_rt+vw_mean_mcap_q3_idiovol_q1_rt+vw_mean_m cap_q4_idiovol_q1_rt+vw_mean_mcap_q5_idiovol_q1_rt ) and so on for other idiovol quintiles and similarly for equal-weighted series. This would give rise to another 5 + 5 portfolio returns series (5 each for vw and ew portfolios). Doing this would produce average returns across the five control quintile portfolios with dispersion in idivol but with similar levels of mcap. This is what is the purpose.

      Hope the matter is clear enough.
      Last edited by Sartaj Hussain; 21 Dec 2021, 12:04.

      Comment


      • #4
        I'm not sure I understand what you are looking for, but I think it is this:

        Code:
        capture program drop one_mdate
        program define one_mdate
            gen cutoffs = .
            _pctile mcap, nq(5)
            forvalues i = 1/4 {
                replace cutoffs = r(r`i') in `i'
            }
            display `"`cutoffs'"'
            xtile mcap_quintile = mcap, cutpoints(cutoffs)
            drop cutoffs
            by mcap_quintile, sort: egen idiovol_quintile = xtile(idiovol), nq(5)
            tab *_quintile
            exit
        end
        
        
        // DOUBLE SORT EACH FISCAL YEAR
        runby one_mdate, by(mdate) status
        
        capture program drop one_weighted_return
        program define one_weighted_return
            if !missing(mcap_quintile,idiovol_quintile){
                egen numerator = total(mcap*rt)
                egen denominator = total(mcap)
                gen vw_mean_rt = numerator/denominator
            }
            exit
        end
        
        runby one_weighted_return, by(mdate mcap_quintile idiovol_quintile)
        
        gen int portfolio_num = (mcap_quintile-1)*5 + idiovol_quintile
        
        by mdate mcap_quintile idiovol_quintile, sort: egen ew_mean_rt = mean(rt)
        drop if missing(mcap_quintile,idiovol_quintile, vw_mean_rt, ew_mean_rt)
        keep mdate *_quintile *_mean_rt
        by mdate *_quintile, sort: gen stock_count = _N
        by mdate *_quintile: egen vw_total = total(vw_mean_rt)
        by mdate *_quintile: egen ew_total = total(ew_mean_rt)
        by mdate *_quintile, sort: keep if _n == 1
        
        foreach x in vw ew {
            by mcap_quintile, sort: egen simple_mean_`x'_mcap = mean(`x'_total)
            by idiovol_quintile, sort: egen simple_mean_`x'_idiovol = mean(`x'_total)
           
        }
        by *_quintile, sort: keep if _n == 1
        
        keep *_quintile simple_mean*
        
        frame put *mcap*, into(mcap_portfolio_results)
        frame mcap_portfolio_results: duplicates drop
        frame put *idiovol*, into(idiovol_portfolio_results)
        frame idiovol_portfolio_results: duplicates drop
        The arrangement of the results is that the simple means (both vw and ew) for each of the mcap quintiles is a data set located in frame mcap_portfolio_results. Similarly, there is a frame idiovol_portfolio_results that contains the results for the five idiovol quintiles.

        Comment


        • #5
          No, Regretfully this is not what i require. Sorry if i could not make it amply clear enough. I try to define this once again, I am attaching a fresh data set as example. We run the appended code first and generate output as 25 series for vw and 25 series for ew. Thereafter, i need to generate by mdate 5 mean series or factors from each of these 25 vw and ew series on the following lines: Lets call each series as control_1_ew to control_5_ew for ew cases and control_1_vw to control_5_vw for vw cases . For ew the calculations should be done as per following:

          control_1_ew = (ew_mean_mcap_q1_idiovol_q1_rt+ew_mean_mcap_q2_idi ovol_q1_rt+ew_mean_mcap_q3_idiovol_q1_rt+ew_mean_m cap_q4_idiovol_q1_rt+ew_mean_mcap_q5_idiovol_q1_rt )/5
          control_2_ew = (ew_mean_mcap_q1_idiovol_q2_rt+ew_mean_mcap_q2_idi ovol_q2_rt+ew_mean_mcap_q3_idiovol_q2_rt+ew_mean_m cap_q4_idiovol_q2_rt+ew_mean_mcap_q5_idiovol_q2_rt )/5
          control_3_ew = (ew_mean_mcap_q1_idiovol_q3_rt+ew_mean_mcap_q2_idi ovol_q3_rt+ew_mean_mcap_q3_idiovol_q3_rt+ew_mean_m cap_q4_idiovol_q3_rt+ew_mean_mcap_q5_idiovol_q3_rt )/5
          control_4_ew = (ew_mean_mcap_q1_idiovol_q4_rt+ew_mean_mcap_q2_idi ovol_q4_rt+ew_mean_mcap_q3_idiovol_q4_rt+ew_mean_m cap_q4_idiovol_q4_rt+ew_mean_mcap_q5_idiovol_q4_rt )/5
          control_5_ew = (ew_mean_mcap_q1_idiovol_q5_rt+ew_mean_mcap_q2_idi ovol_q5_rt+ew_mean_mcap_q3_idiovol_q5_rt+ew_mean_m cap_q4_idiovol_q4_rt+ew_mean_mcap_q5_idiovol_q5_rt )/5

          Similarly for vw, the calculations will be done as per following:-


          control_1_vw = (vw_mean_mcap_q1_idiovol_q1_rt+vw_mean_mcap_q2_idi ovol_q1_rt+vw_mean_mcap_q3_idiovol_q1_rt+vw_mean_m cap_q4_idiovol_q1_rt+vw_mean_mcap_q5_idiovol_q1_rt )/5
          control_2_vw = (vw_mean_mcap_q1_idiovol_q2_rt+vw_mean_mcap_q2_idi ovol_q2_rt+vw_mean_mcap_q3_idiovol_q2_rt+vw_mean_m cap_q4_idiovol_q2_rt+vw_mean_mcap_q5_idiovol_q2_rt )/5
          control_3_vw = (vw_mean_mcap_q1_idiovol_q3_rt+vw_mean_mcap_q2_idi ovol_q3_rt+vw_mean_mcap_q3_idiovol_q3_rt+vw_mean_m cap_q4_idiovol_q3_rt+vw_mean_mcap_q5_idiovol_q3_rt )/5
          control_4_vw = (vw_mean_mcap_q1_idiovol_q4_rt+vw_mean_mcap_q2_idi ovol_q4_rt+vw_mean_mcap_q3_idiovol_q4_rt+vw_mean_m cap_q4_idiovol_q4_rt+vw_mean_mcap_q5_idiovol_q4_rt )/5
          control_5_vw = (vw_mean_mcap_q1_idiovol_q5_rt+vw_mean_mcap_q2_idi ovol_q5_rt+vw_mean_mcap_q3_idiovol_q5_rt+vw_mean_m cap_q4_idiovol_q4_rt+vw_mean_mcap_q5_idiovol_q5_rt )/5

          This elaboration is bit lengthy but this is how calculations have to be made fundamentally. So, essentially, this is mean by mdate of 5 mcap portfolio within each idiovol quintile (for both ew and vw cases).

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int stock_id str52 stock float(mdate rt mcap idiovol)
          1 "3M India Ltd."                             726    .1184294 21170.67  .09459936
          1 "3M India Ltd."                             727  -.03988438 23735.22  .05236398
          1 "3M India Ltd."                             728  -.09855641 22851.65   .0842787
          1 "3M India Ltd."                             729   .01961513 20662.45  .04649255
          1 "3M India Ltd."                             730    .1622525 21122.01  .05208552
          1 "3M India Ltd."                             731  -.04832564 24599.98  .07155111
          1 "3M India Ltd."                             732  -.08436494 23473.42 .037118677
          1 "3M India Ltd."                             733    .2041851 21557.57  .05653918
          1 "3M India Ltd."                             734    .3066516 26015.44   .1024171
          1 "3M India Ltd."                             735   -.1565654 34063.94   .0761481
          1 "3M India Ltd."                             736   -.0071751 28823.54  .11266434
          1 "3M India Ltd."                             737  -.04703041 28697.15   .0739418
          2 "A B B India Ltd."                          726  -.05421241 19995.67  .10871843
          2 "A B B India Ltd."                          727   .05935313  18965.8 .067627124
          2 "A B B India Ltd."                          728  -.09854768 20141.89  .06322751
          2 "A B B India Ltd."                          729    .0414137 18212.47  .06247413
          2 "A B B India Ltd."                          730    .1941809 19014.54   .0660737
          2 "A B B India Ltd."                          731   .12641639  22752.6  .14531854
          2 "A B B India Ltd."                          732    .1422432 25686.47  .09560207
          2 "A B B India Ltd."                          733   .10133003 29410.76  .08255054
          2 "A B B India Ltd."                          734  -.07761712 32467.54  .11415926
          2 "A B B India Ltd."                          735 -.027171543 30035.89  .06083263
          2 "A B B India Ltd."                          736   .20342955 29301.63  .05801463
          2 "A B B India Ltd."                          737   .07998375  35344.2  .07539472
          3 "A B B Power Products & Systems India Ltd." 726   .06354318  3566.42 .016075514
          3 "A B B Power Products & Systems India Ltd." 727   .01940937   3802.7  .03139144
          3 "A B B Power Products & Systems India Ltd." 728  .006240233  3886.61    .124328
          3 "A B B Power Products & Systems India Ltd." 729   .03768517  3921.58  .05144717
          3 "A B B Power Products & Systems India Ltd." 730   .18790914  4079.66  .09119125
          3 "A B B Power Products & Systems India Ltd." 731   .12589514  4856.09  .11065537
          3 "A B B Power Products & Systems India Ltd." 732  -.06910693  5479.74  .14846125
          3 "A B B Power Products & Systems India Ltd." 733    .1898327   5116.1  .05424834
          3 "A B B Power Products & Systems India Ltd." 734  -.05284561  6100.63   .1228909
          3 "A B B Power Products & Systems India Ltd." 735   .31662655  5794.85  .08472132
          3 "A B B Power Products & Systems India Ltd." 736   .07315451  7645.44    .096315
          3 "A B B Power Products & Systems India Ltd." 737 -.029378373  8226.07   .0819109
          4 "A C C Ltd."                                726   .06692457 25013.26  .05451617
          4 "A C C Ltd."                                727  -.07505673 26754.99  .06367326
          4 "A C C Ltd."                                728   .05191258 24817.96  .04167877
          4 "A C C Ltd."                                729   .17999762 26174.73   .0552238
          4 "A C C Ltd."                                730   .03086585 30954.85  .06496176
          4 "A C C Ltd."                                731  -.05234683 31984.87  .05231039
          4 "A C C Ltd."                                732 -.011150457 30391.49  .06558764
          4 "A C C Ltd."                                733   .07725016  30136.1  .05752963
          4 "A C C Ltd."                                734   .09514549 32542.59  .05743942
          4 "A C C Ltd."                                735 -.013368968 35727.47  .07591277
          4 "A C C Ltd."                                736    .0522756  35347.2  .05981381
          4 "A C C Ltd."                                737  .012311548 37293.61  .03130351
          5 "A I A Engineering Ltd."                    726   .02495178 15157.76  .11163152
          5 "A I A Engineering Ltd."                    727   .08844092 15577.01  .07996228
          5 "A I A Engineering Ltd."                    728  .022078203 16996.06  .08275808
          5 "A I A Engineering Ltd."                    729   -.0823358 17418.14  .11070403
          5 "A I A Engineering Ltd."                    730    .0955906 16029.75  .08087932
          5 "A I A Engineering Ltd."                    731     .051809 17600.65  .07386222
          5 "A I A Engineering Ltd."                    732 -.009125889 18557.06  .13231656
          5 "A I A Engineering Ltd."                    733    -.072096 18438.69   .0978856
          5 "A I A Engineering Ltd."                    734   .12311248 17157.35  .06436446
          5 "A I A Engineering Ltd."                    735  -.09286427 19316.34  .06380035
          5 "A I A Engineering Ltd."                    736   .03399865 17575.19  .04568206
          5 "A I A Engineering Ltd."                    737    .1047434 18221.75 .071264036
          6 "A P L Apollo Tubes Ltd."                   726    .1711525  3877.83  .06370289
          6 "A P L Apollo Tubes Ltd."                   727    .3106615  4552.02   .1343903
          6 "A P L Apollo Tubes Ltd."                   728   .21097997  5991.94  .09383714
          6 "A P L Apollo Tubes Ltd."                   729   .07558625  7272.35  .08646914
          6 "A P L Apollo Tubes Ltd."                   730    .0652984  7844.95   .0876136
          6 "A P L Apollo Tubes Ltd."                   731    .3143294  8380.35 .066069156
          6 "A P L Apollo Tubes Ltd."                   732  .021805216 11039.03   .1009317
          6 "A P L Apollo Tubes Ltd."                   733   .17827347 11309.81  .08637045
          6 "A P L Apollo Tubes Ltd."                   734    .3058266 13355.64   .0683895
          6 "A P L Apollo Tubes Ltd."                   735  -.07329819 17492.31  .17121236
          6 "A P L Apollo Tubes Ltd."                   736 -.002590209 16257.71  .11828837
          6 "A P L Apollo Tubes Ltd."                   737   .24788477 16260.83   .1114527
          7 "A U Small Finance Bank Ltd."               726    .3652917 16631.81  .14167368
          7 "A U Small Finance Bank Ltd."               727   -.1039307 22756.09  .14123882
          7 "A U Small Finance Bank Ltd."               728  -.02253566 20454.26  .09675895
          7 "A U Small Finance Bank Ltd."               729    .1833404 20052.01  .08047066
          7 "A U Small Finance Bank Ltd."               730   .07679974 23781.35  .09965545
          7 "A U Small Finance Bank Ltd."               731   .01532577 25679.42  .09570134
          7 "A U Small Finance Bank Ltd."               732   .02171895 26141.96  .07963484
          7 "A U Small Finance Bank Ltd."               733   .28827286 26783.51  .05495229
          7 "A U Small Finance Bank Ltd."               734    .0853433 34609.04  .09901675
          7 "A U Small Finance Bank Ltd."               735   -.1844175 38285.17  .13246192
          7 "A U Small Finance Bank Ltd."               736 -.018984085  31333.9   .1121216
          7 "A U Small Finance Bank Ltd."               737   .04734747 30881.87  .10038955
          8 "Aarti Drugs Ltd."                          726    .3652348  2961.31  .13313535
          8 "Aarti Drugs Ltd."                          727    .5602086  4050.94  .14255284
          8 "Aarti Drugs Ltd."                          728   .13304557  6331.08    .335134
          8 "Aarti Drugs Ltd."                          729  -.12141288  7190.85   .0926184
          8 "Aarti Drugs Ltd."                          730   .04973112  6336.67  .20832872
          8 "Aarti Drugs Ltd."                          731  -.01636965  6667.06  .10740176
          8 "Aarti Drugs Ltd."                          732  -.04739946  6574.79  .06569554
          8 "Aarti Drugs Ltd."                          733  -.06662952  6281.21  .08209758
          8 "Aarti Drugs Ltd."                          734    .0987361  5879.06  .05586975
          8 "Aarti Drugs Ltd."                          735  .018791644  6475.54  .11462262
          8 "Aarti Drugs Ltd."                          736  .021796184  6614.87 .063948065
          8 "Aarti Drugs Ltd."                          737  -.02986646  6733.87   .1548877
          9 "Aarti Industries Ltd."                     726   .05996685 16235.17  .11119487
          9 "Aarti Industries Ltd."                     727   .05581495  17252.7  .05761338
          9 "Aarti Industries Ltd."                     728 -.033955272 18261.52  .10733598
          9 "Aarti Industries Ltd."                     729  -.01980157 17691.77 .070201054
          end
          format %tm mdate
          Code:
          capture program drop one_mdate
          program define one_mdate
              gen cutoffs = .
              _pctile mcap, nq(5)
              forvalues i = 1/4 {
                  replace cutoffs = r(r`i') in `i'
              }
              display `"`cutoffs'"'
              xtile mcap_quintile = mcap, cutpoints(cutoffs)
              drop cutoffs
              by mcap_quintile, sort: egen idiovol_quintile = xtile(idiovol), nq(5)
              tab *_quintile
              exit
          end
          
          
          // DOUBLE SORT EACH FISCAL YEAR
          runby one_mdate, by(mdate) status
          
          capture program drop one_weighted_return
          program define one_weighted_return
              if !missing(mcap_quintile,idiovol_quintile){
                  egen numerator = total(mcap*rt)
                  egen denominator = total(mcap)
                  gen vw_mean_rt = numerator/denominator
              }
              exit
          end
          
          runby one_weighted_return, by(mdate mcap_quintile idiovol_quintile)
          
          gen int portfolio_num = (mcap_quintile-1)*5 + idiovol_quintile
          
          by mdate mcap_quintile idiovol_quintile, sort: egen ew_mean_rt = mean(rt)
          drop if missing(mcap_quintile,idiovol_quintile, vw_mean_rt, ew_mean_rt)
          keep mdate *_quintile *_mean_rt
          by mdate *_quintile, sort: gen stock_count = _N
          by mdate *_quintile, sort: keep if _n == 1
          
          gen str32 group = "mcap_q" + string(mcap_quintile) +"_idiovol_q" +string(idiovol_q) + "_"
          drop *_quintile
          reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
          drop *_count
          Last edited by Sartaj Hussain; 21 Dec 2021, 18:33.

          Comment


          • #6
            OK, this is clearer.

            Code:
            foreach x in ew vw {
                forvalues i = 1/5 {
                    egen control_`i'_`x'= rowmean(`x'_mean_mcap_*_idiovol_q`i'_rt)
                }
            }
            Note that this code does not run in the example data. Thatis because in the example data, there are only two idiovol "quintiles" so many of the variables needed don't exist. Presumably in your real data, there will be enough data to populate 5 idiovol quintiles in each mcap quintile. That will be all that is needed.

            Comment


            • #7
              Absolutely. The actual data is much larger.

              Comment


              • #8
                Thanks indeed for all this support.

                Comment


                • #9
                  Kindly amend the code in #2 so that it yields a matrix of intercepts and t-values that has column wise mcap and row-wise size dimensions. Just like this for ew cases:
                  low 2 3 4 high
                  small 0.013 0.019 0.018 0.018 0.002
                  2 0.018 0.016 0.016 0.017 0.012
                  3 0.015 0.017 0.014 0.017 0.010
                  4 0.015 0.014 0.015 0.013 0.007
                  big 0.012 0.009 0.013 0.010 0.010
                  Well, the code in #6 also needs revision according to this format.
                  Last edited by Sartaj Hussain; 02 Jan 2022, 21:22.

                  Comment


                  • #10
                    P.S. Well, if code in #6 gives averages that have spread in mcap but equal or same levels of idivol, then it doesn't require any revision.

                    Comment


                    • #11
                      Re #9: The other day you said that size is the same thing as mcap, so I don't understand what you want here.

                      Re #10: Yes, it gives averages across the range of mcap with constant level of idiovol. So no revisions needed.

                      Comment


                      • #12
                        The other day you said that size is the same thing as mcap, so I don't understand what you want here.
                        The size and mcap is same. Just used interchangeably.

                        Yes, it gives averages across the range of mcap with constant level of idiovol. So no revisions needed.
                        This is fine but i just need to get transpose as illustrated in #9. (just presentation issue)

                        Moreover, code in #2 yields intercepts and Newey and West (1987) adjusted t-statistics, instead of these numbers.

                        1. Now i need mean values, standard deviations for each case (in matrix form again).

                        and separately:

                        2. Also matrix of intercepts and slope coefficients (for each of X's) with associated Newey and West (1987) adjusted t-statistics, r-squared and adj r-square from regression using rmrf, smb, hml factors as independent variable for each case.

                        So just need your intervention to update the code in #2 to make these calculations.
                        Last edited by Sartaj Hussain; 11 Jan 2022, 06:43.

                        Comment


                        • #13
                          This is fine but i just need to get transpose as illustrated in #9. (just presentation issue)
                          So for this you just need to interchange the -i()- and -j()- options in the -reshape- command so that it reads:
                          Code:
                          reshape wide *_mcap_, i(mcap_quintile) j(idiovol_quintile)
                          1. Now i need mean values, standard deviations for each case (in matrix form again).
                          Mean values and standard deviations of what?

                          2. Also matrix of intercepts and slope coefficients (for each of X's) with associated Newey and West (1987) adjusted t-statistics, r-squared and adj r-square from regression using rmrf, smb, hml factors as independent variable for each case.
                          So that should look like this:
                          Code:
                          frame create newey_results2 str32 indvar int (idiovol_quintile mcap_quintile) ///
                              float(intercept tstat)
                          foreach v of varlist rmrf smb hml {   
                              forvalues iv = 1/5 {
                                  forvalues mc = 1/5 {
                                      newey ew_mean_mcap_q`mc'_idiovol_q`iv'_rt `v', lag(5)
                                      matrix M = r(table)
                                      frame post newey_results2 ("`v'") (`iv') (`mc') (M["b", "_cons"]) (M["t", "_cons"])
                                  }
                              }
                          }
                          frame change newey_results2
                          rename (intercept tstat) =_mcap_
                          reshape wide *_mcap_, i(mcap_quintile) j(idiovol_quintile)
                          Notes:

                          1. None of the data set in this thread contain the variables rmrf, smb, or hml. So this code is untested, but I'm pretty sure it will work.

                          2. -newey- does not calculate an R2 or adjusted R2.

                          Comment


                          • #14
                            Mean values and standard deviations of what?
                            of 25 vw and ew time series of portfolio returns generated as result of running #1

                            newey- does not calculate an R2 or adjusted R2.
                            I just want this time simple regression and not newey and west based.

                            Comment


                            • #15
                              I just want this time simple regression and not newey and west based.
                              Code:
                              frame create regress_results str32 indvar int (idiovol_quintile mcap_quintile) ///
                                  float(intercept tstat r2 adj_r2)
                              foreach v of varlist rmrf smb hml {   
                                  forvalues iv = 1/5 {
                                      forvalues mc = 1/5 {
                                          regress ew_mean_mcap_q`mc'_idiovol_q`iv'_rt `v'
                                          matrix M = r(table)
                                          frame post regress_results ("`v'") (`iv') (`mc') (M["b", "_cons"]) ///
                                          (M["t", "_cons"]) (e(r2)) (e(r2_a))
                                      }
                                  }
                              }
                              frame change regress_results
                              rename (intercept tstat r2 adj_r2) =_idiovol_
                              reshape wide *_idiovol_, i(mcap_quintile) j(idiovol_quintile)
                              To get the means and standard deviations of the vw and ew mean returns in the 25 portfolios, in "matrix" form, the following code, run immediately after the calculation of the vw_ and ew_ mean returns , i.e. after -by mdate *_quintile, sort: keep if _n == 1- in #5, use this code:

                              Code:
                              //  CALCULATE MEANS AND STANDARD DEVIATIONS OF EW AND VW MEAN RETURNS
                              collapse (mean) mean_vw_mean_rt = vw_mean_rt mean_ew_mean_rt = ew_mean_rt ///
                                  (sd) sd_vw_mean_rt = vw_mean_rt sd_ew_mean_rt = ew_mean_rt, ///
                                  by(mcap_quintile idiovol_quintile)
                                  
                              rename *_rt =_idiovol_
                              reshape wide *_rt_idiovol_, i(mcap_quintile) j(idiovol_quintile)

                              Comment

                              Working...
                              X