Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: egen, weights


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   Re: st: egen, weights
Date   Thu, 12 Sep 2002 20:08:41 +0100

Daniel Millimet

> Is there a way to use -egen, pctile- with weights? 
> Note, the weights are not integers, so I cannot simply 
> expand the data set and then use the egen command. 
 
I can't see a way of doing this with -egen, pctile()-,  
which puts a particular percentile in a variable. 

The most obvious reason for wanting to do this 
is that you have groups of a categorical variable 
and you want each group to have its own percentile. 

Here is one way to do it: 

. u auto 

Yes, it's the auto data. 

. gen pctile = . 

Initialise a variable. 

. levels rep78 , local(levels) 

We don't need -levels- (SSC) for this example, 
but it is helpful for more complicated categorical 
variables. 

. foreach l of local levels { 
.	_pctile mpg [w=gear_ratio] if rep78 == `l' 
. 	qui replace pctile = r(r1) if rep78 == `l' 
. } 

For each group of that categorical variable 
	calculate the percentile desired 
	plug it in where appropriate 

. tabdisp rep78, c(pctile) 

See what we just did. 

Nick 
[email protected] 
*
*   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