Skip to content

dont set model scale or mu in GLM - #4032

Closed
jbrockmendel wants to merge 2 commits into
statsmodels:mainfrom
jbrockmendel:glm_attrs
Closed

jbrockmendel wants to merge 2 commits into
statsmodels:mainfrom
jbrockmendel:glm_attrs

Conversation

@jbrockmendel

Copy link
Copy Markdown
Contributor

Closes #4016

@coveralls

coveralls commented Oct 13, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.0007%) to 81.64% when pulling 2530bc7 on jbrockmendel:glm_attrs into 326ae0c on statsmodels:master.

@codecov-io

codecov-io commented Oct 13, 2017 •

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (master@f8a1667). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #4032   +/-   ##
=========================================
  Coverage          ?   79.05%           
=========================================
  Files             ?      542           
  Lines             ?    81029           
  Branches          ?     9205           
=========================================
  Hits              ?    64059           
  Misses            ?    14876           
  Partials          ?     2094
Impacted Files Coverage Δ
statsmodels/genmod/generalized_linear_model.py 88.6% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f8a1667...be89ede. Read the comment docs.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Does coveralls have a config option to ignore decreases of a certain size? The "failure" here is a false-positive.

@josef-pkt

Copy link
Copy Markdown
Member

I think this can wait for the other refactorings, and after glm weights.

my guess is that this PR is fine
see also #2399 and #3357

@josef-pkt

Copy link
Copy Markdown
Member

failing unit tests in several GLM cases across all environments

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Yep, I'll take a look. In the meantime #5147 is there to help.

elif self.scaletype.lower() == 'dev':
return (self.family.deviance(self.endog, mu, self.var_weights,
self.freq_weights, self.scale) /
self.freq_weights, scale) /

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only based on spot checking
my guess is that scale here should be 1.
scale should not be an argument in estimate_scale

@josef-pkt

Copy link
Copy Markdown
Member

when you test locally, you should include the failing test to see whether it's fixed

pytest statsmodels.base.tests.test_generic_methods::TestGenericGLM
or with full path to test_generic_methods

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Hmm doesn't seem to have fixed it

@josef-pkt

Copy link
Copy Markdown
Member

Ok, this needs a look at TestGenericGLM.test_zero_constrained and _fit_zeros

The fail is in bse and it looks like the scale differs, maybe it uses the wrong df_resid

>>> x = np.array([0.064275, 0.065696, 0.064033])
>>> y = np.array([0.064219, 0.065638, 0.063977])
>>> x / y
array([ 1.00087202,  1.00088363,  1.00087531])
>>> (x / y)**2
array([ 1.00174479,  1.00176805,  1.0017514 ])

@josef-pkt

josef-pkt commented Sep 29, 2018 •

Copy link
Copy Markdown
Member

without investigating an example:
I guess the problem is when results.df_resid != model.df_resid which is the case in fit_constrained and similar.
GLM.estimate_scale uses model.df_resid

possibly fit_zeros needs more unit tests for GLM when scale!=1, currently the failing TestGenericGLM is the only one
#4195 fixed BUG in scale for fit_constrained

@josef-pkt

Copy link
Copy Markdown
Member

I haven't tried to figure out why scale in master is correct, i.e. unit test passes in master for fit_zeros.

ok, base.model _fit_zeros has special casing to account for current glm model.scale

        # TODO: do we need to change res._results.scale in some models?
        if hasattr(res_constr.model, 'scale'):
            # Note: res.model is self
            # GLM problem, see #2399,
            # TODO: remove from model if not needed anymore
            res.model.scale = res._results.scale = res_constr.model.scale

my guess is the last line needs to be change to something like res._results.scale = res_constr.scale

@josef-pkt

Copy link
Copy Markdown
Member

maybe another redesign that keeps the scale attribute in model
#4664

@josef-pkt

Copy link
Copy Markdown
Member

This PR cannot be merged without further thought and decisions. We keep it open to see one possible version.

We need temporary model attributes for the implementation of fit, but we don't want those attributes to cause stale state for the results instance.
I'm still not sure how to resolve this dual role. see new issues for auxiliary parameters.
(GAM alpha or pen_weight is even worse to support in a clean way as fit argument)

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

@josef-pkt IIRC you had a (more recent) PR that addressed a related issue. Did you get anywhere with this?

@bashtage

Copy link
Copy Markdown
Member

@jbrockmendel Could you rebase so this can be considered?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GLM attributes

5 participants