Announcement

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

  • ASGEN command for weighted average

    Hello everyone,


    I have to compute the weighted average of democratic scores in the destination countries, weighted it with my shares of emigrants in a certain province in a year.
    This is the Stata command i am using:

    bysort provincia year: asgen weighted_average= democracy_polity if democracy_polity!=. & share_emigration!=., weight(share_emigration)

    However, the new variable does not make much sense since the values do not sum up to 1. How can I create the right variable?

    Thank you,
    Best,
    Margherita


  • #2
    I don't know anything about the detail of asgen (from SSC, as you are asked to explain). But weighted averages are if I understand the idea just

    total of weights X values / total of weights.

    Why anything there should add to 1 is hard to see. Naturally we can always recast that as

    total of fractional weights X values

    if each weight is divided by their total.

    Here is a silly example of mpg weighted prices with the auto data.

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . egen double numer = total(mpg * price) , by(rep78)
    
    . egen double denom = total(mpg), by(rep78)
    
    . gen double wanted = numer/denom
    
    . tabdisp rep78, c(wanted)
    
    ----------------------
    Repair    |
    record    |
    1978      |     wanted
    ----------+-----------
            1 |  4511.7143
            2 |  5559.9608
            3 |  6003.6244
            4 |  5986.6795
            5 |  5394.4319
            . |  5924.3738
    ----------------------
    
    . * check one case 
    
    . su price [aw=mpg] if rep78 == 5
    
        Variable |     Obs      Weight        Mean   Std. dev.       Min        Max
    -------------+-----------------------------------------------------------------
           price |      11         301    5394.432   2223.194       3748      11995
    If that doesn't help with your problem, I think we need to see a data example together with an explanation of what it is that does not sum to 1 and why that is wrong.

    Comment

    Working...
    X