Skip to content

GLM: review offset and exposure #1486

Closed

Description

@josef-pkt

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?

Activity

  1. kshedden commented on Apr 21, 2014

    @kshedden
    Contributor

    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.

  2. josef-pkt commented on Apr 21, 2014

    @josef-pkt
    MemberAuthor

    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)

  3. jseabold commented on Apr 21, 2014

    @jseabold
    Member

    IIRC R glm has no exposure variable just offset which accepts offset variable(s).

  4. kshedden commented on Apr 22, 2014

    @kshedden
    Contributor

    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.

  5. josef-pkt commented on Apr 22, 2014

    @josef-pkt
    MemberAuthor

    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.

  6. josef-pkt commented on Apr 22, 2014

    @josef-pkt
    MemberAuthor

    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

  7. josef-pkt commented on Apr 24, 2014

    @josef-pkt
    MemberAuthor

    From the description in Stata exposure sounds like an offset variable without any specific interpretation

    exposure(varname) include ln(varname) in model with coefficient constrained to 1

    I guess, whether it makes sense or how to interpret it is up to the user.

  8. added a commit that references this issue on May 20, 2014
  9. josef-pkt commented on May 20, 2014

    @josef-pkt
    MemberAuthor

    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,

  10. josef-pkt commented on Jun 2, 2014

    @josef-pkt
    MemberAuthor

    looks like one more missing offset and exposure in initialization in fit
    mu = self.family.fitted(np.dot(wlsexog, start_params))

  11. added a commit that references this issue on Sep 2, 2014
  12. added this to the 0.10 milestone on Apr 17, 2018
  13. bashtage commented on Jul 30, 2026

    @bashtage
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions