explain why patients within a clinic may not be independent;
distinguish fixed effects from random effects;
fit a random-intercept logistic model;
interpret the treatment effect and clinic variation;
explain why a mixed model may change standard errors and p-values.
This lesson uses exactly the same fictional study and simulation as the first lesson. The outcome is 30-day recovery, and 300 patients are nested in 10 clinics.
Recreate the shared medical data
For patient \(i\) in clinic \(j\), the simulation follows
Using the same random seed and the same equations as Lesson 1 produces exactly the same patients. The only change in this lesson is the model used to analyze them.
Why ordinary logistic regression may be insufficient
Patients in one clinic share an environment. Even after accounting for treatment and age, two patients from the same clinic may be more alike than two patients from different clinics. Ordinary logistic regression assumes the remaining observations are independent, so its uncertainty calculation can be misleading when outcomes are clustered.
Compare recovery proportions across clinics:
For clinic \(j\), with \(n_j\) patients, the observed clinic recovery proportion is
Some variation is sampling noise, and some may reflect real clinic-to-clinic differences. A mixed-effects model estimates the amount of extra variation.
glmer() numerically approximates these integrals while estimating the fixed effects and clinic variance. The ordinary glm() is also fitted below so both models use the identical outcome and predictors.
Generalized linear mixed model fit by maximum likelihood (Laplace
Approximation) [glmerMod]
Family: binomial ( logit )
Formula: recovered ~ treatment + age_centered + (1 | clinic)
Data: medical
AIC BIC logLik -2*log(L) df.resid
376.6 391.4 -184.3 368.6 296
Scaled residuals:
Min 1Q Median 3Q Max
-1.3629 -0.7439 -0.5103 0.9906 2.7792
Random effects:
Groups Name Variance Std.Dev.
clinic (Intercept) 0.179 0.4231
Number of obs: 300, groups: clinic, 10
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.91117 0.23550 -3.869 0.000109 ***
treatment 0.59788 0.26507 2.256 0.024096 *
age_centered -0.04999 0.01233 -4.053 5.05e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Correlation of Fixed Effects:
(Intr) trtmnt
treatment -0.616
age_centerd 0.103 -0.088
The notation (1 | clinic) means: allow every clinic to have its own intercept, while estimating those clinic intercepts as draws from one shared distribution.
Interpret the fixed effects
As before, exponentiate fixed-effect coefficients to obtain odds ratios.
Within a clinic, the random intercept cancels from the treatment odds ratio:
The treatment OR is a clinic-specific, or conditional, odds ratio: it compares two otherwise similar patients in the same clinic. As in Lesson 1, the p-value tests a no-association null hypothesis; it does not measure effect size, clinical importance, or the probability that the null is true. Interpret it with the OR, confidence interval, design, and clinical threshold.
The Wald intervals above are convenient teaching approximations. For important research, consider likelihood-profile or bootstrap intervals and a pre-specified analysis plan.
Interpret the random effect
The fitted model estimates the clinic variance \(\widehat\sigma_{clinic}^2\). Its square root returns variation to the log-odds scale:
The clinic SD is measured on the log-odds scale. A value near zero suggests little residual clinic-to-clinic variation after adjustment. A larger value suggests that baseline recovery odds differ meaningfully among clinics.
For logistic mixed models, a commonly used latent-scale intraclass correlation coefficient (ICC) is
The ICC approximates the proportion of latent-scale variation attributable to clinics. It is not literally the observed correlation between two binary outcomes, so state the scale and approximation when reporting it.
Clinic estimates and partial pooling
The estimated clinic deviations are:
For clinic \(j\), ranef() returns the conditional mode
The normal-density term penalizes extreme clinic effects. This is the mathematical source of partial pooling: estimates with weak clinic-level information are pulled more strongly toward \(0\).
Mixed models use partial pooling. A clinic’s estimate is informed by its own patients but pulled toward the overall mean, especially when the clinic has little information. This is usually more stable than fitting an entirely separate model for every clinic.
Compare ordinary and mixed models
To compare like with like, define for each fitted model \(m\)
The two estimates need not be identical. More importantly, the mixed model’s standard error acknowledges the grouping structure. Clustering often reduces the effective amount of independent information, although the exact change depends on the data and model. A p-value crossing 0.05 after accounting for clustering does not mean the effect disappeared; it means the evidence and its uncertainty were evaluated under a different, more suitable model.
Do not choose between these models based on which produces the smaller p-value. Choose based on the study design and how the observations were generated.
Population-average and clinic-specific predictions
A mixed model supports different prediction questions:
Typical-clinic prediction: set the clinic random effect to zero. This is conditional on a clinic with average random intercept.
Known-clinic prediction: include that clinic’s estimated random effect.
Here are typical-clinic probabilities for a 60-year-old:
Setting the clinic deviation to its population mean, \(u_j=0\), gives
In predict(), re.form = NA excludes all fitted random effects and implements this equation. Setting type = "response" returns probability rather than log-odds.
group predicted_recovery_typical_clinic
1 Standard care 0.2867613
2 New treatment 0.4223130
These are not fully marginal, population-averaged probabilities: simply setting \(u_j=0\) does not integrate over the distribution of clinics. That distinction matters when selecting a prediction method for a clinical report.
When should you use a mixed-effects model?
A mixed model is useful when observations are naturally grouped or repeated, for example:
patients within clinics or hospitals;
repeated visits within patients;
students within schools;
measurements made by different clinicians or devices.
The grouping variable should represent a meaningful source of dependence. With very few groups, random-effect variance and small-sample inference can be unstable; alternatives may include clinic fixed effects, cluster-robust methods, or small-sample corrections, depending on the research question and design.
Our random-intercept model assumes the treatment effect is the same across clinics. If the treatment effect plausibly varies by clinic and enough clinics and data are available, a random-slope model may be appropriate:
Here \(u_{0j}\) changes clinic \(j\)’s baseline, while \(u_{1j}\) changes its treatment effect. The R notation (1 + treatment | clinic) estimates both variation terms and their correlation.
That is a more demanding model and should be motivated by the design, not added only because the syntax is available.
Reporting template
We fitted a mixed-effects logistic regression with fixed effects for treatment and age and a random intercept for clinic. The adjusted clinic-specific treatment odds ratio was [OR] (95% CI [lower, upper], Wald p = [p]). The estimated between-clinic random-intercept SD was [SD], corresponding to a latent-scale ICC of [ICC]. Effect magnitude, precision, clinical importance, and study limitations were considered together.
The reporting cell collects the mathematical summaries already derived: