Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: calculating means by group, with weights


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: calculating means by group, with weights
Date   Wed, 25 Jun 2008 17:45:38 +0100 (BST)

--- Gisella Young <[email protected]> wrote:
> I am trying to calculate the mean of a variable ("income") across
> groups ("bracket"), but using the relevant survey weights (variable
> "weight"). I tried saying:
> 
> bys bracket: egen float meanincome = mean(income) [fweight=weight]
> 
> but this is not possible since the egen command does not allow for
> weighting. This is a problem for me as if I do not use the weights
> the means will be wrong. 

Since you are using fweiths there are at least 2 strategies you could
follow. The first is that fweights tell you how many observations a
record represents. You can turn your data into unweighted data by 
-expand-ing it, this way each record stands for 1 observation, and
there is no need to weight. See -help expand- and the example below. 

The other option is to use -collapse- to make a new dataset with
weighted means, and -merge- that back into your original data. See 
-help collapse- and -help merge-. As can be seen in the example below,
the two methods yield exactly the same result (as it should).

*------------- begin example -----------------
sysuse auto, clear
tempfile a b
sort rep78
save `a'

collapse (mean) meanmpg=mpg [fw=tru], by(rep78)
sort rep78
save `b'

use `a', clear
merge rep78 using `b'

expand tru
bys rep78 : egen meanmpg2 = mean(mpg)

tab meanmpg meanmpg2
*-------------- end example -------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      __________________________________________________________
Sent from Yahoo! Mail.
A Smarter Email http://uk.docs.yahoo.com/nowyoucan.html
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index