Announcement

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

  • Summarize variable on two conditions

    Hi,

    I want to get an overview of the average returns (mean_port) for each portfolio number (change5) for each interest rate group (interest3). My data is as following:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float time double mean_port float(change5 interest3)
    738          3.04435198 5 1
    739  2.2211704491304345 5 1
    740  1.7787239991304349 5 1
    741   4.260685091086957 5 1
    742  3.9937772433695646 5 1
    743  1.6834335358695651 5 1
    472   4.030927347586207 1 2
    473  2.6186064764406787 1 2
    474 -1.3769768093103445 1 2
    475  3.2940550017543857 1 2
    476    5.25704224310345 1 2
    518   4.716494105406251 1 2
    519   5.318503456718752 1 2
    520   1.316603170390626 1 2
    521   4.144502554374999 1 2
    522   5.156305800156249 1 2
    523   3.893352732031249 1 2
    524   2.517724156718751 1 2
    525   3.813243186562501 1 2
    526  1.5631468396874995 1 2
    527   5.190297880781248 1 2
    528  2.1100833622950823 1 2
    529          4.41122791 1 2
    530  3.0685434312903226 1 2
    531  3.0657228609836062 1 2
    532  1.7923587693650793 1 2
    533  2.6580123563548392 1 2
    534   2.617864592250001 1 2
    535  2.7895783601666664 1 2
    536   .5556802800000001 1 2
    537  .16628890361016926 1 2
    end
    format %tm time
    I have tried the following code:

    Code:
    foreach interest3 in interest3{ 
        forvalues i=1/5 {
            sum mean_port if (change5 == `i') 
            
        }
    }
    But that doesn't give me the desired results. Does someone know how to do this? The output should give 15 different summaries, for each level of interest3 (ranking 1 to 3) all values for each change5 (ranking 1 to 5).

    Thanks

  • #2
    Juan:
    do you mean something along the following lines?
    Code:
    . bysort change5 interest3 (time): tabstat mean_port, stats(count mean p50 sd min max)
    
    ------------------------------------------------------------------------------------------------------------------------------------------
    -> change5 = 1, interest3 = 2
    
        Variable |         N      Mean       p50        SD       Min       Max
    -------------+------------------------------------------------------------
       mean_port |        25  2.987568  3.065723  1.705078 -1.376977  5.318503
    --------------------------------------------------------------------------
    
    ------------------------------------------------------------------------------------------------------------------------------------------
    -> change5 = 5, interest3 = 1
    
        Variable |         N      Mean       p50        SD       Min       Max
    -------------+------------------------------------------------------------
       mean_port |         6  2.830357  2.632761  1.116929  1.683434  4.260685
    --------------------------------------------------------------------------
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Yes, thank you very much Carlo!

      Comment


      • #4
        Now I need to do the same but with regressions. The data is structured on a different way:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(time excess_ri_port1 excess_ri_port2 excess_ri_port3 excess_ri_port4 excess_ri_port5 mktrf smb hml mom interest3)
        408    -6.0862  -6.084592  -7.516789  -9.230508  -7.184287  5.89  5.53  4.34  2.25 3
        409  -7.915799  -7.423551  -7.407935  -6.985112  -6.306433 -3.46     4  1.01  -.93 3
        410  -4.267283  -7.552239  -6.735311 -3.1535985  -5.397933  -2.8   .97  1.79  -2.5 3
        411  -5.766012  -6.670855  -7.834193   -5.24802   -5.98139  3.79  -.74  1.48  -.02 3
        412 -2.7245796  -6.552633  -6.902941  -6.075362  -5.567965    -4  1.89   .53  -.08 3
        413  -3.774787  -6.596466  -7.676996  -8.079485  -5.564679 -1.16  -.22    -1 -1.02 3
        414  -2.572114  -3.526447 -4.0111613  -4.832021  -2.951513  4.66 -3.05  1.06    .3 3
        415  -6.155262 -4.5445886  -6.251085   -4.94637    -7.0003   1.9  -.46 -2.62  -.55 3
        416  -5.405778  -8.864209 -4.1694107  -4.480172 -4.6766295 -3.58  2.64   .31  1.15 3
        417  -5.167346  -5.090599  -6.991568  -6.278114  -8.121798  3.59 -3.02  -.33   .28 3
        418  -5.193017  -4.914765 -4.4394646  -8.712565  -4.982651 -4.54  -.41   .45 -1.18 3
        419  -5.412989  -5.075597  -7.743191  -7.276817   -6.77974   .02   .29  -.58   .88 3
        420  -3.301854   -4.91427  -4.700615  -6.712114   -1.46246 -1.55  1.51   .96   2.2 3
        421 -4.1672754  -4.306919  -5.916937  -6.810813  -8.784313  1.97   -.9  -.76  1.61 3
        422  -6.097421  -9.160172  -4.577057   -6.72443 -4.4996405  3.45 -1.61 -2.47  -.23 3
        423  -5.592501 -3.8614826 -4.2757745  -6.480077  -1.741828  2.56 -1.67    .4     0 3
        424  -5.050293  -5.964684  -4.086852  -6.373335  -4.411993  1.76  -.28   .46  -.61 3
        425  -5.440351  -6.016227  -4.943147  -7.731796  -4.362458     1  -.06  -.63   3.4 3
        426 -2.2386374  -6.911889   -6.88126  -7.071893  -4.190027  3.99  -1.4 -1.03   .97 3
        427  -4.370318  -4.900883  -6.385117  -6.211898  -9.526418 -4.05  1.21 -2.29  1.89 3
        428  -5.351522  -4.619212  -6.062341 -4.7241836  -5.366378  2.37  -.48 -1.93  5.82 3
        429 -4.2373753  -5.779352  -6.036895  -6.552166  -5.841372  -.71  -.89  -.29  4.58 3
        430  -5.658945  -6.448686   -5.81224   -6.08911  -6.094343  -.32 -3.27   .78  2.15 3
        431  -4.867655  -5.817021  -5.453859  -6.777531  -6.707622  1.84 -1.18  1.03  -.34 3
        432  -2.962077 -3.2679815  -2.870371  -3.996381 -2.8201466   .44   .09   .12  -.73 3
        433 -2.2895257 -3.7956684  -3.379504  -5.220747 -3.6736934  1.88  1.41   .21  1.76 3
        434  -2.804909  -2.479008  -3.886821  -3.872356  -2.465871   .89   .27   .51   2.6 3
        435  -.9820426 -3.1216626  -3.126453  -3.796132  -2.700643   .62   .75   .37  1.17 3
        436 -1.2838748 -2.2350674 -3.2402415  -2.688446   -1.60395   1.5  1.78  -.28  2.09 3
        437  -.4649841 -2.0396426 -4.1546173  -2.786718 -1.3813955   .22  -.63   .33  1.29 3
        end
        format %tm time
        I want to run the following regression for each interest rate group for each change5 group, so again 15 outcomes:
        Code:
        forvalues i=1/5 {
            regress excess_ri_port`i' mktrf smb hml mom, vce(robust)
        }
        Do you know how to do this?

        Thanks

        Comment


        • #5
          Juan:
          is what follows consistent with your research goals?
          Code:
          . forvalues i=1/5 {
            2.
          .     bysort interest3 (time): regress excess_ri_port`i' mktrf smb hml mom, vce(robust)
            3.
          . }
          
          ------------------------------------------------------------------------------------------------------------------------------------------
          -> interest3 = 3
          
          Linear regression                               Number of obs     =         30
                                                          F(4, 25)          =       0.20
                                                          Prob > F          =     0.9344
                                                          R-squared         =     0.0277
                                                          Root MSE          =     1.8682
          
          ------------------------------------------------------------------------------
                       |               Robust
          excess_ri_~1 | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                 mktrf |   -.032486   .1257675    -0.26   0.798    -.2915089     .226537
                   smb |  -.1120999   .2454822    -0.46   0.652    -.6176799    .3934801
                   hml |   .0996707   .2560197     0.39   0.700    -.4276117    .6269532
                   mom |   .1527222   .1716149     0.89   0.382    -.2007253    .5061697
                 _cons |  -4.375985   .3600634   -12.15   0.000    -5.117549    -3.63442
          ------------------------------------------------------------------------------
          
          ------------------------------------------------------------------------------------------------------------------------------------------
          -> interest3 = 3
          
          Linear regression                               Number of obs     =         30
                                                          F(4, 25)          =       1.23
                                                          Prob > F          =     0.3249
                                                          R-squared         =     0.1003
                                                          Root MSE          =     1.8509
          
          ------------------------------------------------------------------------------
                       |               Robust
          excess_ri_~2 | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                 mktrf |   .0606239   .1445305     0.42   0.678    -.2370421      .35829
                   smb |  -.1247829   .2046557    -0.61   0.548    -.5462793    .2967134
                   hml |    .089443   .3269839     0.27   0.787    -.5839929    .7628789
                   mom |   .2619302   .1898805     1.38   0.180    -.1291361    .6529964
                 _cons |  -5.564622     .37118   -14.99   0.000    -6.329082   -4.800163
          ------------------------------------------------------------------------------
          
          ------------------------------------------------------------------------------------------------------------------------------------------
          -> interest3 = 3
          
          Linear regression                               Number of obs     =         30
                                                          F(4, 25)          =       0.21
                                                          Prob > F          =     0.9321
                                                          R-squared         =     0.0222
                                                          Root MSE          =     1.6508
          
          ------------------------------------------------------------------------------
                       |               Robust
          excess_ri_~3 | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                 mktrf |  -.0213444   .1085406    -0.20   0.846     -.244888    .2021992
                   smb |  -.1060273   .1749715    -0.61   0.550    -.4663878    .2543332
                   hml |   .0380117   .1965933     0.19   0.848    -.3668797    .4429031
                   mom |   .0985796   .1454377     0.68   0.504     -.200955    .3981142
                 _cons |  -5.523872   .3466878   -15.93   0.000    -6.237889   -4.809855
          ------------------------------------------------------------------------------
          
          ------------------------------------------------------------------------------------------------------------------------------------------
          -> interest3 = 3
          
          Linear regression                               Number of obs     =         30
                                                          F(4, 25)          =       0.05
                                                          Prob > F          =     0.9953
                                                          R-squared         =     0.0106
                                                          Root MSE          =     1.7992
          
          ------------------------------------------------------------------------------
                       |               Robust
          excess_ri_~4 | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                 mktrf |  -.0336952   .1539283    -0.22   0.829    -.3507164    .2833261
                   smb |  -.0632184   .2164883    -0.29   0.773    -.5090843    .3826476
                   hml |  -.0453621   .2452856    -0.18   0.855    -.5505372    .4598131
                   mom |    .034447   .1731262     0.20   0.844    -.3221131     .391007
                 _cons |  -5.868634    .403641   -14.54   0.000    -6.699948    -5.03732
          ------------------------------------------------------------------------------
          
          ------------------------------------------------------------------------------------------------------------------------------------------
          -> interest3 = 3
          
          Linear regression                               Number of obs     =         30
                                                          F(4, 25)          =       0.30
                                                          Prob > F          =     0.8765
                                                          R-squared         =     0.0413
                                                          Root MSE          =       2.29
          
          ------------------------------------------------------------------------------
                       |               Robust
          excess_ri_~5 | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                 mktrf |   .0128831   .1685353     0.08   0.940     -.334222    .3599881
                   smb |   -.119154   .2910414    -0.41   0.686    -.7185651     .480257
                   hml |   .3293527   .3411003     0.97   0.344    -.3731565    1.031862
                   mom |   .0999447   .1689208     0.59   0.559    -.2479543    .4478437
                 _cons |  -5.053047   .3930418   -12.86   0.000    -5.862531   -4.243562
          ------------------------------------------------------------------------------
          
          .
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment


          • #6
            Yes, thank you so so much Carlo! You are a legend

            Comment

            Working...
            X