*! version 1.0 16jan2014

Title

combomarginsplot -- Combine multiple marginsplots

Syntax

combomarginsplot margins_file_list [,
combomarginsplot_options marginsplot_options]

combomp margins_file_list [,
combomarginsplot_options marginsplot_options]

cmpmargins margins_spec [,
margins_options cmplabel(label)]

combomarginsplot12 margins_file_list [,
combomarginsplot_options marginsplot_options]

combomarginsplot_options

Margins file handling
labels(label_list) list of quoted strings to label the margins in each file

filevarname(newvarname) Specify variable name for file index. The default is "_filenumber".

Plot & CI Plots
file#opts(plot_options) affect
rendition of all plots from #th margins file

lplot#opts(plot_options) affect
rendition of each #th logical plot; i.e., the #th plot from each margins file

fileci#opts(rcap_options) affect
rendition of all confidence interval plots from #th margins file

lci#opts(rcap_options) affect
rendition of each #th logical confidence interval plot; i.e., the #th
confidence interval plot from each margins file

Other
offset Offset the plotting symbols on the x-axis; this can help distinguish clearly overlapping confidence intervals from multiple margins.

offset(number) amount to offset x-axis categories. The default is 0.05. This option implies offset.

ovar(varname) Specify variable name to offset. Default is the variable that defines the plots.

mlabfmt(%fmt) Specify display format for any marker labels.
Useful when you, e.g., label the plotted margins with their values, plotopt(mlabel(_margin)).

savefile(filename [, replace]) Save combined margins file. (Note that the saving() option, if
specified, applies to marginsplot and saves the resulting graph.)

margins_file_list is a list of saved files created by the
saving option of the margins command.

Description

combomarginsplot combines the saved results from multiple calls to margins
into one marginsplot.

combomarginsplot works by appending the saved margins file, treating
each as a distinct level of a factor variable. By default the variable that indexes the file number
is _filenumber, though an alternative variable name can be specified with the filevarname()
option

Note that it is possible to create a combined margins file that is incoherent, which will either
cause marginsplot to return an error or create a nonsensical graph. If you encounter a situation
where you do not get what you expect, however, please contact me!

combomp is simply a shorter name for combomarginsplot. Assuming you use this program 10 times a month,
type 40 words per minute, and work a 50-year career, the abbreviated version will save you 4.5 hours.

cmpmargins is a utility that can (optionally) be used in place of the standard margins command.
It behaves just like margins, except that it attaches an optional label (specified with the cmplabel() option)
to the saved margins file. This will be used by combomarginsplot to label the resulting graph, unless combomarginsplot’s
labels() option is specified. That is, labels for the different margins files can be specified when they are created,
using the cmpmargins command with the cmplabel() option, or they can be specified when combomarginsplot is run,
using its labels() option.

combomarginsplot12 is an older version of the program that should run correctly on Stata versions 12 through 15.

Options

Margins file handling

filevarname() specifies a variable name to index the multiple appended
margins files. combomarginsplot combines the margins files
specified in margins_file_list, creating a combined margins file
that appears to marginsplot to have come from a single margins command.
The multiple margins files appear to marginsplot to correspond
to the levels of a factor variable, which is named by default "_filenumber".
This option changes the name of that new factor variable.

labels() contains a list of labels for each combined file. These are used to label the
values of the new factor variable that indexes the individual margins
file, thereby allowing the multiple margins to be labeled in the
plot. If the labels contains spaces, they must be enclosed in quotation
marks.

Plot & CI Plot

file#opts() specifies formatting options to be applied to all
the plots associated with the #th margins file.

fileci#opts() specifies formatting options to be applied to all
the confidence interval plots associated with the #th margins file.

lplot#opts() specifies formatting options to be applied to all
the #th logical plot from each margins file. Thus, for example,
lplot1opts() would apply formatting options to the first plot
from each margins file.

lci#opts() specifies formatting options to be applied to all
the #th logical confidence interval plot from each margins file.

Saving

savefile() specifies that the combined margins file should be saved. The saved file
corresponds with the format of margins files saved with the margins command,
meaning that multiple saved files can then be re-combined at a second
hierarchical level by combomarginsplot. Note that this option is
distinct from the saving() option, which tells marginsplot to
save the resulting graph.

Examples

Combining results from multiple parallel models.

. sysuse auto

. oprobit rep78 i.foreign mpg price weight

. margins foreign, at(mpg=(10(5)50)) predict(outcome(3)) saving(file1, replace)

. oprobit rep78 i.foreign mpg

. margins foreign, at(mpg=(10(5)50)) predict(outcome(3)) saving(file2, replace)

. oprobit rep78 i.foreign mpg gear

. margins foreign, at(mpg=(10(5)50)) predict(outcome(3)) saving(file3, replace)

. combomarginsplot file1 file2 file3, labels("Full model" "Restricted model" "Gear Model") noci

Using file#opts() and lplot#opts() to make plot clearer

. combomarginsplot file1 file2 file3, labels("Full model" "Restricted model" "Gear Model") noci file1opts(pstyle(p1)) file2opts(pstyle(p2)) file3opts(pstyle(p3)) lplot1(mfcolor(white)) legend(colfirst)

Separating plots from each file using marginsplots's by() option

. combomarginsplot file1 file2 file3, labels("Full model" "Restricted model" "Gear Model") noci by(_filenumber)

Separating plots by a different variable

. combomarginsplot file1 file2 file3, labels("Full model" "Restricted model" "Gear Model") noci by(foreign)

Using transparency to make overlapping CIs visible, separating by model, and repositioning legend

. combomarginsplot file1 file2 file3, labels("Full model" "Restricted model" "Gear Model") by(_filenumber) byopt(legend(at(4) pos(0)) title("Probability of mediocre repair record")) recastci(rarea) ciopt(color(%20)) legend(col(1))

Combining plots for multiple outcomes from one model. (Note that the stock marginsplot command now handles multiple outcomes directly.)

. sysuse auto

. oprobit rep78 i.foreign mpg price weight

. margins foreign, at(mpg=(10(5)50)) expression(predict(outcome(1))+predict(outcome(2))) saving(file4, replace)

. margins foreign, at(mpg=(10(5)50)) expression(predict(outcome(4))+predict(outcome(5))) saving(file5, replace)

. combomarginsplot file4 file5 , labels("Outcomes 1&2" "Outcomes 4&5") noci file1opts(pstyle(p1)) file2opts(pstyle(p2)) lplot1(mfcolor(white))

Author

Nicholas Winter, University of Virginia, USA

nwinter@virginia.edu