Announcement

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

  • Loop Country Year

    Hello,

    I'm encountering issues with my Stata code. I am trying to establish a loop to compute the Gini for each country and each year. So far, here's my code:

    use gini, clear
    gen GINIhearng=.
    gen sumhearng=.
    gen addedhearng=.
    gen LorenzCurvehearng=.
    gen Bhearng=.
    gen Ahearng=.

    levelsof cnt2y , local(clist) clean
    local nc : word count `clist'
    forvalues i=1/`nc' {
    local j: word `i' of `clist'
    dis `j'
    forvalues `j' of varlist cnt2y {
    bysort ehearng cnt2y :replace sumhearng = sum(ehearng)
    replace addedhearng = sum(ehearng)
    replace LorenzCurvehearng = addedhearng/sumhearng

    replace Bhearng = sum(LorenzCurvehearng)
    replace Ahearng = 5000 - Bhearng
    replace GINIhearng = Ahearng/(Ahearng+Bhearng)
    }
    }

    Has anyone any ideas of why the error Stata is giving me is that it's not finding AT2004 which is my first country year code?

  • #2
    I can't explain the particular error you are getting. But I also can't make much sense out of your code. In fact, I don't even see how it gets far enough to give you the error you are reporting. -forvalues `j' of varlist cnt2y- is a syntax error in multiple ways, and you shouldn't be able to get any farther than that.

    Putting that problem aside for the moment, the content of your loop makes no sense. Your outer loop iterates over `i', and your inner loop, if it were valid, would iterate over `j' or perhaps over something else. But the content over which these loops reign is:
    Code:
    bysort ehearng cnt2y :replace sumhearng = sum(ehearng)
    replace addedhearng = sum(ehearng)
    replace LorenzCurvehearng = addedhearng/sumhearng
    
    replace Bhearng = sum(LorenzCurvehearng)
    replace Ahearng = 5000 - Bhearng
    replace GINIhearng = Ahearng/(Ahearng+Bhearng)
    and it makes no mention of `i', or `j', nor anything else that might potentially serve as an iterator. So it just repeats the same thing over and over again (or would if it were syntactically admissible).

    My intuition is that, in fact, no loops are needed for what you want to do. I suspect it can all be accomplished with one or more -bysort- commands, perhaps even with some or all of the ones you have already written there, and the loops are just excess baggage. Anyway, for more concrete advice, I suggest you post back and explain either in very clear language, or with some formulas, just what it is you want to compute here, and then also provide example data from your Stata data set. To show the example data, please use the -dataex- command. And be sure to pick example data for which the calculations can be carried out: for example make sure you have a few different countries and a full range of years for each of those. And make sure to include all variables needed for the calculations, and that those variables have a sufficient number of non-missing values to be usable for calculation.

    If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Alternatively, there are several user-written commands for calculating the Gini index, and one or more of them probably will be suitable for your purposes. That would be easier than trying to reinvent this particular wheel. Run -search Gini- in Stata and you will get a list of possibilities, with links to the packages themselves.

    Comment


    • #3
      Dear forum members,

      I want to estimate the equation given below-

      example data set is here-
      Code:
      clear
      input float(obs edu G year)
       1 9 1 1
       2 5 1 1
       3 4 2 1
       4 6 2 1
       5 1 2 1
       6 2 1 2
       7 3 1 2
       8 5 1 2
       9 7 2 2
      10 9 2 2
      end
      la var obs "id"
      la var edu "years of education"
      la var G "group"
      la var year "year"
      Equation-

      \[ \frac{1}{2\bar{y}} \sum_{g=1}^{G} \sum_{k=1}^{K} p_g p_k \left| \bar{y}_g - \bar{y}_k \right| \]

      where:
      g and k are groups
      pg and pk are the respective groups' share in the total population
      \[ \bar{y} \] is overall mean
      Last edited by Mukesh Punia; 23 Apr 2024, 03:15.
      Best regards,
      Mukesh

      Comment


      • #4
        Code:
        count
        local denom `r(N)'
        by G, sort: gen p = _N/`denom'
        by G: egen ybar = mean(edu)
        
        summ edu, meanonly
        local ybar_grand `r(mean)'
        
        egen tag = tag(G)
        frame put G ybar p if tag, into(working)
        
        frame change working
        rename (ybar p) =_g
        tempfile copy
        save `copy'
        rename *_g *_k
        rename G K
        cross using `copy'
        drop if G == K
        egen grand_sum = total(p_g*p_k*abs(ybar_g - ybar_k))
        local wanted = grand_sum[1]/(2*`ybar_grand')
        frame change default
        frame drop working
        
        display "Desired result = `wanted'"

        Comment


        • #5
          Respected Prof. Clyde Schechter, thank you for your prompt response. Sorry, I checked the post late, so I am a bit late to acknowledge your efforts and help.

          I am using (Stata 15.1 SE) which does not have frames. Is there a way to do the same in STATA 15.1 SE.

          I can provide more data for analysis. I am working on a dataset with three-time (2005, 2015, & 2021) periods and 5 States.

          Thank you - Mukesh
          Last edited by Mukesh Punia; 08 Aug 2024, 10:04.
          Best regards,
          Mukesh

          Comment


          • #6
            Yes.
            Code:
            count
            local denom `r(N)'
            by G, sort: gen p = _N/`denom'
            by G: egen ybar = mean(edu)
            
            summ edu, meanonly
            local ybar_grand `r(mean)'
            
            egen tag = tag(G)
            preserve
            keep if tag
            keep G ybar p
            rename (ybar p) =_g
            tempfile copy
            save `copy'
            rename *_g *_k
            rename G K
            cross using `copy'
            drop if G == K
            egen grand_sum = total(p_g*p_k*abs(ybar_g - ybar_k))
            local wanted = grand_sum[1]/(2*`ybar_grand')
            display "Desired result = `wanted'"
            restore
            Note: The -preserve- and -restore- are there just so that you end up with the original data in memory at the end. If you don't actually need that, you can omit those two commands.

            If you read the Forum FAQ, as all members are requested to do before posting, you will see that it is recommended always to state what version of Stata you are using if you are not using the most current version (which, as of today, is 18.5). This will prevent your getting advice that you cannot implement, and avoid wasting both your time and that of the person who responds to your question.

            Comment


            • #7
              Thank you, Prof. Clyde Schechter,

              At the end of post #3, I added my signature with Stata version, as given below. I am not aware whether it is visible to other members or not.


              Best regards,
              Mukesh

              (Stata 15.1 SE)
              Last edited by Mukesh Punia; 09 Aug 2024, 09:56.
              Best regards,
              Mukesh

              Comment


              • #8
                Oh, I did not see that. I have to say that I generally don't even look at signatures when I read posts here.

                Comment

                Working...
                X