Generalized Linear Mixed Model
The engine for Non-Normal Hierarchical Discovery. GLMM audits nested data with categorical or count outcomes (e.g., binary success/failure), providing a robust multi-level path for complex biological and social data.
What is it?
Generalized Linear Mixed Model is designed to analyze clustered, longitudinal, or repeated measures data by modeling both population trends and correlation structures.
The engine for Non-Normal Hierarchical Discovery. GLMM audits nested data with categorical or count outcomes (e.g., binary success/failure), providing a robust multi-level path for complex biological and social data.
Goals & Indications
- Hierarchical Probability Audit: Determine the likelihood of success while accounting for clustering in non-normal data.
- Categorical Clustering Neutralization: Correct for the correlation of counts or categories within subjects or sites.
- Non-Linear Trajectory Mapping: Audit the recovery paths of binary or discrete outcomes over time in a multi-level field.
Core Idea Diagram
Hypotheses
How it works
- Choose a link function matching the outcome type (e.g. logit for binary).
- Specify fixed covariates and cluster-level random effects.
- Estimate parameters using numerical integration (e.g. Adaptive Gauss-Hermite Quadrature).
- Construct odds ratios or rate ratios adjusted for subject nesting.
Assumptions
Important Note
GLMMs partition variance into fixed effects (population-average effects) and random effects (subject/cluster-specific deviations). Unlike GEE, GLMMs model the data-generating process and allow subject-specific inference. The null hypothesis tests fixed effect parameters while accounting for random effect correlation structure.
Worked Example
| Parameter | Odds Ratio | 95% CI |
|---|---|---|
| Treatment (Fixed) | 2.45 | [1.54, 3.89] |
| Random Intercept SD | 0.84 | Clinic Cluster |
Generalized Linear Mixed Model (Logistic)
Adjust log-odds slope and random intercept variances. Observe how groups (represented by clinics) separate horizontally along their sigmoids.
Hypotheses
Pragmatic null and alternative hypotheses defined in mathematical notation.
H₀: β₁ = 0 (no fixed effect of predictor on outcome, controlling for random effects)
Hₐ: β₁ ≠ 0 (predictor has a fixed effect on outcome)
GLMMs partition variance into fixed effects (population-average effects) and random effects (subject/cluster-specific deviations). Unlike GEE, GLMMs model the data-generating process and allow subject-specific inference. The null hypothesis tests fixed effect parameters while accounting for random effect correlation structure.
Assumptions
The core mathematical criteria needed to ensure that statistical testing remains unbiased and valid.
Diagnostics
Checking residual plots and indices to examine model deviations and ensure standard error integrity.
- Check random effects normality (Q-Q plot of BLUPs)
- Residual plots (Pearson or deviance residuals vs fitted values) to check for patterns
- DHARMa quantile residuals for distributional assumptions (R package)
- Variance inflation factors (VIF) for multicollinearity
- Convergence checks (warnings, Hessian positive definite)
- Overdispersion check for Poisson models (compare variance to mean)
- Caterpillar plots of random effects to identify outlier clusters
- Influence diagnostics (Cook's D, DFBETAS at cluster level)
- Plot observed vs predicted values by cluster
- Check intraclass correlation (ICC) to quantify clustering
- Likelihood ratio tests for random effects significance
- Compare multiple link functions and families using AIC/BIC
- Sensitivity analysis: results with/without outlier clusters
Applied Minds
Review concrete study examples, data layout guidelines, and copy executable syntax scripts.
Insomnia Treatment Response (Binary Outcome, Repeated Measures)
Research question: Does cognitive behavioral therapy for insomnia (CBT-I) improve sleep onset latency remission (SOL ≤ 30 min) compared to sleep education control over 8 weeks? Design: RCT with 80 patients (40 per group), assessed at baseline, weeks 2, 4, 6, 8 (5 timepoints). Outcome: Binary (1 = remission, 0 = insomnia). Random intercept for subjects accounts for within-subject correlation.
# GLMM: Binary outcome (insomnia remission) with repeated measures
# Random intercept model with binomial family (logit link)
library(lme4) # For glmer()
library(DHARMa) # For residual diagnostics
library(effects) # For plotting effects
library(emmeans) # For marginal means and contrasts
library(performance) # For ICC
library(ggplot2)
set.seed(2025)
# Simulate realistic data
n_subjects <- 80
n_timepoints <- 5
subject_id <- rep(1:n_subjects, each=n_timepoints)
treatment <- rep(rep(c("Control", "CBT-I"), each=40), each=n_timepoints)
week <- rep(c(0, 2, 4, 6, 8), times=n_subjects)
# Generate random intercepts (subject-specific baseline log-odds)
subject_intercepts <- rnorm(n_subjects, mean=0, sd=0.8)
subject_intercepts_expanded <- rep(subject_intercepts, each=n_timepoints)
# Fixed effects: baseline log-odds, time effect, treatment effect, interaction
logodds <- -1.5 + # Baseline (control at week 0): ~18% remission
0.15 * week + # Time effect in control
0.5 * (treatment == "CBT-I") + # Treatment main effect
0.10 * week * (treatment == "CBT-I") + # Treatment × time interaction
subject_intercepts_expanded # Random intercept
prob <- plogis(logodds) # Convert to probability
remission <- rbinom(length(prob), size=1, prob=prob)
data <- data.frame(
subject_id = factor(subject_id),
treatment = factor(treatment, levels=c("Control", "CBT-I")),
week = week,
remission = remission
)
# === STEP 1: Descriptive Statistics ===
table(data$remission, data$treatment)
prop.table(table(data$remission, data$treatment), margin=2)
# Remission rate by treatment over time
library(dplyr)
data %>%
group_by(treatment, week) %>%
summarise(n = n(),
remission_rate = mean(remission),
SE = sqrt(remission_rate * (1-remission_rate) / n))
# === STEP 2: Fit GLMM ===
# Random intercept for subject, binomial family with logit link
model <- glmer(remission ~ week * treatment + (1 | subject_id),
data = data,
family = binomial(link = "logit"),
control = glmerControl(optimizer = "bobyqa"))
summary(model)
# Output:
# Fixed effects:
# Estimate Std. Error z value Pr(>|z|)
# (Intercept) -1.52 0.25 -6.08 <.001 ***
# week 0.15 0.03 5.00 <.001 ***
# treatmentCBT-I 0.48 0.35 1.37 .171
# week:treatmentCBT-I 0.10 0.04 2.50 .012 *
#
# Random effects:
# Groups Name Variance
# subject_id (Intercept) 0.64
# === STEP 3: Diagnostics ===
# 3a. Convergence check
if(is.null(model@optinfo$conv$lme4$messages)) {
cat("Model converged successfully\n")
} else {
print(model@optinfo$conv$lme4$messages)
}
# 3b. Check random effects normality (BLUPs)
rand_effects <- ranef(model)$subject_id[[1]]
qqnorm(rand_effects, main="Q-Q Plot: Random Intercepts")
qqline(rand_effects)
shapiro.test(rand_effects) # p > .05 indicates normality
# 3c. DHARMa residuals (simulated quantile residuals)
simulated_residuals <- simulateResiduals(fittedModel = model, n = 1000)
plot(simulated_residuals) # Should show uniform distribution
testDispersion(simulated_residuals) # Check for over/underdispersion
# 3d. Intraclass correlation (ICC)
icc(model) # Proportion of variance due to subjects
# ICC = 0.64 / (0.64 + π²/3) = 0.16 (16% of variance due to subjects)
# 3e. Influential clusters (caterpillar plot)
library(lattice)
dotplot(ranef(model, condVar=TRUE)) # Look for outliers
# === STEP 4: Fixed Effects Tests ===
library(car)
Anova(model, type="III") # Wald tests
# === STEP 5: Marginal Means and Contrasts ===
# Predicted remission rates at week 8
emm <- emmeans(model, ~ treatment | week, at=list(week=8), type="response")
summary(emm)
# Control at week 8: 44% remission (95% CI [35%, 53%])
# CBT-I at week 8: 72% remission (95% CI [63%, 80%])
# Odds ratio for treatment effect at week 8
contrast_emm <- contrast(emmeans(model, ~ treatment | week, at=list(week=8)),
method="pairwise")
summary(contrast_emm, type="response") # OR = 3.2, 95% CI [1.5, 6.8]
# === STEP 6: Visualize Results ===
# Plot 1: Predicted probabilities over time
newdata <- expand.grid(
week = seq(0, 8, by=0.5),
treatment = c("Control", "CBT-I")
)
newdata$pred_prob <- predict(model, newdata=newdata, re.form=NA, type="response")
ggplot(newdata, aes(x=week, y=pred_prob, color=treatment)) +
geom_line(size=1.2) +
geom_point(data=data %>% group_by(treatment, week) %>%
summarise(obs_prob = mean(remission)),
aes(y=obs_prob), size=3, alpha=0.6) +
labs(title="Insomnia Remission Over Time by Treatment",
x="Week", y="Predicted Probability of Remission",
color="Treatment") +
scale_y_continuous(limits=c(0,1), labels=scales::percent) +
theme_minimal()
# Plot 2: Subject-specific trajectories (random effects)
subj_preds <- data.frame(
subject_id = data$subject_id,
week = data$week,
treatment = data$treatment,
pred_prob_subj = predict(model, type="response") # Subject-specific
)
ggplot(subj_preds[subj_preds$subject_id %in% sample(1:80, 20), ],
aes(x=week, y=pred_prob_subj, group=subject_id, color=treatment)) +
geom_line(alpha=0.5) +
facet_wrap(~treatment) +
labs(title="Subject-Specific Remission Trajectories(20 random subjects)",
x="Week", y="Predicted Probability") +
theme_minimal()
# === STEP 7: APA-Style Reporting ===
cat("
=== APA-Style Report ===
A generalized linear mixed model(GLMM) with binomial family(logit link)
was used to analyze insomnia remission over 8 weeks, accounting for repeated
measures with random intercepts for subjects. The model included fixed effects
of time(week), treatment(CBT-I vs control), and their interaction.
Results showed a significant time × treatment interaction(z = 2.50, p = .012),
indicating that CBT-I produced greater improvement over time than control.
At week 8, CBT-I patients had 72% remission rate(95% CI [63%, 80%]) compared
to 44% in controls(95% CI [35%, 53%]). The odds ratio for CBT-I vs control at
week 8 was 3.2 (95% CI [1.5, 6.8]), indicating CBT-I patients had 3.2 times
higher odds of remission. The intraclass correlation was .16, indicating 16%
of variance in remission was due to between-subject differences.
These findings support CBT-I as an effective treatment for insomnia, with
clinically meaningful improvements emerging by week 8.
")The time × treatment interaction (z = 2.50, p = .012) indicates CBT-I produces significantly greater improvement in insomnia remission compared to control. At week 8, CBT-I showed 72% remission vs 44% in controls (OR = 3.2, 95% CI [1.5, 6.8]). ICC = .16 indicates moderate within-subject correlation, justifying mixed model approach. Results align with Edinger et al. (2001) showing CBT-I superiority for insomnia treatment.
Alternatives
Structured fallback pathways for choosing alternative tests when normality or slopes requirements fail.
- Laplace Approximation Audit — Increase the number of 'Adaptive Gauss-Hermite Quadrature' points to stabilize the link.
- glmmTMB Switch — Pivot to the Template Model Builder framework for high-speed, stable non-normal convergence.
- Mixed-NB Model — Use Negative Binomial links within the hierarchy to neutralize extra-Poisson variance.
- Observation-Level Random Effects — Add a unique intercept per data point to soak up dispersion noise.
Post-hoc
Group mean comparisons and correction controls (e.g. Tukey HSD, Bonferroni) to protect against Family-Wise Error Rates.
Post-hoc pairwise tests defined for this model.
All comparisons on link scale (log-odds, log-rate), then exponentiate for OR/RR
Effect Size
Understanding effect sizes (e.g., Cohen's d, Partial Eta-Squared) and clinical impact benchmarks.
OR = 1.5 means 50% higher odds in treatment vs control. OR = 0.67 means 33% lower odds. OR = 1 means no effect. Cohen's d can be approximated: d ≈ log(OR) × √3/π ≈ 0.55 × log(OR)
RR = 1.3 means 30% higher rate in treatment. RR = 0.7 means 30% lower rate. RR = 1 means no effect. More interpretable than OR for common outcomes
ICC = .05 (small, 5% variance due to clusters), .10 (medium, 10%), .20+ (large, 20%+). Justifies need for mixed model. ICC = random_var / (random_var + residual_var)
Proportion of variance explained by fixed effects only (population-average). Small: .02, Medium: .13, Large: .26
Proportion of variance explained by fixed + random effects (total model). Always ≥ Marginal R²
0.2
0.5
0.8
Sample Size
Guidelines for minimum sample requirements and power analysis parameters.
The 'Non-Normal 30/30' Mandate: A minimum of 30 Level-2 clusters is essential. GLMMs (Logistic/Poisson) are even more data-hungry than LMMs; the non-linear link requires massive data to stabilize random effect variance.
| Effect Size | Parameters | Required n |
|---|---|---|
| Small Effect | Odds Ratio = 1.5 (Small) | n ≈ 60 clusters |
| Medium Effect | Odds Ratio = 2.5 (Medium) | n ≈ 30 clusters |
| Large Effect | Odds Ratio = 4.0 (Large) | n ≈ 15 clusters |
The 'Link-Expansion' Penalty: Every level of complexity (e.g., random slopes for multiple variables) increases the risk of 'Non-Convergence'. Only add random effects that survive the LRT strike to preserve your N-efficiency.
Reporting
How to compile statistical results into publication prose matching APA and journal style guides.
A generalized linear mixed model with family and link, e.g., 'binomial family (logit link)' or 'Poisson family (log link)' was used to analyze outcome variable. The model included list fixed effects as fixed effects and random intercepts/slopes for subject/cluster as random effects to account for repeated measures/clustering structure. If applicable: Model selection: 'Negative binomial GLMM was chosen over Poisson due to significant overdispersion (dispersion ratio = X.X, p < .001).' OR 'Random slopes were retained based on likelihood ratio test (χ² = X.X, df = X, p = .XX).' Results showed significant/non-significant effect, z = X.XX, p = .XXX. For binomial: The odds ratio was OR = X.XX (95% CI X.XX, X.XX), indicating interpretation in context. For counts: The rate ratio was RR = X.XX (95% CI X.XX, X.XX), indicating interpretation. The intraclass correlation was ICC = .XX, indicating X% of variance was due to subject/cluster. Model diagnostics: DHARMa residuals indicated adequate/inadequate model fit. Conclude with substantive interpretation in research context.
- Model specification (family, link, random effects structure)
- Fixed effects estimates (β), standard errors, z-values, p-values
- Exponentiated coefficients (OR or RR) with 95% CIs
- Random effects variance components
- ICC (intraclass correlation)
- Model fit: AIC, BIC, or likelihood ratio tests
- Diagnostics: convergence, overdispersion check, residual plots
- Sample size: n observations, k clusters, observations per cluster
Manuscript Lab
Copy standard summary tables and forensic reporting grids to outline analysis details.
| Fixed Effect | B (Log-Odds) | SE | z | p | OR | 95% CI (OR) |
|---|---|---|---|---|---|---|
| (Intercept) | -0.85 | 0.22 | -3.86 | < .001 | 0.43 | [0.28, 0.66] |
| Time (Months) | 0.42 | 0.08 | 5.25 | < .001 | 1.52 | [1.30, 1.78] |
| Treatment (Active) | 1.12 | 0.30 | 3.73 | < .001 | 3.06 | [1.70, 5.52] |
| Time × Treatment | 0.35 | 0.12 | 2.92 | .003 | 1.42 | [1.12, 1.80] |
The Success Multiplier. OR = 1.52 for Time means the odds of success increase by 52% each month for the average subject.
Baseline Heterogeneity. Measures how much subjects differ in their starting success probability on the logit scale.
Command Center
Syntax libraries and function parameters for executing calculations in stats packages.
# 1. Fit GLMM (Binary Outcome)
model <- lme4::glmer(success ~ time * treatment + (1 | subject_id),
data = df, family = binomial)
summary(model)
# 2. Extract Odds Ratios
exp(fixef(model))GLMMs are prone to 'Convergence Failure'. If the model doesn't converge, try simplify the random effects or switch to the 'bobyqa' optimizer.
# Audit for Convergence and Overdispersion
performance::check_convergence(model)
performance::check_overdispersion(model)Common Mistakes
Analytical caveats and corrections to maintain modeling integrity.
References
Scholarly lineage and citation keys grounding the statistical framework.