You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
GLM domain violations with non-canonical links #2208
IRLS produces results with no warnings when the fitted means are outside the family domain. It's not clear that the solution is actually an MLE or if the standard errors, etc. are meaningful. An example is below.
I read an article about this case a few weeks ago, Binomial or Bernoulli (?) with log-link to get easy to interpret odd rations. They reported quite a large fraction of out of domain observations for some cases with larger expected values, and, IIRC, they also reported quite a lot of optimization and convergence problems. (I'm going to look for the article.)
I assume in general that users that use "non-standard" links are supposed to know what they are doing. A common case is estimating binary endog with a linear model which will have probabilities outside of of [0,1].
I don't know if we should do something about those cases.
Ok, now I see the MLE optimization problem, the loglike in your notebook is nan, but IRLS still finds a solution.
Would clipping to the domain make sense, or would we need a more thought out handling of domain issues. The beta regression PR has a reference that uses smooth, monotonic clipping into the interior (0,1). Essentially, this would change the link to a log_clipped link, which I think would distort the risk ratio interpretation.
aside here: I though we could get the risk ratio for the logit case from a non-linear predict method. The Blizzard and Hosmer paper has it in equation 3.
and another predict aside: You are using exp(params) in your notebook. Stata optionally allows this as and option like irr or or as an eform option. I looked into it (creating a eform params table) and it's pretty easy to do, but wasn't sure about all the different names and interpretation exp(params) has across models in Stata.
If you are interested and have some ideas about an interface, then this could be added as a post-estimation method or function for statsmodels 0.8.
(the exp params table is a bit inconsistent: the parameters and standard errors are from the transformation, the conf_int is by endpoint transformation, and the t/z statistic and p-value is for params==0 or exp(params) == 1)
just a random thought:
observations with prob at 1 or larger correspond to the perfect separation case in Logit.
If we do constant clipping (winsorizing) at slightly less than one y_fitted = max(fitted, 1 - epsilon), then the log-like contribution of the (strictly) clipped observation will be independent of local changes in parameters (their score_obs is zero). Which would mean locally in the parameter space this would be the same as dropping those observations in the estimation.
Which, I think, implies that the parameters of the log_clipped link have the interpretation of risk ratio for the non-clipped. The problem is that the effect of dummy or categorical variables is not local, and we would still have the additional effect of those that are clipped in at one discrete value (smoker) but not clipped at the other (non-smoker).
(Estimating log-binomial model and risk-ratio can become it's own issue. There are a lot more articles and discussions/debates/arguments about adjustments to get "good" results.)
I didn't pay enough attention before: I thought non-canonical links also impose domain restrictions.
However, I check with the Stata manual and they allow identity and log link for all families.
Maybe we should issue a warning if users chooses a link that is allowed but doesn't impose domain restrictions.
I'd like to think a bit more about the implication of this, eventually. The Log-Binomial model might make an interesting example case, and has enough specific treatment in the literature.
I never really paid attention to domain restrictions and it's consequences in GLM/LEFM, besides the obvious choice of a domain-restricting link function.
Essentially, GLM/LEFM estimate the parameters consistently as long as the mean function is correctly specified. The estimation depends on the mean function and the variance function, where the latter is just a weighting function.
One question: Is there a difference in estimation based on maximizing the log-likelihood (and using quasi-likelihood interpretation) and directly solving the first order conditions, i.e. estimating equations or moment conditions.
In the Binomial case the variance function is proportional to p * (1-p), which requires that the probability p is in (0, 1). This is the same for EE/IRLS solving and MLE optimization.
IRLS doesn't break, IIUC, because in varfunc we clip p for the variance, but we don't clip or force the loglikelihood (and others, deviance, score ?, ...) into the appropriate domain.
Manipulating the variance function is easier in the minimalistic solving of the estimating equations/moment conditions as in IRLS (IIUC), GEE or GMM. For example what we have here is a variance function that is at a flat minimum outside of (eps, 1-eps).
As long as we use a robust covariance estimator, everything should be fine, and it should be more efficient that the weighting/variance function implied by Poisson if we are close to a full Binomial model.
MLE would have to adjust the entire likelihood function which is not obvious given that it is supposed to represent a given distribution (except for using brute force clipping wherever necessary, and truncation at 1 would imply a possible mass point there).
Looks like IRLS has some hidden advantages (like GEE and GMM) and might not be just another way of solving for the MLE.
Two problems for the interpretation:
If the expected values of the endog are interpreted as probabilities given exog, then the mean function has to be misspecified if E(y|x) > 1.
Marginal effects move E(y|x) smoothly and strictly (if params !=0) monotonically even above 1, while the correct marginal effects should take the boundary into account.
(maybe it's the expectation of a latent y^star: E(y^star | x) that is not restricted to <1 ?)
@kshedden I'm still stuck a bit thinking about this.
I think we could add a "clip to domain" option so that the optimize/gradient based fit also works with links that don't respect the domain, which essentially would use a log-clipped or a identity-clipped link instead of a full log or identity link.
aside: an article about truncated IRLS mentions that both weights and residuals are clipped, we clip the variance. I don't remember/didn't check where residuals are used in the IRLS iteration.
Yes, I think something like this could be done. There are some standard barrier functions that are used for interior point optimization. We would need to extend all the derivatives in the family functions to handle this. I'm not at all familiar with the literature if any in this area.
I think it depends on whether we use hard clipping or smooth, strictly monotonic barrier functions.
Hard clipping with min(threshold, estimated_value) would just result in zero derivatives for the truncated observations. AFAICS.
With smooth clipping, I guess we would need to or it might be better to add the "barrier links" as new links.
(I have some "eclectic" knowledge from what I picked up in different cases and models.)
I don't know how much convergence and optimization problems we get with this. The truncated IRLS article reports getting stuck or cycling in the iterations and introduces a step to jump to new random starting values if that happens. We also have basin-hopping as global optimizer available, if we run into problems.
AFAICS, domain violation are not restricted to non-canonical links.
Gamma has inverse link as natural link, but a linear predictor is not constrained to be positive.
Inverse link has a discontinuity and singularity at zero of we allow for both positive and negative values of the linear predictor. But gamma distribution also assumes a positive mean parameter.
Activity
josef-pkt commentedon Jan 25, 2015
I didn't know log-link is allowed with Binomial.
I read an article about this case a few weeks ago, Binomial or Bernoulli (?) with log-link to get easy to interpret odd rations. They reported quite a large fraction of out of domain observations for some cases with larger expected values, and, IIRC, they also reported quite a lot of optimization and convergence problems. (I'm going to look for the article.)
I assume in general that users that use "non-standard" links are supposed to know what they are doing. A common case is estimating binary endog with a linear model which will have probabilities outside of of [0,1].
I don't know if we should do something about those cases.
josef-pkt commentedon Jan 25, 2015
Blizzard, L. and Hosmer, W. (2006), Parameter Estimation and Goodness-of-Fit in Log Binomial Regression. Biom. J., 48: 5–22. doi: 10.1002/bimj.200410165
http://onlinelibrary.wiley.com/doi/10.1002/bimj.200410165/abstract
There are quite a few articles on Log-Binomial model, but, IIRC, I didn't read any of the others that I downloaded.
kshedden commentedon Jan 25, 2015
I have a notebook about relative risk regression:
http://nbviewer.ipython.org/urls/umich.box.com/shared/static/60n20u2i871xzd7q21gl.ipynb
There are some comments at the bottom of the page about out of range values.
josef-pkt commentedon Jan 25, 2015
Ok, now I see the MLE optimization problem, the loglike in your notebook is
nan, but IRLS still finds a solution.Would clipping to the domain make sense, or would we need a more thought out handling of domain issues. The beta regression PR has a reference that uses smooth, monotonic clipping into the interior (0,1). Essentially, this would change the link to a
log_clippedlink, which I think would distort the risk ratio interpretation.aside here: I though we could get the risk ratio for the logit case from a non-linear predict method. The Blizzard and Hosmer paper has it in equation 3.
and another predict aside: You are using
exp(params)in your notebook. Stata optionally allows this as and option likeirrororas aneformoption. I looked into it (creating a eform params table) and it's pretty easy to do, but wasn't sure about all the different names and interpretationexp(params)has across models in Stata.If you are interested and have some ideas about an interface, then this could be added as a post-estimation method or function for statsmodels 0.8.
(the exp params table is a bit inconsistent: the parameters and standard errors are from the transformation, the conf_int is by endpoint transformation, and the t/z statistic and p-value is for params==0 or exp(params) == 1)
josef-pkt commentedon Jan 25, 2015
just a random thought:
observations with prob at 1 or larger correspond to the perfect separation case in Logit.
If we do constant clipping (winsorizing) at slightly less than one y_fitted = max(fitted, 1 - epsilon), then the log-like contribution of the (strictly) clipped observation will be independent of local changes in parameters (their score_obs is zero). Which would mean locally in the parameter space this would be the same as dropping those observations in the estimation.
Which, I think, implies that the parameters of the
log_clippedlink have the interpretation of risk ratio for the non-clipped. The problem is that the effect of dummy or categorical variables is not local, and we would still have the additional effect of those that are clipped in at one discrete value (smoker) but not clipped at the other (non-smoker).josef-pkt commentedon Jan 27, 2015
(Estimating log-binomial model and risk-ratio can become it's own issue. There are a lot more articles and discussions/debates/arguments about adjustments to get "good" results.)
I didn't pay enough attention before: I thought non-canonical links also impose domain restrictions.
However, I check with the Stata manual and they allow
identityandloglink for all families.Maybe we should issue a
warningif users chooses a link that is allowed but doesn't impose domain restrictions.Warn if link does not respect family domain statsmodels#2208
josef-pkt commentedon Jan 28, 2015
We keep this issue open even after @2212 is merged.
I was still browsing some abstracts while shutting down my firefox tabs with journal pages.
one link similar to Kerby's notebook http://www.ats.ucla.edu/stat/stata/faq/relative_risk.htm
which also uses Poisson regression with robust covariance.
I'd like to think a bit more about the implication of this, eventually. The Log-Binomial model might make an interesting example case, and has enough specific treatment in the literature.
I never really paid attention to domain restrictions and it's consequences in GLM/LEFM, besides the obvious choice of a domain-restricting link function.
Essentially, GLM/LEFM estimate the parameters consistently as long as the mean function is correctly specified. The estimation depends on the mean function and the variance function, where the latter is just a weighting function.
One question: Is there a difference in estimation based on maximizing the log-likelihood (and using quasi-likelihood interpretation) and directly solving the first order conditions, i.e. estimating equations or moment conditions.
In the Binomial case the variance function is proportional to p * (1-p), which requires that the probability p is in (0, 1). This is the same for EE/IRLS solving and MLE optimization.
IRLS doesn't break, IIUC, because in varfunc we clip p for the variance, but we don't clip or force the loglikelihood (and others, deviance, score ?, ...) into the appropriate domain.
Manipulating the variance function is easier in the minimalistic solving of the estimating equations/moment conditions as in IRLS (IIUC), GEE or GMM. For example what we have here is a variance function that is at a flat minimum outside of (eps, 1-eps).
As long as we use a robust covariance estimator, everything should be fine, and it should be more efficient that the weighting/variance function implied by Poisson if we are close to a full Binomial model.
MLE would have to adjust the entire likelihood function which is not obvious given that it is supposed to represent a given distribution (except for using brute force clipping wherever necessary, and truncation at 1 would imply a possible mass point there).
Looks like IRLS has some hidden advantages (like GEE and GMM) and might not be just another way of solving for the MLE.
Two problems for the interpretation:
If the expected values of the endog are interpreted as probabilities given exog, then the mean function has to be misspecified if E(y|x) > 1.
Marginal effects move E(y|x) smoothly and strictly (if params !=0) monotonically even above 1, while the correct marginal effects should take the boundary into account.
(maybe it's the expectation of a latent y^star: E(y^star | x) that is not restricted to <1 ?)
(Mostly guessing, to be continued another day)
Warn if link does not respect family domain statsmodels#2208
Warn if link does not respect family domain statsmodels#2208
Merge pull request #2212 from kshedden/glm-family-warn-bound
josef-pkt commentedon Jan 29, 2015
@kshedden I'm still stuck a bit thinking about this.
I think we could add a "clip to domain" option so that the optimize/gradient based
fitalso works with links that don't respect the domain, which essentially would use alog-clippedor aidentity-clippedlink instead of a fulllogoridentitylink.aside: an article about truncated IRLS mentions that both weights and residuals are clipped, we clip the variance. I don't remember/didn't check where residuals are used in the IRLS iteration.
kshedden commentedon Jan 29, 2015
Yes, I think something like this could be done. There are some standard barrier functions that are used for interior point optimization. We would need to extend all the derivatives in the family functions to handle this. I'm not at all familiar with the literature if any in this area.
josef-pkt commentedon Jan 29, 2015
I think it depends on whether we use hard clipping or smooth, strictly monotonic barrier functions.
Hard clipping with
min(threshold, estimated_value)would just result in zero derivatives for the truncated observations. AFAICS.With smooth clipping, I guess we would need to or it might be better to add the "barrier links" as new links.
(I have some "eclectic" knowledge from what I picked up in different cases and models.)
I don't know how much convergence and optimization problems we get with this. The truncated IRLS article reports getting stuck or cycling in the iterations and introduces a step to jump to new random starting values if that happens. We also have basin-hopping as global optimizer available, if we run into problems.
josef-pkt commentedon Jan 31, 2015
It looks like there is a problem with IRLS for log-binomial with convergence to different estimates.
I was using the notebook http://nbviewer.ipython.org/urls/umich.box.com/shared/static/60n20u2i871xzd7q21gl.ipynb
but get different numbers. Also when I change starting values, then it converges to different values of the parameters. Since we don't have the loglike/llf, it's not possible to see which parameters are minimizing the negative loglikelihood.
josef-pkt commentedon Jan 31, 2015
However, I'm able to replicate the Log-Binomial example from UCLA http://www.ats.ucla.edu/stat/stata/faq/relative_risk.htm
which doesn't have any fittedvalues >= 1
examples in the second half here
http://nbviewer.ipython.org/gist/josef-pkt/f42259d4e73340be2d2b
josef-pkt commentedon Oct 1, 2015
AFAICS, domain violation are not restricted to non-canonical links.
Gamma has inverse link as natural link, but a linear predictor is not constrained to be positive.
Inverse link has a discontinuity and singularity at zero of we allow for both positive and negative values of the linear predictor. But gamma distribution also assumes a positive mean parameter.
Alternative, it looks like log-link is most commonly used with gamma family
see for example https://www.statistics.ma.tum.de/fileadmin/w00bdb/www/czado/lec8.pdf
ENH: Incorporated numpy.allclose to test for IRLS convergence.