Poisson Regression
statsmodels
R programming
statistical modeling
data analysis

Poisson Regression in statsmodels and R

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Poisson regression models count outcomes such as number of calls, claims, or arrivals. Both statsmodels in Python and glm(..., family = poisson()) in R fit the same underlying generalized linear model, so the main concepts and coefficient interpretation are shared across both tools.

When Poisson Regression Makes Sense

Poisson regression is used when the response is a non-negative count and the expected count is modeled on a log scale.

A typical model says:

  • the mean count depends on predictors
  • the log of that mean is linear in the coefficients

That is why exponentiating coefficients matters: exp(beta) gives a multiplicative effect on the expected count, not an additive one.

Fit the Model in statsmodels

Here is a minimal Python example:

python
1import pandas as pd
2import statsmodels.api as sm
3
4df = pd.DataFrame({
5    "count": [1, 3, 2, 5, 4, 7],
6    "ad_spend": [0, 1, 1, 2, 2, 3],
7    "weekend": [0, 0, 1, 0, 1, 1],
8})
9
10X = sm.add_constant(df[["ad_spend", "weekend"]])
11y = df["count"]
12
13model = sm.GLM(y, X, family=sm.families.Poisson())
14result = model.fit()
15
16print(result.summary())
17print(result.params)

This estimates the coefficients using a Poisson GLM with the default log link.

Fit the Same Idea in R

The equivalent model in R is:

r
1df <- data.frame(
2  count = c(1, 3, 2, 5, 4, 7),
3  ad_spend = c(0, 1, 1, 2, 2, 3),
4  weekend = c(0, 0, 1, 0, 1, 1)
5)
6
7fit <- glm(count ~ ad_spend + weekend, family = poisson(), data = df)
8summary(fit)
9coef(fit)

You should expect the coefficient signs and basic interpretation to match the statsmodels output, allowing for formatting differences.

Interpreting Coefficients

Suppose the coefficient for ad_spend is 0.20. Then:

python
import numpy as np
print(np.exp(0.20))

That value is about 1.22, which means a one-unit increase in ad_spend is associated with roughly a 22 percent increase in the expected count, holding other predictors fixed.

This multiplicative interpretation is central to Poisson regression and is the same in Python and R.

Watch for Exposure and Overdispersion

If observations represent different exposure lengths such as time at risk, you often need an offset term rather than pretending every row has equal exposure.

You should also check for overdispersion. A plain Poisson model assumes the variance is close to the mean. If the data are much more variable than that, a negative binomial model or robust standard errors may be more appropriate.

So the workflow is not just "fit the model and read coefficients." It is also "check whether Poisson assumptions are reasonable for this dataset."

That is one reason analysts often compare simple summary statistics, residual patterns, and alternative count models before declaring the Poisson fit acceptable in production analysis or reporting work.

Common Pitfalls

One common mistake is using Poisson regression for continuous or strongly non-count outcomes just because the values are positive.

Another issue is interpreting coefficients additively instead of on the log-count scale. The meaningful interpretation comes after exponentiating.

It is also easy to ignore overdispersion. A model can converge cleanly and still be the wrong distributional choice for the data.

Summary

  • Poisson regression models count outcomes with a log-linked expected value.
  • 'statsmodels and R's glm(..., family = poisson()) fit the same general model.'
  • Exponentiated coefficients represent multiplicative effects on the expected count.
  • Consider offsets when exposure differs across observations.
  • Always check whether overdispersion makes a plain Poisson model inappropriate.
  • Model diagnostics matter as much as fitting.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.