# Multi-level Model in Stata – basic syntax for ‘mixed’

Commands used – xtmixed (older versions, aslo used in Sophia Rabe-hesketh book) or mixed (Stata 15 and newer).

By default, xtmixed includes a random intercept

## Model Specification

• Outcome variable
• Fixed Effects part of the model-
• double pipe – ||
• Random part of the model
• Cluster identfier – example subject id within whom the observations are repeated (correlated residuals are expected within the same subject) – the random Intercept
• Colon
• Random slope variables
• comma, random effect options
• Random Effect Options – Specifed immediately after the RE variables
• covariance(vartype) variance-covariance structure of the random effects
• independent
• exchangeable
• identity
• unstructured
• noconstant – suppress constant term from the random-effects equation
• collinear – keep collinear variables

## Model Options

• Fit Model via maximal likelyghood estimation – mle
• Fit model via restricted maximum likelihood (REML) – reml
• To omit random intercept – noconstant
• Specify Structure of residual errors of lowest level of observation – residual(rspec) – independent, exchangeable, autoregressive ar #, moving-average ma #, unstructured banded #, toeplitz #, or exponential

## Example

``````# Random Intercept without covariates
xtmixed wm || id:, mle

# Random Intercept with covariates
xtmixed gcse lrt || school:, mle

# Random Intercept with covariates and Random-coefficient / random slope
xtmixed gcse lrt || school: lrt, covariance(unstructured) mle
xtmixed gcse lrt || school: lrt, covariance(unstructured) mle variance // to report variances

xtmixed lwage black hisp union married exper yeart ///
educt || nr: exper,  covariance(unstructured) mle
```Code language: Stata (stata)```

## Two levels – repeated observations within states

``````use http://www.stata-press.com/data/r15/productivity

mixed gsp private emp hwy water other unemp || state:```Code language: Stata (stata)```

## Three levels – repeated observations within states within regions

``mixed gsp private emp hwy water other unemp || region: || state:`Code language: Stata (stata)`

Note that the left most part of equation are the observations (LOWERMOST LEVEL), Then is the TOP level. Finally the intermediate level. : state nested within regions

There is no straight line order ! This example is straight from the STATA manual for mixed