Announcement

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

  • Ciplot for multiple groupvars

    Dear All,

    I am trying to create a series of ciplots displaying the mean value and ci for different groups. Unfortunately it appears ciplot allows just for one groupvar with the by() command.

    Here is my dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long country float idno str15 party float(vote dist_lr dist_red dist_immpoor) byte gender float age
     3  3 "AfD"    0 4.2380953         6       9.9 0 2
     3  3 "SPD"    0 1.3809524 3.0526316      4.15 0 2
     3  3 "CDU"    1  .8571429  5.947369         6 0 2
     3  3 "LINKE"  0 3.5714285  .8947368       2.7 0 2
     3  3 "FDP"    0 1.4285712  8.578947      5.95 0 2
     3  3 "GRUNEN" 0 1.7619047  3.421053       1.7 0 2
    10  6 "GP"     0  1.857143        .5  3.333333 1 1
    10  6 "FF"     0  .4285712  2.357143  1.833333 1 1
    10  6 "Lab"    0 1.4285715  .7857144  2.833333 1 1
    10  6 "SF"     0 2.4285715  .2142856  2.333333 1 1
    10  6 "FG"     1  .8571429  3.357143         2 1 1
     6  7 "SFP"    0         0 2.2142859 1.0476189 1 3
     6  7 "SDP"    0  2.857143  .7857143  .3809524 1 3
     6  7 "VIHR"   0 2.2857144  .3571429 2.0476189 1 3
     6  7 "KESK"   0 .14285707  2.428571 2.2380955 1 3
     6  7 "KD"     0  .8571429 2.2857141 2.4358976 1 3
     6  7 "PS"     0 1.5714288  2.785714  6.452381 1 3
     6  7 "KOK"    1  1.714286  4.357143  1.095238 1 3
     6  7 "VAS"    0       4.5 1.4285715 1.1190476 1 3
     7  9 "PS"     1         1      2.25 .58333325 1 3
     7  9 "LR"     0     5.875     7.375  4.541667 1 3
     7  9 "LREM"   0 4.3333335  5.833333 2.3333333 1 3
     7  9 "MoDem"  0     4.125  5.857143 2.0416667 1 3
     7  9 "FI"     0       .75      .625  .6666667 1 3
     7  9 "EELV"   0        .5     2.375 1.7083333 1 3
     7  9 "RN"     0      7.75         4  6.541667 1 3
     8 10 "LIBDEM" 0 .23529434  .9705882  .7450979 1 1
     8 10 "UKIP"   0    4.6875 4.5666666  6.137256 1 1
     8 10 "LAB"    0 2.0588236  .9117647 .19607854 1 1
     8 10 "SNP"    0        .5      .625         1 1 1
     8 10 "GREEN"  0         2      .625  1.627451 1 1
     8 10 "CONS"   1  3.117647  4.147059  4.254902 1 1
    12 11 "VVD"    0 2.6153846       .75 4.5897436 0 1
    12 11 "D66"    1 .15384626      1.75 .33333325 0 1
    12 11 "SP"     0 3.6153846      6.75 1.9166667 0 1
    12 11 "PVV"    0 3.6923075 1.6666665  6.589744 0 1
    12 11 "PvdA"   0 1.3846154  4.666667  .8333333 0 1
    12 11 "CDA"    0 1.8461537       1.5  3.666667 0 1
    12 11 "CU"     0  .0769229  3.318182 1.1282051 0 1
    12 11 "GL"     0  2.692308  5.318182 1.7179487 0 1
     1 13 "SPO"    0         5       2.4 1.3666666 1 1
     1 13 "OVP"    0       2.1       6.2  5.266667 1 1
     1 13 "Grune"  0       6.5       2.5  .6333332 1 1
     1 13 "FPO"    1 .10000038       5.6  6.466667 1 1
     1 13 "NEOS"   0       3.1       6.9  .9666669 1 1
    11 14 "FdI"    0 2.0526314  3.029412 3.1754384 1 0
    11 14 "LN"     0 1.7894735   4.81579  3.280702 1 0
    11 14 "FI"     1 .05263138  4.388889  .3333335 1 0
    11 14 "PD"     0  3.789474 .22222233  3.614035 1 0
    11 14 "M5S"    0 2.2222223  .6052631 .11111116 1 0
    11 14 "SI"     0  5.555555       1.5  5.979167 1 0
     7 17 "RN"     0      2.75         1 3.2083335 0 0
     7 17 "PS"     0         4      2.75 3.9166665 0 0
     7 17 "EELV"   0       4.5     2.625  5.041667 0 0
     7 17 "FI"     0      5.75     4.375 2.6666665 0 0
     7 17 "LREM"   0  .6666665  .8333335         1 0 0
     7 17 "MoDem"  0      .875  .8571429 1.2916665 0 0
     7 17 "LR"     1      .875     2.375 1.2083335 0 0
     6 21 "VIHR"   0 1.2857144  .3571429  5.380952 1 0
     6 21 "KOK"    1  2.714286  4.357143 2.2380953 1 0
     6 21 "PS"     0  2.571429  2.785714 3.1190476 1 0
     6 21 "SFP"    0         1 2.2142859  4.380952 1 0
     6 21 "VAS"    0       3.5 1.4285715  4.452381 1 0
     6 21 "SDP"    0  1.857143  .7857143  2.952381 1 0
     6 21 "KESK"   0  .8571429  2.428571 1.0952377 1 0
     6 21 "KD"     0  1.857143 2.2857141  .8974357 1 0
    11 22 "FdI"    0  7.052631  3.029412  9.842105 0 0
    11 22 "LN"     0  6.789474   4.81579  9.947369 0 0
    11 22 "SI"     0  .5555556       1.5     .6875 0 0
    11 22 "M5S"    0  2.777778  .6052631  6.555555 0 0
    11 22 "PD"     1 1.2105262 .22222233 3.0526316 0 0
    11 22 "FI"     0  4.947369  4.388889         7 0 0
     3 23 "CDU"    0  2.857143  5.947369         6 0 3
     3 23 "LINKE"  0 1.5714285  .8947368       2.7 0 3
     3 23 "AfD"    0  6.238095         6       9.9 0 3
     3 23 "GRUNEN" 0  .2380953  3.421053       1.7 0 3
     3 23 "SPD"    1  .6190476 3.0526316      4.15 0 3
     3 23 "FDP"    0  3.428571  8.578947      5.95 0 3
     3 24 "SPD"    1 1.6190476  .5526316  .8166668 1 2
     3 24 "GRUNEN" 0 1.2380953  .9210527 1.6333332 1 2
     3 24 "AfD"    0  7.238095       3.5  6.566667 1 2
     3 24 "FDP"    0  4.428571  6.078947 2.6166666 1 2
     3 24 "LINKE"  0 .57142854 1.6052632  .6333332 1 2
     3 24 "CDU"    0  3.857143 3.4473686  2.666667 1 2
    10 25 "GP"     0  3.142857         3  3.333333 0 1
    10 25 "SF"     0 2.5714285 2.2857144  2.333333 0 1
    10 25 "FG"     0  5.857143  5.857143         2 0 1
    10 25 "FF"     1  5.428571  4.857143  1.833333 0 1
    10 25 "Lab"    0 3.5714285 3.2857144  2.833333 0 1
    10 26 "FG"     0  .8571429  1.642857  5.333333 1 2
    10 26 "Lab"    0 1.4285715  4.214286  6.166667 1 2
    10 26 "FF"     1  .4285712  2.642857  5.166667 1 2
    10 26 "GP"     0  1.857143       4.5  6.666667 1 2
    10 26 "SF"     0 2.4285715  5.214286  5.666667 1 2
     5 27 "Cs"     0       7.2  6.533333  6.428571 1 2
     5 27 "Vox"    0  9.714286  8.333333       9.8 1 2
     5 27 "Pais"   0  2.733333 2.3333333 2.2857144 1 2
     5 27 "IU"     0 1.8666667       1.4  2.533333 1 2
     5 27 "PSOE"   1       3.6       2.8 3.9333334 1 2
     5 27 "PPP"    0  8.066667  7.266667       7.6 1 2
    end
    label values country country_label
    label def country_label 1 "1. Austria", modify
    label def country_label 3 "3. Germany", modify
    label def country_label 5 "5. Spain", modify
    label def country_label 6 "6. Finland", modify
    label def country_label 7 "7. France", modify
    label def country_label 8 "8. Great Britain", modify
    label def country_label 10 "10. Ireland", modify
    label def country_label 11 "11. Italy", modify
    label def country_label 12 "12. Netherlands", modify
    label values gender gender
    label def gender 0 "0. Female", modify
    label def gender 1 "1. Male", modify
    label values age age_groups
    label def age_groups 0 "0. 30 and Under", modify
    label def age_groups 1 "1. 31-50", modify
    label def age_groups 2 "2. 51-69", modify
    label def age_groups 3 "3. 70 and over", modify
    Here is the code I am using:

    Code:
     ciplot dist_lr if vote==1, by(gender)
    And here is what I would like to do:

    Code:
     ciplot dist_lr if vote==1, by(gender age education)
    I had a look at previous forum conversations and at the help window for ciplot where statsby is suggested for more flexibility. Yet, I have not figure out how to make it work for my specific case.

    Lastly, I would then like to do the same but for different variables (eg. dist_red dist_immdfrace). Perhaps I could then combine the graphs?

    Sincerely
    Mattia

  • #2
    I have tried hard to convince people that ciplot (from SSC) is no longer to be used unless it covers your use case. But if you create a composite


    Code:
    egen group = group(gender age education), label
    then you have just one group variable. See also https://www.stata-journal.com/articl...article=dm0034

    That could easily get messy, as the number of categories concerned might well be 50 to 100, but it's what you're asking for. But in turn combining that graph with yet others is predictably yet messier.

    Is that dataset your entire dataset, or just an example?

    Comment


    • #3
      Dear Nick,

      thanks for the advice. What should I use instead of ciplot? As for the "group" variable, this is not my intended purpose as it creates different combinations of the three specified variables. I am instead trying to get means for the three variables, separately, yet in the same plot. This means that, if for gender I have "male" and "female", for education "low", "medium", "high", and for age "under 30" "30-60" "60 and over", I would like to get 8 means (and CIs). Sorry if I was not clear before.

      The dataset presented here is just a part of the entire dataset.

      Sincerely
      Mattia

      Comment


      • #4
        That's not the standard meaning of a by() option in Stata. The standard meaning is, as you found, that you get cross-combinations of two or more variables.

        But wanting them by(a b c) to mean by(a) AND by(b) AND by(c) -- although by(a b c) won't do it -- makes sense and can be done.

        Here's a sketch. Different dataset, but one outcome and three categorical predictors.

        Code:
        set scheme stcolor
        
        foreach v in race collgrad south {
            webuse nlswork, clear
            levelsof `v', local(levels)
            su `v', meanonly
            local min = r(min) - 0.3
            local max = r(max) + 0.3
            statsby, by(`v') : ci means ln_wage
        
            scatter mean `v' || rcap ub lb `v', name(`v', replace) xsc(r(`min' `max')) xla(`levels', valuelabel) legend(off)
        }
        
        graph combine race collgrad south, row(1) ycommon l1title(ln_wage)

        Comment


        • #5
          Dear Nick,

          It works perfectly. Thanks a lot.

          Sincerely
          Mattia

          Comment

          Working...
          X