Stata 15 help for twoway mspline

[G-2] graph twoway mspline -- Twoway median-spline plots


twoway mspline yvar xvar [if] [in] [, options]

options Description ------------------------------------------------------------------------- bands(#) number of cross-median knots n(#) number of points between knots

cline_options change look of the line

axis_choice_options associate plot with alternative axis

twoway_options titles, legends, axes, added lines and text, by, regions, name, aspect ratio, etc. ------------------------------------------------------------------------- All options are rightmost; see repeated options.


Graphics > Twoway graph (scatter, line, etc.)


twoway mspline calculates cross medians and then uses the cross medians as knots to fit a cubic spline. The resulting spline is graphed as a line plot.


bands(#) specifies the number of bands for which cross medians should be calculated. The default is max{min(b1,b2),b3}, where b1 is round{10*log10(N)}, b2 is round{sqrt(N)}, b3 is min(2,N), and N is the number of observations.

The x axis is divided into # equal-width intervals and then the median of y and the median of x are calculated in each interval. It is these cross medians to which a cubic spline is then fit.

n(#) specifies the number of points between the knots for which the cubic spline should be evaluated. n(10) is the default. n() does not affect the result that is calculated, but it does affect how smooth the result appears.

cline_options specify how the median-spline line is rendered and its appearance; see [G-3] cline_options.

axis_choice_options associate the plot with a particular y or x axis on the graph; see [G-3] axis_choice_options.

twoway_options are a set of common options supported by all twoway graphs. These options allow you to title graphs, name graphs, control axes and legends, add lines and text, set aspect ratios, create graphs over by() groups, and change some advanced settings. See [G-3] twoway_options.


Remarks are presented under the following headings:

Typical use Cautions Use with by()

Typical use

Median splines provide a convenient way to show the relationship between y and x:

. sysuse auto

. scatter mpg weight, msize(*.5) || mspline mpg weight (click to run)

The important part of the above command is mspline mpg weight. On the scatter, we specified msize(*.5) to make the marker symbols half their normal size; see [G-4] relativesize.


The graph shown above illustrates a common problem with this technique: it tracks wiggles that may not be real and can introduce wiggles if too many bands are chosen. An improved version of the graph above would be

. scatter mpg weight, msize(*.5) || mspline mpg weight, bands(8) (click to run)

Use with by()

mspline may be used with by() (as can all the twoway plot commands):

. scatter mpg weight, msize(*.5) || mspline mpg weight, bands(8) ||, by(foreign, total row(1)) (click to run)

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index