Announcement

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

  • weighted average

    Hi,
    I have a database of donations and I want to use it to generate a new var to indicate donors ideology. Firstly, calculate donor's ideology using the variable "dwnom1" of his recipients/candidates. Then calculate candidates' ideology using the donors index we got last step. Here is my code.
    Code:
    bysort donorid: egen donorave = sum(dwnom1*amount)
    bysort donorid: egen donortotal = sum(amount)
    gen donorindex = donorave/donortotal
    bysort candid: egen candave = sum(donorindex*amount)
    bysort candid: egen candtotal = sum(amount)
    gen candindex = candave/candtotal
    But there is a feedback loop, donor contributions to candidates will affect that candidate's scaling because that candidate is included in the estimate of the donor's ideology. I want to leave out certain candidate i when calculating donors' ideology, then use the donors' ideology to scale that candidate i's ideology. Below mathematical expression may help to understand. Is there any possible way to do that? Many thanks!

    Here is my data
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str11 donorid long amount str10 candid double dwnom1
    "29075807371"   250 "cand1297"  -.397
    "9423"         1000 "cand645"   -.225
    "9423"         1000 "cand645"   -.225
    "3734270681"    200 "cand42555"     .
    "4145347783"    750 "cand1327"  -.439
    "2675854749"   1000 "cand1269"  -.305
    "151787"       1000 "cand132"   -.328
    "151787"       1000 "cand132"   -.328
    "143206"        500 "cand364"    .704
    "143206"        500 "cand364"    .704
    "2692876123"    700 "cand1229"   .339
    "29055799837"   250 "cand192"   -.421
    "29356125709"   250 "cand192"   -.421
    "29386443969"   250 "cand192"   -.421
    "70001128"     2053 "cand662"   -.321
    "70001128"     2053 "cand662"   -.321
    "29075800235"   500 "cand1553"   .406
    "52502762825"  1000 "cand1553"   .406
    "29065810925"   500 "cand1553"   .406
    "141572"          3 "cand52256"     .
    "141572"          3 "cand52256"     .
    "223230"        100 "cand1219"  -.145
    "223230"        100 "cand1219"  -.145
    "2469756684"    500 "cand53"    -.667
    "2999006487"    333 "cand1288"   .518
    "29095842545"  1000 "cand1244"  -.169
    "54005220240"   300 "cand43935"     .
    "268748"        500 "cand469"    .031
    "268748"        500 "cand469"    .031
    "103572"       -400 "cand519"    .336
    "103572"       -400 "cand519"    .336
    "165605"       -250 "cand687"    .265
    "165605"       -250 "cand687"    .265
    "165605"       -300 "cand692"    .296
    "165605"       -300 "cand692"    .296
    "2089"         -100 "cand647"     -.1
    "2089"         -100 "cand647"     -.1
    "106971"       -500 "cand509"    .543
    "106971"       -500 "cand647"     -.1
    "106971"       -500 "cand647"     -.1
    "106971"       -500 "cand509"    .543
    "106971"      -1000 "cand157"   -.518
    "106971"      -1000 "cand157"   -.518
    "106971"       -500 "cand465"    .383
    "106971"       -500 "cand465"    .383
    "9910"         -250 "cand726"    .407
    "9910"         -250 "cand726"    .407
    "35691"        -500 "cand469"    .031
    "35691"        -500 "cand469"    .031
    "2559802768"    200 "cand40683"     .
    end

  • #2
    I don't know whether it's my browser settings or what, but your mathematical expression image file doesn't render.

    I'd consider approaching it by looping over two macro lists (candidates and donors) to do the leave-one-out summations in alternation. You'd get the macro lists using the command -levelsof-.

    (That's actually a lie. In truth, I'd just wait until Romalpa Akzo chimes in with an elegant solution in two lines of code.)

    Comment


    • #3
      That image is a .png file and I upload a new one in .jpg format. Hope it works! Thanks for your reminding, Joseph!
      Click image for larger version

Name:	mathmatical expression.jpg
Views:	2
Size:	63.9 KB
ID:	1513617
      Attached Files

      Comment


      • #4
        For excluding focal observations while calculating some statistics, you may like to use asrol, which is available on SSC. Specifically, read Section 8, Example A and Example B in there
        Code:
        ssc install asrol
        help asrol
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          Thanks Professor Shah! I have looked help asrol carefully and the option ex(excluding focal observation) is exactly what I need. But I don't know how to use asrol to calculate weighted mean. All I can think is below code and i know it is invaid syntax, there is no option -wm- in asrol.
          Code:
          bys donorid: asrol dwnom1,stat(wm amount) xf(candid) gen(xfcandid)
          wm: weighted mean, weight:amount.

          Comment


          • #6
            Currently, asrol does not support weights. My asgen program supports weights, but it does not have the xfocal option. If you can provide a simple Excel-based example of how you intend to deal with the wights while excluding some observations from the calculation of weighted mean, I can add the xfocal option to asgen
            Code:
            ssc install asgen
            help asgen
            Regards
            --------------------------------------------------
            Attaullah Shah, PhD.
            Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
            FinTechProfessor.com
            https://asdocx.com
            Check out my asdoc program, which sends outputs to MS Word.
            For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

            Comment


            • #7
              Thank you Professor Shah! Attached Xfocal Example shows a simple example with details of 2 step actions. Would it be possible to use xfocal to deal with that?
              Xfocal Example.xlsx

              Comment


              • #8
                https://www.stata.com/support/faqs/d...ng-properties/ remains relevant. The identity you should exploit is just

                sum of others = sum of all MINUS this value

                which suggests

                Code:
                bysort donorid: egen donorave = sum(dwnom1*amount)
                bysort donorid: egen donortotal = sum(amount)
                replace donorave = donorave - dwnom1 * amount
                replace donortotal = donorave - amount
                gen donorindex = donorave/donortotal
                Missings should be treated as zeros in this procedure.

                Comment


                • #9
                  Wow, that's amazing! I didn't realize that. Many thanks Nick!

                  Comment

                  Working...
                  X