Skip to content

NegativeBinomial df_model df_resid, extra parameter #1624

Open

Description

@josef-pkt

I run into a problem that df_model and df_resid don't count the alpha shape parameter in NegativeBinomial, but the generic fit_regularized does. #1623

(fit_regularized uses number of non-zero params, generic Discrete uses rank(exog))

df_resid is irrelevant for the normal based asymptotic cov_params and bse
the only place I have seen it so far is in aic and in bic and NegativeBinomial overwrites both to add the shape parameter back in.

    @cache_readonly
    def aic(self):
        # + 1 because we estimate alpha
        return -2*(self.llf - (self.df_model + self.k_constant + 1))

    @cache_readonly
    def bic(self):
        # + 1 because we estimate alpha
        return -2*self.llf + np.log(self.nobs)*(self.df_model +
                                                self.k_constant + 1)

but it's added in also for loglike_method = 'geometric' which doesn't have the extra params.

I think df_model and df_resid should be changed to take the extra shape parameters into account, since they affect the mean estimation.

(based on my reading of the test files Stata defines df_m based on exog without constant, but uses all params in aic, auxiliary parameters are stored separately e(k aux))

extra:
DiscreteResults aic and bic do not take into account that there might not be a constant in the model, but it's always subtracted
self.df_model = float(np_matrix_rank(self.exog) - 1)
so this cancels

Activity

  1. josef-pkt commented on Apr 22, 2014

    @josef-pkt
    MemberAuthor

    as reference

    e(N) number of observations
    e(k) number of parameters
    e(k aux) number of auxiliary parameters
    e(k eq) number of equations in e(b)
    e(k eq model) number of equations in overall model test
    e(k dv) number of dependent variables
    e(df m) model degrees of freedom
    
    e(rank) rank of e(V)
    e(rank0) rank of e(V) for constant-only model
    

    example for nbreg with RandHI dataset with 10 regressors including constant

                   e(rank) =  11
                      e(N) =  20190
                     e(ic) =  3
                      e(k) =  11
                   e(k_eq) =  2
                   e(k_dv) =  1
              e(converged) =  1
                     e(rc) =  0
                     e(ll) =  -43383.66207984566
             e(k_eq_model) =  1
                   e(ll_0) =  -44199.2744356343
                  e(rank0) =  2
                   e(df_m) =  9
                   e(chi2) =  1631.224711577292
                      e(p) =  0
                   e(ll_c) =  -62419.58858002518
                 e(chi2_c) =  38071.85300035904
                   e(r2_p) =  .0184530711465952
                  e(k_aux) =  1
                  e(alpha) =  1.292953340588578
    
  2. josef-pkt commented on Apr 22, 2014

    @josef-pkt
    MemberAuthor

    searching for the use of df_resid, df_model

    df_resid is set but never used in discrete_model.py I will be used by some methods inherited by base

    df_model is used in

    def llr_pvalue(self): return stats.chisqprob(self.llr, self.df_model)
    needs k_exog - k_constant
    BUG: if k_constant is zero, then k_constant is incorrectly assumed to be one in discrete df_model
    llnull might not make much sense if there is no constant

    discrete: def aic(self): return -2*(self.llf - (self.df_model+1))
    needs number of parameter = k_exog

    negbin: def aic(self): return -2*(self.llf - (self.df_model + self.k_constant + 1))
    needs number of parameter = k_exog + k_aux = k_params
    BUG: if k_constant = 0, then we don't compensate that df_model is always reduced by one
    BUG: negbin with geometric adds 1 even though k_aux=0

    Multinomial def aic(self): return -2*(self.llf - (self.df_model+self.model.J-1))
    This assumes we always subtracted 1 for a constant from each choice (J-1 constants), which is done in df_model independently of k_constant.

  3. josef-pkt commented on Apr 22, 2014

    @josef-pkt
    MemberAuthor

    We had to add some regression test for NegativeBinomial, where reference numbers were not available see 1df81b3 some additional numbers have been verified since then

  4. josef-pkt commented on Apr 23, 2014

    @josef-pkt
    MemberAuthor

    general to degrees of freedom, df_resid

    http://www.stata.com/manuals13/rlrtest.pdf
    technical note on bottom of page 4:
    regress and similar don't count variance estimation for degrees of freedom (uncorrelated with mean parameters). For lrtest it should be counted and increase df by one, as MLE. (but cancels when comparing to linear models)
    Other models (arch) include the extra estimated parameters in the df, and lrtest might get the df wrong if two models are compared that don't use the same definition (include or not include extra parameters in df count). lrtest has explicit option for users to specify the df that should be used.

    (problems with inconsistent definitions)

  5. josef-pkt commented on May 22, 2014

    @josef-pkt
    MemberAuthor

    Another one: Found in genmod test_glm.py

    include scale estimate in aic or not

        decimal_aic_R = DECIMAL_4
        def test_aic_R(self):
            # R includes the estimation of the scale as a lost dof
            # Doesn't with Gamma though
            if self.res1.scale != 1:
                dof = 2
            else:
                dof = 0
            assert_almost_equal(self.res1.aic+dof, self.res2.aic_R,
                    self.decimal_aic_R)
    

    (I remember this was difficult to match up between Stata and R, comments in tests still point to different definitions between R and Stata and us.)

  6. josef-pkt commented on Jul 22, 2014

    @josef-pkt
    MemberAuthor

    PR #1623 added k_extra as attribute to NegativeBinomial and fixed aic, bic for it.
    But it's not yet fully used and not extended to other models or generic code.

  7. josef-pkt commented on Oct 29, 2022

    @josef-pkt
    MemberAuthor

    bump,

    I just checked this again.
    TestNegativeBinomialPNB2Newton test class verifies df_resid, but there is no code comment about the origin of the reference number. I guess this was manually set.

    current context:
    #8475 fixing default df_resid, df_model in GenericLikelihoodModel
    and trying to decide what the definition of df_resid should be if we have extra parameters in MLE
    We need to change df_resid in NegativeBinomial so that it is nobs - k_params

  8. added this to the 0.15 milestone on Oct 29, 2022
  9. josef-pkt commented on Dec 1, 2022

    @josef-pkt
    MemberAuthor

    removing prio-high

    AFAICS, no bugs, but we will change definitions and refactor together with #8481

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