Announcement

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

  • Weighted mean by group by year.

    Hello.

    I have looked around on the FAQ and this forum, and haven't been able to find an answer.

    I need to calculate two weighted means for each country each year in my time series (1990-2019) This needs to be the average of my variable "per402v2" weighted by each party's % voteshare (pervote). Furthermore it needs to be qualified by wether or not a party scores 1 or 0 on either my "opp" variable and my "gov" variable.

    To summarise: I need the average of my variable "per402v2" weighted each party's % voteshare, for each year, for each country if the party scores either 1 or 0 on "Opp"

    I hope you can help me, thanks in advance.


    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double country2 float year str183 partyname double per402v2 float(opp gov) double pervote
    42 2006 "Alliance for the Future of Austria" 2.509 0 1 4.11
    42 2007 "Alliance for the Future of Austria" 1.6905 1 0 4.11
    42 2008 "Alliance for the Future of Austria" 0.872 1 0 10.7
    42 2006 "Austrian People’s Party" 1.505 0 1 34.33
    42 2007 "Austrian People’s Party" 1.817 1 0 34.33
    42 2008 "Austrian People’s Party" 2.131 0 1 25.98
    42 2006 "Austrian Social Democratic Party" 1.248 1 0 35.34
    42 2007 "Austrian Social Democratic Party" 1.704 0 1 35.34
    42 2008 "Austrian Social Democratic Party" 2.161 0 1 29.26



  • #2
    does this give the desired result,
    Code:
    ssc install _gwtmean
    bys country2 year opp: egen wtmean = wtmean(per402v2), weight(pervote)
    ?

    Comment


    • #3
      That worked perfectly, thank you very much

      Comment


      • #4
        egen doesn't support weights. I don't know the precise grounds for the original decision, but it would have added a layer of complexity that might well have caused many more problems than it ever solved.

        Weighted means are in my experience by far the most common case that people miss. #2 rightly and helpfully identifies a community-contributed work-around in which weights are specified via an option.

        This is to just to point an alternative solution, which would be something like


        Code:
        bys country2 year opp: egen double numer = total(per402v2 * pervote)
        by country2 year opp: egen double denom = total(pervote)
        gen double wanted = numer / denom
        The doubles may be overkill, but should do no harm.

        Comment

        Working...
        X