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
diagnostic: link test - nonlinearity test for many model #1479
estimate y with exog_test = (fittedvalues, fittedvalues**2)
reject correct specification if fittedvalues**2 is significant
there might be something similar for OLS already, but Stata's link test works for discrete models, e.g. after Logit.
For example for Logit we have to run a Logit(endog, exog_test).fit(...) again.
(do we have all information to run model and fit with the same options again?)
if rejected: either some missing non-linearities in the explanatory variables or incorrectly specified link function
edit don't use fittedvalues, use predict(linear=True), fittedvalues should be refactored to return the transformed, expected mean or estimated mean function E(y|x).
You should be able to re-run all models with results.mle_settings, if we need to. I found one example in tsa recently where it wasn't correctly attached, but it's fixed now. If it's not there, it's a bug.
we need also the extra args or kwds for the Model.__init__ to write generic code.
eg. Poisson init_extra_args = ['offset', 'exposure'] or weights and sigma in WLS, GLS.
and more in GLM, RLM.
my_results.model.__class__.__init__(endog, exog, **init_extra_kwds)
instead of checking the model class and conditional if isinstance(my_results.model, sm.Poisson): ...
might be needed also for pairs bootstrap and for multiple imputation
This article includes several LM test for misspecification in binary models implemented as variable addition LM tests using artificial regression following Davidson MacKinnon and Wooldridge.
I have those separately in diagnostics_other, but we can directly use the score_test that uses the hessian and not a OPG approximation.
They have a table about the exog_extra that need to be added for the different tests.
For most of them there should be an analogue for other GLM families, with some differences, e.g. heteroscedasticity in binary models is the same as a non-linear misspecification, while in non-binary models we can separate nonlinearity from heteroscedasticity.
(I don't remember a reference right now for the general GLM case.)
about the API: I would like a GLMResults method like score_test_diagnostic that combines several test implemented as variable addition score tests, i.e. constructs the exog_extra and delegates to score_test.
To have this attached to the Results class, we would need to have a version that computes the same tests for most models. Only RESET test might be possible to get started.
Or we list in the docstring the tests that are available for each family.
Otherwise, I will start a statsmodels.stats.diagnostics_glm with standalone functions that can be added later, and be reused in discrete.
One issue, my guess is that we want to separate mean tests (i.e. variable addition to the mean function) from other specification tests, e.g. on second moments, heteroscedasticity/dispersion and correlation. Those require a different "endog", e.g. squared (pearson) residuals.
name for option is unclear results.score_test_diagnostic(test="reset", **kwds)
or "kind" instead of "test"
and kwds would have two parts, keywords for the construction of exog_extra, e.g. order in RESET test, and keywords for the score_test, e.g. "cov_type" and "observed"
If we use **kwds, then we need to separate them internally. Or we use test_kwds requiring a dict.
Based on my notes when I read this, it just adds group dummies to the added variable test, where groups are defined by binning exog or predicted values. It might be a bit more complicated than that.
According to my notes, Tsiatis takes the full EIM in the score test, the other two tests take short cuts and are not full score_tests, i.e. ignore some correlation.
My guess is that this now takes 5 lines to implement as score test.
Aside about binary models: If nonlinearity and heteroscedasticity are the same/indistinguishable in binary models, then what happens if we use cov_type='HC0' in the score test?
(I guess we might still have interpretation in terms of specific functional forms.)
estimate y with exog_test = (fittedvalues, fittedvalues**2)
reject correct specification if fittedvalues**2 is significant
there might be something similar for OLS already, but Stata's link test works for discrete models, e.g. after Logit.
For example for Logit we have to run a Logit(endog, exog_test).fit(...) again.
(do we have all information to run model and fit with the same options again?)
if rejected: either some missing non-linearities in the explanatory variables or incorrectly specified link function
edit don't use fittedvalues, use predict(linear=True), fittedvalues should be refactored to return the transformed, expected mean or estimated mean function E(y|x).
I am trying to contribute to this issue but having trouble figuring out what needs to be done. Is it about a linktest for GLM that takes model as an argument?
Essentially we need the same thing for other models than OLS.
The call to OLS needs to be replace by a GLM model. The extra keywords that the user used when creating the model like offset, exposure and weights in the GLM case are available in the _init_kwds attribute. There should be some examples for the creation of a new GLM model in the statsmodels source, but I don't remember where.
based on the canary et al article, tsiatis is a proper CMT, score_test for variable addition of dummy variables for bins of predictions or regressors, i.e. score_obs + added variable score_obs,
(note we can do it now also with robust cov)
Canary, Jana D., Leigh Blizzard, Ronald P. Barry, David W. Hosmer, and Stephen J. Quinn. “A Comparison of the Hosmer–Lemeshow, Pigeon–Heyse, and Tsiatis Goodness-of-Fit Tests for Binary Logistic Regression under Two Grouping Methods.” Communications in Statistics - Simulation and Computation 46, no. 3 (March 16, 2017): 1871–94. https://doi.org/10.1080/03610918.2015.1017583.
issue #9103 rainbow test with mahalanobis central observations
same idea can be applied to reset or variable addition test, i.e. break variable addition into 3 sets lower tail, center, upper tail observation based on mahalanobis distance of exog and we could compare with the same break based on ordering by fitted values.
Activity
jseabold commentedon Mar 15, 2014
You should be able to re-run all models with
results.mle_settings, if we need to. I found one example in tsa recently where it wasn't correctly attached, but it's fixed now. If it's not there, it's a bug.josef-pkt commentedon Mar 15, 2014
we need also the extra args or kwds for the
Model.__init__to write generic code.eg. Poisson
init_extra_args= ['offset', 'exposure'] orweightsandsigmain WLS, GLS.and more in GLM, RLM.
my_results.model.__class__.__init__(endog, exog, **init_extra_kwds)instead of checking the model class and conditional
if isinstance(my_results.model, sm.Poisson): ...might be needed also for pairs bootstrap and for multiple imputation
josef-pkt commentedon Jun 7, 2018
bump for 0.10
this is now a two linear using score_test, after #4716
https://gist.github.com/josef-pkt/f20abdbd360c2e549f73e98844f5d2c4
parking a reference
Ramalho, Esmeralda A., and Joaquim J. S. Ramalho. 2012. “Alternative Versions of the RESET Test for Binary Response Index Models: A Comparative Study*.” Oxford Bulletin of Economics and Statistics 74 (1): 107–30. https://doi.org/10.1111/j.1468-0084.2011.00654.x.
This article includes several LM test for misspecification in binary models implemented as variable addition LM tests using artificial regression following Davidson MacKinnon and Wooldridge.
I have those separately in diagnostics_other, but we can directly use the score_test that uses the hessian and not a OPG approximation.
They have a table about the exog_extra that need to be added for the different tests.
For most of them there should be an analogue for other GLM families, with some differences, e.g. heteroscedasticity in binary models is the same as a non-linear misspecification, while in non-binary models we can separate nonlinearity from heteroscedasticity.
(I don't remember a reference right now for the general GLM case.)
about the API: I would like a GLMResults method like
score_test_diagnosticthat combines several test implemented as variable addition score tests, i.e. constructs the exog_extra and delegates to score_test.To have this attached to the Results class, we would need to have a version that computes the same tests for most models. Only RESET test might be possible to get started.
Or we list in the docstring the tests that are available for each family.
Otherwise, I will start a statsmodels.stats.diagnostics_glm with standalone functions that can be added later, and be reused in discrete.
One issue, my guess is that we want to separate mean tests (i.e. variable addition to the mean function) from other specification tests, e.g. on second moments, heteroscedasticity/dispersion and correlation. Those require a different "endog", e.g. squared (pearson) residuals.
name for option is unclear
results.score_test_diagnostic(test="reset", **kwds)or "kind" instead of "test"
and kwds would have two parts, keywords for the construction of exog_extra, e.g. order in RESET test, and keywords for the score_test, e.g. "cov_type" and "observed"
If we use
**kwds, then we need to separate them internally. Or we usetest_kwdsrequiring a dict.josef-pkt commentedon Jun 7, 2018
related article by mostly the same authors.
I just saw it next to the other article, but AFAIR I didn't read this one.
Ramalho, Esmeralda A., Joaquim J. S. Ramalho, and José M. R. Murteira. 2014. “A Generalized Goodness-of-Functional Form Test for Binary and Fractional Regression Models.” The Manchester School 82 (4): 488–507. https://doi.org/10.1111/manc.12032.
josef-pkt commentedon Jun 7, 2018
another reference found in my pile of glm diagnostic papers
Canary, Jana D., Leigh Blizzard, Ronald P. Barry, David W. Hosmer, and Stephen J. Quinn. 2016. “Summary Goodness-of-Fit Statistics for Binary Generalized Linear Models with Noncanonical Link Functions.” Biometrical Journal 58 (3): 674–90. https://doi.org/10.1002/bimj.201400079.
Based on my notes when I read this, it just adds group dummies to the added variable test, where groups are defined by binning exog or predicted values. It might be a bit more complicated than that.
According to my notes, Tsiatis takes the full EIM in the score test, the other two tests take short cuts and are not full score_tests, i.e. ignore some correlation.
My guess is that this now takes 5 lines to implement as score test.
Aside about binary models: If nonlinearity and heteroscedasticity are the same/indistinguishable in binary models, then what happens if we use
cov_type='HC0'in the score test?(I guess we might still have interpretation in terms of specific functional forms.)
stevenlis commentedon Feb 20, 2019
According to the Stata doc on page 3, a
linktestfor a OLS is just :Example for SM:
From Stata:
mina1987 commentedon Apr 9, 2019
I am trying to contribute to this issue but having trouble figuring out what needs to be done. Is it about a linktest for GLM that takes model as an argument?
josef-pkt commentedon Apr 9, 2019
look at https://github.com/statsmodels/statsmodels/blob/master/statsmodels/stats/outliers_influence.py#L102
Essentially we need the same thing for other models than OLS.
The call to OLS needs to be replace by a GLM model. The extra keywords that the user used when creating the model like offset, exposure and weights in the GLM case are available in the
_init_kwdsattribute. There should be some examples for the creation of a new GLM model in the statsmodels source, but I don't remember where.josef-pkt commentedon Oct 30, 2023
bouncing around topics
based on the canary et al article, tsiatis is a proper CMT, score_test for variable addition of dummy variables for bins of predictions or regressors, i.e. score_obs + added variable score_obs,
(note we can do it now also with robust cov)
related issues for reset test, e.g. #3690
Canary, Jana D., Leigh Blizzard, Ronald P. Barry, David W. Hosmer, and Stephen J. Quinn. “A Comparison of the Hosmer–Lemeshow, Pigeon–Heyse, and Tsiatis Goodness-of-Fit Tests for Binary Logistic Regression under Two Grouping Methods.” Communications in Statistics - Simulation and Computation 46, no. 3 (March 16, 2017): 1871–94. https://doi.org/10.1080/03610918.2015.1017583.
TSIATIS, ANASTASIOS A. “A Note on a Goodness-of-Fit Test for the Logistic Regression Model.” Biometrika 67, no. 1 (January 1, 1980): 250–51. https://doi.org/10.1093/biomet/67.1.250.
josef-pkt commentedon Dec 23, 2023
issue #9103 rainbow test with mahalanobis central observations
same idea can be applied to reset or variable addition test, i.e. break variable addition into 3 sets lower tail, center, upper tail observation based on mahalanobis distance of exog and we could compare with the same break based on ordering by fitted values.