Announcement

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

  • Weighted average by two categories

    Dear All,

    I have data for the monthly wage by gender and categories of commuting time. I am trying to calculate the weighted average of the monthly wage by using the number of people in each commuting category by gender. An example of the data is shown below.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(commute female mnthwg)
    1 0       500
    1 1 1883.3334
    1 0 198.33333
    1 0       600
    1 1       400
    1 0      1500
    1 1       345
    1 0 1188.5714
    1 0       364
    1 0       550
    end
    label values commute commutecate
    label def commutecate 1 "less than 16 min." ///
                              2 "from 16 to 32 min." ///
                              3 "from 33 to 60 min." ///
                              4 "from 61 to 90 min." ///
                              5 "from 91 to 720 min.", modify
    label values female sex
    label def sex 0 "male", modify
    label def sex 1 "female", modify
    I tried the following codes but it only gave me the average not the weighted average:
    Code:
    egen weightwage = wtmean(mnthwg), weight(commute)
    Many thanks,
    Maye

  • #2
    That syntax (using _gwtmean from SSC, as you are asked to explain) should give the single weighted mean for all the data.

    https://www.statalist.org/forums/for...and-collapsing gives suggestions for weighted mean calculations.

    Comment


    • #3
      Thank you
      I did the following with reference to the suggested link:

      Code:
      bysort commute female : egen denom = total(weight) 
      by commute female : egen numer = total(weight * mnthwg) 
      gen wtmean = numer/denom

      Comment


      • #4
        PS: posted this before seeing Nick's link.
        Is this what you want:
        Code:
        egen weightwage = wtmean(mnthwg), weight(commute) by(female)
        Last edited by Abdul Adam; 29 Aug 2017, 05:53.

        Comment


        • #5
          Thank you for your reply.
          Both methods yield the same result.

          Comment

          Working...
          X