Announcement

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

  • xtile/pctile combined with egen and foreach

    Dear Statalist,

    I am trying to compute percentiles across distinct values of a given variable.
    A possible approach, as exemplified in this topic was to write something like this:

    Code:
     egen mpg_group = xtile(mpg), by(weight_group) nq(3) 
    However, Stata 13 apparently does not recognize anymore this syntax:

    Code:
     unknown egen function xtile()
    or the alternative:

    Code:
    . bysort weight_group: gen mpg_group = xtile(mpg)
    unknown function xtile()
    I thought then to loop over values of weight_group:

    Code:
    levelsof weight_group, local(levels2)
    foreach k of local levels2 {
        xtile mpg_group_`k' = mpg, nq(3) 
        }
    *
    but the loop fails to assign percentiles by weight_group as it can be verified by comparing mpg_group_0 with mpg_group_1.

    Code:
    corr mpg_group_0 mpg_group_1
    sort mpg_group* weight_group
    order mpg_group* weight_group mpg, last
    Any hints on any alternative syntax or possible solution? Thank you very much.

    Andre

  • #2
    Various different problems here:

    1. xtile()
    as an egen function was only ever a user-written function downloadable via ssc inst egenmore.

    If your Stata 13 doesn't recognise it, that is only because it has not been installed where Stata 13 can see it.

    2. xtile() as a Stata function doesn't exist. This remains true in Stata 14,

    3. I can't reproduce whatever the problem is here as you don't give data or explain where the dataset comes from. We can't see the result of the correlation.

    Downloading egenmore appears be your solution, however.
    Last edited by Nick Cox; 08 Apr 2015, 13:08.

    Comment


    • #3
      Dear Nick,

      Again, thank you. Egenmore is indeed the solution.
      I was replicating my problem using the same data that you used in the post that I linked.

      Yes, you're right - my idea was just a quick a way of visually inspect whether percentiles were assigned taking into consideration the values of weight_group.

      Andre

      Comment


      • #4
        You're right. I didn't click on the link or remember a post I made in 2008....

        Comment


        • #5
          Just by way of explanation, here's what's wrong with:

          Code:
          levelsof weight_group, local(levels2)
          foreach k of local levels2 {
              xtile mpg_group_`k' = mpg, nq(3)
              }
          Look closely at what's inside your loop. You do create different variables for each value of k. But on the right hand side of the equals sign, everything is the same each time through the loop. So you ended up with a bunch of new variables, all containing the same thing. You need to distinguish the iterations of the loop, restricting application to a single different weight_group each time.

          Code:
          levelsof weight_group, local(levels2)
          foreach k of local levels2 {
              xtile mpg_group_`k' = mpg if weight_group == `k', nq(3)
              }

          Comment


          • #6
            Dear Clyde,

            Thank you for your explanation. I was not sure myself whether I would need to restrict the command to each k.
            Andre

            Comment


            • #7
              Dear Sir, what do you mean by weight_group? e.g. i want to make portfolio on the basis of sentiment beta, so i can come up with average returns for each portfolios. the purpose is sentiment risk premia.

              Comment


              • #8
                Dear Sir, what do you mean by weight_group?

                weight_group is the name of a variable used by the person who started this thread. In #1, he shows code using it as a grouping variable for calculating terciles within those groups. It has no meaning outside his own data set and programs..

                i want to make portfolio on the basis of sentiment beta, so i can come up with average returns for each portfolios. the purpose is sentiment risk premia.

                While there are many economists, econometricians, and financiers who are active contributors to this Forum, this community is international and multidisciplinary. I am sure I am not the only one here who finds the jargon in this statement impenetrable and have no idea what you mean. You will expand your options for getting a timely and helpful response by using only language that would be understandable to anybody with a college degree and a basic understanding of statistics.

                Comment


                • #9
                  Dear Clyde Schechter,

                  I am writing my master thesis and I would like to create a variable that indicates in which country-specific income tertile the individual is.

                  I used your above written command:

                  levelsof country , local(countries)
                  foreach i of local countries {
                  xtile countryspectertiles_`i' = income if country == `i', nq(3)
                  }


                  This created a list of variables for the income tertiles for each country. What I would like to do is create a variable indicating in which income tertile the individual is depending on the country he or she resides (so merging this list of variables into one variable).
                  Could you help me create this variable?

                  Thank you in advance,
                  Rita Samudra



                  Comment


                  • #10
                    Code:
                    by country, sort: egen wanted = xtile(income), nq(3)
                    Note: Official Stata does not include an -xtile()- egen function. To get it install the -egenmore- package, maintained by Nick Cox, and available from SSC.

                    Comment


                    • #11
                      Thank you Clyde!!

                      Comment

                      Working...
                      X