Statalist The Stata Listserver


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

st: RE: RE: summing across bins


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: summing across bins
Date   Mon, 13 Feb 2006 15:01:07 -0000

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.

*
*   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