Skip to content

DOC: Clarify/fix docs on GLM scale estimation for negative binomial - #5277

Merged
josef-pkt merged 1 commit into
statsmodels:masterfrom
kshedden:nb_doc
Oct 2, 2018
Merged

josef-pkt merged 1 commit into
statsmodels:masterfrom
kshedden:nb_doc

Conversation

@kshedden

Copy link
Copy Markdown
Contributor

Scale parameter for negative binomial in GLM fitting is fixed at 1.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.001%) to 84.212% when pulling 63cd972 on kshedden:nb_doc into e1d1156 on statsmodels:master.

@codecov

codecov Bot commented Sep 30, 2018

Copy link
Copy Markdown

Codecov Report

Merging #5277 into master will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5277      +/-   ##
==========================================
+ Coverage   81.71%   81.71%   +<.01%     
==========================================
  Files         569      569              
  Lines       89699    89699              
  Branches    10028    10028              
==========================================
+ Hits        73294    73295       +1     
  Misses      14132    14132              
+ Partials     2273     2272       -1
Impacted Files Coverage Δ
statsmodels/genmod/generalized_linear_model.py 90.7% <ø> (ø) ⬆️
...smodels/genmod/generalized_estimating_equations.py 83.1% <ø> (ø) ⬆️
statsmodels/graphics/regressionplots.py 81.67% <0%> (+0.23%) ⬆️

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 e1d1156...63cd972. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member

#2888
GLM NegativeBinomial uses Pearson chi2 as scale, same as R

@kshedden

kshedden commented Sep 30, 2018 via email

Copy link
Copy Markdown
Contributor Author

@josef-pkt

Copy link
Copy Markdown
Member

a quick check using pdb on a test_glm.py example, looks like pearson scale is used for cov_params and bse

(Pdb) self.res1.scale
0.38527329300099739
(Pdb) self.res1.bse
array([  1.99952693e-02,   4.76809235e-01,   6.48344375e-01,
         4.17948821e-01,   1.41510442e-03,   1.07767608e-01,
         1.99551392e+00])
(Pdb) np.sqrt(np.diag(self.res1.normalized_cov_params))
array([  3.22138628e-02,   7.68175065e-01,   1.04453091e+00,
         6.73346570e-01,   2.27983824e-03,   1.73621614e-01,
         3.21492103e+00])

(Pdb) np.sqrt(np.diag(self.res1.normalized_cov_params)) * np.sqrt(self.res1.scal
e)
array([  1.99952693e-02,   4.76809235e-01,   6.48344375e-01,
         4.17948821e-01,   1.41510442e-03,   1.07767608e-01,
         1.99551392e+00])
(Pdb) np.sqrt(np.diag(np.linalg.inv(-self.res1.model.hessian(self.res1.params, o
bserved=False)))) / self.res1.bse
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.])

I don't have a notebook with an example at the moment, but will look into this again.
IIRC, I ran into this comparing GLM with discrete NegativeBinomial

@josef-pkt

Copy link
Copy Markdown
Member

In the code for hessian, if scale=None, then estimate_scale is used

    if scale is None:
            scale = self.estimate_scale(mu)
(Pdb) np.sqrt(np.diag(np.linalg.inv(-self.res1.model.hessian(self.res1.params, o
bserved=False, scale=self.res1.scale)))) / self.res1.bse
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.])
(Pdb) np.sqrt(np.diag(np.linalg.inv(-self.res1.model.hessian(self.res1.params, o
bserved=False, scale=self.res1.scale))))
array([  1.99952693e-02,   4.76809235e-01,   6.48344375e-01,
         4.17948821e-01,   1.41510442e-03,   1.07767608e-01,
         1.99551392e+00])
(Pdb) np.sqrt(np.diag(np.linalg.inv(-self.res1.model.hessian(self.res1.params, o
bserved=False, scale=None))))
array([  1.99952693e-02,   4.76809235e-01,   6.48344375e-01,
         4.17948821e-01,   1.41510442e-03,   1.07767608e-01,
         1.99551392e+00])
(Pdb) np.sqrt(np.diag(np.linalg.inv(-self.res1.model.hessian(self.res1.params, o
bserved=False, scale=1))))
array([  3.22138628e-02,   7.68175065e-01,   1.04453091e+00,
         6.73346570e-01,   2.27983824e-03,   1.73621614e-01,
         3.21492103e+00])

(Pdb) self.res1.model.scaletype
'x2'

(Pdb) self.res1.model.family
<statsmodels.genmod.families.family.NegativeBinomial object at 0x000000000A4E7F6

@josef-pkt

Copy link
Copy Markdown
Member

my mistake

The test case uses
cls.res1 = GLM(cls.data.endog, cls.data.exog, family=fam).fit(scale='x2')

and explicitly asks for pearson scale

@josef-pkt

Copy link
Copy Markdown
Member

This means we have a full test case against R and Stata that scale='chi2' correctly implements QMLE with excess dispersion coefficient as in the statistics literature.
(test results include llf which should be a scale corrected loglike, R and Stata and statsmodels agree on it according to the comments in the test/results module for Committee.)

@josef-pkt

Copy link
Copy Markdown
Member

another check: if I change to default fit(), then bse looks like it is based on the above observed hessian with scale=1

    def test_standard_errors(self):
>       assert_almost_equal(self.res1.bse, self.res2.bse, self.decimal_bse)
E       AssertionError:
E       Arrays are not almost equal to 4 decimals
E
E       (mismatch 100.0%)
E        x: array([  3.2214e-02,   7.6818e-01,   1.0445e+00,   6.7335e-01,
E                2.2798e-03,   1.7362e-01,   3.2149e+00])
E        y: array([  1.9996e-02,   4.7682e-01,   6.4836e-01,   4.1796e-01,
E                1.4151e-03,   1.0777e-01,   1.9956e+00])

..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\genmod\tests\test_glm.
py:63: AssertionError


(Pdb) self.res1.bse
array([  3.22138705e-02,   7.68175719e-01,   1.04453268e+00,
         6.73346516e-01,   2.27983811e-03,   1.73621755e-01,
         3.21492809e+00])

@josef-pkt

Copy link
Copy Markdown
Member

There might have been a change in the default in the glm scale refactoring
4251f0d
added scaletype='x2' to fit, it wasn't there before.

There is also some special casing of NegativeBinomial in the unit tests for llf, so it's not clear to me now what's tested. It looks like Stata uses scale=1 for llf even in the dispersion case.

@josef-pkt

Copy link
Copy Markdown
Member

ok, found it
https://github.com/statsmodels/statsmodels/pull/3856/files#diff-724e15f4019b2a24906f6b271d67e370R735

added NegativeBinomial to the list of families with scale=1.
I didn't pay enough attention in the PR and didn't realize this.
A backwards incompatible change in 0.9, but nobody complained.

@josef-pkt

Copy link
Copy Markdown
Member

So this PR looks good and adjusts the docstrings to the new default.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants