Skip to content

ENH/REF Beta Regression follow-up #7584

Open

Description

@josef-pkt

Beta regression was merged in #7543

Beta regression still needs api improvement and cleanup, part of it is to support a second exog.
For some of it we don't have a consistent pattern and similar problems in ZeroInflated models.

user interface:

unit tests:

  • no unit test for robust cov_types. has unit test for cov_type="eim"

missing functionality

  • support drop data (not all arrays included yet), pickle support not checked
  • offset
  • rebuilding model e.g. for fit_constrained. bootstrap. Those need extra (generic) support for a second, or more exog.
  • post-estimation
    • get_prediction
    • gof, (pit should be easy with get_distribution)
    • predictive evaluation measures ?
    • diagnostics
    • outlier-influence
    • score_test

For much of the latter we have generic versions that work only with one exog and not auxiliary arrays.

Activity

  1. josef-pkt commented on Jul 16, 2021

    @josef-pkt
    MemberAuthor

    need to add references to docs
    (I didn't look at any references in last round before merge)

  2. josef-pkt commented on Jul 16, 2021

    @josef-pkt
    MemberAuthor

    docs
    https://www.statsmodels.org/dev/othermodels.html
    update correct link is https://www.statsmodels.org/dev/other_models.html

    othermodels needs to be added to user-guide.rst

    quite a few problems with docstring
    get_distribution does not have any docstring
    some inherited docstrings are not appropriate

    prsquared docsting is picked up in Attributes looks fine.

  3. josef-pkt commented on Jul 16, 2021

    @josef-pkt
    MemberAuthor

    update: df for likelihood ratio test for Null model has been fixed in #7586

    tests

    no unit tests for res_meth.llnull, res_meth.llr, res_meth.llr_pvalue inherited from base _LLRMixin

    BUG: LR-test compared to null model uses wrong, the mixin uses df_model which only counts 1 constant afair
    df_model = 5, LR-null test has 4 = 6 - 2 in example

    loglike values and test statistic llr agree with betareg/lmtest

    res_meth.llf, res_meth.llnull, res_meth.llr, res_meth.llr_pvalue
    (104.14802840534058,
     60.888095894871256,
     86.51986502093865,
     3.613370296363882e-17)
    
    > library(lmtest)
    > res_meth_null = betareg(methylation ~ 1, data=meth)
    > lrtest(res_meth, res_meth_null)
    Likelihood ratio test
    
    Model 1: methylation ~ gender + CpG | age
    Model 2: methylation ~ 1
      #Df           LogLik Df    Chisq Pr(>Chisq)    
    1   6 104.148028405343                           
    2   2  60.888095894923 -4 86.51987 < 2.22e-16 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    

    edit
    I guess R lrtest has smaller model first

    > lrtest(res_meth_null, res_meth)
    Likelihood ratio test
    
    Model 1: methylation ~ 1
    Model 2: methylation ~ gender + CpG | age
      #Df           LogLik Df    Chisq Pr(>Chisq)    
    1   2  60.888095894923                           
    2   6 104.148028405343  4 86.51987 < 2.22e-16 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    

    I might just make a method to override inherited llr_pvalue.
    The df_model mess in generic likelihood, still needs more review and cleanup.
    I might add a k_null attribute, similar to the df_null in R betareg.
    (df_model, k_constant, has_const, ... are all messy things for inference and other competing use cases.)

    What happens with no constant, no parameter models, e.g. if precision were fixed or only depends on offset?
    analogues of linear regression through the origin, for rsquared and similar.

  4. josef-pkt commented on Jul 16, 2021

    @josef-pkt
    MemberAuthor

    aside:
    set_null_options is not called automatically.
    i.e. if we want to store res_null, then we need to call set_null_options before accessing null

  5. josef-pkt commented on Nov 22, 2021

    @josef-pkt
    MemberAuthor

    R:
    betareg package include influence/leverage measures

    seems there is no direct confidence interval for prediction https://stats.stackexchange.com/questions/86432/how-do-i-predict-with-standard-errors-using-betareg-package-in-r
    references appendix B of Ferrari, S.L.P., and Cribari-Neto, F. (2004). That uses endpoint transformation confint for mean mu based on linpred for mean.
    second answer refers to package 'effects' and might be standard NonlinearDeltaCov version
    Stata also only has se for linpred in predict, and I guess delta methods in margins

  6. josef-pkt commented on Nov 22, 2021

    @josef-pkt
    MemberAuthor

    get_prediction (as in DiscreteResults) works for NonlinearDeltaCov, but not with endpoint transformation, pred.get_prediction_monotonic which is the default for which="mean"

    pm = get_prediction(res_meth._results, which="mean", average=True)
    print(pm.summary_frame())
       predicted        se  ci_lower  ci_upper
    0   0.874042  0.002264  0.869604  0.878481
    
    pm = get_prediction(res_meth._results, which="precision", average=True)
    print(pm.summary_frame())
        predicted          se    ci_lower    ci_upper
    0  590.628202  144.385154  307.638499  873.617905
    
    pm = get_prediction(res_meth._results, which="precision")
    print(pm.summary_frame())
         predicted          se    ci_lower     ci_upper
    0   496.385781  134.213650  233.331859   759.439702
    1   602.850412  145.358309  317.953361   887.747462
    2   396.855731  165.415642   72.647030   721.064431
    3   549.555472  131.058681  292.685179   806.425766
    ...
    

    oops, inconsistent naming, generic is which="linear", betamodel uses which="linpred"
    allowing linear as alias gets endpoint confint work for "mean"

  7. josef-pkt commented on Nov 22, 2021

    @josef-pkt
    MemberAuthor

    linear/linpred doesn't work
    pl = get_prediction(res_meth._results, which="linpred")
    fails with ValueError: shapes (6,6) and (4,36) not aligned: 6 (dim 1) != 4 (dim 0)

    This is still wrong. We are going through regression linear prediction which does not trim params and cov_params to the length of exog.

    I think we don't need endpoint transformation for other exog/link.
    e.g conditional variance of y |x depends on mean and precision parameter.

    The only interesting case might be GaussianHet where variance is equal to the scale parameter.
    TModel would also have monotonic variance when df is fixed, but not otherwise.

    If we stick with endpoint transformation at most for the first link function, then only get_prediction(res_meth._results, which="linear") needs to be fixed.

    update get_prediction works now, PR #7900

  8. josef-pkt commented on Nov 25, 2021

    @josef-pkt
    MemberAuthor

    BUG: results cov_type attribute is missing if cov_type is "nonrobust"

    res_income.cov_type
    AttributeError: 'BetaResults' object has no attribute 'cov_type'

    I'm fixing this in GenericLikelihoodModel.fit add cov_type to kwargs if not there yet.

  9. josef-pkt commented on Nov 25, 2021

    @josef-pkt
    MemberAuthor

    scoretest option 3, using params_constrained in unrestricted model, works.
    score_test results are close to the wald test result

    example: slope variable for precision parameter is zero, i.e. constant precision parameter, constant dispersion.

    from statsmodels.base._parameter_inference import score_test
    mod_restr = BetaModel.from_formula(model, income, link_precision=links.Log())
    res_restr = mod_restr.fit(method="newton")
    params_restr = np.concatenate([res_restr.params, [0]])
    print(params_restr)
    print(res_income._results.params)
    [-0.62254806 -0.01229884  0.1184621   3.57261949  0.        ]
    [-0.78308155 -0.00821684  0.09255381  5.50430969 -0.48352332]
    
    score_test(res_income, params_constrained=params_restr, k_constraints=1)  #, cov_type="nonrobust")
    (array([7.86872382]), array([0.00502971]), 1)
    
    res_income.pvalues[-1]   # pvalue of wald t_test
    0.004679678893544189
    
    res_income.wald_test("precision-2")
    <class 'statsmodels.stats.contrast.ContrastResults'>
    <Wald test (chi2): statistic=[[7.99924769]], p-value=0.004679678893544197, df_denom=1>
    
  10. josef-pkt commented on Nov 26, 2021

    @josef-pkt
    MemberAuthor

    get_influence for BetaNodel needs _deriv_score_obs_dendog

    explicit formulas for computing that is in
    Silvia Ferrari & Francisco Cribari-Neto (2004) Beta Regression for Modelling Rates and Proportions,
    Journal of Applied Statistics, 31:7, 799-815, DOI: 10.1080/0266476042000214501

    AFAICS, I use the same definition of generalized leverage in MLEInfluence.
    Influence for beta regression was one of my example to figure out generic MLEInfluence definitions.

    I have not tried yet whether we can use numdiff for numerical derivatives in these cases #7891

    update
    _deriv_score_obs_dendog has been implemented using _approx_fprime_cs_scalar for derivative "factors"

  11. josef-pkt commented on Sep 28, 2022

    @josef-pkt
    MemberAuthor

    current:

    BetaResults has

    • get_prediction inherited from GenericLikelihoodModel
    • get_distribution model specific
    • get_influence, uses generic MLEInfluence, only smoke tests in othermod
    • score_test standalone function has basic unit tests (not against other packages), not score_test method in BetaResults

    nothing for diagnostic and specification tests

    predict (and get_prediction ?) has which="var" using model method _predict_var

    • not in docstring
    • no unit tests in othermod, AFAICS
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions