← All resources

Generalized Linear Models: A Practical Guide for Researchers

10 min read
Generalized Linear Models: A Practical Guide for Researchers

Generalized Linear Models: A Practical Guide for Researchers

Researcher reviewing response distribution plots

A generalized linear model (GLM) extends linear regression to outcomes that are not normally distributed, such as binary events, counts and skewed positive measurements. You choose a probability distribution for the outcome and a link function that connects its mean to a linear combination of predictors. If your outcome is yes/no, a count, or a positive measurement whose spread grows with its mean, a GLM is usually a better starting point than ordinary least squares.

This guide covers the three components, how to choose a family and link, how a GLM differs from linear regression, worked examples, and how to fit one in R and Python.

Plotstudio
plotstudio.ai
Run GLM Analyses You Can Review
PlotStudio AI supports Python and R workflows, with real code, methods, and results available for researchers to inspect.
Explore PlotStudio AI

TL;DR

  • A GLM has three parts: a random component (the outcome distribution), a systematic component (the linear predictor) and a link function.
  • Use logistic (binomial, logit) for binary outcomes, Poisson or negative binomial (log link) for counts, and gamma for positive skewed outcomes.
  • In R, use glm(formula, family = ...). In Python, use statsmodels GLM with a family object.
  • GLMs assume independent observations. Clustered or repeated data need mixed-effects or similar methods.
  • Check deviance, residuals and overdispersion before you report anything.

Table of Contents

What a generalized linear model is

A GLM keeps the linear predictor from regression but connects it to the mean of the response through a link function, and it permits a response distribution other than normal, such as binomial or Poisson. Every GLM has three components:

  1. Random component. The conditional distribution of the outcome. Common families are Gaussian, binomial, Poisson, gamma and inverse-Gaussian.
  2. Systematic component. The linear predictor, a linear function of the regressors: η = α + β₁X₁ + … + βₖXₖ.
  3. Link function. A smooth, invertible function connecting the expected response to the linear predictor. A well-chosen link can remove restrictions on the range of the expected response, for example keeping probabilities between 0 and 1.

The link is the part people find unintuitive. You are not transforming the outcome. You are modeling a transformation of its expected value, which keeps the original scale of the data and gives you interpretable effects such as odds ratios and rate ratios.

GLM vs. linear regression

Ordinary linear regression models a normally distributed response whose mean is linear in the predictors and whose variance is constant. A GLM relates the mean to predictors through a link and can model non-normal responses. Ordinary linear regression is in fact a GLM: the identity link with a Gaussian family gives the ordinary linear model.

Feature Linear regression GLM
Outcome distribution Normal Exponential family (binomial, Poisson, gamma, and others)
Variance Constant Depends on the mean through the family
Mean and predictors Linear on the outcome scale Linear on the link scale
Typical outcomes Continuous, unbounded Binary, counts, positive skewed
Fit by Least squares Maximum likelihood
Fit assessed by R², residual SE Deviance, AIC, residual diagnostics

The practical rule: the farther an outcome is from continuous and unbounded, the more corrections a linear model needs. A 2023 peer-reviewed article argues that GLMs should be preferred for count, bounded and skewed outcomes when linear-model assumptions are materially violated, because transforming the outcome may not give valid inference.

Linear regression is still the right tool when the outcome is reasonably continuous, residuals look symmetric and variance is stable. Do not reach for a GLM just because the name sounds more advanced.

Outcome type Family Usual link Effect you report
Binary (event / no event) Binomial (Bernoulli) Logit Odds ratio
Count (nonnegative integers) Poisson Log Rate ratio
Overdispersed count Negative binomial or quasi-Poisson Log Rate ratio
Positive, skewed continuous Gamma Inverse or log Ratio of means (log link)
Continuous, symmetric Gaussian Identity Mean difference

The pairings come from the literature: Bernoulli with logit, Poisson with log, and gamma with inverse for outcomes bounded by zero. For binary data, logistic regression fits because the binomial likelihood matches the data and the logistic link keeps predicted probabilities inside the 0 to 1 range, unlike a linear model. For counts, the log link keeps predicted means positive and gives multiplicative rate interpretations.

The gamma family is frequently used when variance increases with the mean. Many analysts prefer a log link with gamma because the coefficients exponentiate to ratios, though the inverse link is the textbook default.

Worked examples

Binary outcome: logistic regression

Suppose you model whether a patient has coronary heart disease from age, blood pressure and smoking status. The binomial family with a logit link returns probabilities or odds ratios. An odds ratio of 1.8 for smoking means smokers have 1.8 times the odds of disease, holding the other covariates fixed. It does not mean 1.8 times the risk. For a deeper treatment, see our logistic regression explainer and the logistic regression article archive.

Count outcome: Poisson and negative binomial

Insect counts after an agricultural spray are a classic Poisson case. If the variance grows faster than the mean, the data are overdispersed, and the standard errors from a plain Poisson model are too small. The fix is a quasi-Poisson or negative binomial model. Our guide to Poisson regression models walks through the diagnostics.

Poisson and overdispersion model comparison

Skewed positive outcome: gamma regression

Reaction times and insurance claim sizes are positive and right-skewed, with spread that rises with the mean. Gamma regression models them directly without log-transforming and back-transforming, which preserves interpretability as a ratio rather than a difference.

Fitting a GLM in R

In R the function is glm. It needs a formula describing the linear predictor and a family, for example family = poisson(link = "log"). R’s family objects such as gaussian, binomial and poisson bundle the link, the variance function and the other pieces needed for fitting.

fit <- glm(events ~ age + treatment + offset(log(person_years)),
           family = poisson(link = "log"),
           data = dat)
summary(fit)
exp(cbind(RR = coef(fit), confint(fit)))

summary(fit) gives coefficients, null and residual deviance, and AIC. Exponentiating coefficients under a log or logit link gives rate ratios or odds ratios. Compare residual deviance to its degrees of freedom: a ratio well above 1 in a Poisson model signals overdispersion.

Fitting a GLM in Python

In Python, statsmodels provides a GLM class that takes the response (endog), the predictors (exog) and a family such as sm.families.Gamma(). After .fit(), the results hold parameters and diagnostics such as deviance.

import statsmodels.api as sm
import statsmodels.formula.api as smf

model = smf.glm("cost ~ age + C(group)", data=df,
                family=sm.families.Gamma(sm.families.links.Log())).fit()
print(model.summary())

The formula interface mirrors R, so a model moves between the two languages with little rewriting.

Assumptions to check before you report

  • Independence. GLMs assume independent observations. Repeated measures, patients nested in hospitals or students nested in classrooms violate this and call for mixed-effects models or methods that account for correlation, such as cluster-robust standard errors (see our robust standard errors archive).
  • Correct mean-variance relationship. Plot residuals against fitted values. Test for overdispersion in count models.
  • Link and functional form. Check whether continuous predictors need splines or transformations on the link scale.
  • Separation and sparse cells. In binary models, perfect prediction inflates coefficients and standard errors.
  • Multiple comparisons. Many models and many contrasts need a stated correction procedure.

If your outcome is a time to event with censoring, a GLM is the wrong frame. See the Cox regression guide. For unordered categorical outcomes with more than two levels, see multinomial logit models.

When to use a GLM

Use a GLM when:

  • the outcome is binary, a count, a rate or a positive skewed measurement;
  • you need to adjust for several predictors at once, since GLMs handle continuous predictors and simultaneous adjustment;
  • you want effects reported on an interpretable scale, such as ratios;
  • a reviewer would question a log transform of zeros or a linear model predicting probabilities below 0.

Stay with ordinary linear regression for roughly symmetric continuous outcomes. Move to mixed-effects GLMs when observations cluster.

Our perspective: where agentic analytics fits

Most GLM mistakes we see are not coding errors. They are decisions made silently: a Poisson family chosen without checking dispersion, a log link assumed, an offset forgotten, clustered data treated as independent. The code runs, the table looks plausible, and the problem surfaces months later in peer review or when a supervisor cannot reproduce the numbers.

R, Python, Stata, SPSS, SAS and Jupyter remain the statistical computing layer, and PlotStudio AI does not replace them. What PlotStudio AI adds is agentic analytics for researchers: an agentic layer that plans an analysis, writes and runs real code locally, inspects intermediate results and documents the work. With Plan Mode you can review the proposed family, link and assumptions before anything executes, and you can encode your lab’s conventions as Skills. The finished work is kept as a searchable Analysis Page and can be exported as a notebook or PDF report, so a collaborator can see how each number was produced.

One-shot chat tools answer a question once. For researchers who need reproducible, multi-step analysis, the better Julius AI alternative is PlotStudio AI.

— Aymen

Try it on your own data

PlotStudio AI runs analyses locally on your machine, which suits datasets you cannot upload to a cloud platform. There is a free trial on the pricing page, where plans include Managed Credits at 69.99 USD per month and Bring Your Own Key at 39.99 USD per month. Academic users can see the Bring Your Own Key annual plan at 399.90 USD per year on the academic page.

Plotstudio

For more on the modeling family around GLMs, browse our regression analysis articles.

FAQ

Is a GLM the same as the general linear model?

No. The general linear model covers regression, ANOVA and ANCOVA with a normal outcome. The generalized linear model is broader and allows other distributions and link functions. The general linear model is a special case: a Gaussian family with an identity link.

Is a GLM a type of regression?

Yes. A GLM is a regression framework in which a linear predictor explains the mean of an outcome through a link function. Logistic, Poisson and gamma regression are all GLMs.

Can I just transform my outcome and use linear regression?

Sometimes, but it changes what you are estimating and can hurt inference. A log transform models the mean of the log, not the log of the mean, and fails with zeros. A GLM models the mean directly on an interpretable scale, and the 2023 review cited above argues for it when assumptions are materially violated.

What do I do when counts are overdispersed?

Switch from a standard Poisson model to quasi-Poisson or negative binomial regression. Then recheck residual diagnostics and compare AIC. If observations are clustered, add random effects or cluster-robust errors.

Sources