Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 28 ยท Generalized Linear Models

GLM Families and Links

Understand generalized linear models in Statsmodels: pick a family and link for the outcome, fit Gaussian, Binomial, Poisson and Gamma GLMs, and read deviance and residuals.

Logistic regression and Poisson regression look different, but they are cousins in one framework: the generalized linear model (GLM). Once you understand the framework, you can model many kinds of outcomes with the same code: yes/no, counts, positive skewed amounts like money or waiting times, and plain numbers.

A GLM has three parts:

  1. A distribution family for the outcome (how it varies around its mean).
  2. A linear predictor eta = b0 + b1*x1 + ....
  3. A link function connecting the mean mu to eta: g(mu) = eta.

Three parts of a GLM

Family How the outcome varies.
Linear predictor A weighted sum of x values.
Link Maps the mean to the sum.

Change the family and link, keep the same workflow.

1 / 2

The smf.glm interface

The call is the same for every family; only family= changes. Families live in sm.families. Because Gaussian with an identity link is ordinary regression, GLM reproduces OLS exactly.

Python Gaussian GLM equals OLS

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Binomial and Poisson as GLMs

The same data that smf.logit fit can be fit with family=Binomial(). A real advantage of the GLM version is that the outcome can be a proportion with weights or a two-column table of successes and failures, handy for grouped data such as "12 of 40 tested items failed".

Python Grouped binomial data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Positive, skewed outcomes: Gamma

Costs, claim amounts and durations are positive and right skewed, with spread that grows with the mean. A log-transformed OLS is one option, but then you model the mean of log(y), not of y. A Gamma GLM with a log link models the mean of y itself on the original scale.

Python Gamma GLM for positive amounts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading GLM output

Quantity Meaning
deviance Twice the gap in log-likelihood versus a perfect (saturated) model; smaller is better
null_deviance Deviance of an intercept-only model
pearson_chi2 Sum of squared Pearson residuals; compare with df_resid to spot overdispersion
aic, bic Compare models of the same family

Pseudo R-squared is 1 - deviance / null_deviance. There are several residual types (resid_response, resid_pearson, resid_deviance); deviance residuals are the usual ones to plot against fitted values. GLM results support get_prediction() and robust covariance via fit(cov_type="HC0").

Python Deviance, pseudo R2 and residuals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing a family: ask what values the outcome can take (0/1, counts, positive reals, any real number) and how its variance behaves. The link is usually the family's default; change it only with a reason. Count-specific advice is in Count Models, binary in Logistic and Probit Regression.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which three components define a GLM?

    A GLM combines an outcome distribution, a linear predictor and a link between them.
  2. 2. Which family and link suit a positive, right-skewed outcome such as insurance claim cost?

    Gamma handles positive skewed data with variance growing with the mean; the log link keeps predictions positive.
  3. 3. What does a Gaussian GLM with identity link reproduce?

    Normal errors with an identity link is the linear model, so GLM reproduces OLS.
  4. 4. How do you compute a deviance-based pseudo R-squared?

    It is the proportional reduction in deviance relative to an intercept-only model.

Summary

  • A GLM = family + linear predictor + link; smf.glm(formula, data, family=...) covers them all.
  • Binomial/logit for yes-no, Poisson/log for counts, Gamma/log for positive skewed values, Gaussian/identity for OLS.
  • Read deviance, null deviance, Pearson chi-square and AIC to judge fit.
  • Pick the family from what the outcome can be and how its variance behaves.

Next: Time Series Basics