Skip to content

ENH: add get_prediction to MNLogit, NonlinearDeltaCov for multinomial #7888

Open

Description

@josef-pkt

In #7870 I leave MNLogit get_prediction as NotImplementedError
OrderModel also does not have get_prediction and not much for predict

The problem in MNLogit is that params is 2-d. We need to ravel to patch params to cov_params.
Similar case is already implemented in discrete MultinomialModel._derivative_exog for margins.

see next comment for using NonlinearDeltaCov for average prediction (probabilities of each choice)

related usecase: I started with some diagnostics common to models with categorical/multinomial response, e.g. base on average probabilities (e.g. stochastically-larger residuals) and Hosmer-Lemeshow type binning.

Activity

  1. added this to the 0.14 milestone on Nov 18, 2021
  2. josef-pkt commented on Nov 18, 2021

    @josef-pkt
    MemberAuthor

    NonlinearDeltaCov seems to work using ravelling (using a model from a unit test
    The inherited wald results method res1._get_wald_nonlinear(func_pred) raises an exception with shape error
    2-d return of func does not work yet in NonlinearDeltaCov.

    from statsmodels.discrete.tests.test_discrete import TestMNLogitNewtonBaseZero
    ut = TestMNLogitNewtonBaseZero()
    ut.setup_class()
    res1 = ut.res1
    
    
    from statsmodels.stats._delta_method import NonlinearDeltaCov
    
    def func_pred(p):
        p = p.reshape(6, 6)
        r = res1.model.predict(p).mean(0)
        return r
    
    nl = NonlinearDeltaCov(func_pred, res1.params.ravel(), res1.cov_params(), deriv=None, func_args=None)
    print(nl.summary())
                                 Test for Constraints                             
    ==============================================================================
                     coef    std err          z      P>|z|      [0.025      0.975]
    ------------------------------------------------------------------------------
    c0             0.2119      0.571      0.371      0.711      -0.908       1.332
    c1             0.1907      0.893      0.213      0.831      -1.560       1.941
    c2             0.1144      1.325      0.086      0.931      -2.482       2.711
    c3             0.0392      0.284      0.138      0.890      -0.518       0.596
    c4             0.0996      2.041      0.049      0.961      -3.900       4.099
    c5             0.1589      2.712      0.059      0.953      -5.157       5.475
    c6             0.1854      7.336      0.025      0.980     -14.193      14.564
    ==============================================================================
    
    res1.predict().mean(0)
    array([0.21186441, 0.19067797, 0.11440678, 0.03919492, 0.09957627,
           0.15889831, 0.18538136])
    

    std err and other columns are not verified yet.

  3. modified the milestones: 0.14, 0.15 on Jun 23, 2023
  4. josef-pkt commented on Jun 23, 2023

    @josef-pkt
    MemberAuthor

    The problem here will be how to vectorize over obs, if exog is not just one row (without aggregation) and we predict several observations.

    i.e. expected, se/sd will be 2-dim, confint will be 3 dim similar to fittedvalues, bse, pvalues, ..., conv_int in results class
    summary/summary_frame would need multi-index, i.e. flattened results

    other option to support:
    getting cov for one multivariate prediction, as they are not independent (as for separate observations)

    related implementation issue: multi-output which in get_prediction, e.g. which="prob" in count models, which is also not vectorized yet (AFAIR, or did I fix that already?)

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions