Announcement

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

  • How to combine the command "inequal" with "by"

    I need to generate Gini and Theil indices for each state (uf) and year (ano) combination of a database consisting of 27 states and 5 years. I tried the following syntax:

    bysort uf ano: inequal rendpc [fw = pesopes]
    But the Stata informs me that the command "inequal" can not be combined with "by".

    I would like your help to find out how I can do this using the "inequal" command, or another command that you can show me.

    Thanks in advance

    Girlan Oliveira

  • #2
    Girlan:
    you can work that drawback around with the following trick:
    Code:
    egen Group=Group(uf ano)
    Then you can use -forvalues- to loop over -Group-.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      As I understand it, this refers to inequal (STB-23) as in

      Code:
      *! version 2.1.0  9/8/94        sg30: STB-23
      *Edward Whitehouse  Institute for Fiscal Studies
      pr def inequal
        version 3.1
        set more 1
        local varlist "req ex max(1)"
        local if "opt"
        local in "opt"
        local weight "fweight"
        parse "`*'"
        confirm new var _use _i _temp 
        di
        di in green "inequality measures of " in yellow "`varlist'"
        di in green _d(78) "-"
        quietly { 
          preserve
          gen byte _use = 1 `if' `in'
          keep if _use==1
          su `varlist' [`weight'`exp']
          local mn = _result(3)
          local tot = _result(1)
          local vari = _result(4)
          sort `varlist'
          local wt : word 2 of `exp'
          if "`wt'"=="" {gen _i = [_n]
                         local wt = 1}
          else gen _i = sum(`wt')
      * relative mean deviation
          gen _temp = sum(`wt'*abs(`varlist'-`mn')) 
          local rmd = _temp[_N]/(2*`mn'*`tot')
      * coefficient of variation
          local cov = `vari'^0.5/`mn'
      * standard deviation of logs
          replace _temp = log(`varlist')
          su _temp [`weight'`exp']
          local sdl = (_result(4))^0.5
      * gini
          replace _temp = sum(`wt'*_i*(`varlist'-`mn'))
          local gini = (2*_temp[_N])/(`tot'^2*`mn')
      * mehran 
          replace _temp = sum(`wt'*_i*(2*`tot'+1 -_i)*(`varlist' - `mn'))
          local mehran = (3*_temp[_N])/(`tot'^3*`mn')
      * piesch
          replace _temp = sum(`wt'*_i*(_i-1)*(`varlist'-`mn'))
          local piesch = 3*_temp[_N]/(2*`tot'^3*`mn')
      * kakwani
          replace _temp = sum(`wt'*((`varlist'^2+`mn'^2)^0.5))
          local kakwani = (1/(2-2^0.5))*((_temp[_N]/(`tot'*`mn')-2^0.5))
      * theil 
          replace _temp = sum(`wt'*((`varlist'/`mn')*(log(`varlist'/`mn'))))
          local theil = _temp[_N]/`tot'
      * mean log deviation
          replace _temp = sum(`wt'*(log(`mn'/`varlist')))
          local mld = _temp[_N]/`tot'
          }
          di in green "relative mean deviation " _col(40) in yellow `rmd'
          di in green "coefficient of variation" _col(40) in yellow `cov'
          di in green "standard deviation of logs" _col(40) in yellow `sdl'
          di in green "Gini coefficient" _col(40) in yellow `gini'
          di in green "Mehran measure" _col(40) in yellow `mehran'
          di in green "Piesch measure" _col(40) in yellow `piesch'
          di in green "Kakwani measure" _col(40) in yellow `kakwani'
          di in green "Theil entropy measure" _col(40) in yellow `theil'
          di in green "Theil mean log deviation measure" _col(40) in yellow `mld'
          di in green _d(78) "-"
        end
      But we should not have to guess. You are asked to explain where user-written programs you refer to come from (FAQ Advice #12).

      Carlo's advice is good, but see also http://www.stata.com/support/faqs/da...ach/index.html

      Comment


      • #4
        Girlan could look at ineqdeco and ineqdec0 on SSC.

        Comment


        • #5
          Thanks Carlo, Nick and Stephen.

          With loops I solved my problem.

          Girlan

          Comment


          • #6
            Gentlemen,

            As I said, I could solve the problem with loops. Now, however, I would like to count on your help in another question. I want to know how do I capture, in the outputs of the command, only the informations they I want, which are the Gini coefficient coefficients, Theil mean log deviation measures and Theil entropy measure for each state (uf) and year (ano).
            The syntax I'm using is as follows:

            levelsof uf, local(ufs)

            foreach c of local ufs {
            levelsof ano if uf == `c', local(anos)

            foreach y of local anos {
            display `"uf == `c', ano == `y'"'
            inequal rendomdefpc [fw=pesopes] if uf == `c' & ano == `y'
            }

            }

            The results provided by Stata are in the logfile attached.

            Thanks in advance

            Girlan Oliveira
            Attached Files

            Comment


            • #7
              As has been recommended, don't use inequal: it doesn't save results. In any case, it's functionality has been superceded by other programs on SSC by various people including me. I refer again to post #4 and the modules cited there. In addition your questions have not been sufficiently clear, i.e. not distinguishing between wanted to generate output per se (going to the output window and/or log file), and saving the estimation results as data. You can derive the latter in multiple ways; here follows an example based on post and its siblings. (Philippe Van Kerm has an article in the Stata Journal about using it for this sort of task; it's a free download.) You should also read up on statsby

              The code below is untested. But you should get the basic idea to follow up (after reading help files for the relevant programs.

              Code:
              tempname out1
              postfile `out1'  year state gini mld theil using combine01.dta , replace
              
              levelsof uf, local(ufs)
              
              foreach c of local ufs {
                    levelsof ano if uf == `c', local(anos)
              
                        foreach y of local anos {
                                        display `"uf == `c', ano == `y'"'
                                        ineqdeco rendomdefpc [fw=pesopes] if uf == `c' & ano == `y'
                                      
                                        local gini = r(gini)
                                        local mld = r(ge0)
                                        local theil = r(ge1)
                                    
                                       post `out1' (`y') (`c')  (`gini') (`mld') (`theil') 
                                        
                    }
              }
              
              postclose `out1'
              
              use combine01, clear
              summarize
              sort state year
              list, noobs sepby(state)
              
              ** alternative **
              statsby gini = r(gini) mld = r(ge0) theil = r(ge1) , by(ufs anos):  ineqdeco rendomdefpc [fw=pesopes], saving(combine02.dta, replace)
              use combine02, clear
              summarize
              sort ufs anos
              list, noobs sepby(ufs)

              Comment

              Working...
              X