GLM vs. GLMM: Choosing and Using Generalized Linear and Generalized Linear Mixed Models
This guide explains how GLMs and GLMMs differ and which one to choose. It covers the three components shared by both models (the random component, the linear predictor, and the link function), the GLM's assumption of independent observations, and why correlated or grouped data can bias standard errors and p-values. It then shows how GLMMs add fixed and random effects to handle repeated measurements, nested data, and between-group variation. Three worked examples cover COVID-era daily case counts, a saturating predator functional response, and a multi-center trial, followed by a model selection checklist, common pitfalls, and best practices.
Introduction: Why Distinguish GLM from GLMM?
In applied data modeling, a common question arises: both GLM and GLMM belong to the “generalized linear” family, so why keep them separate, and which one should you use?
The answer has little to do with the model names and everything to do with data structure. Classical linear regression assumes that the response follows a normal distribution, that variance is constant, and that observations are mutually independent. Real-world data is rarely that clean. The response may be a count, a proportion, or a time-to-event measure that is non-negative, shows non-constant variance, or is strongly skewed. Data is also often collected in groups: repeated measurements on the same patient, multiple students per class, multiple sites within the same region. This grouping makes observations correlated, which directly violates the independence assumption.
GLM addresses the first problem (non-normal responses), and GLMM goes further to handle the second (correlation and hierarchical structure). Once this distinction is clear, choosing a model becomes much easier.
1. The Shared Modeling Framework
To understand how GLM and GLMM differ, start with the three components they have in common. These form the core of generalized linear modeling, and GLMM turns out to be a natural extension of GLM.
1. Random component (response distribution)
The response variable $Y_i$ follows a distribution from the exponential family with mean $\mu_i = E(Y_i)$. This family includes several distributions that are highly useful in practice:
- Poisson distribution: count data, such as the number of events per unit time.
- Binomial distribution: proportions or binary outcomes, such as whether a disease is present.
- Gamma distribution: strictly positive continuous data, such as waiting times or concentrations.
- Normal distribution: the special case underlying classical linear regression.
2. Systematic component (linear predictor)
A linear predictor $\eta_i = x_i^\top \beta$ is constructed, where $x_i$ is the $i$-th row of the design matrix and $\beta$ is the vector of parameters to estimate. Core ideas from linear modeling, including parameter estimation, hypothesis testing, and model comparison, carry over to the generalized framework.
3. Link component (link function)
A smooth, monotone link function $g(\cdot)$ connects the mean to the linear predictor:
$g(\mu_i) = \eta_i = x_i^\top \beta$
The link function acts as a bridge. With an identity link and a normal response, the model reduces to ordinary linear regression. In other words, classical linear regression is just a special case of GLM.
2. GLM: A Generalization for Independent Observations
GLM retains the linear predictor structure while relaxing the assumptions of normality and constant variance. It does, however, rest on one key premise: $Y_1, Y_2, \dots, Y_n$ are mutually independent.
GLM is the default choice when data satisfies three conditions: observations are independent, the response is non-normal, and a non-linear link is needed. Its limits are equally clear. Once observations are correlated, the independence assumption is violated, the standard errors of the estimates become distorted, and significance tests become overconfident.
3. Where GLM Falls Short: The Problem of Correlated Data
Consider the following scenarios:
- Blood pressure measured five times on each of 50 patients.
- A survey of academic performance covering 30 schools with 40 students each.
- Multiple production lines and many product records from the same factory.
What these datasets have in common is hierarchical or nested structure: lower-level observations (individual measurements) are nested within higher-level units (patients, schools, or factories). Observations within the same unit are naturally more alike, which produces intra-class correlation.
Forcing such data into a GLM has two consequences:
- Within-group correlation is mistaken for independent information. The effective sample size is overestimated, standard errors are underestimated, and p-values are too small.
- Differences between individuals or groups cannot be represented. As a result, questions such as whether patients differ systematically from one another cannot be answered.
4. GLMM: Adding Random Effects to the Generalized Linear Framework
GLMM relates to the linear mixed model (LMM) in the same way that GLM relates to linear regression. What it does is straightforward: random effect terms are added to the linear predictor to absorb within-group correlation. Its structure can be written as:
$\eta{ij} = x{ij}^\top \beta + z_{ij}^\top u_j$
where:
- $\beta$ is the fixed effect, describing the average trend across the population.
- $u_j$ is the random effect, usually assumed to follow $u_j \sim N(0, G)$, describing the random deviation of group $j$ from the population.
Fixed effects answer “what is the overall pattern?”, while random effects answer “how do groups vary around it?”. By including a random intercept (each group has its own baseline) and a random slope (the trend can differ by group), the model stays within the generalized linear framework and still accounts for correlation correctly. In a GLMM, $Y_{ij}$ observations are independent only conditional on the random effects, not unconditionally. This is the essential dividing line between GLMM and GLM.
5. Worked Examples
Case 1: Early-pandemic daily new cases (a typical GLM)
In the early stage of a pandemic, daily new cases often grow exponentially. Let $\mu_i$ be the expected number of new cases on day $t_i$. Using a Poisson distribution with a log link:
$\log(\mu_i) = \beta_0 + t_i \beta_1$
Since $E(Y_i) = \mu_i = \exp(\beta_0 + \beta_1 t_i)$, exponential growth becomes a linear relationship on the log scale. The response is a daily count, which makes the Poisson distribution appropriate, and daily case counts can be treated as independent under the modeling assumptions. A standard GLM is therefore sufficient.
Case 2: Predator functional response (a typical GLM with a Gamma distribution)
The prey capture rate $y_i$ rises with prey density $x_i$ but eventually levels off. A model of the form $\mu = \dfrac{\alpha x}{h + x}$ can be used, where $\alpha$ is the maximum capture rate and $h$ is the half-saturation density. The parameters enter non-linearly, but an inverse link linearizes the model:
$\frac{1}{\mu_i} = \beta_0 + \beta_1 \frac{1}{x_i}, \quad \beta_0 = \frac{1}{\alpha},\ \beta_1 = \frac{h}{\alpha}$
The response is a positive continuous quantity, and its standard deviation is approximately proportional to its mean. A Gamma distribution is therefore a suitable choice, and pairing it with a log or inverse link completes the GLM specification.
Case 3: Multi-center clinical trial (a case for GLMM)
Now suppose the case data instead comes from a multi-center trial with 20 hospitals and a number of patients at each. Patients are nested within hospitals, and patients at the same hospital tend to be similar because their care follows comparable clinical practice. In this setting, a hospital-level random intercept (and possibly a random slope) should be added. Only a GLMM can then correctly estimate both the differences between hospitals and the overall treatment effect.
6. Model Selection Checklist
When facing a new task, work through these questions in order:
- What type of variable is the response? Counts point to Poisson, binary or proportion data to binomial, strictly positive continuous data to Gamma, and approximately symmetric continuous data to normal. This determines the distribution and the link function.
- Are the observations mutually independent? If each observation is unrelated to the others (for example, from random, independent sampling), use a GLM. If there is a structure of repeated measurements, nesting, clustering, or time series, use a GLMM.
- Do you need to separate individual or group differences from the overall trend? If so, use a GLMM (fixed effects plus random effects). If only the overall trend matters, use a GLM.
- Is the intra-class correlation substantial? You can estimate the intra-class correlation coefficient (ICC) first. A value clearly above 0 usually means random effects are necessary.
- Is there a hierarchical or crossed structure? Use random intercepts or slopes for hierarchical data, and crossed random effects for crossed groupings.
7. Common Pitfalls and Best Practices
- Pitfall 1: Using a GLM simply because the sample is large. A large number of correlated observations does not mean a large amount of independent information. Ignoring correlation leads to underestimated standard errors.
- Pitfall 2: Adding as many random effects as possible. An overly complex random-effects structure can make the model non-identifiable or prevent it from converging. Start with the simplest structure (a random intercept) and test additional terms step by step.
- Pitfall 3: Failing to check distributional assumptions. For overdispersed count data, a standard Poisson model can underestimate the variance. More flexible alternatives such as the negative binomial can be considered.
- Pitfall 4: Focusing only on fixed effects. The variance components of the random effects often carry important information, such as the magnitude of between-group heterogeneity.
In short: GLM handles independent data with non-normal responses that require a non-linear link, while GLMM additionally handles data with correlation and hierarchical structure. The real criterion for choosing a model is not model complexity, but how the data was generated and collected. When in doubt, plot the grouping structure of the data and estimate the within-group correlation; the answer usually becomes clear.
