Title

mlm_gof -- Goodness-of-fit test after mixed-effects logistic regression

Syntax

mlm_gof [, groups(#)]

groups(#) number of groups; default is data-driven method

mlm_gof requires that the estimation results currently in memory be from melogit.

Description

mlm_gof performs a goodness-of-fit test for binary multilevel logistic models fitted by melogit. It
extends the grouping-based test of Perera, Sooriyarachchi & Wickramasuriya (2016) and Fernando & Sooriyarachchi (2022)
to models with random coefficients (Linden 2026). The test works by dividing observations within each level-2 cluster
into G groups based on their conditional predicted probabilities, then testing whether group membership adds
explanatory power beyond the fitted model via a joint Wald test on G-1 indicator variables. Under a well-fitting
model, the group indicators should be uninformative and the Wald statistic should follow a chi-squared distribution
with G-1 degrees of freedom.

Options

groups(#) specifies the number of groups, G, into which observations are
divided within each level-2 cluster. The default data-driven choice G = min(10, min_cell_n),
where min_cell_n is the minimum number of observations across all level-2 clusters, ensures that every
level-2 cluster can be divided into G non-empty groups, preventing convergence
failures in the augmented model refit. This is equivalent to the fixed G = 10 used
by Perera et al. (2016) and Fernando & Sooriyarachchi (2022) when min_cell_n
>= 10, which covers all scenarios studied in those papers. When min_cell_n < 10,
G is reduced accordingly and a warning is issued. Users may specify a fixed value via
groups(#), subject to the constraint that it does not exceed min_cell_n.
Note that only level-2 cluster sizes are considered when computing min_cell_n;
higher-level units (level-3 clusters and above) are not checked because groups are
formed within level-2 clusters only, and empty-group problems cannot arise at higher
levels. See Linden (2026) for a comprehensive discussion.

Examples

Setup

. webuse bangladesh

Two-level random-intercept model

. melogit c_use i.urban age i.children || district:

. mlm_gof

Two-level random coefficient model with random slope on urban

. melogit c_use i.urban age i.children || district: i.urban, cov(unstructured)

. mlm_gof

Setup

. webuse towerlondon

Three-level nested model, subject nested within family

. melogit dtlm difficulty i.group || family: || subject:

. mlm_gof

Stored results

mlm_gof stores the following in r():

Scalars

r(chi2) Wald chi-squared statistic

r(df) degrees of freedom (= G-1)

r(p) p-value

r(N) number of observations

r(groups) effective number of groups G

r(levels) number of data levels detected

r(reterms) number of random-effect terms (intercepts + slopes)

References

Fernando, G. and R. Sooriyarachchi. 2022. The development of a
goodness-of-fit test for high level binary multilevel models.
Communications in Statistics - Simulation and Computation
51(5): 2710-2730.

Linden, A. 2026. A goodness-of-fit test for mixed-effects logistic regression.
Preprint. arXiv

Perera, A. A. P. N. M., M. R. Sooriyarachchi and S. L. Wickramasuriya.
2016. A goodness of fit test for the multilevel logistic model.
Communications in Statistics - Simulation and Computation
45(2): 643-659.

Citation of mlm_gof

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

Linden, Ariel. 2026. mlm_gof: Stata module for computing the goodness-of-fit
test after mixed-effects logistic regression. Statistical Software Components S459670,
Boston College Department of Economics.

Author

Ariel Linden

President, Linden Consulting Group, LLC

alinden@lindenconsulting.org

Also see

melogit, melogit postestimation