Thank you, Austin, the code is very helpful.
One more question (though trivial):
When calculating the rank, you wrote
> by mi: g r=(srank-srank[1]/2)/srank[_N] if mi==0
I noticed that you divided the first srank value by 2 so that the
percentile for the first obs (with lowest bmi) won't be zero. I'm
wondering if this is a standard formula, and if not, why can't we just
write
by mi: g r=srank/srank[_N] if mi==0
Thanks again,
Yang
On Jan 10, 2008 6:57 PM, Austin Nichols <[email protected]> wrote:
> Yang--
> If you have an arbitrary value, say x=30, and want to calculate what
> quantile (rank or percentile) it would occupy in your data X, one easy
> way is to calculate rank for X, create an extra fake obs with the
> value 30, and interpolate rank with respect to X. Here is a worked
> example:
>
> * set up example:
> webuse nhanes2, clear
> g bmi=wei/hei^2*10^4
> svyset
> g mi=mi(bmi,finalw)
> bys mi (bmi): g srank=sum(finalwgt) if mi==0
> by mi: g r=(srank-srank[1]/2)/srank[_N] if mi==0
>
> * now see what quantile 30 corresponds to:
> set obs `=_N+1'
> replace bmi=30 in l
> ipolate r bmi, gen(est)
> li est r bmi in l
> drop est
> drop in l
>
> * check answer:
> _pctile bmi [pw=fin], nq(100)
> di r(r85)
> di r(r86)
>
> Note that I calculate r (quantile rank) as a proportion between 0 and
> 1 using weights; if you have unweighted data, you can generate a
> weight variable=1 everywhere or equivalently use
>
> bys mi other (bmi): g r=(_n-1/2)/_N if mi==0
>
> in place of
>
> bys mi other (bmi): g srank=sum(finalwgt) if mi==0
> by mi other: g r=(srank-srank[1]/2)/srank[_N] if mi==0
>
>
> On Jan 10, 2008 5:23 AM, Ben Jann <[email protected]> wrote:
> > Maybe also the -relrank- command is of help (or the -mm_relrank()-
> > Mata function from the -moremata- package). See
>
> > On Jan 10, 2008 5:03 AM, Alan Neustadtl <[email protected]> wrote:
> > > help pctile might be useful as well as reading the FAQ title "How can
> > > I calculate percentile ranks?
> > > How can I calculate plotting positions?" written by Nick Cox. The
> > > URL is http://www.stata.com/support/faqs/stat/pcrank.html
>
> > > On Jan 9, 2008 9:09 PM, Yang Lu <[email protected]> wrote:
> > > > But what if I have some arbituary values and want STATA to return
> > > > their percentile? I know I can write a do file to make it automatic
> > > > but would be surprised if such a syntax did not already exist (though
> > > > I didn't find it. I use stata 9.) I'm sorry if someone asked this
> > > > before; I'm new to the list but I did check before I wrote this email.
> > > > Thanks a lot!
>
> *
> * 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/
>
*
* 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/