Announcement

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

  • histogram for four groups

    Dear All, Suppose that I have data on Gini coefficient.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float gini
      59
    48.9
      35
      57
      40
    36.7
    57.3
      49
      60
    51.3
    50.1
      54
    52.5
    59.1
    38.3
      51
      43
      57
      43
    45.5
    53.3
    59.5
    38.2
    44.5
    39.9
    39.7
    44.5
      57
    47.8
    42.9
    56.5
    51.6
    43.8
      26
      63
      46
    47.6
    25.7
    63.3
    48.2
    37.9
      57
    48.4
    50.6
    44.1
    35.7
    42.4
    23.1
    38.1
    39.9
    34.6
    19.5
    31.5
    35.7
      41
    33.3
    42.8
      30
    24.9
    32.1
    31.3
    27.4
    28.1
    30.7
      28
      35
    26.9
    20.2
    48.5
    31.6
    27.8
    22.9
      32
    35.5
    34.4
    end
    I'd like to separate the data into four categories, with 30, 40, and 50 as the threshold. Clearly, these four segments include (gini<=30), (30<gini<=40), (40<gini<=50), and (gini>50). My question is how to draw a histogram in such a case? Thanks.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    You need to create such a variable beforehand,

    Comment


    • #3
      Dear Nick, Do you mean this?
      Code:
      recode gini 0/30 =1 30/40 = 2 40/50 = 3 50/max = 4, gen(gini4)
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(gini gini4)
        59 4
      48.9 3
        35 2
        57 4
        40 2
      36.7 2
      57.3 4
        49 3
        60 4
      51.3 4
      50.1 4
        54 4
      52.5 4
      59.1 4
      38.3 2
        51 4
        43 3
        57 4
        43 3
      45.5 3
      53.3 4
      59.5 4
      38.2 2
      44.5 3
      39.9 2
      39.7 2
      44.5 3
        57 4
      47.8 3
      42.9 3
      56.5 4
      51.6 4
      43.8 3
        26 1
        63 4
        46 3
      47.6 3
      25.7 1
      63.3 4
      48.2 3
      37.9 2
        57 4
      48.4 3
      50.6 4
      44.1 3
      35.7 2
      42.4 3
      23.1 1
      38.1 2
      39.9 2
      34.6 2
      19.5 1
      31.5 2
      35.7 2
        41 3
      33.3 2
      42.8 3
        30 1
      24.9 1
      32.1 2
      31.3 2
      27.4 1
      28.1 1
      30.7 2
        28 1
        35 2
      26.9 1
      20.2 1
      48.5 3
      31.6 2
      27.8 1
      22.9 1
        32 2
      35.5 2
      34.4 2
      end
      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment


      • #4
        I don't like recode personally, especially for measured variables, and (much more important) your syntax relies on the reader knowing how recode behaves with ambiguous limits. So -- if obliged to do this (and why categorise measured variables any way?) -- I would use much more explicit syntax:

        Code:
        gen throwingawayinformation = cond(gini <= 30, 1, cond(gini  <= 40, 2 , cond(gini <= 50, 3, 4))) if gini < .
        label def degraded 1 "(0, 30]" 2 "(30, 40]" 3 "(40, 50]" 4 "(50, 100]"
        label val throwingaway degraded
        Nested cond() statements are easier to write and read than often implied -- with a little practice. Here

        if gini <= 30 then 1
        otherwise if gini <= 40 then 2
        otherwise if gini <= 50 then 3
        otherwise 4

        always remembering to map missings to missing.

        Comment


        • #5
          Dear Nick, Many thanks for the suggestion. Now, I do the following
          Code:
          histogram throwingaway, discrete freq addlabels ylabel(,grid)
          and have
          Click image for larger version

Name:	gini4.png
Views:	1
Size:	50.6 KB
ID:	1465887

          An additional question is how can I replace 1 with, say, (0,30], 2 with (30,40', and so on in the x axis? Thanks in advance.
          Ho-Chuan (River) Huang
          Stata 17.0, MP(4)

          Comment


          • #6
            Code:
            xla(1/4, valuelabel)

            Comment


            • #7
              Dear Nick, This is great. Thanks.
              Ho-Chuan (River) Huang
              Stata 17.0, MP(4)

              Comment


              • #8
                The graph is here.
                Click image for larger version

Name:	gini4.png
Views:	1
Size:	54.2 KB
ID:	1466168
                Ho-Chuan (River) Huang
                Stata 17.0, MP(4)

                Comment

                Working...
                X