You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
just some things that are unclear and potentially bugs
(while briefly browsing the code)
in GLM.fit offset and exposure are exclusive not additive
GLMResuts.llf special cases NegativeBinomial (I don't see why) and does not reuse mu and ignores offset and exposure for negbin.
not clear to me:
exposure in GLM._check_inputs is always log transformed, should this depend on link ?
Does offset and exposure have a meaning outside of count or Poisson?
But they can be used to fix any parameters, but we don't take advantage of that yet AFAIK.
Other mu is attached to model and not to results. (?)
Can we get score_obs/jac from the last WLS results?
It seems to me that 'exposure' only makes sense with the log link. In that case, when you log the exposure time and add it to the linear predictor with a fixed coefficient of 1, you get a situation where increasing the exposure time by a factor of K always increases the expected response by a factor of K. This is generally what you want to happen.
But it's not clear how a probability parameter should scale with exposure time. Clearly it can't just be multiplied by the exposure time. So it is confusing to think about exposure with logit and cloglog.
The power transform link family also poses difficulties, as you cannot achieve the default scaling (double exposure = double expected response) by adding a constant to the linear predictor.
Given these difficulties, one option is to remove the exposure option, and just instruct people to use log(exposure) in the offset for models using the log link.
Another option is to leave exposure in as a convenience argument, and raise an exception or warning if the link is not the log function.
When both exposure and offset are set, I think they should both contribute additively to the linear predictor (not as now, where I believe one overrides the other).
The current situation is not ideal, because it would lead people into thinking that they are correctly handling varying exposure times, when they are not.
Thanks Kerby, that is more clear than what my rough impression was.
One question: "exposure" literally only makes statistical sense in a few special cases like Poisson, however can we misuse or consider it as just a multiplicative weight factor that might not have a strict "exposure" interpretation.
e.g. prior weights w
with endog = y * w and fit model with exposure = w except that doesn't work because of the exposure transformation. and it might not make sense. (thinking about robust GLM)
I think we should raise or warn for anything but the narrow cases for exposure.
Do you know what R does?
(I'd like to settle and fix this tomorrow, today it's family and finishing up discrete NegativeBinomial)
I'm working on a PR for this issue. One initial question: we call _check_inputs before calling the superclass constructor. This means that inside of _check_inputs, we could be dealing with any allowed data structure (list, tuple, pandas, numpy, ...). This complicates the implementation of _check_inputs because we have to repeat some of the data handling work of the superclass constructor. Can we reverse this and call the superclass constructor first?
Also, a general question about auxiliary data like offset, exposure, etc.: It seems like the current practice is to keep these variables out of the object if they are not used in a particular model (using delattr if needed to explicitly remove it from the object). An alternative is to always have the attribute be defined, but with a None value if not present. I prefer the latter, but wonder if there are some considerations here that I am not aware of.
I'm working on a PR for this issue. One initial question: we call _check_inputs before calling the superclass constructor. This means that inside of _check_inputs, we could be dealing with any allowed data structure (list, tuple, pandas, numpy, ...). This complicates the implementation of _check_inputs because we have to repeat some of the data handling work of the superclass constructor. Can we reverse this and call the superclass constructor first?
np.asarray should handle any conversion from any array_like data structure
My guess is that we cannot reverse this right away, because the generic super code doesn't know what shape the extra arrays are supposed to be. IIRC the only thing super does is check for missing values and drop rows in all affected arrays and also does it for square array sigma in GLS.
You could open a new issue, and we can investigate how this works generically and across model, discrete CountModel has the same code.
Also, a general question about auxiliary data like offset, exposure, etc.: It seems like the current practice is to keep these variables out of the object if they are not used in a particular model (using delattr if needed to explicitly remove it from the object). An alternative is to always have the attribute be defined, but with a None value if not present. I prefer the latter, but wonder if there are some considerations here that I am not aware of.
I would still remove exposure when it is not userdefined to avoid the confusion from an extra attribute for models where exposure doesn't make sense.
Otherwise, I don't remember a discussion explicitly about this, and it is inconsistent across models and attributes, often we also define neutral values like weights=ones in WLS (which causes extra calculations). I think so far this was just considered as "implementation detail" without consistent rules.
related: #1609 storing a transformed __init__ argument makes recreating the model more difficult
Fixed: offset and exposure now combine additively (statsmodels/genmod/generalized_linear_model.py), predict() accepts both, llf/llf_scaled use one generic path with no NegativeBinomial special-case, and fit-initializations starting mu includes offset/exposure. Traces to PR #1635 plus sibling issue #1684 fix. Closing as fixed.
Activity
kshedden commentedon Apr 21, 2014
It seems to me that 'exposure' only makes sense with the log link. In that case, when you log the exposure time and add it to the linear predictor with a fixed coefficient of 1, you get a situation where increasing the exposure time by a factor of K always increases the expected response by a factor of K. This is generally what you want to happen.
But it's not clear how a probability parameter should scale with exposure time. Clearly it can't just be multiplied by the exposure time. So it is confusing to think about exposure with logit and cloglog.
The power transform link family also poses difficulties, as you cannot achieve the default scaling (double exposure = double expected response) by adding a constant to the linear predictor.
Given these difficulties, one option is to remove the exposure option, and just instruct people to use log(exposure) in the offset for models using the log link.
Another option is to leave exposure in as a convenience argument, and raise an exception or warning if the link is not the log function.
When both exposure and offset are set, I think they should both contribute additively to the linear predictor (not as now, where I believe one overrides the other).
The current situation is not ideal, because it would lead people into thinking that they are correctly handling varying exposure times, when they are not.
josef-pkt commentedon Apr 21, 2014
Thanks Kerby, that is more clear than what my rough impression was.
One question: "exposure" literally only makes statistical sense in a few special cases like Poisson, however can we misuse or consider it as just a multiplicative weight factor that might not have a strict "exposure" interpretation.
e.g. prior weights
wwith
endog = y * wand fit model withexposure = wexcept that doesn't work because of the exposure transformation. and it might not make sense. (thinking about robust GLM)I think we should raise or warn for anything but the narrow cases for exposure.
Do you know what R does?
(I'd like to settle and fix this tomorrow, today it's family and finishing up discrete NegativeBinomial)
jseabold commentedon Apr 21, 2014
IIRC R
glmhas no exposure variable just offset which accepts offset variable(s).jseabold commentedon Apr 21, 2014
Unclear what Stata does without running examples/looking at pdf docs.
http://www.stata.com/help.cgi?glm
(Unread) R-help thread on offset vs exposure in stata.
https://stat.ethz.ch/pipermail/r-help/2010-November/thread.html#259924
kshedden commentedon Apr 22, 2014
I'm working on a PR for this issue. One initial question: we call _check_inputs before calling the superclass constructor. This means that inside of _check_inputs, we could be dealing with any allowed data structure (list, tuple, pandas, numpy, ...). This complicates the implementation of _check_inputs because we have to repeat some of the data handling work of the superclass constructor. Can we reverse this and call the superclass constructor first?
Also, a general question about auxiliary data like offset, exposure, etc.: It seems like the current practice is to keep these variables out of the object if they are not used in a particular model (using delattr if needed to explicitly remove it from the object). An alternative is to always have the attribute be defined, but with a None value if not present. I prefer the latter, but wonder if there are some considerations here that I am not aware of.
josef-pkt commentedon Apr 22, 2014
np.asarrayshould handle any conversion from any array_like data structureMy guess is that we cannot reverse this right away, because the generic super code doesn't know what shape the extra arrays are supposed to be. IIRC the only thing super does is check for missing values and drop rows in all affected arrays and also does it for square array sigma in GLS.
You could open a new issue, and we can investigate how this works generically and across model, discrete CountModel has the same code.
josef-pkt commentedon Apr 22, 2014
I would still remove exposure when it is not userdefined to avoid the confusion from an extra attribute for models where exposure doesn't make sense.
Otherwise, I don't remember a discussion explicitly about this, and it is inconsistent across models and attributes, often we also define neutral values like weights=ones in WLS (which causes extra calculations). I think so far this was just considered as "implementation detail" without consistent rules.
related: #1609 storing a transformed
__init__argument makes recreating the model more difficultjosef-pkt commentedon Apr 24, 2014
From the description in Stata
exposuresounds like an offset variable without any specific interpretationexposure(varname) include ln(varname) in model with coefficient constrained to 1I guess, whether it makes sense or how to interpret it is up to the user.
Merge pull request #1635 from kshedden/glm_offset5
josef-pkt commentedon May 20, 2014
see PR #1635 for allowing offset and exposure at the same time, exposure only with log link
another bug: predict doesn't have exposure and offset arguments,
josef-pkt commentedon Jun 2, 2014
looks like one more missing offset and exposure in initialization in fit
mu = self.family.fitted(np.dot(wlsexog, start_params))Merge pull request statsmodels#1635 from kshedden/glm_offset5
bashtage commentedon Jul 30, 2026
Fixed: offset and exposure now combine additively (statsmodels/genmod/generalized_linear_model.py), predict() accepts both, llf/llf_scaled use one generic path with no NegativeBinomial special-case, and fit-initializations starting mu includes offset/exposure. Traces to PR #1635 plus sibling issue #1684 fix. Closing as fixed.