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
NegativeBinomial df_model df_resid, extra parameter #1624
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
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
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.
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
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.
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.)
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.
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
Activity
josef-pkt commentedon Apr 22, 2014
as reference
example for nbreg with RandHI dataset with 10 regressors including constant
josef-pkt commentedon Apr 22, 2014
searching for the use of df_resid, df_model
df_resid is set but never used in
discrete_model.pyI will be used by some methods inherited by basedf_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
geometricadds 1 even though k_aux=0Multinomial
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.
josef-pkt commentedon Apr 22, 2014
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
josef-pkt commentedon Apr 23, 2014
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
lrtestmight 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).lrtesthas explicit option for users to specify the df that should be used.(problems with inconsistent definitions)
josef-pkt commentedon May 22, 2014
Another one: Found in genmod
test_glm.pyinclude scale estimate in aic or not
(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.)
josef-pkt commentedon Jul 22, 2014
PR #1623 added
k_extraas 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.
josef-pkt commentedon Oct 29, 2022
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
josef-pkt commentedon Dec 1, 2022
removing prio-high
AFAICS, no bugs, but we will change definitions and refactor together with #8481