Skip to content

WIP: Work on fixing scale in GLM #2351 - #2353

Closed
kshedden wants to merge 3 commits into
statsmodels:masterfrom
kshedden:glm_fix_2351
Closed

kshedden wants to merge 3 commits into
statsmodels:masterfrom
kshedden:glm_fix_2351

Conversation

@kshedden

@kshedden kshedden commented Apr 5, 2015

Copy link
Copy Markdown
Contributor

#2351

The issue in loglike seems to be whether the scale parameter is the MLE or the unbiased method of moments estimate. The calculations were a bit convoluted, but it boils down to this difference.

For the current code, if using the power family with exponent 1 we ignore the provided scale parameter and implicitly replace it with the MLE. If instead we try to use the provided scale parameter, tests fail because the provided scale is the unbiased estimate not the MLE.

I implemented a simple calculation of the loglike value that in my view should be the only thing there. The older code in the "if" block is needed to get the tests to pass in the special case of power link with exponent 1. We should rewrite the tests so the code in the if block can be removed.

I also fixed unrelated issues in the deviance and resid_deviance methods.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.0%) to 83.91% when pulling cc471a7 on kshedden:glm_fix_2351 into a1f53b0 on statsmodels:master.

Comment thread statsmodels/genmod/families/family.py Outdated

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.

I think scale should be the same as self.variance(mu) already in the common, regular use case, but we should be able to fix scale independently of variance(mu)

I'm not sure yet

@josef-pkt

Copy link
Copy Markdown
Member

We need to decide whether scale is the variance in the Gaussian case or if scale is the "dispersion factor". we might be mixing the two definitions in different parts of the code.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 83.91% when pulling 38f55ed on kshedden:glm_fix_2351 into a1f53b0 on statsmodels:master.

@kshedden

kshedden commented Apr 6, 2015

Copy link
Copy Markdown
Contributor Author

I think scale should multiply the variance function. In the default case, varfunc is identically 1, so the variance is scale * varfunc = scale. As an alternative, someone could conceivably use the log link and varfunc(mu) = mu as the varfunc. This would give Poisson-like behavior with a Gaussian model.

However the issue would remain when we attempt to compare to statsmodels.OLS or R lm, where the loglike value that is returned is the maximum value of the llf (evaluated at the MLE for both regression parameters and scale). Our genmod llf function by default uses self.scale, which is not the MLE.

It seems to me that what we should do is remove the special code from the Gaussian loglike function that switches the provided scale with the MLE. Then in the tests we would change the genmod.llf call to pass in the MLE for scale.

Thus, for genmod we consistently return as llf the loglikelihood evaluated at the usual genmod params estimate and the usual genmod (approximately unbiased) estimate of scale. This will be consistent across all GLM families, since we don't currently have code to compute the scale MLE in general (I think R has this but I don't see it as a high priority or a new feature).

@kshedden

kshedden commented Apr 6, 2015

Copy link
Copy Markdown
Contributor Author

The alternative is that we provide an option to calculate scale as the MLE. Then when we compare to R and Stata in the tests we would invoke this option for the Gaussian family.

@josef-pkt

Copy link
Copy Markdown
Member

We have three issues with scale and loglike that require decision about definition or version that we use

    1. bias corrected versus MLE scale: what you mentioned in the comment. The effect is small except in very small samples. In OLS we use the scale estimate with (nobs - kvars) denominator but we use MLE scale (concentrated out) in the loglike and llf. Both versions are fine with me, using MLE scale in gaussian MLE looks "standard" even if scale for Wald inference is the degrees of freedom corrected. I never looked at the other families in GLM that estimate scale.
      (Which scale we use is kind of secondary if we only look at inference for mean function, in large samples.)
    1. Definition of scale in the QMLE interpretation, when scale is not equal to one for Poisson and so on. loglike definition corrects for the over or underdispersed scale in SAS and SPSS if I remember their documentation correctly.
    1. fixed, "model independent" scale or what do we use in the likelihood ratio test in QMLE: This is why I looked into this again, because llnull, the loglike for the intercept only model, has two different definitions with respect to scale. The first, use the scale of the reduced model, i.e. scale of intercept only model, when calculating the llf/loglike, and the second one is to fix the scale at the larger model. (My usual test cases are binomial and poisson so I didn't actually look at the scale !=1 case before.

The last is one of the main use cases for llf /loglike: llnull and compare_lr_test. The latter doesn't exist yet outside of linear models OLS to GLS. The simple LR test just uses the llf of two nested, but independently estimated models, a different version might use the same scale estimate for both models when calculating the llf.

A 4th issue will come up later, if we allow for non-native variance, like additional heteroscedasticity, estimating a variance function additionally to a mean function. We will have to adjust all statistics including loglike and llf for that case.

@josef-pkt

Copy link
Copy Markdown
Member

coming back to the original question

I think it's better to use MLE scale in loglike even if it's irrelevant. That makes it cleaner to say that maximizing the loglike actually calculates the MLE. (In the simple Gaussian case the estimate of the mean parameters doesn't depend on the scale estimate, so it's irrelevant, but in other cases we might want to have really MLE loglike.)

scale as function argument should fix the scale so we can use any provided scale chosen by the calling function for calculating loglike. This allows us to override it in non-MLE cases

Comment thread statsmodels/genmod/families/family.py Outdated

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.

this doesn't use the scale that the user provides

@kshedden

kshedden commented Apr 6, 2015

Copy link
Copy Markdown
Contributor Author

The complication not discussed yet is that I believe obtaining the MLE of the scale parameter is not just a matter of changing the denominator of the unbiased scale estimate. This is only true in the Gaussian case. For example, I believe in the overdispersed Poisson case it would involve numerical optimization. We could add MLE as a scale_type option and then raise an exception if the family is not Gaussian.

@josef-pkt

Copy link
Copy Markdown
Member

Quasi-Poisson wouldn't be MLE, from what I have seen the main choices for scale are based on Pearson or deviance residuals, (implemented but not used as far as I have seen).
Assuming we leave generialized Poisson to discrete.
Inverse-Gaussian and Gamma would need a MLE scale, but I never looked at those.

@kshedden

kshedden commented Apr 7, 2015

Copy link
Copy Markdown
Contributor Author

I added scaletype='mle' as an option in estimate_scale and forced llf to use this scale rather than whatever is specified in fit. This required providing a scaletype keyword argument to estimate_scale. One concern is that I'm not sure that what we are calling the MLE is really the MLE, say, in the gamma family case. However the tests pass so it apparently agrees with R/Stata.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 83.92% when pulling 6e5d307 on kshedden:glm_fix_2351 into a1f53b0 on statsmodels:master.

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.

3 participants