Generalized Linear Models vs. Mixed Models: How to Choose
This article clarifies the differences between LM, GLM, and GLMM, and explains why the choice between a generalized linear model and a mixed model is not about nonlinearity but about two things: whether the response distribution and link scale are correctly expressed, and whether the random error structure still satisfies independence. It walks through the three components of a GLM (distribution, link function, linear predictor), the correlation scenarios that break the independence assumption, an operational rule for fixed versus random effects, a step-by-step modeling and diagnostic checklist, and practical engineering notes for reproducible workflows.
1. First, Get the Concepts Straight: Three Model Families That Are Easy to Confuse
Many modeling debates actually come from mixed terminology. Three groups of concepts need to be separated first. The general linear model (usually written as LM) assumes the response variable is normally distributed, has homogeneous variance, and that effects add up in a constant additive form. The generalized linear model (GLM) extends the general linear model in two ways: it allows the response variable to come from an exponential family distribution, and it uses a link function to connect the linear predictor to the mean of the response. The generalized linear mixed model (GLMM) then embeds random effects into the GLM structure to describe hierarchical and clustered relationships among observations.
In the original text, “one pill lowers a fever by 0.1 degrees, two pills lower it by 0.2 degrees” is a constant additive effect, the most basic case. A pattern of increasing marginal effects, such as “two pills lower the fever by 0.4 degrees,” usually suggests that the relationship between the response and the predictors is not linear on the identity scale, and needs to be expressed through a transformation or through a suitable distribution and link function. The core idea of GLM is exactly this: instead of forcing a nonlinear relationship to be straight, first identify the distribution family of the response variable, then choose a link function that maps the linear predictor to the mean domain.
2. The Three Components of a GLM: Distribution, Link Function, Linear Predictor
Deciding whether GLM is appropriate is essentially answering three questions.
First, what is the range and shape of the response variable’s distribution. Indicators that are continuous and symmetric and can take any real value often start from the normal distribution; nonnegative, right-skewed count or monetary indicators can consider the Poisson or gamma distribution; proportions that fall between 0 and 1 can consider the binomial or beta distribution. Choosing the wrong distribution distorts all subsequent inference and interval estimation.
Second, on what scale are the effects additive. This is the most easily overlooked point. If a factor appears as a multiplicative amplification on the original scale, it is additive on the log scale, and a log link is more natural. The judgment method is simple: first look at the plot of the response against the predictors, then look at the shape of the residuals as they vary with fitted values.
Third, does the variance change with the mean. GLM allows the variance to be a function of the mean, which is precisely where it fits real data better than the general linear model. Count data commonly show “larger mean, larger fluctuation,” and proportion data commonly show “fluctuation compressed near 0 or 1.” These do not violate model assumptions; rather, they are the basis for choosing the distribution family.
A schematic model specification can be written as follows; an actual implementation only needs to swap the distribution family and link function:
# Conceptual notation, not a specific implementationg(E[y]) = b0 + b1*x1 + b2*x2# Poisson count: g = log, Var(y) = mu# Binomial proportion: g = logit, Var(y) = mu*(1-mu)/n# Gamma continuous: g = log, Var(y) ∝ mu^2
3. Error Independence: The Real Dividing Line Between GLM and GLMM
The key insight of the original text is that what truly requires a mixed model is not nonlinearity in the mean relationship, but a change in the random error structure and correlation. This can be stated more rigorously: when correlations exist among the observations that cannot be absorbed by the fixed effects already included, the independent and identically distributed error assumption fails, and random effects or an equivalent variance–covariance structure must be introduced.
Common failure scenarios include: repeated measurements and longitudinal data, where the same subject is observed multiple times; panel data, where the individual and time dimensions cross; and clustered data, where students are nested in classes, classes in schools, and patients in departments or institutions. These structures share one feature: “observations within the same cluster resemble each other more.” Treating them as independent samples systematically underestimates standard errors, and significance conclusions become overly optimistic.
It is worth stressing that heterogeneous variance by itself does not necessarily mean a GLMM is needed. If the magnitude of fluctuation varies only with the mean or with some observed covariate, it can be handled within the GLM framework through a suitable distribution family and variance function. Random effects are required only when the correlation comes from unobserved cluster-level differences.
4. Fixed Effects or Random Effects: An Operational Criterion
The decision logic given in the original text is very practical and can be formalized into two criteria.
The first criterion is the sampling scope. If the schools, classes, or individuals in the data cover the entire population of interest, and you want to give a specific effect estimate for each level directly, put them into the model as fixed effects. Cluster-level differences are then modeled explicitly and no longer enter the random error, so GLM is sufficient.
The second criterion is the inference goal. If these levels are only a sample drawn from a larger population, and the goal is to generalize to the population rather than describe these specific levels, or if there are many levels with a small sample size per level, they should be treated as random effects. A random effect amounts to imposing a shared distributional assumption on the cluster-level effects, which produces shrinkage estimates on sparse clusters and better stability.
A compromise and safe approach is to fit both specifications and compare them: compare effect estimates, interval widths, and predictive performance under the fixed-effects and random-effects specifications. If the conclusions agree, the results are insensitive to the specification; if the differences are clear, you need to go back to the data-generating mechanism and ask where the correlation comes from.
5. Modeling Workflow and Diagnostic Checklist
It is advisable to proceed in the following order and avoid piling on complexity from the start.
- Clarify the analysis goal and the target population of inference; first decide whether you “want to describe these clusters” or “want to generalize to the population.”
- Plot the distribution of the response and the relationships, and choose the distribution family and link function accordingly.
- Fit a baseline GLM, and check the relationship between residuals and fitted values, overdispersion, and influential outliers.
- Test the clustered structure, compare fit and prediction with and without cluster-level random effects, and check whether the intraclass correlation is non-negligible.
- If random effects are retained, check whether the random-effect variance is near the boundary, whether the number of clusters is sufficient, and whether convergence is stable.
- Run sensitivity analysis: whether conclusions remain robust after changing the distribution family, changing the random-effect structure, or removing high-influence points.
- When reporting, give both the effect scale (original scale and link scale) and uncertainty intervals, and avoid reporting only significance.
Overdispersion is the most common trap in count models: when the conditional variance is clearly larger than the mean, a Poisson specification underestimates uncertainty, and you can switch to a negative-binomial-type distribution or introduce a dispersion parameter correction within the GLM framework. Convergence failures often indicate that the random-effect structure is too complex or that there are too few observations per cluster; the structure should be simplified rather than forcing parameter tuning.
6. Engineering Implementation Notes
In real business settings, the model is only one link in the chain. On the data side, consistent metric definitions, rules for handling missing values and anomalies, and time alignment of features often affect conclusions more than the choice of model family. Engineering practice can rely on general-purpose capabilities to make the process repeatable: object storage to hold raw and intermediate datasets, a message queue to decouple feature computation from training jobs, a container platform to host reproducible training and inference environments, and logging and monitoring with alerting to track data drift and shifts in the prediction distribution. General cloud infrastructure from Baidu AI Cloud can serve as the foundation for such a reproducible modeling workflow, making model iteration traceable and rollback-capable.
7. Conclusion
Distinguishing GLM from GLMM does not depend on whether the relationship is nonlinear, but on two things: whether the distribution of the response and the link scale are correctly expressed, and whether the random errors still satisfy independence. The first determines the distribution family and link function; the second determines whether random effects are needed. Putting these two criteria ahead of modeling dissolves most arguments about “which model to use.”
