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
Implement generalized additive models, ordered model (ordinal) #4908
It seems that there are a couple fledgling Python projects working on similar things, but might it be worthwhile implementing these models within statsmodels? This is one domain where I feel that R still surpasses Python, so there's certainly some fertile ground for new development.
update (josef)
second part of this issue is for ordered model starting #4908 (comment)
GAM merged in #5481
OrderedModel (in miscmodels) merged in #7035
sandbox.gam predates statsmodels and uses backfitting instead of plain scipy optimizers which is more useful for including kernel regression but not as useful for splines.
GAM is a difficult PR because it needs to be integrated with several other things.
I applied for a NUMFOCUS small development grant that includes merging GAM, but the decision is not out yet. If I get it, then GAM should be merged within a month or two.
Conceptually what is incomplete or insufficient in GAM are covariance matrices. R's mgcv has several options and changed its behavior several times during its lifetime.
Currently my plan is mainly to focus on the standard HC1 sandwich covariance for M-estimators.
It is however difficult to work on that until after GAM has been integrated with the penalized mixin class #4576 where I fixed some bugs that are still in the GAM version.
Another conceptual issue that I was struggling with are effective degrees of freedom for generic cases, e.g. when we don't have directly the WLS/IRLS interpretation available.
As a related issue, I added experimental support for GLMInfluence and MLEInfluence #4732
The basic statistics looke good but it needs a second round of review and will need extensions.
I did a brief check, and I don't seem to have any up to date notebooks for penalized mixin usage. (My old notebooks, e.g. on my computer, don't work correctly anymore because I changed the defaults in the classes, and it looks like I never updated them) #4683 and the notebooks there seem to use the merged versions.
I could take a look at the PenalizedMixin class and see if there's anything helpful I could contribute with regard to classes for specific distributions. It will certainly require some research on my end to familiarize myself better with the relevant mathematics.
The basic structure is penalized maximum likelihood similar to GAM. Our GAM uses the PenalizedMixin with a specific L2 penalization structure for the spline terms.
The GAM PR (GSOC project) also had added a penalized IRLS optimization method in analogy to the unpenalized GLM.
The GAM PR does not include anything specific for the additive linear model, e.g. penalized least squares, but includes penalized MLE with Gaussian family as (essentially) equivalent model.
The PenalizedMixin is designed for using different penalization classes, e.g. I was using "structured" SCAD that can penalize linear transformation of the parameters with an example for penalization of pairwise contrasts for a categorical variable.
To follow up on the above: I just got a notification that the NumFOCUS small development grant has been approved, so finishing and merging GAM, and refactoring of Probability Plots is on my schedule for the next one to two months.
It would be great if you were to work on something related because that would make discussing and reviewing problems and design decisions much easier.
I won't start right away, I have two PRs to finish before switching topics again (and my wife an I are buying a house on Monday).
Congrats! I'd be happy to help out. Just as a heads up, my background is primarily in statistics / mathematics and so my software development skills are definitely not expert level.
Are there any papers you'd recommend that give a general survey of the literature?
I need to look it up, I don't find anything right now.
The main reference for the GAM implementation is Simon Wood and his mgcv package which should have additional references.
I have piles of articles that I printed but I haven't looked at splines and GAM much since 2015, although a lot more on general penalized MLE for various models.
In general there was debate between two groups/approaches, one put effort in selecting knots and knot location, and the second put more effort on penalization with a simple knot selection.
I hat strong preference for the later because knot selection is much more computing intensive,
e.g. using equal spaced knots and simple difference penalization
Eilers, Paul H. C.; Marx, Brian D. Flexible smoothing with B -splines and penalties. Statist. Sci. 11 (1996), no. 2, 89--121. doi:10.1214/ss/1038425655. https://projecteuclid.org/euclid.ss/1038425655
They, or Marx with co-authors, have several similar articles. Eilers has a (relatively simple) book, AFAIR, that I partially read but don't have available right now. (AFAIR, it wasn't detailed enough to help a lot with implementation details).
For more general or other penalization topics, I'm a big fan of Jianqing Fan's work.
my background is primarily in statistics / mathematics and so my software development skills are definitely not expert level.
That's great, we don't have enough statisticians working on statsmodels.
(My background is economics and econometrics and I had and have to learn "statistician's statistics", and also parts of "Modern Econometrics", while working on scipy and statsmodels.)
(For GSOC I have been arguing that statistics, econometrics or math background is more important than software development skill or familiarity with Python, because the latter are easier to pick up quickly at the level that we require.)
I still had the google search for eilers and marx open
Eilers, P. H. and Marx, B. D. (2010), Splines, knots, and penalties. WIREs Comp Stat, 2: 637-653. doi:10.1002/wics.125
AFAIR, this is an overview article that I read early on
How do you feel about incorporating penalized ordinal regression into statsmodels? I came across this Cross Validated post suggesting there may be some interest in this feature:
2015 was a year when I wrote a lot of prototypes #2600 has a draft version for ordered models, with logit and probit. There is a large demand for it but personally I have too many "high priority" projects which often can be my taste and not current user demand.
Note, I write a lot of prototype code not because I want to add immediately a new model, but more to help be understand the underlying structure for medium to long term planning for statsmodels, e.g. what changes or enhancements do we need to the generic structure of our models, results, and supporting functions, and it helps in reviewing PRs when someone is working on something related.
In terms of approaches to penalized estimation Kerby Shedden and I are splitting the work. Kerby added elastic net based on coordinate descend closer to the machine learning literature. I'm working mainly on problems where we don't have too many explanatory variables and where we can use standard optimization. For example GAM and PenalizedMixin are based on standard dense linear algebra or scipy optimizers, and we just use a penalized version of our standard models.
(I need to run. Bottom line: There is a large number of models that are still missing and that would find a large user base when they become available. What gets added depends on a mix of priority and personal interest of developers.)
Would you say then that getting an OrderedLogit model (e.g., proportional odds model) might be a worthwhile goal for the near term? I could work on this.
@josef-pkt I have a very simple working example (uniformly distributed ordered categories and predictors unrelated to those categories) where it seems we can consistently estimate the parameters of a proportional odds model. I will need to figure out how to fit the pieces into the statsmodels framework, but in the meantime is there a way I can share a notebook for you to look over when you have time?
put a notebook in a gist which makes it easy to look at.
(aside for later if needed: When we include notebooks in the statsmodels documentation, then we remove the output so that the repository memory usage doesn't get increased by generated plots and tables.)
there is also a difference in tradition, origin in literature for essentially the same models.
e,g GLM binomial with probit link is the same as discrete Probit
in GLM the distribution is binomial and probit is just a specific link function.
in discrete Probit is build on a latent underlying variable that has a normal distribution, but we only observe a "censored" version.
GLM comes from the statistics tradition and Probit from the econometrics tradition.
The difference is in the surrounding code/results and what is easier to add under one or the other version.
My OrderedModel follows the latent variable interpretation so it should be able to share pattern and code with censored or truncated models.
I updated it from the simple "null" model of before so that now there are at least non-zero coefficients to estimate, and the simulation parameters can be tweaked to sanity check that it works as intended.
Activity
jbrockmendel commentedon Aug 15, 2018
There exists
statsmodels.sandbox.gam.sandboxcode is not for the most part in useable form, but there may be something worth salvaging from it.josef-pkt commentedon Aug 15, 2018
#4575 and precursor PRs
sandbox.gam predates statsmodels and uses backfitting instead of plain scipy optimizers which is more useful for including kernel regression but not as useful for splines.
dsaxton commentedon Aug 15, 2018
Thanks, is there a certain place (conceptually) that would make a sensible starting point for new contributions?
josef-pkt commentedon Aug 15, 2018
GAM is a difficult PR because it needs to be integrated with several other things.
I applied for a NUMFOCUS small development grant that includes merging GAM, but the decision is not out yet. If I get it, then GAM should be merged within a month or two.
Conceptually what is incomplete or insufficient in GAM are covariance matrices. R's mgcv has several options and changed its behavior several times during its lifetime.
Currently my plan is mainly to focus on the standard HC1 sandwich covariance for M-estimators.
It is however difficult to work on that until after GAM has been integrated with the penalized mixin class #4576 where I fixed some bugs that are still in the GAM version.
One related possibility is to get started with the pattern of penalized models would be to add an official PoissonPenalized class. The plan is to add several XXXPenalized classes.
Currently I'm using generic version as test cases, but there are no public model specific classes yet.
e.g. https://github.com/statsmodels/statsmodels/pull/4576/files#diff-998ca927d33550860463cd03cb0cbbdcR20
The main work would be to see what Results methods need to be overwritten from the basic model class, e.g. inherited from Poisson results.
Another conceptual issue that I was struggling with are effective degrees of freedom for generic cases, e.g. when we don't have directly the WLS/IRLS interpretation available.
As a related issue, I added experimental support for GLMInfluence and MLEInfluence #4732
The basic statistics looke good but it needs a second round of review and will need extensions.
josef-pkt commentedon Aug 15, 2018
I did a brief check, and I don't seem to have any up to date notebooks for penalized mixin usage. (My old notebooks, e.g. on my computer, don't work correctly anymore because I changed the defaults in the classes, and it looks like I never updated them)
#4683 and the notebooks there seem to use the merged versions.
dsaxton commentedon Aug 15, 2018
I could take a look at the PenalizedMixin class and see if there's anything helpful I could contribute with regard to classes for specific distributions. It will certainly require some research on my end to familiarize myself better with the relevant mathematics.
josef-pkt commentedon Aug 16, 2018
The basic structure is penalized maximum likelihood similar to GAM. Our GAM uses the PenalizedMixin with a specific L2 penalization structure for the spline terms.
The GAM PR (GSOC project) also had added a penalized IRLS optimization method in analogy to the unpenalized GLM.
The GAM PR does not include anything specific for the additive linear model, e.g. penalized least squares, but includes penalized MLE with Gaussian family as (essentially) equivalent model.
The PenalizedMixin is designed for using different penalization classes, e.g. I was using "structured" SCAD that can penalize linear transformation of the parameters with an example for penalization of pairwise contrasts for a categorical variable.
Penalization is a large topic, so it might be useful for you to narrow down to what you are interested in.
e.g. #4038
I have been using a 'topic-penalization' label for related issues (likely missing some issue) https://github.com/statsmodels/statsmodels/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen++label%3Atopic-penalization+
I'm available to answer question related to this (or other topics outside tsa) AFAIR or as far as I can quickly look them up.
dsaxton commentedon Aug 17, 2018
Are there any papers you'd recommend that give a general survey of the literature?
josef-pkt commentedon Aug 17, 2018
To follow up on the above: I just got a notification that the NumFOCUS small development grant has been approved, so finishing and merging GAM, and refactoring of Probability Plots is on my schedule for the next one to two months.
It would be great if you were to work on something related because that would make discussing and reviewing problems and design decisions much easier.
I won't start right away, I have two PRs to finish before switching topics again (and my wife an I are buying a house on Monday).
dsaxton commentedon Aug 17, 2018
Congrats! I'd be happy to help out. Just as a heads up, my background is primarily in statistics / mathematics and so my software development skills are definitely not expert level.
josef-pkt commentedon Aug 17, 2018
I need to look it up, I don't find anything right now.
The main reference for the GAM implementation is Simon Wood and his mgcv package which should have additional references.
I have piles of articles that I printed but I haven't looked at splines and GAM much since 2015, although a lot more on general penalized MLE for various models.
In general there was debate between two groups/approaches, one put effort in selecting knots and knot location, and the second put more effort on penalization with a simple knot selection.
I hat strong preference for the later because knot selection is much more computing intensive,
e.g. using equal spaced knots and simple difference penalization
Eilers, Paul H. C.; Marx, Brian D. Flexible smoothing with B -splines and penalties. Statist. Sci. 11 (1996), no. 2, 89--121. doi:10.1214/ss/1038425655. https://projecteuclid.org/euclid.ss/1038425655
They, or Marx with co-authors, have several similar articles. Eilers has a (relatively simple) book, AFAIR, that I partially read but don't have available right now. (AFAIR, it wasn't detailed enough to help a lot with implementation details).
For more general or other penalization topics, I'm a big fan of Jianqing Fan's work.
That's great, we don't have enough statisticians working on statsmodels.
(My background is economics and econometrics and I had and have to learn "statistician's statistics", and also parts of "Modern Econometrics", while working on scipy and statsmodels.)
(For GSOC I have been arguing that statistics, econometrics or math background is more important than software development skill or familiarity with Python, because the latter are easier to pick up quickly at the level that we require.)
josef-pkt commentedon Aug 18, 2018
I still had the google search for eilers and marx open
Eilers, P. H. and Marx, B. D. (2010), Splines, knots, and penalties. WIREs Comp Stat, 2: 637-653. doi:10.1002/wics.125
AFAIR, this is an overview article that I read early on
https://www.researchgate.net/publication/290086196_Twenty_years_of_P-splines
seems to be a more recent version on the same topic, but AFAIR I haven't read it
dsaxton commentedon Aug 18, 2018
How do you feel about incorporating penalized ordinal regression into
statsmodels? I came across this Cross Validated post suggesting there may be some interest in this feature:https://stats.stackexchange.com/questions/168262/ordinal-logistic-regression-in-python
Coincidentally one of my PhD advisor's former students (Bret Hanlon) has done some work on implementing these models in R:
https://arxiv.org/pdf/1706.05003.pdf
I will get in touch with him and see if he has any ideas on this front.
josef-pkt commentedon Aug 18, 2018
2015 was a year when I wrote a lot of prototypes
#2600 has a draft version for ordered models, with logit and probit. There is a large demand for it but personally I have too many "high priority" projects which often can be my taste and not current user demand.
Note, I write a lot of prototype code not because I want to add immediately a new model, but more to help be understand the underlying structure for medium to long term planning for statsmodels, e.g. what changes or enhancements do we need to the generic structure of our models, results, and supporting functions, and it helps in reviewing PRs when someone is working on something related.
In terms of approaches to penalized estimation Kerby Shedden and I are splitting the work. Kerby added elastic net based on coordinate descend closer to the machine learning literature. I'm working mainly on problems where we don't have too many explanatory variables and where we can use standard optimization. For example GAM and PenalizedMixin are based on standard dense linear algebra or scipy optimizers, and we just use a penalized version of our standard models.
(I need to run. Bottom line: There is a large number of models that are still missing and that would find a large user base when they become available. What gets added depends on a mix of priority and personal interest of developers.)
dsaxton commentedon Aug 19, 2018
Would you say then that getting an OrderedLogit model (e.g., proportional odds model) might be a worthwhile goal for the near term? I could work on this.
12 remaining items
dsaxton commentedon Aug 20, 2018
@josef-pkt I have a very simple working example (uniformly distributed ordered categories and predictors unrelated to those categories) where it seems we can consistently estimate the parameters of a proportional odds model. I will need to figure out how to fit the pieces into the
statsmodelsframework, but in the meantime is there a way I can share a notebook for you to look over when you have time?josef-pkt commentedon Aug 20, 2018
put a notebook in a gist which makes it easy to look at.
(aside for later if needed: When we include notebooks in the statsmodels documentation, then we remove the output so that the repository memory usage doesn't get increased by generated plots and tables.)
josef-pkt commentedon Aug 20, 2018
(written earlier today)
there is also a difference in tradition, origin in literature for essentially the same models.
e,g GLM binomial with probit link is the same as discrete Probit
in GLM the distribution is binomial and probit is just a specific link function.
in discrete Probit is build on a latent underlying variable that has a normal distribution, but we only observe a "censored" version.
GLM comes from the statistics tradition and Probit from the econometrics tradition.
The difference is in the surrounding code/results and what is easier to add under one or the other version.
My OrderedModel follows the latent variable interpretation so it should be able to share pattern and code with censored or truncated models.
dsaxton commentedon Aug 20, 2018
I've created a gist here: https://gist.github.com/dsaxton/92baf15b74c859e714a83f09029bf5b4
I updated it from the simple "null" model of before so that now there are at least non-zero coefficients to estimate, and the simulation parameters can be tweaked to sanity check that it works as intended.
bashtage commentedon Jul 19, 2019
Master has some support for GAM. Closing.
bashtage commentedon Jul 19, 2019
Probably worth saving the notebook.
[-]Implement generalized additive models[/-][+]Implement generalized additive models, ordered model (ordinal)[/+]josef-pkt commentedon Jun 4, 2021
both GAM and OrderedModel have been merged in initial versions.
comment with notebook is cross-linked in #7069
closing this