Skip to content

ENH: overdispersed Binomial, Beta-Binomial #2632

Open

Description

@josef-pkt

(this showed up already several times, but I didn't see an issue)

see lecture slides at https://www.statistics.ma.tum.de/personen/professorinnen/claudia-czado/forschung/lecture-slides/
for some issues and diagnostics

Note:
Binomial count data can be over- or under (?) dispersed

Binary, Bernoulli data cannot be over or under dispersed, but could have heteroscedasticity in the underlying latent variable (Stata's hetprobit)

Issue for binomial count data is similar to Poisson, BetaBinomial would be analog of NegativeBinomial.

see also #2890

Activity

  1. added this to the 0.8 milestone on Oct 1, 2015
  2. modified the milestones: 0.8, 0.9 on Aug 10, 2016
  3. josef-pkt commented on Apr 14, 2017

    @josef-pkt
    MemberAuthor

    Prentice, R. L. 1986. “Binary Regression Using an Extended Beta-Binomial Distribution, With Discussion of Correlation Induced by Covariate Measurement Errors.” Journal of the American Statistical Association 81 (394): 321–27. doi:10.2307/2289219.
    This shows that Beta-Binomial can be extended to negative correlation and under dispersion. It's still a proper distribution without the beta-binomial restriction. It also means that no excess dispersion is in the interior of the parameter space and score test for binomial versus extended beta-binomial, i.e. under or over dispersion, is in the interior of the parameter space.

    Williams, D. A. 1982. “Extra-Binomial Variation in Logistic Linear Models.” Journal of the Royal Statistical Society. Series C (Applied Statistics) 31 (2): 144–48. doi:10.2307/2347977.
    This is using quasi-likelihood with variance functions from beta-binomial and normal random effects model. However, both are fully specified models.
    Also, estimation works by iterating with pre-specified weights and updating dispersion parameter using pearson statistic, which we will have in GLM with #3410

    Note: beta-binomial model has a dispersion that varies with the count (number of trials). If there is no differential "exposure", then it reduces to GLM type dispersion (constant factor with standard variance function).

  4. josef-pkt commented on Jan 14, 2020

    @josef-pkt
    MemberAuthor

    random find

    aod: Analysis of Overdispersed Data
    https://cran.r-project.org/web/packages/aod/index.html
    R package that includes beta-binomial regression (MLE)

  5. josef-pkt commented on Nov 7, 2020

    @josef-pkt
    MemberAuthor

    In Stata, including zero inflated versions

    Hardin, J. W., and J. M. Hilbe. 2014. “Estimation and Testing of Binomial and Beta-Binomial Regression Models with and without Zero Inflation.” Stata Journal 14 (2): 292–303.

  6. josef-pkt commented on Nov 21, 2020

    @josef-pkt
    MemberAuthor

    Based on a quick literature search I didn't find much on formulas for score, hessian for beta-binomial.

    Palm et al 2021 article has score functions using digamma function. and hessian based on trigamma function. (I didn't look closely at it.)
    The likelihood function can be expressed in terms of special Beta function or Gamma function. I don't know if there is a special function for derivatives of Beta function.

    Palm, Bruna G., Fábio M. Bayer, and Renato J. Cintra. 2021. “Signal Detection and Inference Based on the Beta Binomial Autoregressive Moving Average Model.” Digital Signal Processing 109 (February): 102911. https://doi.org/10.1016/j.dsp.2020.102911.
    (published in the future)

  7. josef-pkt commented on Nov 23, 2020

    @josef-pkt
    MemberAuthor

    a bit painful

    Palm et al don't use the same definition of the dispersion parameter
    theirs is alpha + beta
    the "usual" GLM is 1 / (alpha + beta + 1)

    note, Beta "prior", mixing distribution becomes a degenerate 1-point distribution (no heterogeneity) if alpha + beta -> inf
    GLM dispersion -> 0 to approach Binomial without mixing

    The Stata article Hardin and Hilbe seems to have a typo
    It says dispersion parameter sigma = 1 / (alpha * beta), but AFAICS, it should be sigma = 1 / (alpha + beta) which would then be just the reciprocal of Palm et al.

    The main point is that I cannot just copy formulas for score and hessian if parameterization differs

  8. josef-pkt commented on Oct 5, 2021

    @josef-pkt
    MemberAuthor

    Cepeda-Cuervo, Edilberto, and María Victoria Cifuentes-Amado. "Double generalized beta-binomial and negative binomial regression models." Revista Colombiana de Estadística 40, no. 1 (2017): 141-163.

    includes a link function for the dispersion parameter, similar to betareg.BetaModel, and WIP dispersion models #7778

    derivatives, score and hessian in appendix
    they define loglike in terms of Beta functions,
    scipy has betaln
    related derivatives of beta function
    https://brilliant.org/wiki/beta-function/#derivative-of-the-beta-function
    https://math.stackexchange.com/questions/1464499/how-to-find-the-derivative-of-the-beta-function

  9. josef-pkt commented on Oct 6, 2021

    @josef-pkt
    MemberAuthor

    I need code with different parameterizations.

    Parameterization across articles is confusing and I don't have an overview anymore. I went through this already once.

    For "foreign" variance function for GLM-Binomial #1777, we have several options that don't all map into Beta-Binomial.

    For Beta-Binomial itself the main issue is what is used as "dispersion" parameter, and how that maps to a, b of the Beta mixing distribution.

    • d1 = a + b: simplest

    basic form of variance function

    V(y) = [n * p * (1 - p)] * [1 + d * (n_i - 1)]
    If all n_i are the same then theta = scale = [1 + d * (n_trials - 1)].
    If all n_i=1, then there is no excess dispersion, i.e. reduces to Bernoulli.

    The V(y) function is also in this way in Simonoff 2003 Analyzing Categorical Data book p. 400, and p. 94
    (theta in Simonoff) d = 1 / (a + b + 1)

    Note: a+b+1 >= 1 (from Beta distribution a>0, b>0
    d in [0, 1]
    e.g. Logit link for d

    Hinde 2013 working paper Beta Binomial Regression uses sigma where d = sigma / (1 + sigma)
    then, sigma = d / (1 - d) in (0, inf)
    log-link for sigma

    in original Beta-Binomial parameterization (Hinde 2013, Wikipedia)

    E(y) = n * a / ( a + b)
    v(y) = n * a * b * (a + b + n) / [ ( a + b)**2 * (a + b + 1)]
    = n * [a / ( a + b)] * [b / ( a + b)] * [ (a + b + n) / (a + b + 1)]
    = n p * (1 - p) * (d1 + n) / (d1 + 1)

    overdispersion is [ (a + b + n) / (a + b + 1)]

    still need parameters when Beta-Binomial reduces to Binomial
    V_B(y) = n p * (1 - p)
    need d->0 for V_BB -> V_B
    or sigma -> 0
    does this mean d1 -> inf ? (sounds correct, Beta collapses to a single point, variance of Beta -> 0, i.e. no heterogeneity)
    check references for overdispersion tests.

    now, we still need loglike in parameterization, d1, d and sigma.
    ...

    update
    d1 = 1 / sigma d1 is precision (as in Beta distribution), s is scale
    follows from last equality in
    d = 1 / (a + b + 1) = 1 / (d1 + 1) == s / (s + 1)

    also overdispersion:
    (d1 + n) / (d1 + 1) = (d1 + 1 + n - 1) / (d1 + 1) = 1 + (n - 1) / (d1 + 1) = 1 + (n - 1) * d
    where d = 1 / (d1 + 1)

  10. josef-pkt commented on Oct 6, 2021

    @josef-pkt
    MemberAuthor

    We can use var_weights in GLM to adjust the variance function because n_trials is known, and d or sigma can be estimated on the outside.
    I'm not sure how much bse, cov_params will differ if we don't take estimated d or sigma into account.
    I guess, we stll need to use HC cov_type.

  11. josef-pkt commented on Oct 6, 2021

    @josef-pkt
    MemberAuthor

    Cepeda-Cuervo, Edilberto, and María Victoria Cifuentes-Amado. "Double generalized beta-binomial and negative binomial regression models." Revista Colombiana de Estadística 40, no. 1 (2017): 141-163.

    also uses d1: phi = a + b
    based on BetaRegression parameterization.
    They use logit-link for mu and log link for phi
    1st and second derivatives are analytical formulas and derivative_factors, e.g. equs (23)-(27) (*) for general expression of xxx_factors.
    (needs digamma and trigamma functions)

    (*) we might be able to use this for generic code. chain rule similar to derivative factors in glm.

  12. josef-pkt commented on Oct 7, 2021

    @josef-pkt
    MemberAuthor

    here's the code 4 parameterization, scipy/standard and 3 with mean + precision/scale/dispersion

    def loglike_bb0(a, b, n_trials=None, endog=None):
        n = n_trials
        t1 = - (special.betaln(k + 1, n - k + 1) + np.log(n + 1))
        t2 =  special.betaln(k + a, n - k + b) - special.betaln(a, b)
        return t1 + t2
    ​
    def loglike_bb_prec(mean, precision, n_trials=None, endog=None):
        n = n_trials
        a = mean * precision
        b = (1 - mean) * precision
        t1 = - (special.betaln(k + 1, n - k + 1) + np.log(n + 1))
        t2 =  special.betaln(k + a, n - k + b) - special.betaln(a, b)
        return t1 + t2
    ​
    def loglike_bb_scale(mean, scale, n_trials=None, endog=None):
        n = n_trials
        a = mean / scale
        b = (1 - mean) / scale
        t1 = - (special.betaln(k + 1, n - k + 1) + np.log(n + 1))
        t2 =  special.betaln(k + a, n - k + b) - special.betaln(a, b)
        return t1 + t2
    ​
    def loglike_bb_disp(mean, dispersion, n_trials=None, endog=None):
        n = n_trials
        precision = 1 / dispersion - 1  # Note dispersion parameter in [0, 1]
        a = mean * precision
        b = (1 - mean) * precision
        t1 = - (special.betaln(k + 1, n - k + 1) + np.log(n + 1))
        t2 =  special.betaln(k + a, n - k + b) - special.betaln(a, b)
        return t1 + t2
        
    def convert_from_dist(n, a, b):
        precision = a + b
        mean = a / precision
        return mean, precision
    ​
    def convert_ms(mean, precision, n_trials):
        n = n_trials
        a = mean * precision
        b = (1 - mean) * precision
        return n, a, b
    
    k0, n0 = 5, 11
    m, prec = convert_from_dist(n0, k0, n0-k0)
    m, prec
    (0.45454545454545453, 11)
    0
    convert_ms(m, prec, n0)
    (11, 5.0, 6.0)
    
    n, a, b
    (7, 5, 6)
    ​
    stats.betabinom.logpmf(k, n, a, b)
    array([-3.20093812, -2.13049671, -1.63802023, -1.48386955, -1.60165258,
           -1.99469517, -2.73663252, -4.07640686])
    
    loglike_bb0(a, b, n_trials=n, endog=k)
    array([-3.20093812, -2.13049671, -1.63802023, -1.48386955, -1.60165258,
           -1.99469517, -2.73663252, -4.07640686])
    
    loglike_bb_prec(m, prec, n_trials=n, endog=k)
    array([-3.20093812, -2.13049671, -1.63802023, -1.48386955, -1.60165258,
           -1.99469517, -2.73663252, -4.07640686])
    
    loglike_bb_scale(m, 1 / prec, n_trials=n, endog=k)
    array([-3.20093812, -2.13049671, -1.63802023, -1.48386955, -1.60165258,
           -1.99469517, -2.73663252, -4.07640686])
    
    md, vd = stats.betabinom.stats(n, a, b)
    md / n, vd
    (0.45454545454545453, array(2.60330579))
    
    def var_prec(m, precision, n_trials=n):
        n = n_trials
        d1 = precision
        return n * m * (1 - m) * (d1 + n) / (d1 + 1)
    ​
    def var_scale(m, scale, n_trials=n):
        n = n_trials
        s = scale
        return n * m * (1 - m) * (1 + s / (s + 1) * (n - 1))
    ​
    def var_scale_(m, scale, n_trials=n):
        n = n_trials
        d1 = 1 / scale
        return n * m * (1 - m) * (d1 + n) / (d1 + 1)
    ​
    def var_disp(m, disp, n_trials=n):
        n = n_trials
        return n * m * (1 - m) * (1 + disp * (n - 1))
         
    var_prec(m, prec, n_trials=n)
    2.6033057851239665
    
    disp = 1 / (a + b + 1)
    var_disp(m, disp, n_trials=n)
    2.6033057851239665
    
    var_scale(m, 1 / prec, n_trials=n), var_scale_(m, 1 / prec, n_trials=n)
    (2.6033057851239665, 2.6033057851239665)
    
  13. agartland commented on Nov 11, 2021

    @agartland

    Hi, this looks promising. We have loads of over-dispersed count data and are currently using corncob in R to model it with a beta-binomial. I'm interested to continue investigating how its working for our data across spectrum of datasets. If it helps you, here are the links to the paper and package. We'd be interested to help you test, though not sure I can be much help in implementing.

    https://cran.r-project.org/web/packages/corncob/corncob.pdf
    https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7514055/

  14. josef-pkt commented on Nov 11, 2021

    @josef-pkt
    MemberAuthor

    Thanks for the comment and references.

    The implementation right now is mostly generic based on #7793
    For the specific case, we need unit tests and, if possible, analytical derivatives for better numerical performance.

    I'm currently working on post-estimation results, like get_prediction, get_influence, get_diagnostic. Those are mostly not available for multilink models yet, but should become available before the next release.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions