Skip to content

WIP: PCA class - #1596

Closed
jseabold wants to merge 18 commits into
statsmodels:masterfrom
jseabold:add-pca
Closed

jseabold wants to merge 18 commits into
statsmodels:masterfrom
jseabold:add-pca

Conversation

@jseabold

Copy link
Copy Markdown
Member

Fair warning, I haven't looked at this code in 2 years. Rescued and rebased from upstream. Deleted the branch in upstream to keep things tidy.

Would be nice to replicate the notebook of @kshedden with the class to get an idea of what needs to be fixed, added, generalized, etc. Comments (or PRs) very welcome.

http://nbviewer.ipython.org/urls/umich.box.com/shared/static/6m7f4lw9bdog241kqcmb.ipynb

TODO

  • add tests (correlation only eyeball tests so far)
  • move out of sandbox
  • replicate notebook / add notebook to examples
  • add fertility data to datasets
  • code review
  • add to release notes
  • add to main docs
  • scree plot
  • projections method in results
  • MLE (or information criteria) for selecting keepdim
  • factor rotations
  • add pandas wrapping
  • where should this code live?
  • symmetric eigen decompositions

@jseabold jseabold added PR and removed comp-stats labels Apr 16, 2014
@jseabold

Copy link
Copy Markdown
Member Author

comp-dimensionreduction ?

@josef-pkt

Copy link
Copy Markdown
Member

This needs unit tests to verify results.
I don't think cov, corr is completely correct.

@josef-pkt

Copy link
Copy Markdown
Member

I had started a sandbox subdirectory "datarich" borrowing the term from the econometrics literature that I was looking at.

@jseabold

Copy link
Copy Markdown
Member Author

See #1238.

@jseabold

Copy link
Copy Markdown
Member Author

It doesn't look to me like cov is completely finished. Correlation matrix agrees with Stata.

@jseabold

Copy link
Copy Markdown
Member Author

Eyeball tests agree with Stata and R. Tests now pass against your old matlab ones as well.

@jseabold

Copy link
Copy Markdown
Member Author

What bells and whistles should we add to the PCA results? Scree plots, a projection method, what else?

@jseabold

Copy link
Copy Markdown
Member Author

I added some bells and whistles to the todo checklist. Any other suggestions are welcome.

@josef-pkt

Copy link
Copy Markdown
Member

my original usecase PCR
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/datarich/factormodels.py
I have to see if I had more examples or other versions of PCRegression

@jseabold

Copy link
Copy Markdown
Member Author

Yeah, I'm headed that way for diffusion index stuff and principal fitted components (Cook). That code should still work with this if the imports get fixed. I might have changed the defaults from covariance to correlation (or vice versa) given the changes I had to make to the tests.

I wonder if we shouldn't deprecate the sandbox code. I didn't want to get in that kind of a situation, but we know this code has been used.

@josef-pkt

Copy link
Copy Markdown
Member

I don't remember what the code was supposed to do with respect to demean.

demean is an option, but if demean=False, we still use corrcoef or cov in pca, but not in pca_svd.

Comment thread statsmodels/tools/pca.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.

if keepdim is None, then proj is useless, it should be just X

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah I saw that. I'm just going to change it to a method I think.

@josef-pkt

Copy link
Copy Markdown
Member

my guess is that xreduced/projections is not in the original space if corr=True, My guess is that we need to revert the standardization.

@josef-pkt

Copy link
Copy Markdown
Member

In terms of methods: what I was playing around for some time (but don't have committed the examples AFAICS) is about the properties of PCA as linear or affine transformation.

In terms of regression we have PCA as a stateful transform, x -> f and f -> x (x is data, f is factors)
If we just use eigenvectors of the original data, then this is just a linear transform given by the eigenvector (?) matrix, with demean and standardization (corr) we need to add those back into the transformation.

we need x -> f for a new x with old transformation matrix to make predictions.
I don't remember what f -> x is useful for.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling acb3d26 on jseabold:add-pca into d7ff182 on statsmodels:master.

@jseabold

Copy link
Copy Markdown
Member Author

Added a scree plot method. Question. Should we give an option to plot the explained/cumulative explained variance in the scree plot instead of just the eigenvalues?

Added the notebook from @kshedden. Refactored it to take advantage of pandas features and the new PCA class. Raises another question. Should we use the wrapping stuff and preserve the pandas indices, if given. The plotting code could be simplified a bit if they were labelled pandas objects.

@jseabold

Copy link
Copy Markdown
Member Author

I'm not sure PCA makes the most sense in stastmodels/tools. Especially re: the docs. Tools wouldn't be the first place I checked for dimension reduction code. I'm not wild about datarich either. This is non-standard. Maybe it should just go in stats/?

@jseabold

Copy link
Copy Markdown
Member Author

@kshedden where did you get this fertility data? From the world bank API? Link? I recall there being some fertility rate data floating around a few years ago that I was looking at that was definitely non-free.

@jseabold

Copy link
Copy Markdown
Member Author

Nevermind, I see the indicator name and can read the WB terms of use.

@kshedden

Copy link
Copy Markdown
Contributor

Yes, I think an option to make the plot in terms of variance explained is
quite important.

On Wed, Apr 23, 2014 at 8:29 AM, Skipper Seabold
[email protected]:

Added a scree plot method. Question. Should we give an option to plot the
explained/cumulative explained variance in the scree plot instead of just
the eigenvalues?

Added the notebook from @kshedden https://github.com/kshedden.
Refactored it to take advantage of pandas features and the new PCA class.
Raises another question. Should we use the wrapping stuff and preserve the
pandas indices, if given. The plotting code could be simplified a bit if
they were labelled pandas objects.

Reply to this email directly or view it on GitHubhttps://github.com//pull/1596#issuecomment-41112470
.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling cf7c773 on jseabold:add-pca into a6a927c on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

@jseabold

Copy link
Copy Markdown
Member Author

NIce.

Add a fit method? It will make it easier to use wrapped results Possible to do without though a la filters, exponential smoothing. This would be our only class that does the 'fitting' in the __init__.

@josef-pkt

Copy link
Copy Markdown
Member

I don't know, I haven't gone through many usecases yet for the PCA class.
If fit is essentially in __init__, then self would be the results class. We can try to get a pandas wrapper around it.

Alternative: the PCA class is storing the full svd, or an svd up to a max number of components. fit(n_comp) returns a results class that produces everything for a given, fixed number of components.

I guess that the PCA class will have different use cases than the pure pca_svd function.

For examples like Kerby's and the one in the blog the main target is to make it convenient. For large scale PCR we have to take into account that we only want to (calculate and) store a few components or non at all.

@josef-pkt

Copy link
Copy Markdown
Member

for example in one of the talks at pycon for natural language processing with scikit-learn, the initial number of "features" was huge, and after PCA he kept 50 components.
If we do something similar, then we don't have any problems handling 50 exogs in any of our models.

@jseabold

Copy link
Copy Markdown
Member Author

I left it without a fit for now. We don't really need a wrapper here (yet).

@jseabold

Copy link
Copy Markdown
Member Author

While I'm thinking about it. We should switch to using the symmetric eigendecomposition routines.

@kshedden

Copy link
Copy Markdown
Contributor

I redid my PCA notebook example, the new version is on the wiki page (same place as before, also linked below). I think I was incorrectly centering before, which basically had the effect of removing the first PC factor, which captured the variation in overall fertility level.

http://nbviewer.ipython.org/urls/umich.box.com/shared/static/6m7f4lw9bdog241kqcmb.ipynb

@kshedden

Copy link
Copy Markdown
Contributor

I find the terminology for the return values of the two PCA functions a bit confusing. What is currently called "factors" is often called "scores" or "factor scores". What is currently called "evecs" is often called "loadings" or "factor loadings".

@jseabold

Copy link
Copy Markdown
Member Author

That was one of the motivations for the convenience class. Is it clearer?

Sent from my mobile

@kshedden

Copy link
Copy Markdown
Contributor

I hadn't noticed the PCA class. It is more clear, especially for loadings. But the word "scores" is very commonly used in PCA and does not seem to appear anywhere. Perhaps a mention in the docstring would be helpful.

@josef-pkt

Copy link
Copy Markdown
Member

IMO, scores is used in too many different meanings. (e.g. scikit-learn)
I finally got used to calling the first derivative of the loglikelihood function score, score_obs, and I would rather avoid other meanings of the same word. (explanation in docstring are of course useful)

background: I wrote the PCA function just after coming from matlab and doing "Factor Models" and principal component regression, and the PCA terminology didn't make any sense to me.

open PR for PCA class that needs more review and merging #1596

@josef-pkt

Copy link
Copy Markdown
Member

(silly thing to link to the PR from the PR, very early in the morning local time)

@bashtage

Copy link
Copy Markdown
Member

Heavy overlap with #2004 now

@josef-pkt

Copy link
Copy Markdown
Member

PR #2004 has been merged.
This PR can be closed unless there is still something in here that we need to use and include

@springcoil

Copy link
Copy Markdown

+1 for this to be closed.

@bashtage

Copy link
Copy Markdown
Member

If there is a chance this will be merged, I'm happy to rebase
On Tue, Feb 16, 2016 at 9:03 AM springcoil [email protected] wrote:

+1 for this to be merged.

—
Reply to this email directly or view it on GitHub
#1596 (comment)
.

@josef-pkt

Copy link
Copy Markdown
Member

@bashtage Your PCA version has been merged a year ago.
This is Skipper's older version, but I haven't checked whether there are still extras or examples left in here that's not in master yet.

@bashtage

Copy link
Copy Markdown
Member

Ok. Perils is github email responses. I think I strictly nested, including
notebooks.
On Tue, Feb 16, 2016 at 9:17 AM Josef Perktold [email protected]
wrote:

@bashtage https://github.com/bashtage Your PCA version has been merged
a year ago.
This is Skipper's older version, but I haven't checked whether there are
still extras or examples left in here that's not in master yet.

—
Reply to this email directly or view it on GitHub
#1596 (comment)
.

@josef-pkt

Copy link
Copy Markdown
Member

Thanks, closing then

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.

6 participants