[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: RE: Why can't I egen means with pweights?
> -----Original Message-----
> From: Laura Schechter [mailto:schechte@are.Berkeley.EDU]
> Sent: Wednesday, April 23, 2003 12:50 PM
> To: email@example.com
> Subject: st: Why can't I egen means with pweights?
> I have been trying to program a specific bootstrap in
> stata, and need
> to use the weighted means of certain variables over time.
> it seems to me that the egen command doesn't work with
> weights. For now, I
> have just been using the series of commands
> svymean varname [pweight=x], by(month)
> matrix e=e(est)
> generate ET=el(e,1,1) if month==1
> replace ET=el(e,1,2) if month==2
> etc. etc. etc.
> But this is really slow, and it would be so convenient if the
> egen command
> would work with weights so I could just say something like
> egen ET=mean(varname) [pweight=x], by(month)
> Does anyone know a way of doing this??
> Thank you so so much!
> Laura (Schechter) (from UC Berkeley)
I don't think there is a preprogrammed thing for this (although Nick Cox
may prove me wrong), but you can do this yourself fairly easily:
. sort month
. by month: gen ET = sum(varname*wgt) / sum(wgt)
. by month: replace ET=ET[_N]
Remembering that the weighted mean is simply the sum of the (variable
times the weight) divided by the sum of the weights. The second line
creates the "running" mean within month, the third line replaces the
weighted mean for all observations with the final one for each month,
which is the overall weightd mean for the month.
* For searches and help try: