Stata 15 help for xtmixed

xtmixed has been renamed to mixed. xtmixed continues to work but, as of Stata 13, is no longer an official part of Stata. This is the original help file, which we will no longer update, so some links may no longer work.

-------------------------------------------------------------------------------

Title

[XT] xtmixed -- Multilevel mixed-effects linear regression

Syntax

xtmixed depvar [fe_equation] [|| re_equation] [|| re_equation ...] [, options]

where the syntax of fe_equation is

[indepvars] [if] [in] [weight] [, fe_options]

and the syntax of re_equation is one of the following:

for random coefficients and intercepts

levelvar: [varlist] [, re_options]

for a random effect among the values of a factor variable

levelvar: R.varname [, re_options]

levelvar is a variable identifying the group structure for the random effects at that level or _all representing one group comprising all observations.

fe_options Description ------------------------------------------------------------------------- Model noconstant suppress constant term from the fixed-effects equation -------------------------------------------------------------------------

re_options Description ------------------------------------------------------------------------- Model covariance(vartype) variance-covariance structure of the random effects noconstant suppress constant term from the random-effects equation collinear keep collinear variables fweight(exp) frequency weights at higher levels pweight(exp) sampling weights at higher levels -------------------------------------------------------------------------

vartype Description ------------------------------------------------------------------------- independent one variance parameter per random effect, all covariances zero; the default unless a factor variable is specified exchangeable equal variances for random effects, and one common pairwise covariance identity equal variances for random effects, all covariances zero; the default for factor variables unstructured all variances and covariances distinctly estimated -------------------------------------------------------------------------

options Description ------------------------------------------------------------------------- Model mle fit model via maximum likelihood, the default reml fit model via restricted maximum likelihood pwscale(scale_method) control scaling of sampling weights in two-level models residuals(rspec) structure of residual errors

SE/Robust vce(vcetype) vcetype may be oim, robust, or cluster clustvar

Reporting level(#) set confidence level; default is level(95) variance show random-effects parameter estimates as variances and covariances noretable suppress random-effects table nofetable suppress fixed-effects table estmetric show parameter estimates in the estimation metric noheader suppress output header nogroup suppress table summarizing groups nostderr do not estimate standard errors of random-effects parameters nolrtest do not perform LR test comparing to linear regression display_options control column formats, row spacing, and display of omitted variables and base and empty cells

EM options emiterate(#) number of EM iterations, default is 20 emtolerance(#) EM convergence tolerance, default is 1e-10 emonly fit model exclusively using EM emlog show EM iteration log emdots show EM iterations as dots

Maximization maximize_options control the maximization process; seldom used matsqrt parameterize variance components using matrix square roots; the default matlog parameterize variance components using matrix logarithms

coeflegend display legend instead of statistics -------------------------------------------------------------------------

indepvars may contain factor variables; see fvvarlist. depvar, indepvars, and varlist may contain time-series operators; see tsvarlist. bootstrap, by, jackknife, mi estimate, rolling, and statsby are allowed; see prefix. pweights and fweights are allowed; see weight. coeflegend does not appear in the dialog box. See [XT] xtmixed postestimation for features available after estimation.

Menu

Statistics > Longitudinal/panel data > Multilevel mixed-effects models > Mixed-effects linear regression

Description

xtmixed fits linear mixed models. Mixed models are characterized as containing both fixed effects and random effects. The fixed effects are analogous to standard regression coefficients and are estimated directly. The random effects are not directly estimated but are summarized according to their estimated variances and covariances. Although random effects are not directly estimated, you can form best linear unbiased predictions (BLUPs) of them (and standard errors) by using predict after xtmixed; see [XT] xtmixed postestimation. Random effects may take the form of either random intercepts or random coefficients, and the grouping structure of the data may consist of multiple levels of nested groups. As such, mixed models are also known in the literature as multilevel models and hierarchical linear models. The overall error distribution of the linear mixed model is assumed to be Gaussian, and heteroskedasticity and correlations within lowest-level groups also may be modeled.

Options

+-------+ ----+ Model +------------------------------------------------------------

noconstant suppresses the constant (intercept) term and may be specified for the fixed effects equation and for any or all the random-effects equations.

covariance(vartype), where vartype is

independent|exchangeable|identity|unstructured

specifies the structure of the covariance matrix for the random effects and may be specified for each random-effects equation. An independent covariance structure allows a distinct variance for each random effect within a random-effects equation and assumes that all covariances are zero. exchangeable covariances have common variances and one common pairwise covariance. identity is short for "multiple of the identity"; that is, all variances are equal and all covariances are zero. unstructured allows for all variances and covariances to be distinct. If an equation consists of p random-effects terms, the unstructured covariance matrix will have p(p+1)/2 unique parameters.

covariance(independent) is the default, except when the random-effects equation consists of the factor-variable specification R.varname, in which case covariance(identity) is the default, and only covariance(identity) and covariance(exchangeable) are allowed.

collinear specifies that xtmixed not omit collinear variables from the random-effects equation. Usually there is no reason to leave collinear variables in place, and in fact doing so usually causes the estimation to fail because of the matrix singularity caused by the collinearity. However, with certain models (for example, a random-effects model with a full set of contrasts), the variables may be collinear, yet the model is fully identified because of restrictions on the random-effects covariance structure. In such cases, using the collinear option allows the estimation to take place with the random-effects equation intact.

fweight(exp) specifies frequency weights at higher levels in a multilevel model, whereas frequency weights at the first level (the observation level) are specified in the usual manner, for example, [fw=fwtvar1]. exp can be any valid Stata expression, and you can specify fweight() at levels two and higher of a multilevel model. For example, in the two-level model

. xtmixed fixed_portion [fw = wt1] || school: ... , fweight(wt2) ...

variable wt1 would hold the first-level (the observation-level) frequency weights, and wt2 would hold the second-level (the school-level) frequency weights.

pweight(exp) specifies sampling weights at higher levels in a multilevel model, whereas sampling weights at the first level (the observation level) are specified in the usual manner, for example, [pw=pwtvar1]. exp can be any valid Stata expression, and you can specify pweight() at each levels two and higher of a multilevel model. For example, in the two-level model

. xtmixed fixed_portion [pw = wt1] || school: ... , pweight(wt2) ...

variable wt1 would hold the first-level (the observation-level) sampling weights, and wt2 would hold the second-level (the school-level) sampling weights.

See Remarks on using sampling weights below for more information regarding the use of sampling weights in multilevel models.

Weighted estimation, whether frequency or sampling, is not supported under restricted maximum-likelihood estimation (REML).

mle and reml specify the statistical method for fitting the model.

mle, the default, specifies that the model be fit using maximum likelihood.

reml, specifies that the model be fit using restricted maximum likelihood (REML), also referred to as residual maximum likelihood.

pwscale(scale_method), where scale_method is

size|effective|gk

controls how sampling weights (if specified) are scaled in two-level models. See Survey data under Remarks in [XT] xtmixed for more details on using pwscale().

scale_method size specifies that first-level (observation-level) weights be scaled so that they sum to the sample size of their corresponding second-level cluster. Second-level sampling weights are left unchanged.

scale_method effective specifies that first-level weights be scaled so that they sum to the effective sample size of their corresponding second-level cluster. Second-level sampling weights are left unchanged.

scale_method gk specifies the Graubard and Korn (1996) method. Under this method, second-level weights are set to the cluster averages of the products of the weights at both levels, and first-level weights are then set equal to one.

pwcale() is supported only with two-level models.

residuals(rspec), where rspec is

restype [, residual_options]

specifies the structure of the residual errors within the lowest-level (the second level of a multilevel model with the observations comprising the first level) groups of the linear mixed model. For example, if you are modeling random effects for classes nested within schools, then residuals() refers to the residual variance-covariance structure of the observations within classes, the lowest-level groups.

restype is

independent|exchangeable|ar #|ma #|unstructured|banded #|toeplitz #|exponential

By default, restype is independent, which means that all residuals are i.i.d. Gaussian with one common variance. When combined with by(varname), independence is still assumed, but you estimate a distinct variance for each level of varname. Unlike with the structures described below, varname does not need to be constant within groups.

restype exchangeable estimates two parameters, one common within-group variance and one common pairwise covariance. When combined with by(varname), these two parameters are distinctly estimated for each level of varname. Because you are modeling a within-group covariance, varname must be constant within lowest-level groups.

restype ar # assumes that within-group errors have an autoregressive (AR) structure of order #; ar 1 is the default. The t(varname) option is required, where varname is an integer-valued time variable used to order the observations within groups and to determine the lags between successive observations. Any nonconsecutive time values will be treated as gaps. For this structure, # + 1 parameters are estimated (# AR coefficients and one overall error variance). restype ar may be combined with by(varname), but varname must be constant within groups.

restype ma # assumes that within-group errors have a moving average (MA) structure of order #; ma 1 is the default. The t(varname) option is required, where varname is an integer-valued time variable used to order the observations within groups and to determine the lags between successive observations. Any nonconsecutive time values will be treated as gaps. For this structure, # + 1 parameters are estimated (# MA coefficients and one overall error variance). restype ma may be combined with by(varname), but varname must be constant within groups.

restype unstructured is the most general structure; it estimates distinct variances for each within-group error and distinct covariances for each within-group error pair. The t(varname) option is required, where varname is a nonnegative-integer-valued variable that identifies the observations within each group. The groups may be unbalanced in that not all levels of t() need to be observed within every group, but you may not have repeated t() values within any particular group. When you have p levels of t(), then p*(p+1)/2 parameters are estimated. restype unstructured may be combined with by(varname), but varname must be constant within groups.

restype banded # is a special case of unstructured that restricts estimation to the covariances within the first # off-diagonals and sets the covariances outside this band to zero. The t(varname) option is required, where varname is a nonnegative-integer-valued variable that identifies the observations within each group. # is an integer between zero and p-1, where p is the number of levels of t(). By default, # is p-1; that is, all elements of the covariance matrix are estimated. When # is zero, only the diagonal elements of the covariance matrix are estimated. restype banded may be combined with by(varname), but varname must be constant within groups.

restype toeplitz # assumes that within-group errors have Toeplitz structure of order #, for which correlations are constant with respect to time lags less than or equal to # and are zero for lags greater than #. The t(varname) option is required, where varname is an integer-valued time variable used to order the observations within groups and to determine the lags between successive observations. # is an integer between one and the maximum observed lag (the default). Any nonconsecutive time values will be treated as gaps. For this structure, # + 1 parameters are estimated (# correlations and one overall error variance). restype toeplitz may be combined with by(varname), but varname must be constant within groups.

restype exponential is a generalization of the autoregressive (AR) covariance model that allows for unequally spaced and noninteger time values. The t(varname) option is required, where varname is real-valued. For the exponential covariance model, the correlation between two errors is the parameter rho, raised to a power equal to the absolute value of the difference between the t() values for those errors. For this structure, two parameters are estimated (the correlation parameter rho and one overall error variance). restype exponential may be combined with by(varname), but varname must be constant within groups.

residual_options are by(varname) and t(varname).

by(varname) is for use within the residuals() option and specifies that a set of distinct residual-error parameters be estimated for each level of varname. In other words, you use by() to model heteroskedasticity.

t(varname) is for use within the residuals() option to specify a time variable for the ar, ma, toeplitz, and exponential structures, or to ID the observations when restype is unstructured or banded.

+-----------+ ----+ SE/Robust +--------------------------------------------------------

vce(vcetype) specifies the type of standard error reported, which includes types that are robust to some kinds of misspecification and that allow for intragroup correlation; see [R] vce_option. vce(oim) is the default. If vce(robust) is specified, robust variances are clustered at the highest level in the multilevel model.

vce(robust) and vce(cluster clustvar) are not supported with REML estimation.

+-----------+ ----+ Reporting +--------------------------------------------------------

level(#); see [R] estimation options.

variance displays the random-effects and residual-error parameter estimates as variances and covariances. The default is to display them as standard deviations and correlations.

noretable suppresses the random-effects table from the output.

nofetable suppresses the fixed-effects table from the output.

estmetric displays all parameter estimates in the estimation metric. Fixed-effects estimates are unchanged from those normally displayed, but random-effects parameter estimates are displayed as log-standard deviations and hyperbolic arctangents of correlations, with equation names that organize them by model level. Residual-variance parameter estimates are also displayed in their original estimation metric.

noheader suppresses the output header, either at estimation or upon replay.

nogroup suppresses the display of group summary information (number of groups, average group size, minimum, and maximum) from the output header.

nostderr prevents xtmixed from calculating standard errors for the estimated random-effects parameters, although standard errors are still given for the fixed-effects parameters. Specifying this option will speed up computation times. nostderr is available only when residuals are modeled as independent with constant variance.

nolrtest prevents xtmixed from fitting a reference linear regression model and using this model to calculate a likelihood-ratio test comparing the mixed model to ordinary regression. This option may also be specified upon replay to suppress this test from the output.

display_options: noomitted, vsquish, noemptycells, baselevels, allbaselevels, cformat(%fmt), pformat(%fmt), sformat(%fmt), and nolstretch; see [R] estimation options.

+------------+ ----+ EM options +-------------------------------------------------------

These options control the EM (expectation-maximization) iterations that take place before estimation switches to a gradient-based method. When residuals are modeled as independent with constant variance, EM will either converge to the solution or bring parameter estimates close to the solution. For other residual structures or for weighted estimation, EM is used to obtain starting values.

emiterate(#) specifies the number of EM (expectation-maximization) iterations to perform. The default is emiterate(20).

emtolerance(#) specifies the convergence tolerance for the EM algorithm. The default is emtolerance(1e-10). EM iterations will be halted once the log (restricted) likelihood changes by a relative amount less than #. At that point, optimization switches to a gradient-based method, unless emonly is specified.

emonly specifies that the likelihood be maximized exclusively using EM. The advantage of specifying emonly is that EM iterations are typically much faster than those for gradient-based methods. The disadvantages are that EM iterations can be slow to converge (if at all) and that EM provides no facility for estimating standard errors for the random-effects parameters. emonly is available only with unweighted estimation and when residuals are modeled as independent with constant variance.

emlog specifies that the EM iteration log be shown. The EM iteration log is, by default, not displayed unless the emonly option is specified.

emdots specifies that the EM iterations be shown as dots. This option can be convenient because the EM algorithm may require many iterations to converge.

+--------------+ ----+ Maximization +-----------------------------------------------------

maximize_options: difficult, technique(algorithm_spec), iterate(#), [no]log, trace, gradient, showstep, hessian, showtolerance, tolerance(#), ltolerance(#), nrtolerance(#), and nonrtolerance; see [R] maximize. Those that require special mention for xtmixed are listed below.

For the technique() option, the default is technique(nr). The bhhh algorithm may not be specified.

matsqrt (the default), during optimization, parameterizes variance components by using the matrix square roots of the variance-covariance matrices formed by these components at each model level.

matlog, during optimization, parameterizes variance components by using the matrix logarithms of the variance-covariance matrices formed by these components at each model level.

The matsqrt parameterization ensures that variance-covariance matrices are positive semidefinite, while matlog ensures matrices that are positive definite. For most problems, the matrix square root is more stable near the boundary of the parameter space. However, if convergence is problematic, one option may be to try the alternate matlog parameterization. When convergence is not an issue, both parameterizations yield equivalent results.

The following option is available with xtmixed but is not shown in the dialog box:

coeflegend; see [R] estimation options.

Remarks

Remarks are presented under the following headings:

Remarks on specifying random-effects equations Remarks on using sampling weights

Remarks on specifying random-effects equations

Mixed models consist of fixed effects and random effects. The fixed effects are specified as regression parameters in a manner similar to most other Stata estimation commands, that is, as a dependent variable followed by a set of regressors. The random-effects portion of the model is specified by first considering the grouping structure of the data. For example, if random effects are to vary according to variable school, then the call to xtmixed would be of the form

. xtmixed fixed_portion || school: ... , options

The variable lists that make up each equation describe how the random effects enter into the model, either as random intercepts (constant term) or as random coefficients on regressors in the data. One may also specify the variance-covariance structure of the within-equation random effects, according to the four available structures described above. For example,

. xtmixed f_p || school: z1, covariance(unstructured) options

will fit a model with a random intercept and random slope for variable z1 and treat the variance-covariance structure of these two random effects as unstructured.

If the data are organized by a series of nested groups, for example, classes within schools, then the random-effects structure is specified by a series of equations, each separated by ||. The order of nesting proceeds from left to right. For our example, this would mean that an equation for schools would be specified first, followed by an equation for classes. As an example, consider

. xtmixed f_p || school: z1, cov(un) || class: z1 z2 z3, nocons cov(ex) options

where variables school and class identify the schools and classes within schools, respectively. This model contains a random intercept and random coefficient on z1 at the school level and has random coefficients on variables z1, z2, and z3 at the class level. The covariance structure for the random effects at the class level is exchangeable, meaning that the random effects share a common variance and common pairwise covariance.

Group variables may be repeated, allowing for more general covariance structures to be constructed as block-diagonal matrices based on the four original structures. Consider

. xtmixed f_p || school: z1 z2, nocons cov(id) || school: z3 z4, nocons cov(un) options

which specifies four random coefficients at the school level. The variance-covariance matrix of the random effects is the 4 x 4 matrix where the upper 2 x 2 diagonal block is a multiple of the identity matrix and the lower 2 x 2 diagonal block is unstructured. In effect, the coefficients on z1 and z2 are constrained to be independent and share a common variance. The coefficients on z3 and z4 each have a distinct variance and a variance distinct from that of the coefficients on z1 and z2. They are also allowed to be correlated, yet they are independent from the coefficients on z1 and z2.

For mixed models with no nested grouping structure, thinking of the entire estimation data as one group is convenient. Toward this end, xtmixed allows the special group designation _all. xtmixed also allows the factor variable notation R.varname, which is shorthand for describing the levels of varname as a series of indicator variables. See Random-effects factor notation and crossed-effects models in [XT] xtmixed for more details.

Remarks on using sampling weights

Sampling weights are treated differently in multilevel models than they are in standard models such as OLS regression. In a multilevel model, observation-level weights are not indicative of overall inclusion. Instead, they indicate inclusion conditional on the corresponding cluster being included at the next highest-level of sampling.

For example, if you include only observation-level weights in a two-level model, xtmixed will assume sampling with equal probabilities at level two, and this may or may not be what you intended. If the sampling at level two is weighted, then including only level-one weights can lead to biased results even if weighting at level two has been incorporated into the level-one weight variable. For example, it is a common practice to multiply conditional weights from multiple levels into one overall weight. By contrast, weighted multilevel analysis requires the component weights from each level of sampling.

Even if you specify sampling weights at all model levels, the scale of sampling weights at lower levels can affect your estimated parameters in a multilevel model. That is, not only do the relative sizes of the weights at lower levels matter, the scale of these weights matters also. To deal with this, xtmixed has the pwscale() option for rescaling weights in two-level models; see above for more information on pwscale(). Three scaling methods are offered, with each method known to perform well under certain data situations and posited models.

In general, exercise caution when using sampling weights with xtmixed; see Survey data in [XT] xtmixed for more information.

Examples

--------------------------------------------------------------------------- Setup . webuse nlswork

Random-intercept model, analogous to xtreg . xtmixed ln_w grade age c.age#c.age ttl_exp tenure c.tenure#c.tenure || id:

Random-intercept and random-slope (coefficient) model . xtmixed ln_w grade age c.age#c.age ttl_exp tenure c.tenure#c.tenure || id: tenure

Random-intercept and random-slope (coefficient) model, correlated random effects . xtmixed ln_w grade age c.age#c.age ttl_exp tenure c.tenure#c.tenure || id: tenure, cov(unstruct)

--------------------------------------------------------------------------- Setup . webuse pig

Two-level model . xtmixed weight week || id:

Two-level model with robust standard errors . xtmixed weight week || id:, vce(robust)

--------------------------------------------------------------------------- Setup . webuse productivity

Three-level nested model, observations nested within state nested within region, fit by maximum likelihood . xtmixed gsp private emp hwy water other unemp || region: || state:, mle

--------------------------------------------------------------------------- Setup . webuse pig

Two-way crossed random effects . xtmixed weight week || _all: R.id || _all: R.week

--------------------------------------------------------------------------- Setup . webuse ovary

Linear mixed model with MA 2 errors . xtmixed follicles sin1 cos1 || mare: sin1, residuals(ma 2, t(time))

--------------------------------------------------------------------------- Setup . webuse childweight

Linear mixed model with heteroskedastic error variances . xtmixed weight age || id:age, residuals(independent, by(girl))

---------------------------------------------------------------------------

Saved results

xtmixed saves the following in e():

Scalars e(N) number of observations e(k) number of parameters e(k_f) number of FE parameters e(k_r) number of RE parameters e(k_rs) number of standard deviations e(k_rc) number of correlations e(k_res) number of residual-error parameters e(N_clust) number of clusters e(nrgroups) number of residual-error by() groups e(ar_p) AR order of residual errors, if specified e(ma_q) MA order of residual errors, if specified e(res_order) order of residual-error structure, if appropriate e(df_m) model degrees of freedom e(ll) log (restricted) likelihood e(chi2) chi-squared e(p) p-value for chi-squared e(ll_c) log likelihood, comparison model e(chi2_c) chi-squared, comparison model e(df_c) degrees of freedom, comparison model e(p_c) p-value, comparison model e(rank) rank of e(V) e(rc) return code e(converged) 1 if converged, 0 otherwise

Macros e(cmd) xtmixed e(cmdline) command as typed e(depvar) name of dependent variable e(wtype) weight type (first-level weights) e(wexp) weight expression (first-level weights) e(fweightk) fweight expression for kth-highest level, if specified e(pweightk) pweight expression for kth-highest level, if specified e(ivars) grouping variables e(title) title in estimation output e(redim) random-effects dimensions e(vartypes) variance-structure types e(revars) random-effects covariates e(resopt) residuals() specification, as typed e(rstructure) residual-error structure e(rstructlab) residual-error structure output label e(rbyvar) residual-error by() variable, if specified e(rglabels) residual-error by() group labels e(pwscale) sampling-weight scaling method e(timevar) residual-error t() variable, if specified e(chi2type) Wald; type of model chi-squared test e(clustvar) name of cluster variable e(vce) vcetype specified in vce() e(vcetype) title used to label Std. Err. e(method) ML or REML e(opt) type of optimization e(optmetric) matsqrt or matlog; random-effects matrix parameterization e(emonly) emonly, if specified e(ml_method) type of ml method e(technique) maximization technique e(properties) b V e(estat_cmd) program used to implement estat e(predict) program used to implement predict e(asbalanced) factor variables fvset as asbalanced e(asobserved) factor variables fvset as asobserved

Matrices e(b) coefficient vector e(N_g) group counts e(g_min) group-size minimums e(g_avg) group-size averages e(g_max) group-size maximums e(tmap) ID mapping for unstructured residual errors e(V) variance-covariance matrix of the estimators e(V_modelbased) model-based variance

Functions e(sample) marks estimation sample

Reference

Graubard, B. I., and E. L. Korn. 1996. Modelling the sampling design in the analysis of health surveys. Statistical Methods in Medical Research 5: 263-281.


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