help myrank

Title

myrank -- Generate axis variable based on ranks

Syntax

myrank newvar = varname
[if] [in]
[
,
descending
over(varname)
gap(#)
varlabel(string)
]

Description

myrank generates a variable for a graph axis that is based on
(unique or distinct) ranks of values of a numeric variable, with various
possible twists. First, if a grouping variable is specified, ranks are
determined separately within groups of that variable but remain distinct
overall. Second, given the first twist, gaps of integer length may be
added between groups. Third, ranking by default is ascending, lowest to
highest, but optionally may be descending, highest to lowest.

Remarks

See Cox (2024b) for the context here and examples of a simple graphical
method, showing side-by-side quantile plots using a special axis variable.
This command is at root only a simple helper command to carry out
preliminary calculation of that axis variable, allowing such graphs to follow more easily.

The command name myrank is to be parsed "my rank variable". The
element "rank" is key. Ranks are always unique or distinct, echoing a
convention used by quantile plots.

The first element "my" is at best harmless whimsy, but it arises because
mentions of a command named just rank would be harder to spot
among other uses of the term.

See Cox (1999, 2005, 2007, 2024a) and
search qplot, sj for more on quantile plots in Stata. The literal
values of the rank variable created by myrank have simple meaning only for the first group
so created. However, the purpose is not to use those ranks literally,
but only to create a variable to use as one axis.

Local macros with names mid1, mid2, and so forth are inserted
in the calling program's space containing the mid-ranks (means of the
new variable) in the first group and any later groups of the new variable.
If over() is not specified, or if it is specified and contains only
one distinct value, then there will be just one group.

Local macros with names gap1, gap2, and so forth are inserted
in the calling program's space containing the positions of the gaps
after groups 1, 2, and so forth of the new variable.

These local macros are potentially of use for specifying axis labels and
vertical separator lines in a later graph command. As is true of all local
macros, the macros will only be visible in the calling program's name space
and will be overwritten easily, as by a later use of this command. Hence
copy whatever is of value to somewhere safe.

The myaxis command (Cox 2021) is for a related but distinct problem.

Options

over(varname) specifies a grouping variable, typically but not
necessarily a numeric or string variable with categorical flavour.

gap(#) specifies a gap of integer length to be added to ranks
to amplify visual separation between groups if over() is also
specified. The gap is fixed at 0 otherwise.

descending specifies ranking with highest value first. The
default rank order is ascending with lowest value first.

varlabel(string) specifies a variable label for the new variable.

Examples

. sysuse auto

The ranks of mpg will just run from 1 to 74 for 74 values if no
over() option is specified. The plot created is a variation on
that produced by quantile or qplot.

. myrank rank1=mpg

. scatter mpg rank1, xla(none) xtitle(Values in order) name(G1, replace)

With over(foreign), two groups of values are separated, for
foreign==0 and for foreign==1. The ranks of mpg
are created as 1 to 52 for the first group and as 55 to 76 for the
second group, given the extra gap of 2. Typically those ranks will
(indeed should) not appear as axis labels, which are better reserved
for explaining which group is which. Note here the use of local macros
as explained above in Remarks.

. myrank rank2=mpg, over(foreign) gap(2)

. scatter mpg rank2, xla(`mid1' "Domestic" `mid2' "Foreign", tlc(none))

xli(`gap1', lp(solid)) xtitle("") name(G2, replace)

With over(rep78), five groups of values are separated, from
rep78==1 to rep78==5. The ranks of mpg are created
as 1 to 2 for the first group, 5 to 12 for the second group, and so on,
up to 67 to 77 for the last group, given the extra gaps of 2. Values
that are missing on rep78 are ignored. As above, axis labels
should be used to show group information.

. myrank rank3=mpg, over(rep78) gap(2)

. scatter mpg rank3, xla(`mid1' "1" `mid2' "2" `mid3' "3" `mid4' "4" `mid5' "5", tlength(0))

xli(`gap1' `gap2' `gap3' `gap4', lp(solid)) xtitle(Repair record 1978) name(G3, replace)

As these graphs are just applications of scatter, other
twoway graphs can be added. Here means of each group
are shown by horizontal lines.

. egen mean = mean(mpg), by(rep78)

. separate mean, by(rep78) veryshortlabel

. scatter mpg rank3, xla(`mid1' "1" `mid2' "2" `mid3' "3" `mid4' "4" `mid5' "5", tlength(0))

xli(`gap1' `gap2' `gap3' `gap4', lp(solid)) xtitle(Repair record 1978)

|| line mean? rank3, sort lc(stc2 ..) legend(off) note(horizontal lines show means)

ytitle("`: var label mpg'") name(G4, replace)

For the record, see summary statistics for the new variables.

. tabstat rank1, s(min max)

. tabstat rank2, s(min max) by(foreign)

. tabstat rank3, s(min max) by(rep78)

For comparison, how far can we get with say graph dot?

. bysort rep78 (mpg) : gen dotrank = _n

. graph dot (asis) mpg, over(dotrank, label(nolabels)) over(rep78)

linetype(line) lines(lc(gs12) lw(vvthin)) vertical nofill exclude0

b2title(Repair record 1978) name(G5, replace)

Author

Nicholas J. Cox

Department of Geography

Durham University

Durham, UK

n.j.cox@durham.ac.uk

References

Cox, N.J. 1999.
Quantile plots, generalized.
Stata Technical Bulletin 51: 16-18.
https://www.stata.com/products/stb/journals/stb51.pdf

Cox, N.J. 2005.
Speaking Stata: The protean quantile plot.
Stata Journal 5: 442-460.
https://journals.sagepub.com/doi/pdf/10.1177/1536867X0500500312

Cox, N.J. 2007.
Stata tip 47: Quantile-quantile plots without programming
Stata Journal 7:275-279.
https://journals.sagepub.com/doi/pdf/10.1177/1536867X0700700213

Cox, N.J. 2021.
Speaking Stata: Ordering or ranking groups of observations.
Stata Journal 21: 818-837.
https://journals.sagepub.com/doi/pdf/10.1177/1536867X211045582

Cox, N.J. 2024a.
Speaking Stata: Quantile–quantile plots, generalized.
Stata Journal 24: 514-534.
https://journals.sagepub.com/doi/pdf/10.1177/1536867X241276114

Cox, N.J. 2024b.
Speaking Stata: Getting by without the by() option: Some graphics for unequal groups.
Stata Journal 24: 766-776.
https://journals.sagepub.com/doi/pdf/10.1177/1536867X241297949

Also see

Help: [G-2] graph dot, [R] diagnostic plots,
myaxis (if installed), qplot (if installed)