-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Nick Cox
Sent: 13 February 2006 15:01
To: [email protected]
Subject: st: RE: RE: summing across bins
In addition to other comments, see
FAQ . . Calculating variables containing weighted group
summary statistics
. . . . . . . . . . . . . . . . . . . . . N. J. Cox
and S. P. Jenkins
1/03 I want to calculate a variable containing weighted
group summary statistics, but I do not want to
collapse the data and egen does not support weights.
How can I do this?
http://www.stata.com/support/faqs/data/weighted.html
Nick
[email protected]
David Harrison
It is fairly simple to write a new subcommand for -egen- to do a
weighted mean...
*! DAH 1.0.0 13feb2006
program _gwmean
version 8
gettoken type 0 : 0
gettoken vn 0 : 0
gettoken eqs 0 : 0
syntax varname(numeric) [if] [in] , Wvar(varname) [
BY(varlist) ]
quietly {
marksample touse
bysort `touse' `by': gen `type' `vn' =
sum(`wvar'*`varlist')/sum(`wvar'*!missing(`varlist')) if `touse'==1
by `touse' `by': replace `vn' = `vn'[_N]
}
end
If you save the above as _gwmean.ado (in your ado path),
then you will
have access to a new egen subcommand -wmean()-. The
compulsory option
-wvar()- specifies the variable of weights.
Lenna Nepomnyaschy
I just downloaded the ado file binsm, which allows you to graph mean
values (or other values) of yvar on bins of xvar. But, I
need to use
weights. Does anyone have another way to do this that allow weights?
the binsm command does not.
Alternatively, does anyone know how to create a variable
with weighted
means across various groups. the command
egen newvar=mean(oldvar), by(group)
does not allow weights.