Tutorials › Generalized Linear Mixed Models (GLMM) › Fitting Binary GLMMs with Stata melogit

Software and Case Studies · Tutorial 673 of 698

Fitting Binary GLMMs with Stata melogit

You will specify a binary random-intercept model in Stata, map the output to its model components, and report a fixed-effect estimate with its uncertainty and context.

Intermediate 10 min read

What You'll Learn

  • Write a binary random-intercept model and its assumptions.
  • Use Stata factor-variable notation and the melogit command.
  • Identify fixed effects, uncertainty measures, and random-intercept variance in output.
  • Convert a treatment coefficient to a conditional odds ratio and confidence interval.
  • Check coding, convergence, and common random-intercept model problems.

Why Fit a Binary Random-Intercept Model?

A binary response, such as remission versus no remission, can be observed repeatedly within clinics, hospitals, or other groups. Outcomes from the same group may be more alike than outcomes from different groups. A random-intercept model represents this clustering while estimating how measured predictors relate to the outcome. This tutorial focuses on specifying and fitting that model with Stata’s melogit, then reading and reporting a fixed-effect estimate.

The model is appropriate when each analysis row represents one person with a binary outcome, the grouping variable identifies the cluster, and the scientific question concerns conditional associations given a cluster’s random intercept. It does not by itself correct a poorly defined analysis sample, incorrect outcome coding, or an unsuitable predictor specification.

Definition: For person \(i\) in clinic \(j\), let \(Y_{ij}\) be a binary outcome coded 1 for the event and 0 otherwise. A binary random-intercept GLMM specifies \(Y_{ij}\mid b_j\sim\operatorname{Bernoulli}(p_{ij})\), with $$ \operatorname{logit}(p_{ij})=\log\left(\frac{p_{ij}}{1-p_{ij}}\right) =\beta_0+\boldsymbol{x}_{ij}^{T}\boldsymbol{\beta}+b_j,\qquad b_j\sim N(0,\sigma_b^2). $$ Here \(\boldsymbol{x}_{ij}\) contains the predictors, \(\boldsymbol{\beta}\) their fixed-effect coefficients, and \(b_j\) the clinic-specific random intercept. The model assumes conditional independence of outcomes given the random intercept and included predictors, and a normal distribution for the clinic intercepts with common variance \(\sigma_b^2\).

The fixed effects describe covariate associations conditional on the clinic random intercept. The random intercept allows each clinic to have a different baseline log odds. Its normal distribution is an assumption about variation across clinics, not a claim that binary outcomes themselves are normally distributed.

From Model to Stata Command

In Stata, melogit fits a mixed-effects logistic regression. The general random-intercept form is melogit outcome predictors || cluster:. Each observation should have a valid 0/1 outcome and a cluster identifier. Use factor-variable notation for categorical predictors, such as i.treatment, and continuous-variable notation, such as c.age. Set or verify the reference category before interpreting a treatment coefficient; for a treatment coded 0 and 1, ib0.treatment explicitly declares 0 as the reference.

The model’s likelihood accounts for the shared random intercept within each cluster. In schematic form, it integrates the conditional Bernoulli likelihood over the distribution of clinic intercepts:

$$ L(\boldsymbol{\beta},\sigma_b^2) =\prod_j \int \left\{\prod_i p_{ij}(b)^{y_{ij}}[1-p_{ij}(b)]^{1-y_{ij}}\right\} \phi(b;0,\sigma_b^2)\,db. $$

Here \(\phi(b;0,\sigma_b^2)\) is the normal density for the random intercept. Because the integral is generally not available in a simple closed form, software uses numerical methods to estimate the model. The practical consequence is that a coefficient is not calculated by applying ordinary logistic regression separately to each clinic or by treating all records as independent.

Worked Example: Preparing the Patient-Level Command

Suppose a hypothetical dataset has one row per patient, a binary variable remission coded 1 for remission and 0 otherwise, a treatment variable coded 0 for standard care and 1 for a new program, a continuous baseline score, and a clinic identifier. The baseline score is measured in points.

The model with treatment and baseline score as fixed effects and a clinic random intercept is:

$$ \operatorname{logit}\{P(Y_{ij}=1\mid b_j)\} =\beta_0+\beta_1\operatorname{treatment}_{ij} +\beta_2\operatorname{score}_{ij}+b_j. $$

A direct Stata specification is melogit remission ib0.treatment c.score || clinic:. The outcome is on the left of the command, treatment is categorical with 0 as its reference, score is continuous, and the term after the double bar requests a random intercept for clinic. The trailing colon is part of Stata’s random-effects syntax.

Before fitting, check that the dataset contains one eligible patient per row, remission is coded only 0 or 1, treatment values and their meanings are correct, and each patient has a clinic identifier. For example, if a value of 2 is actually used to represent “no remission,” it must not be allowed to enter as a third outcome category; recode it according to the study’s coding rules first. A command cannot repair a mistaken definition of the variables.

Reading the Fixed-Effect Output

By default, the fixed-effect table reports estimates on the log-odds scale. Typical columns include the coefficient, its standard error, a z statistic, a p-value, and a confidence interval. The coefficient is the estimated change in conditional log odds for a one-unit predictor increase, holding the other fixed predictors constant and comparing observations at the same random-intercept value. For a binary predictor, it compares the coded category with its reference category.

A coefficient can be exponentiated to obtain a conditional odds ratio. For treatment, \(\exp(\beta_1)\) is the treatment-to-reference odds ratio at the same clinic random intercept and the same values of other covariates. Exponentiating the endpoints of a log-odds confidence interval gives the corresponding odds-ratio interval. Stata’s or option displays exponentiated fixed-effect estimates, but the default coefficient scale is often useful for checking calculations and matching a model equation.

Worked Example: Reporting a Clinic Treatment Estimate

Consider a clearly hypothetical analysis of 240 patients from 12 clinics. The outcome is remission (1 = yes, 0 = no); treatment is 1 for the new program and 0 for standard care; and baseline score is measured in points. The fitted command is melogit remission ib0.treatment c.score || clinic:. Suppose the relevant illustrative fixed-effect output is:

TermCoefficientSEzp-value95% confidence interval
New program vs standard care-0.5500.240-2.290.022-1.020 to -0.080

The coefficient is a conditional log-odds contrast. Its Wald statistic is the estimate divided by its standard error:

$$ z=\frac{-0.550}{0.240}=-2.29. $$

The two-sided p-value of 0.022 is below 0.05, and the reported 95% interval for the log-odds coefficient excludes 0. The corresponding conditional odds ratio is:

$$ \exp(-0.550)=0.577. $$

Exponentiating the interval endpoints gives \(\exp(-1.020)=0.361\) and \(\exp(-0.080)=0.923\). Thus the illustrative conditional odds ratio is 0.577, with a 95% confidence interval from 0.361 to 0.923. In context, the fitted model estimates that, at the same baseline score and clinic random-intercept value, the new-program group has 0.577 times the odds of remission of the standard-care group. The interval and p-value indicate evidence of a negative conditional treatment association in this hypothetical analysis; they do not establish a causal effect.

The fixed-effect interpretation is distinct from a comparison of overall or population-averaged probabilities. It conditions on the random intercept, and the odds ratio is not a probability ratio. In the output, also inspect the clinic random-intercept variance: it describes estimated between-clinic heterogeneity on the log-odds scale, not the treatment effect.

Understanding the Random-Effects Section

Stata’s output includes a random-effects section identifying the grouping level and the random-intercept variance, commonly labelled var(_cons) under the clinic grouping. If, for example, the estimated variance is 0.64, the estimated standard deviation is \(\sqrt{0.64}=0.80\) log-odds units. This describes the spread of clinic intercepts under the model. It is not a variance of the 0/1 patient outcomes and should not be reported as a fixed-effect coefficient.

A variance estimate near zero means the fitted model estimates little residual clinic-to-clinic variation after accounting for predictors, but it does not automatically prove that clustering is absent. Estimates can be uncertain, especially with few clusters. Conversely, a substantial variance suggests baseline odds differ across clinics under the model, but it does not identify why they differ.

Worked Example: Turning a Clinic Intercept into a Conditional Probability

Continue the hypothetical clinic analysis. Suppose the estimated intercept is \(-0.800\), the treatment coefficient is \(-0.550\), and the baseline-score coefficient is \(0.100\) per point. For a patient with score 0 in a clinic whose random intercept is \(b_j=0\), the fitted linear predictor under standard care is \(-0.800\), so the conditional probability of remission is:

$$ p=\frac{\exp(-0.800)}{1+\exp(-0.800)} =\frac{0.4493}{1+0.4493}=0.310. $$

For an otherwise identical patient in the new-program group, the linear predictor is \(-0.800-0.550=-1.350\), giving:

$$ p=\frac{\exp(-1.350)}{1+\exp(-1.350)} =\frac{0.2592}{1+0.2592}=0.206. $$

These are model-based conditional probabilities for the specified score and \(b_j=0\), not probabilities averaged over the distribution of clinics. The odds ratio remains \(\exp(-0.550)=0.577\); the difference between the two displayed probabilities is not itself the odds ratio.

Checks, Alternatives, and Common Failure Modes

A successful command is not sufficient evidence that the model is appropriate. Check convergence messages and whether estimates or standard errors are implausibly large. Review outcome frequencies by treatment and clinic, the number of clinics, missingness, and the distribution of continuous predictors. Complete or near-complete separation (a predictor pattern that nearly perfectly predicts the outcome), very sparse outcome information within clinics, or too few clusters can make estimates unstable or make the random-effect variance difficult to estimate. A boundary estimate near zero deserves consideration rather than automatic interpretation as a substantive finding.

The model assumes a linear predictor for continuous covariates on the log-odds scale, a common fixed treatment effect across clinics unless interactions or random slopes are included, and normally distributed random intercepts. Check whether these assumptions are scientifically defensible. A plot of observed outcomes across clusters can reveal sparse clusters or striking differences, but observed proportions alone do not diagnose the full conditional model. Investigate data errors and coding first if the output is unexpected.

An ordinary logistic regression without a clinic effect ignores the specified cluster structure and may understate uncertainty when within-clinic outcomes are correlated. A logistic model with cluster-robust standard errors changes the uncertainty calculation but does not estimate clinic-specific random intercepts. Generalized estimating equations target a population-averaged association rather than this model’s cluster-conditional association. These are alternatives for different assumptions or estimands, not interchangeable command choices. The earlier material on GLMM software and on fitting logistic mixed models with glmer addresses related software choices; here the focus is the Stata specification and its output.

Common mistake: “The treatment coefficient is \(-0.550\), so treatment reduces the probability of remission by 55 percentage points.” This is incorrect: \(-0.550\) is a conditional change in log odds, not a probability change. Exponentiating gives a conditional odds ratio of 0.577; probabilities depend on the other predictors and the clinic random intercept. Describe the contrast as an odds ratio, or calculate probabilities for explicitly stated covariate and random-intercept values.

A concise report should identify the outcome, the comparison, the model’s conditional interpretation, the odds ratio and interval (or the coefficient and interval on the log-odds scale), and the clustering level. Include the number of patients and clinics when relevant. State the reference category and units for continuous predictors. Avoid reporting a p-value alone: the effect estimate and uncertainty convey direction and plausible magnitude.

1
Verify the data.
Confirm the 0/1 outcome, predictor coding and reference levels, one-row-per-person structure, and cluster identifier.
2
Fit the specified model.
Use melogit with the fixed predictors and the random-intercept term after the double bar.
3
Inspect and report.
Check convergence and the random-effects estimate, then report the fixed-effect estimate on a clearly identified scale with its uncertainty and context.

Check Your Understanding

Answer each question, then compare your reasoning with the brief solutions below.

  1. For a binary patient outcome clustered within clinics, what does || clinic: request in a melogit command?
  2. If treatment is coded 0 for usual care and 1 for a new program, what does the treatment coefficient compare when 0 is the reference?
  3. A treatment coefficient is 0.30. What is the conditional odds ratio, to three significant figures?
  4. Why is a coefficient of \(-0.55\) not a 55-percentage-point decrease in outcome probability?
  5. Name two checks to make if melogit reports convergence problems or unexpectedly large standard errors.

Solutions. (1) It specifies a clinic-level random intercept. (2) It compares the new program with usual care, conditional on the random intercept and other fixed predictors. (3) \(\exp(0.30)=1.35\), so the conditional odds are estimated to be 1.35 times as large. (4) The coefficient is on the log-odds scale; probabilities also depend on the other predictors and random intercept. (5) Check outcome and predictor coding, sparse outcome patterns by clinic, the number of clinics, missingness, and whether estimates indicate separation or other instability.

Clinical Biostats