*! cmp 8.7.8 18 March 2024
help cmp

Title

Conditional mixed process estimator with multilevel random effects and coefficients

Syntax

cmp setup

- or -

cmp eq [eq ...] [if] [in] [weight] , indicators(exp [exp ...])
[level(#)
quietly
nolrtest
init(vector) from(vector) noestimate
covariance(covopt [covopt ...])

intpoints(# [# ...]) intmethod([ghermite | mvaghermite] , [tolerance(#) iterate(#)])

redraws(# [# ...]),
[type(halton | hammersley | ghalton | random) antithetics steps(#) scramble[(sqrt | negsqrt | fl)])

ghkdraws([#] , [type(halton | hammersley | ghalton | random) antithetics scramble[(sqrt | negsqrt | fl)]])

nodrop interactive
structural reverse psampling(# #)
resultsform(structural | reduced)
lf
ml_opts
svy svy_opts
display_opts eform_opts]

- or (after the above) -

cmp [, [display_opts eform_opts level(#) resultsform(structural | reduced)]]

where covopt is one of

unstructured | exchangeable | independent

and eq is

[fe_equation] [|| re_equation] [|| re_equation ...]

Each fe_equation is an equation to be estimated, defined largely according to the ml model eq syntax. That is, fe_equation is enclosed in
parentheses, optionally prefixed with a name for the equation:

(
[eqname:]
depvar [depvar2] =
[indepvars]
[, noconstant offset(varname:varname_o) exposure(varname:varname_e) truncpoints(exp exp) iia]
)

indepvars may include factor variables; see fvvarlist. Importantly, indepvars may also include the linear predictors associated with
any equations in the model. Such references should be by eqname rather than depvar, if the two differ, and suffixed with a #. This feature
allows dependent variables to depend on each other in their continuous and perhaps
incompletely observed form. It also allows for simultaneous, as distinct from strictly recursive, systems of equations.

varname_y2 is included only for interval-censored data, in a syntax analogous to that of
intreg. truncpoints(exp exp) is allowed for equations involving all model types except mulitnomial and rank-ordered probit. iia is meaningful only for
multinomial probit models without alternative-specific regressors.

Each exp in the required indicators() option is an expression that evaluates to a cmp indicator variable, which communicates required observation-level
information about the dependent variable(s) in the corresponding equation, and can be a constant, a variable name, or a complex mathematical expression. It can contain spaces
or parentheses if it is double-quoted.
Each exp must evaluate to 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9, potentially varying by observation. For a multinomial probit equation group with alternative-specific regressors,
the corresponding indicator expressions should all evaluate to 0's and 6's, and should be enclosed in an additional pair of parentheses. The same goes for
rank-ordered probit groups, except with 9's instead of 6's.

In random effect/coefficent models, each re_equation specifies the effects, potentially at multiple levels, according to the syntax

levelvar: [varlist] [, noconstant covariance(covopt)] [weight]

where covopt is as defined above. In these models, the optional intpoints and redraws() options are relevant.

cmp may be prefixed with svy ... :. However, using the svy option instead is recommended because it will result in more-informative results display.

pweights, fweights, aweights, and iweights are allowed at all levels; see help weights. Group-level,
weights, specificed in the re_equation syntax above, should be constant within groups. Weights for a given level need be specified in just one
equation.

The syntax of predict following cmp is

predict [type] {newvarname|stub*|newvarlist} [if exp] [in range]
[, statistic equation([#eqno|eqname #eqno|eqname...])
outcome(outcome) nooffset reducedform condition(a b [, equation(#eqno|eqname)])]

where statistic is xb, stdp, stddp, residuals, lnl, scores,
pr[(a b)], e[(a b)], or ystar(a b); and a and b
may be numbers or variables; a missing (a > .) means minus infinity, and b missing (b > .) means plus infinity; see missing.

cmp shares features of all estimation commands; see help estcom.

UPDATES

Major features have been added to cmp since Roodman (2011), and are documented only here. They include:

* The rank-ordered probit model is available. It generalizes the multinomial probit model to fit ranking data. See asroprobit.

* The fractional probit model is available. It uses the quasi-likelihood method of Papke and Wooldridge (1996). See fracreg.

* Truncation is now a general modeling feature rather than a regression type. This allows modeling of a pre-censoring truncation process in
all models except multinomial and rank-ordered probit.

* References to any equation's linear predictor (XB) can appear on the right side of any equation, even when it is modeled as latent (not fully observed), and
even if the resulting equation system is simultaneous rather than recursive.

* Multilevel random effects and coefficients can now be modeled, using simulation or (adaptive) quadrature. These
can be correlated within and across equations. For such multidimensional effects, quadrature is done on "sparse grids" for efficiency (Heiss and Winschel 2008).

Versions of cmp back to 7.1.0 are on GitHub. In Stata 13 or later, they can be installed with
net install cmp, replace from(https://raw.github.com/droodman/cmp/vX.Y.Z) where X.Y.Z is the version number.

Versions 8.0.0 and 8.2.0, released in mid-2017 and early 2018, include changes that can somewhat affect results in hierarchical models. The previous version, 7.1.0,
avoids these changes.

Versions 8.6.2, released in June 2021, requires Stata 13 or later. Previous versions work in Stata 11 and 12 too.

Donate?

Has cmp improved your expertise, career, or marriage?
Consider giving back through a donation to support the work of its author, David Roodman.

Description

cmp fits a large family of multi-equation, multi-level, conditional mixed-process estimators. Right-side references to left-side variables must together have
a recursive structure when those references are to the observed, censored variables; but references to the (latent) linear predictors may be
collectively simultaneous. The various terms in that description can be defined as follows:

* "Multi-equation" means that cmp can fit Seemingly Unrelated (SUR), instrumental variables (IV) systems, and some simultaneous-equation systems. Single-equation models can be fit too.

* "Multi-level" means that random coefficients and effects (intercepts) can be modeled at
various levels in hierarchical fashion, the classic example being a model of education outcomes with unobserved school and class effects. Since the models
can also be multi-equation, random effects at a given level are allowed by default to be correlated across equations. E.g., school and class
effects may be correlated across outcomes such as math and readings scores. Effects at different levels, however, are assumed uncorrelated
with each other, with the observation-level errors, and with the regressors.

* "Mixed process" means that different equations can have different kinds of
dependent variables (response types). The choices, all generalized linear models with a Gaussian error distribution, are: continuous and unbounded (the classical linear regression
model), tobit (left-, right-, or bi-censored), interval-censored, probit, ordered probit, multinomial
probit, rank-ordered probit, and fractional probit. Pre-censoring truncation can be modeled for most response types. A dependent variable in one equation can appear on the right side of another equation.

* "Conditional" means that the model can vary by observation. An equation can be dropped for observations for which it is not relevant--if, say, a worker
retraining program is not offered in a city then the determinants of uptake cannot be modeled there. The type of a dependent variable can even vary by
observation. In this sense, the model is conditional on the data.

* "Recursive" means, however, that when censored dependent variables appear in each others' equations, these references must break the equations into
stages. If A, B, C, and D are all binary dependent variables, modeled as probits, then A and B could be modeled
determinants of C and C as a determinant of D--but D could not then be a
modeled determinant of A, B, or C.

* "Simultaneous" means that that recursivity is not required in the references to the (latent) linear predictors of dependent variables. If A*, B*, C*, and D*
are the hypothesized, unobserved linear predictors behind the observed A, B, C, and D--e.g., if A=0 when A*<0 and A=1 when A*>=0,
etc.--then D* can appear in any of the equations even though D cannot. The same holds even if D* is completely censored, i.e., completely unobserved.

Broadly, cmp is appropriate for two classes of models: 1) those in which the posited data-generating process is fully modeled; and 2) those in which
some equations are structural, while others are reduced form, providing instruments for identification of the parameters in the structural equations,
as in two-stage least squares. In the first case, cmp is a full-information maximum likelihood (FIML) estimator, and all estimated
parameters are structural. In the latter, it is a limited-information (LIML) estimator, and only the final stage's or stages' coefficients are structural.

cmp's modeling framework embraces those of the official Stata commands probit, ivprobit, treatreg,
biprobit, tetrachoric, oprobit, mprobit, asmprobit, asroprobit, tobit, ivtobit,
cnreg, intreg, truncreg, fracreg, heckman, heckprob, heckoprobit,
xtreg, xtprobit, xttobit, xtintreg, xtheckman, meintreg, meprobit, meoprobit, metobit, metobit,
eprobit; to lesser degrees regress, sureg,
and reg3; and user-written ssm, polychoric, triprobit,
mvprobit, bitobit,
mvtobit, oheckman, switch_probit, reoprob,
cdsimeq, and bioprobit.

While lacking the specialized post-estimation features in many of those packages, cmp
goes beyond them in the generality of model specification. Thanks to the flexibility of ml, on which it is built, it accepts linear coefficient constraints
as well as all weight types, vce types (robust, cluster, etc.), and svy settings. And it offers
more flexibility in model construction. For example, one can regress a continuous variable on two endogenous variables,
one binary and the other sometimes left-censored, instrumenting each with additional variables. And cmp usually allows the model to vary by observations.
Equations can have different samples, overlapping or non-overlapping. Heckman selection modeling can be incorporated into a wide variety of models through auxilliary
probit equations. In some cases, the gain is consistent estimation where it was difficult before. Sometimes the gain is in efficiency.
For example if C is continuous, B is a sometimes-left-censored determinant of C, and A is an instrument, then the effect of B on C can be
consistently estimated with 2SLS (Kelejian 1971). However, a cmp estimate that uses the information that B is censored will be more efficient if it is based
on a more accurate model.

To inform cmp about the natures of the dependent variables and about which equations apply to which observations, the user must include the
indicators() option after the comma in the cmp command line. This must contain one expression for each equation. The expression can
be a constant, a variable name, or a formula. Formulas that contain spaces or parentheses should be enclosed in
quotes. For each observation, each expression must evaluate to one of the following codes, with the meanings shown:

0 = observation is not in this equation's sample

. = observation is in this equation's sample but dependent variable unobserved for this observation

1 = equation is "continuous" for this observation, i.e., is linear with Gaussian error or is an uncensored observation in a tobit equation

2 = observation is left-censored for this (tobit) equation at the value stored in the dependent variable

3 = observation is right-censored at the value stored in the dependent variable

4 = equation is probit for this observation

5 = equation is ordered probit for this observation

6 = equation is multinomial probit for this observation

7 = equation is interval-censored for this observation

8 = equation is truncated on the left and/or right (obsolete because truncation is now a general modeling feature)

9 = equation is rank-ordered probit for this observation

10 = equation is frational probit for this observation

For clarity, users can execute the cmp setup subcommand, which defines global macros that can then be used in cmp command lines:

$cmp_out = 0

$cmp_missing = .

$cmp_cont = 1

$cmp_left = 2

$cmp_right = 3

$cmp_probit = 4

$cmp_oprobit = 5

$cmp_mprobit = 6

$cmp_int = 7

$cmp_trunc = 8 (deprecated)

$cmp_roprobit = 9

$cmp_frac = 10

Equations are specified according to the ml model
syntax. This means that for instrumented regressions, cmp differs from ivregress, ivprobit,
ivtobit, and similar commands
in not automatically including exogenous regressors (included instruments) from the second stage in the first stage. So you must arrange for this
yourself. For example, ivreg y x1 (x2=z) corresponds to cmp (y=x1 x2) (x2=x1 z), ind($cmp_cont $cmp_cont).

At its heart cmp is an SUR (seemingly unrelated regressions) estimator. With major exceptions explained just below, it treats the equations as related to each other only in
having errors that jointly normally distributed. Mathematically, the likelihood it computes is conditioned on observing all right-side
variables, including those that also appear on the left side of equations. However, Maximum likelihood (ML) SUR estimators are appropriate for
many multi-equation models that are not SUR, meaning ones in which endogenous variables appear on the right side of other equations. Models of this
kind for which ML SUR is consistent must satisfy two criteria:

1) They are recursive. In other words, the equations can be arranged so that the matrix of coefficients of
the dependent variables in each others' equations is triangular. As emphasized above, this means the models have clearly defined stages, though there can be more than one
equation per stage.

2) They are "fully observed." Dependent
variables in one stage enter subsequent stages only as observed. Returning to the example in the first paragraph, if C is a categorical
variable modeled as ordered probit, then C, not the latent variable underlying it, call it C*, must figure in the model for D.

As an illustration of the ideas here, some Stata estimation commands have wider applicability than many realize. sureg (X=Y) (Y=Z), isure typically
matches ivregress 2sls X (Y=Z) exactly even though
the documentation does not describe sureg as an instrumental variables (IV) estimator.
(Iterated SUR is not a true ML estimator, but it converges to the same solution as ML-based SUR, as implemented, for example, in the demonstration command
mysureg. See Pagan (1979) on the LIML/iterated SUR connection.) And
biprobit (X=Y) (Y=Z) will consistently estimate an IV model in which X and Y are binary.

Version 6 of cmp, introduced in 2013, can handle violations of both conditions. This it does using the standard technique for estimating simultaneous-equation systems, which transforms
a simultaneous system into an SUR one. Condition 2 is no longer required: models may refer to latent
variables, using a # suffix. cmp (y1 = y2# x1) (y2 = x2), ind($cmp_probit $cmp_probit) models y1 and y2 as probit and y1 as depending on the unobserved
linear predictor behind y2. The #-suffixed references should be to names of equations rather than dependent variables, though these are the same by default. So,
equivalent to the previous example is cmp (eq1:y1 = eq2# x1) (eq2:y2 = x2), ind($cmp_probit $cmp_probit). In addition, references to (latent) linear predictors
need not satisfy condition 1. So cmp (y1 = y2# x1) (y2 = y1# x2), ind($cmp_probit $cmp_probit) is acceptable. References to censored variables must still be recursive:
cmp (y1 = y2 x1) (y2 = y1 x2), ind($cmp_probit $cmp_probit) will not work as intended. Fortunately, this requirement is not as restrictive as it seems because
many models with non-recursive dependencies on censored variables are logically impossible (see discussion in Roodman 2011).

When fitting simultaneous linear (uncensored) systems, you must #-suffix enough references so that those not suffixed relate recursively. So
cmp (y1 = y2 x1) (y2 = y1 x2), ind($cmp_cont $cmp_cont) will fail, but cmp (y1 = y2 x1) (y2 = y1# x2), ind($cmp_cont $cmp_cont) and
cmp (y1 = y2# x1) (y2 = y1# x2), ind($cmp_cont $cmp_cont) will work and produce the same results (the latter a bit more slowly).

In order to model random coefficients and effects, cmp borrows syntax from xtmixed. It is best explained with
examples. This eq fragment specifies an equation with two levels of random effects corresponding to groups defined by the variables school and
class:

(math = age || school: || class:)

Coming first, school is understood to be "above" class in the hierarchy. At a given level, random effects can be
assumed present in some equations and not others. Those in more than one equation at a given level are assumed to be (potentially) correlated
across equations (an assumption that can be overridden through constraints or the covariance() option). This specifies a school effect only for math but not reading scores,
and potentially correlated class effects for both:

(math = age || school: || class:) (reading = age || class:)

This adds random coefficients on age at the class level in both equations. The two new coefficients are potentially correlated with each other and with the random intercepts at the same level:

(math = age || school: || class: age) (reading = age || class: age)

Weights of various types may be specified at each level. These should be defined by variables or expressions that are constant within each group
of the given level. Within a given group, aweights and pweights are rescaled to sum to the number of groups in the next level
down (or number of observations if it is the bottom level). pweights imply vce(cluster groupvar) where
groupvar defines the highest level in the hierarchy having
pweights. iweights and fweights are not rescaled; the latter affect the reported sample size. Since weights must be the same
across equations, they need be specified only once for each level. So these are equivalent:

(math = age || school: || class: [pw=weightvar1]) (reading = age || class:)

(math = age || school: || class: [pw=weightvar1]) (reading = age || class: [pw=weightvar1])

and the contradiction here would cause an error:

(math = age || school: || class: [pw=weightvar1]) (reading = age || class: [pw=weightvar2])

Like xtreg, xtprobit, xttobit, xtintreg, xtheckman, and xtmixed,
cmp uses adaptive quadrature by default to integrate likelihoods over the unobserved random effects (see [R] xtmixed). Quadrature estimates these
integrals by evaluating the integrands at a few strategically chosen points (12 by default), and summing using particular weights. It is extremely efficient for computing
one-dimensional integrals, but unreliable. Adaptive quadrature is more reliable, but slower. It iteratively adjusts the points
through a change of variables in order to concentrate them near the main mass of the integrated
function.

The traditional generaization of quadrature to multidimensional integrals, such as arise with correlated random effects and coefficients,
is inefficient. A d-dimensional integral with 12 integration points for each dimension will take 12^d evaluations. Heiss and Winschel (2008) put forward an alternative called sparse-grid integration
that reduces the number of evaluations needed for a given level of accuracy. cmp uses this method by default for multidimensional random
effects/coefficients problems. Still, despite the reduction, for higher-dimensional problems, practicality may still require the user to reduce the precision of the grid below the default
of 12, using the intpoints() option.

cmp also offers a competing method for computing these integrals: simulation. This differs in taking a large, representative set of draws from the hypothesized distributions,
evaluating the integrand at each, and taking the simple average (Train 2009; Greene 2011,
chap 15). In the author's experience, despite the innovation of sparse grids, adaptive quadrature under-performs simulation on multidimensional random effects/coefficients. That
is, simulation tends to achieve higher precision for a given runtime, sometimes especially when used
with the DFP search algorithm (invoked with tech(dfp)). Adaptive quadrature appears superior for single-effect models; and for it, the default Berndt-Hall-Hall-Hausman
search method often works best.

To trigger simulation, include the redraws() option. This sets the number of draws per observation
at each level, along with
the type of sequence (Halton, Hammersley, generalized Halton, pseudorandom), whether antithetics are also drawn, and, in the Halton and Hammersley cases,
whether and how the sequences should be scrambled. (See Gates 2006 for more on all these concepts except scrambling, for which see Kolenikov 2012.)
For (generalized) Halton and Hammersley sequences, it is preferable to make the number of draws prime, to insure more variable coverage of the
distribution from observation to observation, making coverage more uniform overall. Increasing the
number of draws increases precision at the expense of time. In a bid for speed cmp can begin by estimating
with just 1 draw per observation per random effect (plus the antithetics if specified). It can then use the result of this rough search as the starting point for an
estimate with more draws, then repeat, multiplying by a fixed amount each time until reaching the specified number of draws. The steps(#) suboption
of redraws() can override the default number of steps, which is just 1. redraws(50 50) would
specify immediate estimation with the full 50
draws per observation in a three-level model (with two levels of random effects). See options below for more.

Estimation problems with observations that are censored in three or more equations,
such as a trivariate probit, require calculation of cumulative
joint normal distributions of dimension three or higher. This is a non-trivial problem. The preferred technique again involves simulation: the method of
Geweke, Hajivassiliou, and Keane (GHK). (Greene 2011; Cappellari and Jenkins 2003; Gates 2006). cmp accesses the algorithm
through the separately available Mata function ghk2(), which must be installed for cmp to work. Modeled
on the built-in ghk() and ghkfast(), ghk2() gives users choices about the length and nature of the
sequences generated for the simulation,
which choices cmp largely passes on through the optional ghkdraws() option, which includes type(), antithetics, scramble()
suboptions. See options
below for more.

cmp starts by fitting each equation separately in order to obtain a good starting point for the full model fit. Sometimes in this preparatory step,
convergence difficulties make a reported parameter covariance matrix singular, yielding missing
standard errors for some regressors. Or variables are found to be collinear. In order to maximize the chance of convergence, cmp ordinarily
drops such regressors from the equations in which they cause trouble, reruns the single-equation fit, and then leaves them out for the full model too. The
nodrop option prevents this behavior.

On estimation with interval-censored or truncated equations

For equations with interval-censored observations, list two variables before the =, somewhat following the syntax of intreg. For
example, cmp (y1 y2 = x1 x2), ind($cmp_int) indicates that the dependent variable is censored to intervals whose lower bounds are in y1 and upper
bounds are in y2. Missing values in y1 are treated as -infinity and those in y2 are treated as +infinity. For observations in which y1 and y2 coincide, there
is no censoring, and the likelihood is the same as for $cmp_cont.

For equations with truncated distributions--which can be any model type besides multinomial and rank-ordered probit--use the truncpoints(exp exp) option within the specification for the
given equation to provide truncation points. Like indicator expressions, the truncation points can be constants,
expressions, or variable names, with missing values interpreted as above. Observations in which the observed value lies outside the truncation
range are automatically dropped for that equation. An example is below.

On estimation with multinomial probit equations

Multinomial probits can be specified with two different syntaxes, roughly corresponding to the Stata commands mprobit and
asmprobit. In
the first syntax, the user lists a single equation, just as for other dependent variable types, and puts a 6 ($cmp_mprobit) in the
indicators() list. The dependent variable holds the choice made in each case. Like
mprobit, cmp treats all regressors as determinants of choice for all alternatives. In particular,
it expands the specified equation to a group with one "utility" equation for each possible choice. All equations in the group include all regressors, except for the first,
which has none. This one corresponds to the lowest value of the dependent variable, which is taken as the base alternative. The next, corresponding to the
second-lowest value, is the "scale alternative," meaning that to normalize results, the variance of its error term is fixed. (The value it is fixed at
depends on whether the structural option is invoked, on which see below.) In the first syntax,
the single eq can contain an iia option after the comma so that cmp, like mprobit, will impose the Independence of
Irrelevant Alternatives assumption. I.e., cmp will assume
that errors in the utility equations are uncorrelated and have unit variance.

Such models, ones without exclusion restrictions and without the IIA assumption, are formally identified as long as at least one regressor varies over
alternatives (Keane 1992). However, Keane emphasizes that fits for such models tend to be unstable if there are no exclusion
restrictions. There are two ways to impose exclusion restrictions with cmp. First, as with mprobit, you can use constraints.

Second, you can use cmp's other multinomial
probit syntax. In this "alternative-specific" syntax, you list one equation in the cmp command line for each alternative, including the base alternative. Different equations may include different
regressors. Unlike asmprobit, cmp does not force regressors that appear in more than one equation
to have the same coefficient across alternatives, although again this restriction can be imposed through constraints. When using
the alternative-specific syntax, the dependent variables listed should be a set of dummies, as can be generated with xi, noomit from the
underlying choice variable. The first equation is always treated as the base alternative, so here you can control which alternative is the base alternative
by reordering the equations. In
general, regressors that appear in all other equations should be excluded from the base alternative. Otherwise, unless a constraint is imposed to reduce the degrees
of freedom, the model will not be identified. (cmp automatically excludes the constant from the base alternative equation.) Variables that are specific
to the base alternative, however, or to a strict subset of alternatives, can be included in the base alternative equation. In the second syntax, the IIA is not
assumed, nor available through an option. It can still be imposed through constraints.

To specify an alternative-specific multinomial probit group, include expressions in the indicators() that evaluate to 0 or 6
($cmp_out or $cmp_mprobit) for each equation (0 indicating that the choice is not available for given observations). Enclose the
whole list in
an additional set of parentheses. Note that unlike with asmprobit, there should be still be one row in the data set per case, not per case and
alternative. So instead of variables that vary by alternative, there must be a version of that variable for each
alternative--not a single travel time variable that varies by mode of travel, but an air travel time variable, a bus travel time variable, and so
on. An alternative-specific multinomial example is also below.

In a multinomial probit model with J choices, each possible choice has its own structural equation, including an error term. These error terms have some
covariance structure. It is impossible, however, to estimate all the entries of the JxJ covariance matrix (Train 2003;
Long and Freese (2006)). What is used
in the computation of the likelihood is the (J-1)x(J-1) covariance matrix of the differences of the non-base-alternative errors from the base-alternative error. So by
default, cmp, much like asmprobit, interprets the sigma and rho parameters relating to these equations as characterizing these
differenced errors. To eliminate an excessive degree of scaling freedom, it constrains
the error variance of the first non-base-alternative equation (the "scaling alternative") to 2, which it would be anyway if the errors for the first two
structural equations were i.i.d. standard normal (so that their difference has variance 2).

The disadvantage of this parameterization is that it is hard to think about if you want to impose additional constraints on it. As an alternative,
cmp, like asmprobit, offers a structural option. When this is included, cmp creates a full set of parameters
to describe the covariance of the J structural errors. To remove the excessive degrees of freedom, it then constrains the base alternative error to have
variance 1 and no correlation with the other errors; and constrains the error for the second, scaling alternative to also have variance 1. To impose the
IIA under this option, one would then constrain various "atanhrho" and "lnsig" parameters to 0. An example below shows how to estimate the same IIA model
with and without the structural parameterization.

The intuitiveness of the structural parameterization comes at a cost, however (Bunch (1991);
Long and Freese (2006), pp. 325-29). Though the particular set of
constraints imposed seems innocent, it actually results in a mapping from the space of allowed structural covariances to the space of possible
covariance matrices for the relative-differenced errors that is not onto. That is, there are positive definite (J-1)x(J-1) matrices,
valid candidates for the covariance of the relative-differenced errors, which are not compatible with the assumptions that the first two alternatives'
structural errors have variance one and that the first, base alternative's error is uncorrelated with all other structural errors. So the structural
option can prevent cmp from reaching the maximum-likelihood
fit. Long and Freese (2006) describe how changing which
alternatives are the base and scaling alternatives, by reording the equations, can sometimes free an estimator to find the true maximum within the structural
parametrization.

On estimation with rank-ordered probit equations

Specification and treatment of rank-ordered probit equations is nearly identical to that in the second syntax for multinomial probits described just
above. Equations must be listed for every alternative. Indicators for these equations must be either 0 or 9 ($cmp_out or $cmp_roprobit) for
each observation, and grouped in an extra set of parentheses. Constraints defining the base and scale alternatives are automatically imposed in the same way. The structural option too
works identically. One option relating specifically to rank-ordered probit is reverse. It instructs cmp to interpret lower-numbered
rankings as higher instead of lower.

Tips for achieving and speeding convergence

If you are having trouble achieving (or waiting for) convergence with cmp, these techniques might help:

1. Changing the search techniques using ml's technique() option, or perhaps the search parameters, through its
maximization options. cmp accepts all these and passes them on to ml. The default Newton-Raphson search method
usually works very well once ml has found a concave region. The DFP algorithm (tech(dfp)) often works better before then, and the two
can be mixed, as with tech(dfp nr). See the details of the technique() option at ml.

2. If the estimation problem requires the GHK algorithm (see above), change the number of draws per observation in the simulation sequence using
the ghkdraws() option. By default, cmp uses twice the square root of the number of observations for which the
GHK algorithm
is needed, i.e., the number of observations that are censored in at least three equations. Raising simulation accuracy by increasing the number of
draws is
sometimes necessary for convergence and can even speed it by improving search precision. On the other hand, especially when the number of observations is
high, convergence can be achieved, at some loss in precision, with remarkably few draws per observations--as few as 5 when the sample size is 10,000 (Cappellari and Jenkins
2003). And taking fewer draws can slash execution time.

3. Under the same circumstances, in Stata 15 or later, you can also specify ghkdraws(0). This tells cmp to dispense with the GHK algorithm
for computing cumulative multivariate normal distributions and instead use quadrature, as implemented by the built-in mvnormal() family
of functions. When it works, this can run much faster. However, for cumulative probabilities that are close to zero, the functions sometimes return zero or small negative
values, which have undefined log likelihoods. If this happens, cmp will fail with the "initial values not feasible" message.

4. If getting many "(not concave)" messages, try the difficult option, which instructs ml to
use a different search algorithm in non-concave regions.

5. If the search appears to be converging in likelihood--if the log likelihood is hardly changing in each iteration--and yet fails to converge, try
adding a nrtolerance(#) or nonrtolerance option to the command line after the comma. These are ml options that control when convergence is declared. (See
ml_opts, below.) By default, ml declares convergence when the log likelihood is changing very little with successive iterations (within
tolerances adjustable with the tolerance(#) and ltolerance(#) options) and when the calculated gradient vector is close enough to zero.
In some difficult problems, such as ones with nearly collinear regressors, the imprecision of floating point numbers prevents ml from quite satisfying the second criterion.
It can be loosened by using the nrtolerance(#) to set the scaled gradient tolerance to a value larger than its default of 1e-5, or eliminated altogether
with nonrtolerance. Because of the risks of collinearity, cmp warns when the condition number of an equation's regressor matrix exceeds 20 (Greene 2000, p. 40).

6. Try cmp's interactive mode, via the interactive option. This
allows the user to interrupt maximization by hitting Ctrl-Break or its equivalent, investigate and adjust the current solution, and then restart
maximization by typing ml max. Techniques for exploring and changing the current solution include displaying the current coefficient and gradient vectors
(with mat list $ML_b or mat list $ML_g) and running ml plot. See help ml, [R] ml, and
Gould, Pitblado, and Sribney (2006) for
details. cmp is slower in interactive mode.

Options

indicators(exp [exp ...]) is required. It should pass a list of expressions that evaluate to 0, 1, 2, 3,
4, 5, 6, 7, 8, or 9 for every
observation, with one expression for each equation and in the same order. Expressions can be constants, variable names, or
formulas. Individual formulas that contain spaces or parentheses should be enclosed in quotes.

level(#) specifies the confidence level, in percent,
for confidence intervals of the coefficients; see help level. The
default is controlled by set level and is usually 95.

resultsform(structural | reduced) affects how results are stored and displayed after fitting models with
#-suffixed references. resultsform(structural), the default, displays results
corresponding to the specified model. resultsform(reduced) switches to the reduced form, in which #-suffixed references
are substituted out. After estimation,
you can switch between the two forms by typing cmp, resultsform(reduced) and
cmp, resultsform(structural). The main value of this option is that it allows predict
and margins to be executed against the reduced-form
results, which capture direct and indirect effects of exogenous regressors on endogenous ones, as they cascade through the #-references. (Effects
transmitted through censored variables or through uncensored ones
not referred to with the # suffix are still left out.) In the case of predict, the same result can be achieved on the fly by adding a
reducedform option to that command
(see below for more); this switches to the reduced form just long enough to generate predictions. margins, however,
can be confused by this sleight of hand--as in margins, predict(reducedform)--and return no results. To apply this option in svy
estimation, it is usually best to use cmp's svy option after the comma (described below) rather than to prefix the cmp
command with svy :.

quietly suppresses most output: the results from any single-equation initial fits and the iteration log during the full model fit.

nolrtest suppresses calculation and reporting of the likelihood ratio (LR) test of overall model fit, relative to
a constant(s)-only model. This has no effect if data are pweighted or errors are robust or clustered.
In those cases, the likelihood function does not reflect the non-sphericity of the errors, and so is a pseudolikelihood. The
LR test is then invalid and is not run anyway.

intpoints(# [# ...]) Sets the precision level for quadrature-based modelling of random effects and coefficients. The default is 12 and the maximum 25. In one-dimensional cases,
such as a single-equation random effects model, this sets the number of integration points in the standard Gaussian-Hermite quadrature algortihm. In higher-dimensional
problems--with more than one random effect or coefficient within or accross equations--it sets the precision level, which in turn determines the number of inegration points. As in the
one-dimensional case, a precision level of k means that polynomial integrands (which do not arise in cmp) would be exactly estimated if they have degree 2k-1 or
less (Heiss and Winschel 2008). The option should list one number (#) for each level of the model above the base (e.g., two numbers in a three-level model).

intmethod([ghermite | mvaghermite] , [tolerance(#) iterate(#)]) is also relevant only for quadrature-based
modelling of random effects and coefficients. ghermite specifies
classic, non-adaptive Gauss-Hermite quadrature, which is extremely fast, but can be unreliable especially when groups have many observations or subgroups (Rabe-Hesketh, Skrondal, and
Pickles 2002). mvaghermite, the default, specifies adaptive quadrature using the method developed by Naylor and Smith and first applied in Stata by Rabe-Hesketh, Skrondal, and
Pickles in gllamm. The tolerance(#) option sets the tolerance for determining convergence of adaptation; the default is
1e-3. (Before cmp version 8.0.0, the default was 1e-8, but it was found that increasing the tolerance saved time while hardly affecting
results.) iterate(#) sets the the maximum number of iterations that are allowed before giving up on convergence; its default is 1001.

redraws(# [# ...] , [type(halton | hammersley | ghalton | random) antithetics steps(#) scramble(sqrt | negsqrt | fl)]) is
relevant for random coefficient/effects models, and triggers simulation- rather than quadrature-based modelling. The option
should begin with one number (#) for each level of the model above the base (e.g., two numbers
in a three-level model); these specify the number of draws per observation from the simulated distributions of the random effects. The optional
type() suboption sets the sequence type; the default is halton. The optional antithetics suboption doubles the number of draws
at all levels by including antithetics. For more on these concepts, see See Drukker and Gates (2006). The optional steps(#)
suboption sets the number of times to fit the model as the number of draws
at each level is geometrically increased to the specified final values. Under this option, the preliminary runs all use the Newton-Raphson search algorithm
and ml's nonrtolerance tolerance(0.1) options in order to accept coarse fits. This stepping is meant
only to increase speed by using fewer draws until the search is close to the maximum. The optional scramble() option "scrambles"
the digit sequences to reduce cross-sequence correlations (Kolenikov 2012). The square-root scrambler scramble(sqrt) multiplies each digit in the
sequence for prime p by floor(sqrt(p)), modulo p. scramble(negsqrt) multiples by the negative square root. scramble(fl) multplies
by the number specific to p recommended by Faure and Lemieux (2009). scramble(sqrt) and scramble(lr) have no effect for models with
at most two random effect/coefficient components because for primes 2 and 3, their multipliers are 1.

lf makes cmp use its lf-method evaluator instead of the lf0-method one. (See help ml.) This forces Stata's Maximum Likelihood estimation package
to compute the first derivatives of the likelihood numerically instead of using cmp to compute them analytically. This substantially slows
estimation but occassionally improves convergence.

ghkdraws([#] , [type(halton | hammersley | ghalton | random) antithetics scramble(sqrt | negsqrt | fl) ]) governs the draws used in GHK simulation of
higher-dimensional cumulative multivariate normal distributions--or specifies using quadrature rather than the GHK algorithm. It is similar to the redraws option. However, it takes at most one number;
if it, or the entire option, is omitted, the number of draws is set rather arbitrarily to twice the square root of the number of observations
for which the simulation is needed. (Simulated maximum likelihood is consistent as long as the number of draws rises with the square root of the
number of observations. In practice, a higher number of observations often reduces the number of draws per observation needed
for reliable results. Train 2009, p. 252.) As in the redraws() option, the optional type(string) suboption specifies the type of sequence in
the GHK simulation, halton being the default;
antithetics requests antithetic draws; and scramble() applies a scrambler.

In Stata 15 and later, ghkdraws(0) instructs cmp to discard
the GHK algorithm in favor of quadrature, as implemented in the built-in mvnormal() family
of functions. When it works, this can run much faster. However, for cumulative probabilities that are close to zero, the functions sometimes return zero or small negative
values, which have undefined log likelihoods. If this happens, cmp will fail with the "initial values not feasible" message.

nodrop prevents the dropping of regressors from equations in which they receive missing standard errors in initial single-equation
fits. It also prevents the removal of collinear variables.

covariance(covopt [covopt ...]) offers a shorthand for constraining the cross-equation correlation structure of the errors at each
level--shorthand, that is, compared to using constraint. There should be one covopt for each level in the model, ordered from highest to lowest, the
lowest being observation-level. Each covopt can be unstructured, exchangeable, or independent. unstructured, the default, imposes no
constraint. exchangeable specifies that all correlations between random effects, coefficients, or residual errors in different equations, within a given level, are the same;
and likewise for all the variances of all these areas. independent sets all cross-equation correlations at a given level to zero. Above the base level, the default unstructured,
while theoretically meaningful, may make the model infeasible. Try fitting with (for a two-level example) cov(indep unstruct). Separately, the re_equation
syntax documented above also includes a covariance() option. With the same choices and defintitions, it controls variances and covariances within a given equation at a given level.

interactive makes cmp fit the full model in ml's interactive mode.
This allows the user to interrupt the model fit with Ctrl-Break or its equivalent, view and adjust the trial solution with such
commands as ml plot, then restart optimization by typing ml max. See help ml, [R] ml, and
Gould, Pitblado, and Sribney (2006) for
details. cmp runs more slowly in interactive mode.

init(vector) and from(vector) are synonyms. They pass a row vector of user-chosen starting values for the full model fit, in the manner of the ml init, copy
command. The vector must contain exactly one element for each parameter cmp will estimate, and in the same order as cmp reports the parameter estimates
in the output. Thus, at the end will be the initial guesses for the lnsig_i parameters, then those for the atanhrho_ij, then those
for any ordered-probit cuts. (cmp normally also
reports sig_i's and rho_ij's, but these are not additional parameters, merely transformed versions of underlying ones, and should be ignored in building
the vector of starting values.) The names of the row and columns of the vector do
not matter.

noestimate simplifies the job of constructing an initial vector for the init() option. It instructs cmp to stop before fitting the full model and
leave behind an e(b) return vector with one labeled entry for each free parameter. To view this vector, type mat list e(b). You can copy or edit this vector, such as
with "mat b=e(b)", then pass it back to cmp with the init() option.

structural forces the structural covariance parameterization for all multinomial and rank-ordered equation groups. See above for more.

reverse instructs cmp to interpret lower-numbered ranks in rank-ordered probit equations as being higher.

psampling(# #) makes cmp perform "progressive sampling," which can speed estimation on large data sets. First it estimates on
a small subsample, then a larger one, etc., until reaching the full sample. Each iteration uses the previous one's estimates as a starting point.
The first argument in the option sets the initial sample size, either in absolute terms (if it is at least 1) or as a fraction of
the full sample (if it is less than 1). The second argument is the factor by which the sample should grow in each iteration. This process is
analogous to but distinct from the stepping that occurs by default in simulating random effects.

ml_opts: cmp accepts the following standard ml options, which affect the full-model and initial, single-equation fits: trace
gradient
hessian
showstep
technique(algorithm_specs)
vce(oim|opg|robust|cluster)
iterate(#)
tolerance(#)
ltolerance(#)
gtolerance(#)
nrtolerance(#)
nonrtolerance
shownrtolerance
difficult
constraints(numlist|matname)

display_opts: cmp accepts the following standard ml display options, which affect how results are presented: noheader
nofootnote
neq(#)
showeqns
plus
nocnsreport
noomitted
vsquish
noemptycells
baselevels
allbaselevels
cformat(%fmt)
pformat(%fmt)
sformat(%fmt)
nolstretch
coeflegend

eform_opts: cmp accepts the following ml eform options:
eform(strings:string)
eform
hr
shr
irr
or
rrr

svy indicates that ml is to pick up the svy settings set
by svyset and use the robust variance estimator. This option
requires the data to be svyset. svy may
not be specified with vce() or weights. See help svy estat.

svy_opts: Along with svy, users may also specify any of these related ml options, which affect how the svy-based
variance is estimated:
nosvyadjust
subpop:(subpop_spec)
srssubpop. And users may specify any of these ml options, which affect output display: deff
deft
meff
meft
eform
prob
ci. See help svy estat.

On predict and margins after cmp

Options for predict after cmp are:

equation([#eqno|eqname #eqno|eqname...]) specify equation(s) for predictions

xb linear prediction

stdp standard error of linear prediction

stddp standard error of difference in linear predictions

lnl observation-level log likelihood (in hierarchical models, averaged over groups)

scores derivative of the log likelihood with respect to xb or parameter

residuals residuals relative to linear prediction

pr[(a b)] probability of positive outcome (probit) or given outcome (ordered probit, multinomial) or linear predictor being in given range (otherwise). Omitting (a b) defaults to (0 .)

e[(a b)] truncated expected value: E[y|a<y<b]. Omitting (a b) defaults to (. .), meaning unbounded

ystar(a b) censored expected value: E(y*), y* = max(a, min(y, b))

condition(c d [, equation(#eqno|eqname)]) condition a pr, e, or ystar statistic on bounding another equation's (latent) linear predictor between c and d

outcome(outcome) specify outcome(s), for ordered probit only

nooffset ignore any offset() or exposure() variable

reducedform predict based on reduced form; relevant for linear systems

The pr[(a b)], e[(a b)], ystar(a b), and condition() options should not include a comma between the
bounds. In all cases, a missing value means negative infinity in the first bound and positive infinity in the second.

All the above options can also be used in the predict() option of margins to obtain marginal effects on them.

cmp's most unusual predict option is condition(). It allows computed probabilities and expectations for one equation's outcome to be conditioned
on the bounding of another equation's outcome (or latent variable). E.g., one can use the option to predict the probability of a student's math score being between 5 and 10 if her reading
score is between 4 and 8 (with something like pr(5 10) eq(math) cond(4 8, eq(reading))), or her expected math score conditioned the same way
(e eq(math) cond(4 8, eq(reading))), or even her expected math score when both variables are so bounded (e(5 10) eq(math) cond(4 8, eq(reading))). Probability
estimates for (ordered) probit equations can be conditioned too. To condition on a censored variable being within a certain range, refer to the associated cut points for its hypothesized
latent variable, whether it is fixed (in probit, tobit, and interval regressions) or estimated (ordered probit). For example, to condition on a probit-modeled outcome
x being 0 or 1, respectively, use cond(. 0, eq(x)) or cond(0 ., eq(x)). The Heckman selection model examples below illustrate.

eqno can be an equation name (if not set explicitly, an equation's name is that of its dependent variable). Or it can be an equation number preceded by a
#. The default equation is #1, unless the provided variable list has one entry for each equation, or takes the form stub*. These request
prediction variables for all equations, with names as given or as automatically generated beginning with stub.

In contrast, for ordered probit equations, if pr is specified, predict will by default compute probability variables for all outcomes. The
names for these variables will be automatically generated using a provided variable name as a stub. This stub may be directly provided in the command line--in which case
it should not include a *--or may itself be automatically generated by a cross-equation stub*. Thus it is possible to generate probabilities
for all outcomes in all ordered probit equations with a single, terse command. Alternatively, the outcome(outcome)
option can be used to request probabilities for just one outcome. outcome can be a value for the dependent variable, or a category number preceded by a #.
For example, if the categorical dependent variable takes the values 0, 3, and 4, then outcome(4) and outcome(#3) are synonyms. (outcome()
also implies pr.)

In explaining the multi-equation and multi-outcome behavior of predict after cmp, examples are worth a thousand words.

The flexibility of cmp affects the use of predict and margins after estimation. Because the censoring type (probit, tobit, etc.) can technically
vary by observation, the default statistic for predict is always xb, linear fitted values. This is unlike for probit and oprobit, after which
the default is pr, predicted probabilities of outcomes. So to obtain probilities predicted by (ordered) probit equations, remember to include the
pr option in the predict command line or predict(pr) in the margins command line. (For ordered probit equations,
an outcome() option will also imply pr.)

When using margins to estimate marginal effects for a cmp estimate,
users may need to include the force and/or noestimcheck options in the margins command line, because
of a bug in some versions of margins. Also, in some cases cmp may first need to be run with the resultsform(reduced)
option (see above) in order to produce any results.

For non-hierarchical models, to compute marginal effects on the probability of a particular outcome, such as (1,1) from a bivariate probit model, you can preserve the data, set the outcome variables to the values of interest,
run margins on the exponential of the predicted log likelihood, using the option expression(exp(predict(lnl))), then restore the original data set. The method is slow
but intuitive and effective. Examples are below for bivariate probit and multinomial probit models.

When non-final-stage equations are uncensored, predict's reducedform option affects how predictions are made. Suppose we estimate:

sysuse auto

cmp (price = foreign#) (foreign = mpg), ind($cmp_cont $cmp_probit)

Of necessity, since foreign# is not observed, predict pricehat will effectivly first predict values for foreign using the second equation, then use
those to predict values for price in the first equation. That is, it will use the reduced-form coefficients. Now suppose that we treat the second equation
as uncensored:

cmp (price = foreign#) (foreign = mpg), ind($cmp_cont $cmp_cont)

In this case, predict pricehat, reducedform will predict as in the previous case. predict pricehat will instead predict values for
price based on the observed values of foreign.

Examples of predict and margins after cmp are below.

Citation

cmp is not an official Stata command. It is a free contribution to the research community.
Please cite it as such:

Roodman, D. 2011. Estimating fully observed recursive mixed-process models with cmp. Stata Journal 11(2): 159-206.

Published examples

See Google Scholar.

Introductory examples

The purpose of cmp is not to match standard commands. But replications
are the best way to introduce how to use cmp (colored text is clickable):

* Define indicator macros for clarity.

. cmp setup

. webuse laborsup

. regress kids fem_inc male_educ

. cmp (kids = fem_inc male_educ), ind($cmp_cont) quietly

. sureg (kids = fem_inc male_educ) (fem_work = male_educ), isure

. cmp (kids = fem_inc male_educ) (fem_work = male_educ), ind($cmp_cont $cmp_cont) quietly

. mvreg fem_educ male_educ = kids other_inc fem_inc

. cmp (fem_educ = kids other_inc fem_inc) (male_educ = kids other_inc fem_inc), ind(1 1) qui

. ivreg fem_work fem_inc (kids = male_educ), first

. cmp (kids = fem_inc male_educ) (fem_work = kids fem_inc), ind($cmp_cont $cmp_cont) qui

. ivregress liml fem_work (kids = male_educ other_inc fem_inc)

. cmp (kids = fem_inc male_educ other_inc) (fem_work = kids), ind($cmp_cont $cmp_cont) qui

. probit kids fem_inc male_educ

. predict p

. margins, dydx(*)

. cmp (kids = fem_inc male_educ), ind($cmp_probit) qui

. predict p2, pr

. margins, dydx(*) predict(pr)

. oprobit kids fem_inc male_educ

. margins, dydx(*) predict(outcome(#2))

. cmp (kids = fem_inc male_educ), ind($cmp_oprobit) qui

. margins, dydx(*) predict(eq(#1) outcome(#2) pr)

. gen byte anykids = kids > 0

. biprobit (anykids = fem_inc male_educ) (fem_work = male_educ)

. margins, dydx(fem_inc) predict(p11)

. cmp (anykids = fem_inc male_educ) (fem_work = male_educ), ind($cmp_probit $cmp_probit)

. preserve

. replace anykids=1

. replace fem_work=1

. margins, dydx(fem_inc) expression(exp(predict(lnl))) force // marginal effect on probability of (1,1)

. restore

. tetrachoric anykids fem_work

. cmp (anykids = ) (fem_work = ), ind($cmp_probit $cmp_probit) nolr qui

. ivprobit fem_work fem_educ kids (other_inc = male_educ), first

. version 13: margins, predict(pr) dydx(*)

. cmp (fem_work = other_inc fem_educ kids) (other_inc = fem_educ kids male_educ), ind($cmp_probit $cmp_cont)

. margins, predict(pr) dydx(*) force

. treatreg other_inc fem_educ kids, treat(fem_work = male_educ)

. cmp (other_inc = fem_educ kids fem_work) (fem_work = male_educ), ind($cmp_cont $cmp_probit) qui

. tobit fem_inc kids male_educ, ll

. margins, dydx(*) predict(pr(17,.))

. cmp (fem_inc = kids male_educ), ind("cond(fem_inc>10, $cmp_cont, $cmp_left)") qui

. margins, dydx(*) predict(pr(17 .))

. ivtobit fem_inc kids (male_educ = other_inc fem_work), ll first

. cmp (fem_inc=kids male_educ) (male_educ=kids other_inc fem_work), ind("cond(fem_inc>10,$cmp_cont,$cmp_left)" $cmp_cont)

. webuse intregxmpl

. intreg wage1 wage2 age age2 nev_mar rural school tenure

. cmp (wage1 wage2 = age age2 nev_mar rural school tenure), ind($cmp_int) qui

. webuse laborsub

. truncreg whrs kl6 k618 wa we, ll(0)

. cmp (whrs = kl6 k618 wa we, trunc(0 .)), ind($cmp_cont) qui

. webuse 401k

. fracreg probit prate mrate ltotemp age i.sole

. margins, dydx(mrate)

. cmp (prate = mrate ltotemp age i.sole), ind($cmp_frac) qui

. margins, dydx(mrate) predict(pr)

. webuse fitness

. churdle linear hours age i.smoke distance i.single, select(commute whours age) ll(0)

. gen byte hours_pos = hours > 0

. cmp (hours = age i.smoke distance i.single, trunc(0 .)) (hours_pos = commute whours age), nolr ind("cond(hours_pos, $cmp_cont, $cmp_out)" $cmp_probit) covar(indep) qui

. webuse sysdsn3

. mprobit insure age male nonwhite site2 site3

. margins, dydx(nonwhite) predict(outcome(2))

. cmp (insure = age male nonwhite site2 site3, iia), nolr ind($cmp_mprobit) qui

. margins, dydx(nonwhite) predict(eq(#2) pr)

. webuse travel

. asmprobit choice travelcost termtime, casevars(income) case(id) alternatives(mode) struct

. predict pr, pr // probability of choosing each outcome in each case

. drop invehiclecost traveltime partysize

. reshape wide choice termtime travelcost pr, i(id) j(mode)

. constraint 1 [air]termtime1 = [train]termtime2

. constraint 2 [train]termtime2 = [bus]termtime3

. constraint 3 [bus]termtime3 = [car]termtime4

. constraint 4 [air]travelcost1 = [train]travelcost2

. constraint 5 [train]travelcost2 = [bus]travelcost3

. constraint 6 [bus]travelcost3 = [car]travelcost4

. cmp (air:choice1=t*1) (train: choice2=income t*2) (bus: choice3=income t*3) (car: choice4=income t*4), ind((6 6 6 6)) constr(1/6) nodrop struct tech(dfp) ghkd(200)

. predict cmppr1, eq(air) pr

. predict cmppr2, eq(train) pr

. predict cmppr3, eq(bus) pr

. predict cmppr4, eq(car) pr

. webuse wlsrank

. asroprobit rank high low, casevars(female score) case(id) alternatives(jobchar) reverse

. predict pr, pr1 // probability of ranking each outcome first in each case

. reshape wide rank high low pr, i(id) j(jobchar)

. constraint 1 [esteem]high1=[variety]high2

. constraint 2 [esteem]high1=[autonomy]high3

. constraint 3 [esteem]high1=[security]high4

. constraint 4 [esteem]low1=[variety]low2

. constraint 5 [esteem]low1=[autonomy]low3

. constraint 6 [esteem]low1=[security]low4

. cmp (esteem:rank1=high1 low1)(variety:rank2=female score high2 low2)(autonomy:rank3=female score high3 low3)(security:rank4=female score high4 low4),ind((9 9 9 9)) tech(dfp) ghkd(200, type(hammersley)) rev constr(1/6)

. predict cmppr1, eq(esteem) pr

. predict cmppr2, eq(variety) pr

. predict cmppr3, eq(autonomy) pr

. predict cmppr4, eq(security) pr

. webuse class10

. eprobit graduate income i.roommate, endogenous(hsgpa = income i.hscomp) entreat(program = i.campus i.scholar income) vce(robust)

. cmp (graduate = program##(c.income roommate c.hsgpa)) (program = i.campus i.scholar income) (hsgpa = income i.hscomp), vce(robust) ind($cmp_probit $cmp_probit $cmp_cont) qui nolr

* Heckman selection models

. webuse womenwk

. gen selectvar = wage<.

. heckman wage education age, select(married children education age) mills(heckman_mills)

. margins, dydx(*) predict(ycond)

. margins, dydx(*) predict(yexpected)

. cmp (wage = education age) (selectvar = married children education age), ind(selectvar $cmp_probit) nolr qui

. margins, dydx(*) predict(e eq(wage) condition(0 ., eq(selectvar)))

. margins, dydx(*) expression(predict(e eq(wage) cond(0 ., eq(selectvar))) * predict(pr eq(selectvar)) )

. predict xb, eq(selectvar) xb

. predict e, eq(selectvar) e(0 .)

. gen cmp_mills = e - xb

. gen wage2 = wage > 20 if wage < .

. heckprob wage2 education age, select(married children education age)

. margins, dydx(*) predict(pcond)

. cmp (wage2 = education age) (selectvar = married children education age), ind(selectvar*$cmp_probit $cmp_probit) qui

. margins, dydx(*) predict(pr eq(wage2) condition(0 ., eq(selectvar)))

. gen wage3 = (wage > 10)+(wage > 30) if wage < .

. heckoprobit wage3 education age, select(married children education age)

. cmp (wage3 = education age) (selectvar = married children education age), ind(selectvar*$cmp_oprobit $cmp_probit) nolr qui

* Simultaneous equation and latent variable models

. sysuse auto

. cmp (price = foreign#) (foreign = mpg), ind($cmp_cont $cmp_cont) nolr

. predict pricehat1

. predict pricehat2, reducedform

. cmp (price = foreign#) (foreign = mpg), ind($cmp_cont $cmp_probit)

. replace foreign = . in 1/20

. cmp (price = foreign#) (foreign = mpg), ind($cmp_cont $cmp_probit) // sample does not shrink

. webuse klein

. reg3 (consump wagepriv wagegovt) (wagepriv consump govt capital1), ireg3

. cmp (consump = wagepriv# wagegovt) (wagepriv = consump# govt capital1), ind($cmp_cont $cmp_cont) nolr tech(dfp) qui

. webuse supDem

. reg3 (qDemand: quantity price pcompete income) (qSupply: quantity price praw), endog(price) first

. cmp (price = quantity# pcompete income) (quantity = price# praw), ind($cmp_cont $cmp_cont) nolr tech(dfp) qui

. cmp, resultsform(reduced)

. margins, dydx(*) predict(eq(quantity))

. egen priceO = cut(price), at(25 27 31 33 35 37 39) icodes

. egen quantityO = cut(quantity), at(5 7 9 11 13 15 17 19 21) icodes

. cmp (price: priceO = quantity# pcompete income) (quantity: quantityO = price# praw), ind($cmp_oprobit $cmp_oprobit) nolr qui tech(dfp)

. cmp, resultsform(reduced)

. margins, dydx(praw) predict(outcome(3) eq(quantity) pr)

* Hierarchical/random effects models

. webuse union

. xtprobit union age grade not_smsa south##c.year

. gsem (union <- age grade not_smsa south##c.year M[idcode]@1), probit intp(12)

. cmp (union = age grade not_smsa south##c.year || idcode:), ind($cmp_probit) qui

. webuse nlswork3

. xttobit ln_wage union age grade not_smsa south south#c.year, ul(1.9)

. gsem (ln_wage <- union age grade not_smsa south south#c.year M[idcode]@1), family(gaussian, rcensored(1.9)) intp(12)

. replace ln_wage = 1.9 if ln_wage > 1.9

. `"cmp (ln_wage = union age grade not_smsa south south#c.year || idcode:), ind("cond(ln_wage<1.899999, $cmp_cont, $cmp_right)") nolr qui"'

. webuse nlswork5

. xtintreg ln_wage1 ln_wage2 union age grade south south#c.year occ_code

. gsem (ln_wage1 <- union age grade south south#c.year occ_code M[idcode]@1), family(gaussian, udepvar(ln_wage2)) intp(12)

. cmp (ln_wage1 ln_wage2 = union age grade south south#c.year occ_code || idcode:), ind($cmp_int) nolr qui

. webuse tvsfpors

. meoprobit thk prethk cc#tv || school: || class:

. cmp (thk = prethk cc#tv || school: || class:), ind($cmp_oprobit) intpoints(7 7) nolr qui

. webuse productivity

. xtmixed gsp private emp hwy water other unemp || region: || state:

. cmp (gsp = private emp hwy water other unemp || region: || state:), nolr ind($cmp_cont)

. webuse womenhlthre

. gen byte goodhlth = health > 3

. xteprobit goodhlth exercise grade, entreat(insured = grade i.workschool)

. cmp (goodhlth = insured##c.(exercise grade) || personid:) (insured = grade i.workschool || personid:), ind($cmp_probit $cmp_probit) intp(7) nolr

. webuse wagework

. xtheckman wage age tenure, select(working = age market)

. cmp (wage = age tenure || personid:) (working = age market || personid:), ind(working*$cmp_cont $cmp_probit) intp(7) nolr

These examples go beyond standard commands (though gsem can do some):

. webuse laborsup

* Trivariate seemingly unrelated ordered probit

. gen byte kids2 = kids + int(uniform()*3)

. gen byte kids3 = kids + int(uniform()*3)

. cmp (kids=fem_educ) (kids2=fem_educ) (kids3=fem_educ), ind($cmp_oprobit $cmp_oprobit $cmp_oprobit) nolr qui

* Regress an unbounded, continuous variable on an instrumented, binary one. 2SLS is consistent but less efficient.

. cmp (other_inc = fem_work) (fem_work = kids), ind($cmp_cont $cmp_probit) qui robust

. ivreg other_inc (fem_work = kids), robust

* Now regress it on a left-censored one, female income, which is only modeled for observations in which the woman works.

. gen byte ind2 = cond(fem_work, cond(fem_inc, $cmp_cont, $cmp_left), $cmp_out)

. cmp (other_inc=fem_inc kids) (fem_inc=fem_educ), ind($cmp_cont ind2)

* Correlated random coefficient and random effect

. use http://www.stata-press.com/data/mlmus3/gcse

. cmp (gcse = lrt || school: lrt), ind($cmp_cont) nolr

* Multinomial probit with heterogeneous preferences (random effects by individual)

. use http://fmwww.bc.edu/repec/bocode/j/jspmix.dta

. cmp (tby = sex, iia || scy3:), ind($cmp_mprobit) nolr

* Random effects probit dependent on latent first stage

. webuse union

. cmp (union = age not_smsa black# || idcode:) (black = south#c.year), ind($cmp_probit $cmp_probit) nolr

These illustrate subtleties of predict after cmp:

. webuse laborsup

* Bivariate seemingly unrelated ordered probit

. gen byte kids2 = kids + int(uniform()*3)

. cmp (kids=fem_educ) (kids2=fem_educ), ind($cmp_oprobit $cmp_oprobit) nolr tech(dfp) qui

* Predict fitted values. Fitted values are always the default, as is equation #1

. predict xbA

* Two ways to predict fitted values for all equations

. predict xbB*

. predict xbC xbD

* Get scores for all equations and parameters

. predict sc*, score

* Get observation-level log-likelihoods

. predict lnl, lnl

* Two ways to predict kids=0, using (default) first equation

. predict prA, pr outcome(0)

. predict prB, outcome(#1)

* Predict kids2=4, using second equation

. predict prC, outcome(4) eq(kids2)

* Predict all outcomes, all equations.

. predict prD*, pr

* Same but resulting variable names for the two equations start with prE and prF respectively.

. predict prE prF, pr

* Predict all outcomes, equation 2. Generates variables prG_Y where Y is outcome number (not outcome value).

. predict prG, eq(#2) pr

References

Bunch, D.S. 1991. Estimability in the multinomial probit model. Transportation Research. 25B(1): 1-12.

Cappellari, L., and S. Jenkins. 2003. Multivariate probit regression using simulated maximum likelihood.
Stata Journal 3(3): 278-94.

Drukker, D.M., and R. Gates. 2006. Generating Halton sequences using Mata. Stata Journal 6(2): 214-28. http://www.stata-journal.com/article.html?article=st0103

Faure, H., and C. Lemieux. 2009. Generalized Halton Sequences in 2008: A Comparative Study. ACM Transactions on Modeling and Computer Simulation 19(4): article 15.

Gates, R. 2006. A Mata Geweke-Hajivassiliou-Keane multivariate normal simulator. Stata Journal 6(2): 190-213. http://www.stata-journal.com/article.html?article=st0102

Gould, W., J. Pitblado, and W. Sribney. 2006. Maximum Likelihood Estimation with Stata. 3rd ed. College Station: Stata Press.

Greene, W.H. 2002. Econometric Analysis, 5th ed. Prentice-Hall.

Greene, W.H. 2011. Econometric Analysis, 7th ed. Prentice-Hall.
Chapter 15

Heiss, F., and V. Winschel. 2008. Likelihood approximation by numerical integration on sparse grids. Journal of Econometrics 144(1): 62-80.

Keane, M.P. 1992. A note on identification in the multinomial probit model. Journal of Business and Economics Statistics 10(2): 193-200.

Kelejian, H.H. 1971. Two-stage least squares and econometric systems linear in parameters but nonlinear in the endogenous variables.
Journal of the American Statistical Association 66(334): 373-74.

Kolenikov, S. 2012. Scrambled Halton sequences in Mata. Stata Journal 12(1): 29-44.

Long, J. S., and J. Freese. 2006. Regression models for categorical dependent variables using Stata. 2nd ed. College Station, TX: Stata Press.

Naylor, J.C., and A.F.M. Smith. 1982. Applications of a method for the efficient computation of posterior distributions. Applied Statistics 31(3): 214-25.

Pagan. A. 1979. Some consequences of viewing LIML as an iterated Aiken estimator. Economics Letters 3:369-372.

Papke, L. E., and J. M. Wooldridge. 1996. Econometric methods for fractional response variables with an application
to 401(k) plan participation rates. Journal of Applied Econometrics 11: 619–32.

Pitt, M.M., and S. R. Khandker. 1998. The impact of group-based credit programs on poor households in Bangladesh: Does the gender of participants matter?
Journal of Political Economy 106(5): 958-96.

Rabe-Hesketh, S., A. Skrondal, and A. Pickles. 2002. Reliable estimation of generalized linear mixed models using adaptive quadrature. Stata Journal 2(1):1-21.

Rivers, D., and Q. Vuong. 1988. Limited information estimators and exogeneity tests for simultaneous probit models. Journal of Econometrics 39: 347-66.

Roodman, D. 2011. Estimating fully observed recursive mixed-process models with cmp. Stata Journal 11(2): 159-206.

Smith, R.J., and R.W. Blundell. 1986. An exogeneity test for a simultaneous equation tobit model with an application
to labor supply. Econometrica 54(3): 679-85.

Train, K. 2009. Discrete Choice Methods with Simulation. 2nd ed. Cambridge University Press. http://elsa.berkeley.edu/books/choice2.html

Author

David Roodman

david@davidroodman.com

Acknowledgements

Thanks to Kit Baum, David Drukker, Arne Hole, Stanislaw Kolenikov, and Mead Over for comments, and to Florian Heiss and Viktor Winschel for permission to adapt
their sparse grid Mata code.