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

RE: st: RE: rank error?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: rank error?
Date   Wed, 28 May 2003 19:49:11 +0100

Jeph Herrin

> Nick Cox wrote:
> >>
> >>Not clear to me that -group(X)- assigns values in
> >>order of increasing  X (even if, as E Berkhout suggests
> >>in a separate posting, I sort -sort X- first).
> >>
> >>
> ...
> >
> > A look at the code for each shows that both
> > produce sorted values and that's the intent
> > in each case.
> >
> > -levels- is not appropriate directly for
> > problems in which categories are defined by
> > numbers with fractional parts. Otherwise
> > I can't comment further on what is not clear
> > to you without knowing what your doubts are.
> >
>
> Well, when I look at the documentation for these two,
> neither mentions anything about returning sorted values.

Not so.

-egen-
======

See [R] egen p.325:

"The order of the groups is that of the sort order of varlist."
An example follows.

-levels-
========

See help for -levels- (my emphasis):

-levels- displays a sorted list of the distinct values of
                    ^^^^^^
varname, which should be a variable taking on integer or
string values, or in other words, a categorical variable,
very broadly so defined.

> That raises some doubt. And when I look at the code for
> -egen-'s -group- I see
>
> 		sort `touse' `varlist'
> 		quietly by `touse' `varlist': /*
> 			*/ gen `type' `g'=1 if _n==1 & `touse'
> 		replace `g'=sum(`g')
>
> - it's not clear to me that I don't need the last line to have
> one inserted before it
>                  sort `varlist'
> 		replace `g'=sum(`g')
>
> to get the ordering (of -g- on `varlist') you say is
> clearly intended.
> Is the preceeding -by : gen- guaranteed not to unsort the
> -sort-?

Exactly. The -sort- gets things into the right order
for calculation. Nothing in the following code you quote
undoes the -sort-.

However, -egen- as a whole is -sortpreserve-, i.e. after -egen-
has finished it restores the sort order when it started.
If you want the data -sort-ed, then you must do that explicitly.

This is part of the same Stata principle: only a command which
is designed to -sort- the data will change the sort order of your
data.
(-sort- is the obvious example, but -tsset- is another.)

> Not the sort of thing I usually count on. So there's another
> reason for doubt.
>
> Sure, I could add the line and restore my confidence, but I
> don't like fiddling with official Stata code. Or, I could test
 out -egen-'s -group- to see if it has the behaviour I want, but
> obviously it was much easier to code my own ranking.

It seems to me that all these doubts are answerable by reference
to the documentation or by understanding what the code, which is
open, actually does.

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