statsmodels/glm-logged-exposure
| Rule | glm-logged-exposure |
|---|---|
| Pack | statsmodels |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
A statsmodels GLM with a log link takes the natural log of exposure before adding
it to the linear predictor. Passing logged values as exposure applies the log
twice and may produce nonfinite values.
sm.GLM(y, X, family=sm.families.Poisson(), exposure=np.log(time_at_risk))sm.GLM(y, X, family=sm.families.Poisson(), exposure=time_at_risk)What the rule reports
Section titled “What the rule reports”The rule checks a direct NumPy or math log call passed as exposure to
GLM, GLM.from_formula, or the formula API's glm when the log import
is established. It checks the fifth positional argument of GLM as
well as the keyword. For plain imports, the rule skips names with
competing bindings in the file.
How to fix it
Section titled “How to fix it”Pass positive exposure on the original scale, or supply the logged values as
offset. If the call already has an offset, account for both contributions
before editing. In a Poisson fit on 0.15.0, raw exposure and a logged offset
produced identical coefficients. Logged exposure changed both coefficients.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
Statsmodels GLM takes the natural log of exposure. Pass positive exposure on its original scale or use offset for values already on the log scale.Browse the other rules of the statsmodels pack.