NAG Library Routine Document

g01arf (plot_stem_leaf)

1
Purpose

g01arf produces a stem and leaf display for a single sample of observations.

2
Specification

Fortran Interface
Subroutine g01arf ( range, prt, n, y, nstepx, nstepy, unit, plot, ldplot, lines, sorty, iwork, ifail)
Integer, Intent (In):: n, nstepx, nstepy, ldplot
Integer, Intent (Inout):: ifail
Integer, Intent (Out):: lines, iwork(n)
Real (Kind=nag_wp), Intent (In):: y(n)
Real (Kind=nag_wp), Intent (Inout):: unit
Real (Kind=nag_wp), Intent (Out):: sorty(n)
Character (1), Intent (In):: range, prt
Character (1), Intent (Inout):: plot(ldplot,nstepx)
C Header Interface
#include <nagmk26.h>
void  g01arf_ (const char *range, const char *prt, const Integer *n, const double y[], const Integer *nstepx, const Integer *nstepy, double *unit, char plot[], const Integer *ldplot, Integer *lines, double sorty[], Integer iwork[], Integer *ifail, const Charlen length_range, const Charlen length_prt, const Charlen length_plot)

3
Description

g01arf produces a stem and leaf display for a single sample of n observations. The stem and leaf display shows data values separated into the form of a ‘stem’ and a ‘leaf’. For example, a value of 473 could be represented as 47 3 where the stem is 47 and the leaf is 3. The data is scaled using a value known as the ‘leaf digit unit’. In the above example the leaf digit unit would be 1.0.
The following example illustrates a stem and leaf display.
For the 10 observations:
1.8 2.3 2.1 1.9 2.1 2.4 2.0 2.0 1.9 2.1  
the stem and leaf display is:
1  1  8
3  1  99
5  2  00
5  2  111
2  2
2  2  3
1  2  4
where the leaf digit unit is 0.1 so that 1 8 represents 1.8 (i.e., 18×0.1). The leaf digit unit distinguishes between the numbers 18.0, 1.8, 0.18, etc. which may otherwise all be represented by 1 8.
Included in the above display is an initial column specifying the cumulative count of values, up to and including that particular line, from either the top or bottom of the display, whichever is smaller. An exception to this is when the line on which the median lies is reached, in which case the actual count of values on that line is displayed, rather than a cumulative count, and this is highlighted by enclosing the count in parentheses. In this case the median is 2.05 and thus falls between the two lines at which the cumulative count has reached n/2 where n is the number of observations.
Some of the other features of the stem and leaf display are illustrated by the following two examples.
For the 30 observations:
-19.0 -3.0 -1.0 0.0 1.0 2.0 2.0 3.0 3.0 3.0 4.0 4.0 4.0 4.0 4.0 5.0 5.0 5.0 5.0 6.0 6.0 6.0 7.0 7.0 8.0 10.0 11.0 11.0 13.0 31.0  
the stem and leaf display may be:
 1   1.  9
 1   1*
 1  -0.
 3  -0*  13
15  +0*  012233344444
15  +0.  55556667788
 5   1*  011
 2   1.  3
 1   2
 1   2.
 1   3   1
In the above display all the data are plotted and the leaf digit unit is 1.0. Also in this display different leaves, that is different digits, may be plotted on a particular line. In this case we have 5 possible digits per line, that is 2 lines per stem, and these are represented as follows:
Alternatively the stem and leaf display may look like:
      LO   -19

  2   -0*  3
  3   +0T  1
  5   +0*  01
 10   +0T  22333
( 9)  +0F  444445555
 11   +0*  66677
  6   +0T  8
  5    1*  011
  2    1T  3

      HI   31
Again the leaf digit unit is 1.0 but in this display just the data between the fences, which are the hinges ±112× the inter-hinge range, are plotted. Any data points that fall outside the fences are presented separately in the display under the headings LO for those points below the lower fence and HI for those points above the upper fence.
Again in this display different leaves, that is different digits, may be plotted on a particular line. However in this case we have 2 possible digits per line, that is 5 lines per stem, and these are represented as follows
A display may also allow 10 different digits (0 to 9) per line, that is 1 line per stem, or just 1 digit per line, that is 10 lines per stem, as in the first of the three examples above.
Note that the median here is 4.5. This falls between two lines in the first display but is highlighted on the second display since it lies on a particular line.
Finally if there are positive and negative numbers on the display these are highlighted by a + or - sign where the distinction is required, that is near the zero-point.
If there are too many leaves to fit in the plot width allowed, g01arf plots as many leaves as possible and places an asterisk to the right to indicate that some leaves are not displayed. If this occurs and you wish to be able to plot all the leaves then the width of the plot may be adjusted.
Options also allow the leaf unit and the height of the display to be specified by you or calculated by g01arf. These arguments may be used to control the type of the display you wish to obtain. Fixing the unit and changing the height of the display may alter the number of lines used per stem, that is the number of different digits per line. g01arf will choose a display for the fixed unit that attempts to make as much use of the available height as possible, thus increasing the height may allow for more lines per stem whereas decreasing the height may force the display to use fewer lines per stem. Similarly you may wish to fix the height and vary the leaf digit unit used on the display. See Section 9 for further details.
The display is returned in a character array with the option of printing the display.

4
References

Erickson B H and Nosanchuk T A (1985) Understanding Data Open University Press, Milton Keynes
Tukey J W (1977) Exploratory Data Analysis Addison–Wesley
Velleman P F and Hoaglin D C (1981) Applications, Basics, and Computing of Exploratory Data Analysis Duxbury Press, Boston, MA

5
Arguments

1:     range – Character(1)Input
On entry: indicates whether you wish to scale the plot to the extremes of the data or to the fences.
range='E'
The display is a plot to the extremes, that is a plot of all the data.
range='F'
The display is a plot of the data between the fences.
Constraint: range='E' or 'F'.
2:     prt – Character(1)Input
On entry: indicates whether the stem and leaf display is to be output to an external file.
prt='N'
The display is not output to an external file.
prt='P'
The display is output to the current advisory message unit as defined by x04abf. Only the first 132 characters of each line are actually printed.
Constraint: prt='P' or 'N'.
3:     n – IntegerInput
On entry: n, the number of observations.
Constraint: n2.
4:     yn – Real (Kind=nag_wp) arrayInput
On entry: the n observations.
5:     nstepx – IntegerInput
On entry: the number of character positions to be plotted horizontally.
Constraint: nstepx35.
6:     nstepy – IntegerInput
On entry: the maximum number of character positions to be plotted vertically.
If nstepy0 a suitable value will be used by g01arf for the number of character positions to be plotted vertically. This will clearly be less than or equal to the value of ldplot.
Constraint: nstepy0 or nstepy5.
7:     unit – Real (Kind=nag_wp)Input/Output
On entry: indicates the leaf digit unit to be used.
If unit>0.0 and is not a power of ten, it will be converted to the nearest power of ten below the input value for unit.
If unit0.0, the optimum unit will be used. This is based on the range of the data to be plotted and the number of lines available for the display.
On exit: contains the actual unit used in the stem and leaf display.
8:     plotldplotnstepx – Character(1) arrayOutput
On exit: the stem and leaf display.
9:     ldplot – IntegerInput
On entry: the first dimension of the array plot as declared in the (sub)program from which g01arf is called.
Constraint: ldplotmax5,nstepy.
10:   lines – IntegerOutput
On exit: the actual number of lines needed for the display.
11:   sortyn – Real (Kind=nag_wp) arrayOutput
On exit: the observations sorted into ascending order.
12:   iworkn – Integer arrayWorkspace
13:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1 or 1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1 or 1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this argument, the recommended value is 0. When the value -1 or 1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6
Error Indicators and Warnings

If on entry ifail=0 or -1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
ifail=1
On entry, ldplot=value.
Constraint: ldplot5.
On entry, ldplot=value and nstepy=value.
Constraint: ldplotnstepy.
On entry, n=value.
Constraint: n2.
On entry, nstepx=value.
Constraint: nstepx35.
On entry, nstepy>0 and nstepy<5: nstepy=value.
ifail=2
On entry, prt=value.
Constraint: prt='P' or 'N'.
On entry, range=value.
Constraint: range='E' or 'F'.
ifail=3
Lines needed for display (value) exceed nstepy (value).
ifail=4
A value exceeds maximum allowed for an integer.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

7
Accuracy

Accuracy is limited by the number of significant figures that may be represented on the display which will depend on the data, the number of lines available and the unit used.

8
Parallelism and Performance

g01arf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9
Further Comments

g01arf uses integer representations of the data. If very large data values are being used they should be scaled before using this routine. The largest integer can be found by calling x02bbf.
If an asterisk is plotted at the end of a line to indicate that some leaves are not displayed you should increase nstepx if they wish to be able to print the rest of the leaves on that line.
Note that if you request g01arf to print the plot only the first 132 characters of each line are printed. The full plot is stored in the array plot so you do have the option of printing a plot which has more than 132 characters on a line.
When the leaf digit unit is set, the number of lines per stem is decided as follows:
Let r be the range of the data to be plotted:
Let l be the number of lines available for the plot:
Let e= r/unit+1l ,
The time taken by the routine increases with n.

10
Example

A program to produce two stem and leaf displays for a sample of 30 observations. The first illustrates a plot produced automatically by g01arf and the second shows how to print the display under your control.

10.1
Program Text

Program Text (g01arfe.f90)

10.2
Program Data

Program Data (g01arfe.d)

10.3
Program Results

Program Results (g01arfe.r)