BUG: Fix problems with predict handling offset and exposure #1679

Merged
merged 1 commit into from May 21, 2014

Projects

None yet

2 participants

@kshedden
Contributor

This PR accomplishes three things in GLM.predict:

  1. predict should ignore offset and exposure if exog is provided.
  2. If offset or exposure are explicitly provided, they should override any offset or exposure that were part of the fit.
  3. exposure is not allowed unless the link is the log function
@kshedden
Contributor

Rephrasing #1 for clarity:

  1. predict should ignore offset and exposure from the fit if exog is provided

Also note that offset and exposure are new arguments.

@josef-pkt josef-pkt commented on the diff May 21, 2014
statsmodels/genmod/generalized_linear_model.py
"""
- offset = getattr(self, 'offset', 0)
- exposure = getattr(self, 'exposure', 0)
+
+ # Use fit offset if appropriate
+ if offset is None and exog is None and hasattr(self, 'offset'):
+ offset = getattr(self, 'offset')
+ elif offset is None:
+ offset = 0.
+
+ if exposure is not None and not isinstance(self.family.link,
+ families.links.Log):
@josef-pkt
josef-pkt May 21, 2014 Member

just a comment and to add link to #1509 possible problems with instance checks

@josef-pkt josef-pkt commented on the diff May 21, 2014
statsmodels/genmod/generalized_linear_model.py
- offset = getattr(self, 'offset', 0)
- exposure = getattr(self, 'exposure', 0)
+
+ # Use fit offset if appropriate
+ if offset is None and exog is None and hasattr(self, 'offset'):
+ offset = getattr(self, 'offset')
+ elif offset is None:
+ offset = 0.
+
+ if exposure is not None and not isinstance(self.family.link,
+ families.links.Log):
+ raise ValueError("exposure can only be used with the log link function")
+
+ # Use fit exposure if appropriate
+ if exposure is None and exog is None and hasattr(self, 'exposure'):
+ # Already logged
@josef-pkt
josef-pkt May 21, 2014 Member

Already in log. ? I was wondering for a while why we are logging https://docs.python.org/2/library/logging.html
minor

@josef-pkt
Member

looks good to me

@josef-pkt josef-pkt merged commit ede3157 into statsmodels:master May 21, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@kshedden kshedden deleted the kshedden:glm_predict_offset branch Jun 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment