Announcement

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

  • Calculate gini coefficients for each groups of a dataset

    Hello,

    I want to calculate ginicoefficients for each group of a dataset. The table shows an example of my data. There are 28 observation. Each belongs to one of the seven groups. I would like to create a variable that shows the gini-coeffcients for each group.

    Thanks a lot,
    kind regards,
    Chris


    Click image for larger version

Name:	Unbenannt.png
Views:	2
Size:	47.8 KB
ID:	1573734


    Attached Files

  • #2
    Code:
    ssc install ineqdeco, replace
    ssc install ineqdec0, replace
    help ineqdeco  // inter alia read the section on Saved Results
    
    ineqdec0 Income, by(GROUP)   // use ineqdec0 for this if you want to include the zero-valued obs
    return list
    Welcome to Statalist. Please take some minutes to read and digest the Forum FAQ (hit the black bar at top of page). There's wise advice about how to post effectively, including using -dataex- to post example data. (It's great that you provide a sample; but attaching files is not the way to go.) There's also useful advice about -search-ing before posting. (This topic has come up before. There are related posts about how to save the by-group GIni coefficients to a new variable.)

    PS I hope you have more obs per group than shown in your example, else the sample sizes are likely too small to provide reliable results.

    Good luck

    Comment


    • #3
      Here is a snippet of my dataset.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str8 ID int group float income_pc
      "01051001" 1051  639.3264
      "01051002" 1051  424.4469
      "01051003" 1051  306.6302
      "01051004" 1051 548.79254
      "01051005" 1051  311.2771
      "01053001" 1053  255.2192
      "01053002" 1053 474.31525
      "01053003" 1053 527.14197
      "01053004" 1053  254.1557
      "01053005" 1053  499.8528
      "01054001" 1054  514.4382
      "01054002" 1054  846.9942
      "01054003" 1054 1449.7985
      "01054004" 1054  877.7455
      "01054005" 1054  1001.039
      "01055001" 1055   418.779
      "01055002" 1055 318.44305
      "01055004" 1055  585.9078
      "01055006" 1055  484.2312
      "01055007" 1055  950.6077
      "01056001" 1056  435.9151
      "01056002" 1056  450.8452
      "01056003" 1056   390.736
      "01056004" 1056  443.6511
      "01056005" 1056  916.5355
      "01057001" 1057  423.9086
      "01057002" 1057 291.88678
      "01057003" 1057   386.712
      "01057004" 1057  499.0727
      "01057005" 1057  490.2137
      "01058001" 1058 277.67184
      "01058003" 1058  338.9327
      "01058004" 1058   1248.96
      "01058005" 1058  379.1982
      "01058007" 1058 262.26837
      "01059001" 1059  485.3735
      "01059002" 1059 295.47534
      "01059005" 1059  326.2719
      "01059006" 1059 261.53494
      "01059008" 1059 1482.1683
      "01060002" 1060 539.05566
      "01060003" 1060 210.09763
      "01060004" 1060  456.9526
      "01060005" 1060  714.7492
      "01060006" 1060 181.82126
      "01061001" 1061  205.6357
      "01061002" 1061  2511.281
      "01061003" 1061 262.70682
      "01061004" 1061  510.0822
      "01061005" 1061 121.01418
      "01062001" 1062 1145.5166
      "01062003" 1062  330.3263
      "01062004" 1062  891.2172
      "01062005" 1062  257.3554
      "01062006" 1062 1463.7435
      end


      As described in the opening post, I want to calculate a variable that equals the ginicoefficient for each group.


      I used the following command

      Code:
      gen gini =.
      ineqdeco income_pc , by(group)
      foreach z in 1051 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062    {
      replace gini=r(gini_`z') if income_pc==`z'
      }

      but no values are replaced

      Code:
      foreach z in 1051 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062{
        2.
      . replace gini=r(gini_`z') if income_pc==`z'
        3.
      . }
      (0 real changes made)
      (0 real changes made)
      (0 real changes made)
      (0 real changes made)
      (0 real changes made)
      (0 real changes made)
      (0 real changes made)
      (0 real changes made)
      (0 real changes made)
      (0 real changes made)
      (0 real changes made)
      Whats wrong in my code?

      Thanks a lot!
      Chris

      Comment


      • #4
        In your code, you need to "store" the gini coefficients looping across groups, not incomes:
        Code:
        gen gini =.
        ineqdeco income_pc , by(group)
        foreach z in 1051 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062    {
        replace gini=r(gini_`z') if group==`z'
        }

        Comment

        Working...
        X