Announcement

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

  • Weighted quartiles by year in multi-year data

    I have multi-year data on states. For each year, I want to compute weighted quartiles for certain variables, so that I can group the states. The quartile groups will change each year. My instinct was to try
    Code:
    bysort year: xtile newvar=var [fweight=weight], nq(4)
    , but that didn't work. Any thoughts?

  • #2
    ...but that didn't work.
    is something you should never say without explanation. There are so many ways that something can "not work." Did Stata crash? Did it hang? Did you get an error message? Did it run but produce results that were not what you expected? (If so, what did you expect and how were the results you got different.)

    And actually in addition to explaining, it is usually a good idea to also show, by copy/pasting from the Results window or your log file and without editing, the exact command you ran (which you did) and what Stata gave you in response.

    That said, in this case there is an obvious problem. The -egen- command does not in general support weights. Some particular -egen- functions do. -xtile()-, which, by the way, is not an official -egen- command but is part of the -egenmore- suite, does. But because -egen- itself does not allow weights, weights must be specified differently from standard syntax:
    [/code]
    bysort year: xtile newvar = var, nq(4) weights(weight)
    [code]

    Comment


    • #3
      Apologies. It says: "xtile may not be combined with by", as follows:

      Code:
      . bysort year month: xtile denq=denials_l [fweight=covempma], nq(4)
      xtile may not be combined with by
      r(190);
      I'm not sure I understand your reply. When I input the code you suggest, it says that the variable I'm trying to compute quartiles for is an unknown function. Apologies if I'm misunderstanding:

      Code:
      .  bysort year: egen xtile denq_a = denials_l, nq(4)
      unknown egen function denials_l,()
      r(133);

      Comment


      • #4
        Sorry, my code is wrong. It should be:
        Code:
        bysort year: egen  denq_a = xtile(denials_l), nq(4) weights(weight)

        Comment


        • #5
          Thank you; this does exactly what I need.

          Comment

          Working...
          X