Multilevel models

Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.

estat

Random-intercept intraclass correlation

Stata

estat icc

R Package:lme4

{ v <- lme4::VarCorr(fit); between <- as.numeric(v$id[1, 1]); between / (between + stats::sigma(fit)^2) }

Differs from Stata Fixed workflow; adapt the named data columns explicitly. ICC is constant only for a random-intercept Gaussian model; random slopes make it covariate-dependent.

2 more examples and alternatives

Random-effects covariance matrix

Stata

estat recovariance

R Package:lme4

as.matrix(lme4::VarCorr(fit)$id)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Estimated covariance conditional on the chosen random-effects structure.

Variance components as standard deviations and correlations

Stata

estat sd

R Package:lme4

as.data.frame(lme4::VarCorr(fit))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Includes residual standard deviation and random-effect correlation.

mecloglog

Complementary-log-log random-intercept model with Laplace approximation

Stata

mecloglog binary x || id:, intpoints(1)

R Package:lme4

lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(link = "cloglog"), nAGQ = 1, control = lme4::glmerControl(optimizer = "bobyqa"))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Stata’s integration settings must be matched; one Laplace point differs from adaptive quadrature.

Complementary-log-log random-intercept model with adaptive quadrature

Stata

mecloglog binary x || id:, intpoints(9)

R Package:lme4

lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(link = "cloglog"), nAGQ = 9, control = lme4::glmerControl(optimizer = "bobyqa"))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Stata’s integration settings must be matched; one Laplace point differs from adaptive quadrature.

meglm

Generalized mixed model with cloglog link

Stata

meglm binary x || id:, family(binomial) link(cloglog)

R Package:lme4

lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(link = "cloglog"), nAGQ = 7)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Explicit family/link choice; defaults and integration accuracy differ.

Generalized mixed model with logit link

Stata

meglm binary x || id:, family(binomial) link(logit)

R Package:lme4

lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(link = "logit"), nAGQ = 7)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Explicit family/link choice; defaults and integration accuracy differ.

Generalized mixed model with probit link

Stata

meglm binary x || id:, family(binomial) link(probit)

R Package:lme4

lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(link = "probit"), nAGQ = 7)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Explicit family/link choice; defaults and integration accuracy differ.

melogit

Logistic random-intercept model with Laplace approximation

Stata

melogit binary x || id:, intpoints(1)

R Package:lme4

lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(link = "logit"), nAGQ = 1, control = lme4::glmerControl(optimizer = "bobyqa"))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Stata’s integration settings must be matched; one Laplace point differs from adaptive quadrature.

1 more examples and alternatives

Logistic random-intercept model with adaptive quadrature

Stata

melogit binary x || id:, intpoints(9)

R Package:lme4

lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(link = "logit"), nAGQ = 9, control = lme4::glmerControl(optimizer = "bobyqa"))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Stata’s integration settings must be matched; one Laplace point differs from adaptive quadrature.

menbreg

Negative-binomial mixed model with quadratic mean variance

Stata

menbreg nb x || id:

R Package:glmmTMB

glmmTMB::glmmTMB(nb ~ x + (1 | id), data = data, family = glmmTMB::nbinom2())

Differs from Stata glmmTMB uses Laplace integration; Stata menbreg defaults to intpoints(7) adaptive quadrature and needs intpoints(1) for a comparable integration approximation. Match NB2 dispersion versus its inverse before comparing ancillary parameters; identical integration does not itself guarantee estimator parity.

menl

Nonlinear exponential growth with random asymptote coefficient

Stata

menl y = {A}*exp({B}*x), define(A: {a0}+{U[id]})

R Package:nlme

nlme::nlme(y ~ A * exp(B * x), data = data, fixed = A + B ~ 1, random = A ~ 1 | id, start = c(A = 2, B = .25))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Nonlinear mixed-model workflow; Stata menl syntax/parameter definitions must match the desired model.

meologit

Random-intercept cumulative-logit model

Stata

meologit ordinal x || id:

R Package:ordinal

ordinal::clmm(ordinal ~ x + (1 | id), data = data, link = "logit", nAGQ = 7)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Threshold signs and quadrature defaults must be aligned.

meoprobit

Random-intercept cumulative-probit model

Stata

meoprobit ordinal x || id:

R Package:ordinal

ordinal::clmm(ordinal ~ x + (1 | id), data = data, link = "probit", nAGQ = 7)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Latent residual variance is fixed by the probit link.

mepoisson

Poisson random-intercept model with Laplace approximation

Stata

mepoisson count x || id:, intpoints(1)

R Package:lme4

lme4::glmer(count ~ x + (1 | id), data = data, family = stats::poisson(link = "log"), nAGQ = 1, control = lme4::glmerControl(optimizer = "bobyqa"))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Stata’s integration settings must be matched; one Laplace point differs from adaptive quadrature.

Poisson random-intercept model with adaptive quadrature

Stata

mepoisson count x || id:, intpoints(9)

R Package:lme4

lme4::glmer(count ~ x + (1 | id), data = data, family = stats::poisson(link = "log"), nAGQ = 9, control = lme4::glmerControl(optimizer = "bobyqa"))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Stata’s integration settings must be matched; one Laplace point differs from adaptive quadrature.

meprobit

Probit random-intercept model with Laplace approximation

Stata

meprobit binary x || id:, intpoints(1)

R Package:lme4

lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(link = "probit"), nAGQ = 1, control = lme4::glmerControl(optimizer = "bobyqa"))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Stata’s integration settings must be matched; one Laplace point differs from adaptive quadrature.

Probit random-intercept model with adaptive quadrature

Stata

meprobit binary x || id:, intpoints(9)

R Package:lme4

lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(link = "probit"), nAGQ = 9, control = lme4::glmerControl(optimizer = "bobyqa"))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Stata’s integration settings must be matched; one Laplace point differs from adaptive quadrature.

mixed

Quadratic fixed trend with random intercept

Stata

mixed y c.x##c.x || id:

R Package:lme4

lme4::lmer(y ~ x + I(x^2) + (1 | id), data = data, REML = FALSE)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. R default REML differs from Stata mixed default ML; explicitly select the target likelihood.

4 more examples and alternatives

REML random-intercept model

Stata

mixed y x || id:

R Package:lme4

lme4::lmer(y ~ x + (1 | id), data = data, REML = FALSE)

Differs from Stata REML=FALSE explicitly matches Stata mixed default ML. lme4 does not print Stata large-sample fixed-effect z tests by default; match covariance structures and request the intended inference separately.

Independent random intercept and slope

Stata

mixed y x || id: x, covariance(independent)

R Package:lme4

lme4::lmer(y ~ x + (x || id), data = data, REML = FALSE)

Differs from Stata REML=FALSE explicitly matches Stata mixed default ML. lme4 does not print Stata large-sample fixed-effect z tests by default; match covariance structures and request the intended inference separately.

Correlated random intercept and slope

Stata

mixed y x || id: x, covariance(unstructured)

R Package:lme4

lme4::lmer(y ~ x + (x | id), data = data, REML = FALSE)

Differs from Stata REML=FALSE explicitly matches Stata mixed default ML. lme4 does not print Stata large-sample fixed-effect z tests by default; match covariance structures and request the intended inference separately.

ML random-intercept model

Stata

mixed y x || id:, mle

R Package:lme4

lme4::lmer(y ~ x + (1 | id), data = data, REML = FALSE)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. R default REML differs from Stata mixed default ML; explicitly select the target likelihood.

predict

Conditional fitted values including random effects

Stata

predict fitted, fitted

R Package:lme4

stats::predict(fit)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Prediction level is explicit; population and cluster-specific predictions have different interpretations.

3 more examples and alternatives

Conditional mixed-model residuals

Stata

predict residual, residuals

R Package:lme4

stats::residuals(fit)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Prediction level is explicit; population and cluster-specific predictions have different interpretations.

Empirical Bayes random intercepts and slopes

Stata

predict u0 u1, reffects

R Package:lme4

lme4::ranef(fit)$id

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Prediction level is explicit; population and cluster-specific predictions have different interpretations.

Fixed-effects-only predictions

Stata

predict xb, xb

R Package:lme4

stats::predict(fit, re.form = NA)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Prediction level is explicit; population and cluster-specific predictions have different interpretations.

quadchk

Sensitivity of mixed-logit estimates to quadrature points

Stata

quadchk

R Package:lme4

{ fits <- lapply(c(5, 9, 15), function(q) lme4::glmer(binary ~ x + (1 | id), data = data, family = stats::binomial(), nAGQ = q)); data.frame(points = c(5, 9, 15), logLik = vapply(fits, function(f) as.numeric(stats::logLik(f)), numeric(1)), slope = vapply(fits, function(f) unname(lme4::fixef(f)[2]), numeric(1))) }

Fixed workflow; adapt the named data columns explicitly. Manual quadrature sensitivity check; not an automatic convergence guarantee.

xtologit

Random-intercept cumulative-logit model

Stata

xtologit ordinal x

R Package:ordinal

ordinal::clmm(ordinal ~ x + (1 | id), data = data, link = "logit", nAGQ = 12)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Threshold signs and quadrature defaults must be aligned.

xtoprobit

Random-intercept cumulative-probit model

Stata

xtoprobit ordinal x

R Package:ordinal

ordinal::clmm(ordinal ~ x + (1 | id), data = data, link = "probit", nAGQ = 12)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Latent residual variance is fixed by the probit link.