Statalist The Stata Listserver


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

st: RE: summing across bins


From   "David Harrison" <[email protected]>
To   <[email protected]>
Subject   st: RE: summing across bins
Date   Mon, 13 Feb 2006 14:31:11 -0000

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.

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Lenna
Nepomnyaschy
Sent: 13 February 2006 13:31
To: [email protected]
Subject: st: summing across bins

Hi,

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.

Thank you very much for your help,
Lenna Nepomnyaschy

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

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

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