Okay, let's break down how the offset parameter in statsmodels (and in GLMs in general) works to account for the time factor (t_i) in your Poisson regression model.
The Goal:
Your data is (t_i, c_i), where t_i is the duration and c_i is the number of occurrences during that duration. You are essentially modeling the rate of occurrences per unit of time, not just the raw count. The expected number of occurrences (E[c_i]) is the rate multiplied by the duration:
E[c_i] = rate_i * t_i
Standard Poisson Regression:
A standard Poisson regression model models the expected value (lambda_i = E[c_i]) as a function of predictor variables (X_i), using the log link function:
log(E[c_i]) = log(lambda_i) = beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip}
This means:
E[c_i] = exp(beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip})
In this standard form, E[c_i] represents the expected total count for the given predictors X_i.
Incorporating the Time Factor (Duration):
We need the model to predict the rate (rate_i), not the total count. Let's assume the rate is modeled on the log scale based on your predictors X_i:
log(rate_i) = beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip}
This implies:
rate_i = exp(beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip})
Now, substitute this back into our initial goal equation E[c_i] = rate_i * t_i:
E[c_i] = exp(beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip}) * t_i
To fit this using a GLM with a log link function, we take the natural logarithm of both sides:
log(E[c_i]) = log( exp(beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip}) * t_i )
Using the logarithm property log(a * b) = log(a) + log(b):
log(E[c_i]) = log( exp(beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip}) ) + log(t_i)
Using the logarithm property log(exp(x)) = x:
log(E[c_i]) = (beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip}) + log(t_i)
The Role of the offset Parameter:
The statsmodels GLM formulation (and the standard GLM definition) allows for an offset term that is added to the linear predictor before applying the inverse of the link function. The model fits:
link_function(E[Y]) = linear_predictor + offset
For Poisson regression, the link function is log, so the model fits:
log(E[c_i]) = (beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip}) + offset_i
Comparing this to our derived equation log(E[c_i]) = (beta_0 + beta_1*x_{i1} + ... + beta_p*x_{ip}) + log(t_i), we see that the offset_i should be log(t_i).
In Summary:
- You provide the counts
c_i as the response (endog).
- You provide your predictor variables
X_i (including an intercept if needed) as exog.
- You specify the
Poisson() family, which uses the log link function by default.
- You provide
log(t_i) as the offset argument.
statsmodels then fits a model where the logarithm of the expected count (E[c_i]) is the sum of the linear combination of your predictors and the provided offset (log(t_i)). Mathematically, it's fitting:
log(E[c_i]) = (Linear Predictor based on X_i) + log(t_i)
Which is equivalent to:
E[c_i] = exp( (Linear Predictor based on X_i) + log(t_i) )
E[c_i] = exp(Linear Predictor based on X_i) * exp(log(t_i))
E[c_i] = exp(Linear Predictor based on X_i) * t_i
Since exp(Linear Predictor based on X_i) is our modeled rate_i, the model successfully fits E[c_i] = rate_i * t_i.
The coefficients (beta) estimated by statsmodels then represent the effect of the corresponding predictor X_i on the log of the rate (log(rate_i)), holding other predictors constant. This is exactly what you want when accounting for different exposure times.