help for qqplotg

Quantile-quantile plots, generalized

qqplotg
varname1
varname2
[if] [in]
,
[
a(str)
flip
transform(specification)
{
dvm
|
dvp
}
generate(stub)
by(byvar)
missing
lpolyopts(options)
rlopts(options)
graph_options]

qqplotg
varname
[if] [in]
,
over(groupvar)
[
a(str)
flip
transform(specification)
{
dvm
|
dvp
}
generate(stub)
by(byvar)
missing
lpolyopts(options)
rlopts(options)
graph_options]

Description

qqplotg plots the quantiles of one distribution against the
quantiles of another distribution. Here quantiles means ordered values.
It is a generalization of official command qqplot. Names for
this plot include quantile-quantile plot and q-q or Q-Q plot.

The two distributions may be of unequal size: if so, corresponding
quantiles are calculated by interpolation.

There are two main syntaxes. In the first, emulating qqplot, the
two distributions are given by the values of two variables,
varname1 and varname2.

In the second, the distributions are given by the values of varname
for two distinct groups of groupvar named in the compulsory option
over(). The help for qqplot explains how to set up such a
plot, but a one-line command may be convenient.

By default a reference line of equality is shown to aid in identifying
any systematic or random differences between the two distributions.

Optionally, the distributions may be plotted as differences between
corresponding quantiles versus their means; or as differences between
corresponding quantiles versus fraction of the data (a.k.a. cumulative
probability or plotting position). In each case, a smooth will be added
using twoway lpoly of the difference over its support.

Transformations on the fly are supported. It is suggested as essential
practice to supply an informative note or title (unless an informative
text caption is given otherwise); and as good practice to use axis
labels on the original scale. See nicelabels and mylabels
(Cox 2022) for support.

Options

over() is a required option whenever you need to specify
a group variable that takes on precisely two distinct numeric or string
values.

group() is allowed as a synonym.

a() specifies a within the plotting position recipe
(i - a) / (n + 1 - 2a) for distinct or unique ranks
i running over the integers from 1 to sample size n. The
default is 0.5, yielding (i - 0.5) / n. Alternatives should
specify a number such as a(0) or a numeric expression such as
a(1/3). For more detail, see Cox (2014).

flip swaps axes as compared with the default. This can be
especially helpful when a first pass shows that two groups would be
better plotted the other way but you have no desire to recode
groupvar.

transform() specifies a transformation to apply to what
is plotted on both axes. There are two syntaxes. 1. A bare function name
such as ln or sqrt will be applied directly. Do not supply
parentheses (). 2. An expression mentioning @ will be
applied with @ replaced on the fly with the appropriate variable
name. Hence @^(1/3) specifies cube roots of zero or positive
values and 1/@ specifies reciprocals. If dvm or dvp
is also specified, then transforms are calculated first.

A warning will be displayed if the transform creates missing
values. For example, taking logarithms of zero or negative values would
do that.

dvm plots differences between corresponding quantiles
versus their means as an alternative to plotting quantile versus
quantile. The reference becomes the horizontal line defining difference
zero.

diffvsmean is allowed as a synonym.

dvp plots differences between corresponding quantiles
versus their plotting positions as an alternative to plotting quantile
versus quantile. The reference becomes the horizontal line defining
difference zero.

dvm and dvp may not be specified together.

generate(stub) generates the quantiles as two
new variables, variously stub1 and stub2; OR
stubd and stubm if dvm is also specified; OR
stubd and stubp if dvp is also specified.

by(byvar, byopts) is supported to produce separate plots
for the distinct values of a variable byvar. By default
byopts includes legend(off) note(""). Missing values of
byvar will be ignored unless the further option missing is
specified.

lpolyopts() are options of twoway lpoly that tune
the smooth that appears with options dvm or dvp. Note that
lpolyopts(nodraw) suppresses display of such graphs.

rlopts() may be used to tune the rendering of reference lines.

graph_options are other options allowed with
scatter. Specifically aspect(1) may be a good idea with
quantile-quantile plots.

Remarks

Quantile plots have a long history, especially but not only in the form
of (1) plotting quantiles against rank order or cumulative probability
(equivalent to plotting versus the quantiles of a uniform distribution)
(2) plotting quantiles against equivalent quantiles of a normal or
Gaussian distribution (also known (e.g.) as a normal probability plot,
normal scores plot, normal plot, probit plot, or fractile diagram). The terminology
quantiles appears to have been introduced in the late 1930s, so
names may vary. Modern history starts with an outstanding paper by Wilk
and Gnanadesikan (1968). Chambers, Cleveland, Kleiner, and Tukey (1983)
and Cleveland (1993, 1994) remain authoritative and lucid. For
Stata-related discussions, see for example Cox (2005, 2007). The help
for qplot includes much more discussion.

Plots such as those from the dvm and dvp options are
known as delta plots in psychology (De Jong et al. 1994;
Speckman et al. 2008.)

It may often be sensible and sufficient to plot selected quantiles,
especially if a dataset is very large. That idea is not supported here,
but see for example Cox (2016).

qqplotg does not explicitly support comparison with expected
quantiles from some reference distribution, but the examples include
calculations for a normal quantile plot and associated plots. The
procedure boils down to calculating plotting positions, possibly
obtaining parameter estimates, and pushing plotting positions through
code for a quantile function. For more, see Cox (2007, 2014).

Examples

. sysuse auto, clear

. nicelabels mpg, local(la) tight

. qqplotg mpg, over(foreign) flip xla(`la') yla(`la') subtitle(raw scale) name(QQG1, replace)

. mylabels `la', myscale(@^(1/3)) local(la2)

. qqplotg mpg, over(foreign) flip transform(@^(1/3)) xla(`la2') yla(`la2') subtitle(cube root scale) name(QQG2, replace)

. mylabels `la', myscale(ln(@)) local(la3)

. qqplotg mpg, over(foreign) flip transform(ln) xla(`la3') yla(`la3') subtitle(log scale) name(QQG3, replace)

. mylabels `la', myscale(1/@) local(la4)

. qqplotg mpg, over(foreign) flip transform(1/@) xla(`la4') yla(`la4') ysc(reverse) xsc(reverse) subtitle(reciprocal scale) name(QQG4, replace)

. graph combine QQG1 QQG2 QQG3 QQG4, name(QQG5, replace)

. * using egen is over the top here, but extends easily to groups

. egen rank = rank(mpg), unique

. egen n = count(mpg)

. su mpg

. gen normal = r(mean) + r(sd) * invnormal((rank - 0.5)/n)

. label var normal "Expected normal quantiles"

. qqplotg mpg normal, name(QQG6, replace)

. qqplotg mpg normal, dvp lpolyopts(kernel(biweight) bw(0.1)) name(QQG7, replace)

. use ozone, clear

. qqplotg stamford yonkers, xla(0(50)150) yla(0(50)250) name(QQG8, replace)

. mylabels 10 20 50 100 200, myscale(ln(@)) local(la)

. qqplotg stamford yonkers, xla(`la') yla(`la') transform(ln) subtitle(log scale) name(QQG9, replace)

. qqplotg stamford yonkers, by(month, subtitle(log scale)) transform(ln) xla(`la') yla(`la') name(QQG10, replace)

Author

Nicholas J. Cox, Durham University

n.j.cox@durham.ac.uk

Also see

Help for

qqplot

qplot (Stata Journal) (if installed)

distplot (Stata Journal) (if installed)

nicelabels (Stata Journal) (if installed)

mylabels (Stata Journal) (if installed)

stripplot (SSC) (if installed)

References

Chambers, J. M., W. S. Cleveland, B. Kleiner, and P. A. Tukey. 1983.
Graphical Methods for Data Analysis.
Belmont, CA: Wadsworth.

Cleveland, W. S. 1993.
Visualizing Data.
Summit, NJ: Hobart Press.

Cleveland, W. S. 1994.
The Elements of Graphing Data.
Summit, NJ: Hobart Press.

Cox, N. J.
2005. Speaking Stata: The protean quantile plot. Stata Journal 5:
442-460.

Cox, N. J.
2007. Stata tip 47: Quantile-quantile plots without programming.
Stata Journal 7: 275-279.

Cox, N. J.
2014. How can I calculate percentile ranks?
How can I calculate plotting positions?
http://www.stata.com/support/faqs/statistics/percentile-ranks-and-plotting-positions/

Cox, N. J.
2016. Speaking Stata: Letter values as selected quantiles.
Stata Journal 16: 1058-1071.

Cox, N. J.
2022. Speaking Stata: Automating axis labels: Nice numbers and transformed scales.
Stata Journal 22: 975-995.

De Jong, R., C. C. Liang and E. Lauber. 1994.
Conditional and unconditional automaticity: A dual-process model of
effects of spatial stimulus-response concordance.
Journal of Experimental Psychology: Human Perception and Performance 20: 731-750.

Speckman, P. L., J. N. Rouder, R. D. Morey and M.S. Pratte.
2008. Delta plots and coherent distribution ordering.
The American Statistician 62: 262-266.
doi: 10.1198/000313008X333493

Wilk, M. B. and R. Gnanadesikan. 1968.
Probability plotting methods for the analysis of data. Biometrika 55: 1-17.
https://doi.org/10.2307/2334448