Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: collapse with multiple sets of weights


From   "Dimitriy V. Masterov" <[email protected]>
To   [email protected]
Subject   Re: st: collapse with multiple sets of weights
Date   Thu, 12 Apr 2012 16:58:05 -0400

Scott,

That's very clever and easy. Thanks.

DVM

On Thu, Apr 12, 2012 at 11:55 AM, Scott Merryman
<[email protected]> wrote:
> Normalize the weights to sum to n and rescale population so the
> weighting cancels out:
>
> clear
> input pop avgwage
> 10      20
> 20      10
> 40      30
> end
>
> egen double w=total(pop)
> replace  w=3*pop/w
> gen double pop2 =pop/w^2
> collapse (sum) pop2  (mean) avgwage [w=w]
> l
>
>
> Scott
>
>
> On Thu, Apr 12, 2012 at 10:05 AM, Dimitriy V. Masterov
> <[email protected]> wrote:
>> I am trying to aggregate some spatial data. Here's a simplified
>> version of my problem. I have two variables, average wage and
>> population, in three areas. I would like to sum the population, but
>> calculate a weighted mean of the average wage. Is there a way to do
>> this in one step using collapse?
>>
>> For example, the following code gets the mean right, but the population "wrong":
>> set obs 3;
>> input
>> pop avgwage;
>> 10      20;
>> 20      10;
>> 40      30;
>> egen double w=total(pop);
>> replace w=pop/w;
>> collapse (sum) pop (mean) avgwage [w=w];
>>
>> I would like to see 70, not 90.
>>
>> Is there a collapse-like command where I apply the weight only to the
>> mean calculation? I suppose I could do this in two collapse steps, but
>> that seems clunky.
>>
>> DVM
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index