coefplot looks for variables corresponding to the collected coefficient names and then uses their variable labels for the categorical axis. For factor variables, coefplot additionally takes value labels into account (the rule is to print the value label, if a value label is defined, and otherwise print the variable label or name along with the level). Here is an example with categorical variables and interaction terms:
. sysuse auto, clear (1978 automobile data) . keep if rep78>=3 (10 observations deleted) . regress mpg headroom i.rep##i.foreign Source | SS df MS Number of obs = 59 -------------+---------------------------------- F(6, 52) = 5.33 Model | 829.833037 6 138.305506 Prob > F = 0.0002 Residual | 1350.40425 52 25.9693125 R-squared = 0.3806 -------------+---------------------------------- Adj R-squared = 0.3091 Total | 2180.23729 58 37.5902981 Root MSE = 5.096 ------------------------------------------------------------------------------- mpg | Coefficient Std. err. t P>|t| [95% conf. interval] --------------+---------------------------------------------------------------- headroom | -1.120753 .9886418 -1.13 0.262 -3.104608 .8631023 | rep78 | 4 | -.3064994 1.973721 -0.16 0.877 -4.267059 3.654061 5 | 11.91038 3.856209 3.09 0.003 4.17233 19.64843 | foreign | Foreign | 3.710693 3.149593 1.18 0.244 -2.609427 10.03081 | rep78#foreign | 4#Foreign | 1.675263 3.941705 0.43 0.673 -6.234348 9.584873 5#Foreign | -8.972643 5.129298 -1.75 0.086 -19.26533 1.320046 | _cons | 22.61131 3.33317 6.78 0.000 15.92282 29.29981 ------------------------------------------------------------------------------- . coefplot, xline(0)Code
To display the actual coefficient names instead of variable labels, specify the
nolabels option:
. coefplot, xline(0) nolabels
Code
Sometimes, the nolabels
option is helpful just to find out what the exact names of the coefficients are,
so that they can be addressed in options such as the ones described in the following
examples. To find out how an estimation command names its coefficients you can also
type matrix list e(b)
after estimating the model, or replay the model with the
coeflegend option:
. regress, coeflegend noheader ------------------------------------------------------------------------------- mpg | Coefficient Legend --------------+---------------------------------------------------------------- headroom | -1.120753 _b[headroom] | rep78 | 4 | -.3064994 _b[4.rep78] 5 | 11.91038 _b[5.rep78] | foreign | Foreign | 3.710693 _b[1.foreign] | rep78#foreign | 4#Foreign | 1.675263 _b[4.rep78#1.foreign] 5#Foreign | -8.972643 _b[5.rep78#1.foreign] | _cons | 22.61131 _b[_cons] -------------------------------------------------------------------------------Code
An easy way to provide labels for the coefficients is to define appropriate
variable and value labels before applying coefplot (see
help label).
However, not all coefficients have corresponding variables (e.g.
_cons). To provide labels for such coefficients or to assign custom
labels to coefficients without manipulating variable labels, use the
coeflabels() option:
. sysuse auto, clear (1978 automobile data) . keep if rep78>=3 (10 observations deleted) . regress mpg headroom i.rep##i.foreign (output omitted) . coefplot, xline(0) coeflabels(1.foreign = "Foreign Car" _cons = "Constant")Code
coeflabels()
has a wrap() and a truncate() suboption
to deal with long labels. These suboptions apply to all coefficient labels,
whether they are automatically generated or provided within
coeflabels().
For example, to limit the line with to 20 characters and
wrap long labels to multiple lines, type:
. coefplot, xline(0) coeflabels(, wrap(20))
Code
Multiline labels can also be created explicitly using compound double quotes
within coeflabels().
Such labels will not be altered by wrap() or truncate():
. coefplot, xline(0) coeflabels(4.rep78 = `""Repair Record" "1978 = 4""', wrap(20))
Code
Furthermore, to change the symbol used in interaction terms, specify
the interaction()
suboption:
. coefplot, xline(0) coeflabels(, interaction(" x "))
Code
Sometimes it is useful to add headings between coefficients to better arrange
a graph. This can be achieved by the
headings() option:
. sysuse auto, clear (1978 automobile data) . keep if rep78>=3 (10 observations deleted) . regress mpg headroom i.rep##i.foreign (output omitted) . coefplot, xline(0) omitted baselevels drop(_cons) /// > headings(3.rep78 = "{bf:Repair Record}" /// > 0.foreign = "{bf:Car Type}" /// > 3.rep78#0.foreign = "{bf:Interaction Effects}")Code
In this example, omitted
requests to plot omitted coefficients and
baselevels
requests to plot base level coefficients. Omitted coefficients and base
levels coefficients are always equal to zero, but in some cases it can be
helpful to include them in a graph for reasons of clarity. The
{bf} tag changes text to bold; see
help text
for details on text in graphs.
Instead of adding headings you can also define groups of coefficients and
add group labels using the
groups() option:
. coefplot, xline(0) omitted baselevels drop(_cons) ///
> groups(?.rep78 = `""{bf:Repair}" "{bf:Record}""' ///
> ?.foreign = "{bf:Car Type}" ///
> ?.rep78#?.foreign = "{bf:Interaction Effects}")
Code
Furthermore, headings() and
groups() can be combined:
. coefplot, xline(0) omitted baselevels drop(_cons) ///
> headings(3.rep78 = "{it:Repair record:}" ///
> 0.foreign = "{it:Car type:}", nogap) ///
> groups(headroom 1.foreign = "{bf:Main Effects}" ///
> ?.rep78#?.foreign = "{bf:Interaction Effects}")
Code
In this example, the
nogap
suboption was specified within
headings()
to omit extra space before the headings.
If bycoefs
is specified, integer numbers (1, 2, 3 etc.)
are used to identify the elements on the categorical axis. Hence, use
numbers instead of coefficient names within
headings() and
groups() in this case:
. sysuse auto, clear (1978 automobile data) . regress price mpg headroom weight turn (output omitted) . estimates store Total . regress price mpg headroom weight turn if foreign==0 (output omitted) . estimates store Domestic . regress price mpg headroom weight turn if foreign==1 (output omitted) . estimates store Foreign . coefplot Domestic || Foreign || Total, drop(_cons) yline(0) /// > bycoefs byopts(yrescale) vertical /// > group(1 2 = "{bf:Subgroup results}", nogap) ylabel(0, add)Code
Option ylabel(0, add)
has been added to ensure that zero is included in each subgraph.
Equation labels provide yet another layer of labels; see option
eqlabels().
The default is to place the equation labels on the left hand side,
similar to group labels:
. sysuse auto, clear (1978 automobile data) . gen mpp = mpg/8 . mlogit rep78 mpp i.foreign if rep>=3 (output omitted) . coefplot, omitted keep(*:) coeflabels(mpp = "Milage") /// > eqlabels("Equation 1" "Equation 2" "Equation 3")Code
However, you can also set the equation labels as headings between equations
using the asheadings
suboption:
. coefplot, omitted keep(*:) coeflabels(mpg = "Milage") ///
> eqlabels("{bf:Equation 1}" "{bf:Equation 2}" "{bf:Equation 3}", asheadings)
Code
Note that, in this case, the
headings()
option is not allowed.
By default coefplot labels equations using the equation names provided in the results:
. sysuse auto, clear (1978 automobile data) . version 15: mean price weight, over(rep78) Mean estimation Number of obs = 69 1: rep78 = 1 2: rep78 = 2 3: rep78 = 3 4: rep78 = 4 5: rep78 = 5 -------------------------------------------------------------- Over | Mean Std. err. [95% conf. interval] -------------+------------------------------------------------ price | 1 | 4564.5 369.5 3827.174 5301.826 2 | 5967.625 1265.494 3442.372 8492.878 3 | 6429.233 643.5995 5144.95 7713.516 4 | 6071.5 402.9585 5267.409 6875.591 5 | 5913 788.6821 4339.209 7486.791 -------------+------------------------------------------------ weight | 1 | 3100 370 2361.676 3838.324 2 | 3353.75 157.6834 3039.098 3668.402 3 | 3299 137.3722 3024.878 3573.122 4 | 2870 213.8489 2443.271 3296.729 5 | 2322.727 123.7893 2075.709 2569.745 -------------------------------------------------------------- . coefplot, keep(*:)Code
Use the labels suboption of the
eqlabels() option
to instruct coefplot to treat the equation names as variable names and retrieve the
corresponding labels from the data:
. coefplot, keep(*:) eqlabels(, labels)
Code
The default is to plot all labels on the left of the plot region. Use option
yscale(alt)
to move labels to the right:
. sysuse auto, clear (1978 automobile data) . keep if rep78>=3 (10 observations deleted) . regress mpg headroom i.rep##i.foreign (output omitted) . coefplot, xline(0) omitted baselevels drop(_cons) yscale(alt) /// > headings(3.rep78 = "{bf:Repair Record}" /// > 0.foreign = "{bf:Car Type}" /// > 3.rep78#0.foreign = "{bf:Interaction Effects}")Code
Group labels and equation labels are rendered as additional axes
(axis 2 for group labels; axis 2 or 3 for equation labels, depending on whether
groups were specified), so you have to employ the
axis()
suboption to move these:
. coefplot, xline(0) omitted baselevels drop(_cons) yscale(alt axis(2)) ///
> groups(?.rep78 = `""{bf:Repair}" "{bf:Record}""' ///
> ?.foreign = "{bf:Car Type}" ///
> ?.rep78#?.foreign = "{bf:Interaction Effects}", angle(rvertical))
Code
Moving group labels to the right can also be useful if you want to add an
extra set of individual coefficient labels, without actually forming groups.
Here is an example in which
groups()
is used to add information on the sample sizes of factor levels:
. regress mpg i.rep i.foreign (output omitted) . coefplot, xline(0) omitted baselevels drop(_cons) yscale(alt axis(2)) /// > groups(3.rep78 = "N = 30" /// > 4.rep78 = "N = 18" /// > 5.rep78 = "N = 11" /// > 0.foreign = "N = 38" /// > 1.foreign = "N = 21", nogap angle(horizontal))Code
Labels on the left of the plot region will always be right-aligned in Stata and currently there is no option to change that. Left-aligned labels, however, can be very effective in coefficient plots. An approach to produce left-aligned labels is to plot the labels on the right, but then shift them to the left using negative gaps:
. sysuse auto, clear (1978 automobile data) . keep if rep78>=3 (10 observations deleted) . regress mpg i.rep##i.foreign (output omitted) . coefplot, xline(0) drop(_cons) omitted baselevels yscale(noline alt) /// > graphregion(margin(l=65)) coeflabels(, notick labgap(-125)) /// > headings(3.rep78 = "{bf:Repair Record}" 0.foreign = "{bf:Car Type}" /// > 3.rep78#0.foreign = "{bf:Interaction Effects}", labgap(-130))Code
In this example, yscale(alt noline) removes the axis line and places
the labels on the right of the plot region so they will be left-aligned.
coeflabels(, notick labgap(-125)) removes the ticks and shifts the
labels to the left. Finally, graphregion(margin(l=65)) adds space
on the left of the plot region to hold labels.
Determining the exact values of the required gaps and margins can be tedious.
An alternative is to plot the labels on the right and then open the graph
editor, select "Grid Edit" grid mode, and manually drag the labels to the left
of the plot region. Using the recording function of the graph editor it is
possible to automate this action. Moreover, you can inspect the file containing
the graph recording to see the commands that have been recorded. Moving the labels just
takes one command that will look something like .move yaxis1 leftof 8 5
(how exactly the command looks might depend on context). You can copy that command
and run it from your do-file by prefixing it with gr_edit (an undocumented
feature of Stata). Here is an example:
. coefplot, xline(0) drop(_cons) omitted baselevels yscale(noline alt) /// > coeflabels(, notick labgap(5)) /// > headings(3.rep78 = "{bf:Repair Record}" 0.foreign = "{bf:Car Type}" /// > 3.rep78#0.foreign = "{bf:Interaction Effects}", labgap(0)) . gr_edit .move yaxis1 leftof 8 5Code
By default, if only one model (or only one model per subgraph) is plotted,
coefplot draws a grid line for each coefficient. If multiple models are plotted,
coefplot draws grid lines between coefficients. Use the
grid() option to change
these defaults or turn the grid lines off. Also use the option to determine the
rendering of these grid lines (for the grid lines on the other axis use the regular
axis label options):
. sysuse auto, clear (1978 automobile data) . keep if rep78>=3 (10 observations deleted) . regress mpg headroom i.rep##i.foreign (output omitted) . coefplot, xline(0) xlabel(, grid) /// > grid(between glpattern(dash) glwidth(*2) glcolor(gray))Code
Alternatively, you can use option
ytick()
(or xtick() if
option vertical
has been specified) to manually define the grid lines:
. coefplot, xline(0) ytick(1.5 3.5 4.5 6.5, notick glstyle(refline))
Code