Skip to content

GAM from GSoC 2015 - #2744

Closed
DonBeo wants to merge 7 commits into
statsmodels:masterfrom
DonBeo:gam_v2
Closed

DonBeo wants to merge 7 commits into
statsmodels:masterfrom
DonBeo:gam_v2

Conversation

@DonBeo

@DonBeo DonBeo commented Dec 23, 2015

Copy link
Copy Markdown
Contributor

@josef-pkt
This is the cleaned version of the GAM branch.
In order to make it compatible with the latest version of statsmodels I had to remove the with_metaclass command.

In addition the GAMGLMResults is not working properly anymore as 3 tests fail.
They do not fail in my old version.

Let me know.

@josef-pkt

Copy link
Copy Markdown
Member

@DonBeo amend the commit and remove the statespace pyx file and force push
That file should be in gitignore, I think @ChadFulton

about GAMGLMResults

my guess
The original PR has a change in generalized_linear_model.py that you didn't copy over
https://github.com/statsmodels/statsmodels/pull/2435/files#diff-724e15f4019b2a24906f6b271d67e370
That will be a general solution, but is not yet in master. You could add that change to your branch as separate commit, otherwise you will not get the correct Results class

Otherwise, looks good AFAICS, TravisCI is working and we should get results after 11 to 20 minutes.

Thanks

@josef-pkt

Copy link
Copy Markdown
Member

with_metaclass is similar.
There is the compatibility code in the old PR, that is missing here
https://github.com/statsmodels/statsmodels/pull/2435/files#diff-3608ad6d7b5b9cf9f6e21df980bf6566
You need to copy this also over. Because your PR is so far the only one that uses this, I had added it for your PR. It is necessary for python compatibility. (taken from six or scikit-learn or a similar recipe.)

@josef-pkt

Copy link
Copy Markdown
Member

based on TravisCI test problems: the csv files are not included in the distribution package.
I'll need to check how those get to be included in the dist archive file.

@josef-pkt

Copy link
Copy Markdown
Member

the csv files are included for all results directories in setup.py. However, all current tests\results folders also have an __init__.py.
@DonBeo Try to put an empty __init__.py into the gam\tests\results directory and we see if then the csv files get included

@DonBeo

DonBeo commented Dec 23, 2015

Copy link
Copy Markdown
Contributor Author

I think all the previous problems are solved.
I think that there is something wrong in the way the SE is computed in the partial_values function.
see for example test_partial_values2 in statsmodels/gam/tests/test_gam.py

I think this is something we can solve later

@DonBeo DonBeo changed the title create GAM toolbox GAM from GSoC2015 Dec 23, 2015
@DonBeo DonBeo changed the title GAM from GSoC2015 GAM from GSoC 2015 Dec 23, 2015
Comment thread statsmodels/gam/gam.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be self.cov_params instead of self.normalized_cov_params b/c of the missing scale for gaussian for example

@josef-pkt

Copy link
Copy Markdown
Member

you didn't fix, amend or rebase the first commit, so the pyx file is part of the history here.

about the partial predict se: From a brief looks, I think the scale is missing. In Binomial and Poisson the scale is fixed at 1, but for Gaussian the scale is based on the residual errors like in OLS.
normalized_cov_params does not include the scale estimate, but the official cov_params does.
cov_params should also have an option to select columns, so it won't be necessary to do it directly.

In general normalized_cov_params shouldn't be used directly by the results because (as in this and other cases) it is not the actual covariance of the parameters (e.g. also when we use sandwich covariance)

@josef-pkt

Copy link
Copy Markdown
Member

It's starting to look good, The first python version is green on Travis. So there are only some compatibility problems

matplotlib use is optional and needs to be protected, see for example test_mice.py which was also fixed, tests need skipif, importing matplotlib needs try except protection, and we also have some helper functions for it
https://github.com/statsmodels/statsmodels/pull/2740/files

 File "/home/travis/miniconda2/envs/statsmodels-test/lib/python2.7/site-packages/statsmodels-0.8.0-py2.7-linux-x86_64.egg/statsmodels/gam/gam_cross_validation/gam_cross_validation.py", line 9, in <module>
    import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot

The problem on two other python versions that I checked is that we also use older patsy in the test runs that don't have the spline code yet. Either we update the patsy requirement or we need to make these parts optional based on recent enough patsy version.
Do you know in which patsy version the splines have been added?

@DonBeo

DonBeo commented Dec 24, 2015

Copy link
Copy Markdown
Contributor Author

According to https://github.com/pydata/patsy/commits/master/patsy/splines.py bsplines are part of patsy from 2013. Patsy seems to be almost a standalone library. It requires only

Python (version 2.6, 2.7, or 3.3+)
Six
NumPy

I assume that we can update the patsy requirements without side effects.

  1. In class GLMGAMResults(GLMResults): I can not call self.cov_params I do not understand how to access to this value.

  2. Regarding the .pyxfile I need to understand how to remove it from the history. My knowledge of git is close to 0

@DonBeo

DonBeo commented Dec 24, 2015

Copy link
Copy Markdown
Contributor Author

in one of the travis there is an error related to the use of super https://travis-ci.org/statsmodels/statsmodels/jobs/98645458

I do not have a python 2 environment. According to http://python-future.org/overview.html
I have to include something like this:

from __future__ import absolute_import, division, print_function
from builtins import (bytes, str, open, super, range,
                      zip, round, input, int, pow, object)

Is it the correct way to do it in statsmodels?

@josef-pkt

Copy link
Copy Markdown
Member

patsy: travisci uses patsy 0.2 for some python, 0.2 is too old for a package that should be updated with statsmodels.

super just needs the arguments super(this_class_name, self)

What's the problem with self.cov_params() in results? That should work. Note, it's a method.

Comment thread statsmodels/gam/smooth_basis.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this super is missing the arguments like the other ones super(BSplines, self)

@DonBeo

DonBeo commented Dec 24, 2015

Copy link
Copy Markdown
Contributor Author

Even with cov_params() the tests fail. I do not know what is the error due. Maybe we should go on and classify this as a bug

@josef-pkt

Copy link
Copy Markdown
Member

Do you have unit tests for cov_params or bse itself to know whether those are correct?

With no or little penalization and not a large number of knots cov_params and bse should be the same as or close to the standard GLM results.
I don't remember the details well enough to see right away how these are calculated. cov_params might currently not take the penalization into account.

Comment thread statsmodels/gam/gam.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is inefficient calculation. It calculates (nobs, nobs) and takes only the diagonal.

in getprediction I use this (which I'm too tired to understand)
(exog * np.dot(covb, exog.T).T).sum(1)

where
exog = smoother.basis_[:, mask]
covb = partial_cov_params

@DonBeo

DonBeo commented Dec 24, 2015

Copy link
Copy Markdown
Contributor Author

apparently cov_params has some problem with the penalization. I added a test if alpha=0 then cov_params is equivalent to the one obtained with GLM while cov_params>>0 if alpha=1.e-16

@groakat

groakat commented Feb 7, 2016

Copy link
Copy Markdown

Hey guys, has there been any progress in merging this PR?

@DonBeo

DonBeo commented Feb 7, 2016

Copy link
Copy Markdown
Contributor Author

Unfortunately during this period I did not have time to work on it. I have the impression that the first penalized linear model should be merged.

@groakat

groakat commented Feb 7, 2016

Copy link
Copy Markdown

Ok so maybe I will clone from master and merge it locally. I just wanted to play around with GAMs and see if they are useful for a project of mine.

@DonBeo

DonBeo commented Feb 7, 2016

Copy link
Copy Markdown
Contributor Author

It's probably the easiest solution if you need it now. A part from cov_params that as you can see from the discussion has some bugs all the rest should be fine. Please report it if you find any bug

@springcoil

Copy link
Copy Markdown

So I had a quick look at the code today, and while I think it has a few edge cases and bugs, it should probably be merged if it passes the tests. Since GAM is really something Python needs. Is this the most up to date version?

@springcoil

Copy link
Copy Markdown

Ok I'll see if I can pull these files into another PR and see if I can get that working, next week or the week after.

@DonBeo

DonBeo commented Mar 19, 2016

Copy link
Copy Markdown
Contributor Author

I think this code has still some small bugs that should be reviewed.
Testing was quiet difficult as the GAM R libraries are based on a slightly different set of smooth functions.
I think the code is good enough to stay at least in the sandbox.

@springcoil

Copy link
Copy Markdown

Do you have any ideas on what the bugs are?

@DonBeo

DonBeo commented Mar 19, 2016

Copy link
Copy Markdown
Contributor Author

On this page look at the comment " DonBeo commented on Dec 24, 2015 "

@springcoil

Copy link
Copy Markdown

Ok so it looks like there are a few cosmetic things to investigate and some
errors in the plot values. But a bit of work can make this be fixed.
I'll see if I can come up with a test or two to fill in your TODO's.

On Sat, Mar 19, 2016 at 4:15 PM, DonBeo [email protected] wrote:

On this page look at the comment " DonBeo commented on Dec 24, 2015 "

—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#2744 (comment)

Peadar Coyle
Skype: springcoilarch
www.twitter.com/springcoil
peadarcoyle.wordpress.com

@DonBeo

DonBeo commented Mar 19, 2016

Copy link
Copy Markdown
Contributor Author

Thanks it would be very good. I would really like to have this merged but I have been too busy!!!
I think you can send a patch request to this PR and please write me whenever you need help.

@chriscate

Copy link
Copy Markdown

I'm just curious if any further work been done on this. Perhaps in a different PR?

@josef-pkt

Copy link
Copy Markdown
Member

Unfortunately it's currently asleep and waits for the wake-up call. There is no other PR that continued this work.

@DonBeo

DonBeo commented Sep 28, 2016 •

Copy link
Copy Markdown
Contributor Author

I would like to complete it but in the last period I have been too busy. I think it is reasonable to wait that penalized estimator are merged before to continue this one.

The PR also contains different parts as the smooth basis and the cross validation. Maybe we should merge some of them as independent PR

@denadai2

denadai2 commented Jan 3, 2017

Copy link
Copy Markdown

is there any news? :(

@DonBeo

DonBeo commented Jan 3, 2017

Copy link
Copy Markdown
Contributor Author

Unfortunately I can not find enough time to do this. If anyone wants to collaborate I am happy to provide all the required information.

I'd like to complete it but I don't really have the time at the moment :-(

@dswah

dswah commented May 10, 2017

Copy link
Copy Markdown

@DonBeo @josef-pkt @cgc17 @denadai2 @springcoil

I've written a python implementation of GAMs using penalized B splines inspired heavily by Simon Wood and his mgcv C-RAN package.

check it out here https://github.com/dswah/pyGAM
feedback is most welcome.

I've included several model families, as well as link functions and distributions that you can mix and match to make very custom GAMs.

I wish I had done more research and contributed to this PR before starting from scratch...

Comment thread statsmodels/gam/gam.py
penal = kwds.pop('penal', None)
# I keep the following instead of adding default in pop for future changes
if penal is None:
print("Define a penalty")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like loglike will break if this is allowed. Maybe this should be a ValueError?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants