Announcement

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

  • Double sorted portfolios using loops

    Dear all,

    I sort all stock into quintiles of smoothing and quintiles of dividend yield. Then average the institutional holdings across each of resulting 25 dividend yield-smoothing portfolios.

    Code:
    xtile g1 = DIVY, nq(5)
    xtile g2 = Smooth, nq(5)
    forvalues i=1(1)5{
      forvalues j=1(1)5{
      egen dys`ij'=mean(Ins) if g1==`i'&g2==`j'
      }
       }
    I know there is no such thing as dys`ij', however is there a way of generating a new variable based on each i and j?

    Thanks

  • #2
    One variable will suffice:

    Code:
    xtile g1 = DIVY, nq(5)  
    xtile g2 = Smooth, nq(5)  
    egen dys = mean(Ins), by(g1 g2)

    Comment


    • #3
      Thanks Nick,

      What if the portfolio is rebalanced each year, as following code does not work.

      bys year: egen dys = mean(Ins), by(g1 g2)

      Comment


      • #4
        Don't mix syntaxes then. Choose one of these:

        Code:
        bys year g1 g2: egen dys = mean(Ins)
        Code:
        egen dys = mean(Ins), by(year g1 g2)

        Comment


        • #5
          thanks again, one more question that how to display the mean for each 25 portfolio?

          Comment


          • #6
            Display means: graph (what kind?)? tabulate?

            25 means: for one specific year?

            Comment


            • #7
              Every year, 25 portfolios are formed by smoothing (g1) and dividend yield (g2), and it rebalances every year. In my sample (year 2005-2015), I wish to display the mean of Ins over 11 years for each smoothing-dividend yield portfolio.

              Comment


              • #8
                I use following code to generate 25 portfolios, I wonder if I could display it as a matrix that sorted by g1 and g3?

                Code:
                egen group = group (g1 g3)
                sort g1 g3
                tabstat Ins, statistics(mean) by(group) columns(variables)
                group | e(Ins)
                -------------+-----------
                1 | .2585343
                2 | .3571414
                3 | .3537546
                4 | .3683974
                5 | .3668072
                6 | .3029167
                7 | .3524086
                8 | .3680042
                9 | .3753953
                10 | .3668159
                11 | .2398807
                12 | .3264467
                13 | .3356975
                14 | .3619037
                15 | .2993288
                16 | .1762831
                17 | .3246617
                18 | .280326
                19 | .3094842
                20 | .260087
                21 | .3019937
                22 | .3408796
                23 | .3575331
                24 | .3791005
                25 | .3416753
                -------------+-----------
                Total | .3232011

                also I run the ttest for the difference group by group, is there a simpler way (or loop) to do this?

                Code:
                gen v1=1 if g1==1 & g3==1
                replace v=0 if g1==5 & g3==1
                ttest Ins,by(v1)
                gen v2=1 if g1==1 & g3==2
                replace v2=0 if g1==5 & g3==2
                ttest Ins,by(v2)
                gen v3=1 if g1==1 & g3==3
                replace v3=0 if g1==5 & g3==3
                ttest Ins,by(v3)
                gen v4=1 if g1==1 & g3==4
                replace v4=0 if g1==5 & g3==4
                ttest Ins,by(v4)
                gen v5=1 if g1==1 & g3==5
                replace v5=0 if g1==5 & g3==5
                ttest Ins,by(v5)

                Comment

                Working...
                X