On Fri, 17 Jan 2003 00:16:17 -0500 (EST) Nirmala Ravishankar
<[email protected]> wrote:
> I am a first-time Stata user, and have a relatively basic question. Is
> there some way to calculate the weighted mean and median of a variable
> without using the "collapse" command? As part of a program, I need to
> calculate the weighted mean and median to perform other operations on the
> dataset, but using "collapse" clears the original variables from the
> workspace. I tried "egen" but that doesn't allow for weighted means.
>
> Here are the specifics: I have household income data for US 1994, from
> which I want to calculate the median income. Then I want to disaggregate
> households by state and calculate the regional medians. Finally, I want to
> calculate the ratio of the regional medians to the national median. Using
> "collapse" to calculate the national median removes the original
> observations from the working space.
Suppose that in your dataset there is a row for each household, and
columns hold variables such as HH_id, weight, HH_income, state_id
su HH_income [aw = weight], de
local nat_med = r(p50)
lab var nat_med "National median income"
local nat_mean = r(mean)
lab var nat_med "National mean income"
/* ensure state id consecutive integers */
egen stategp = group(state)
ge s_med = .
ge s_mean = .
ge rel_s_med = .
forvalues s = 1/50 { /* assuming 50 states */
su HH_income [aw = weight] if stategp == `s', de
replace s_med = r(p50)
replace s_mean = r(mean)
replace rel_s_med = s_med/`nat_med'
{
lab var nat_med "State median income"
lab var nat_mean "State mean income"
lab var rel_s_med "State median/National median"
summ
Stephen
----------------------
Professor Stephen P. Jenkins <[email protected]>
Institute for Social and Economic Research (ISER)
University of Essex, Colchester, CO4 3SQ, UK
Tel: +44 (0)1206 873374. Fax: +44 (0)1206 873151.
http://www.iser.essex.ac.uk
*
* 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/