Repository navigation
Maximum likelihood factor analysis - #4156
Conversation
|
@kshedden Can you review #3294 a bit? I think it should be ready to merge except for the location of the rotation option. @kshedden @yl565 The main question right now is whether these two can be merged into master fast so it will be in 0.9, or if we postpone it to 0.10 to have more time to integrate this. I only briefly looked at the PR. It seems to me that factanal here could be made into a private method and added as option to the fit method in the class in #3294. That leaves the rotation and results methods from here which would need to be added to the results class in #3294. |
Codecov Report
@@ Coverage Diff @@
## master #4156 +/- ##
==========================================
+ Coverage 79.38% 79.39% +0.01%
==========================================
Files 554 555 +1
Lines 82603 82830 +227
Branches 9428 9452 +24
==========================================
+ Hits 65572 65764 +192
- Misses 14883 14905 +22
- Partials 2148 2161 +13
Continue to review full report at Codecov.
|
|
@josef-pkt, @yl565 Yes I think #3294 can be merged first and then I will modify #4156 to adapt to it. One thing that would be desirable in my view is to add an argument |
|
@kshedden Do you mean passing cov additional to endog or cov as a substitute for endog? We need endog to compute the factors. In general I would like more functions/classes that directly work with covariance, correlation or moment matrices or allow a call-back function for their computation. (main usecase that I ran into was robust or regularized covariance, but for FA and similar it is also possible to use polychoric or non-pearson correlation, AFAICS) |
|
I would like to take cov as an alternative to endog. I don't think the model parameters depend on endog once cov is available (under the usual estimators). endog is required to obtain the scores, but this is an optional step that is not always needed. There are quite a few practical situations where you have a covariance matrix that needs to be factorized, but no data. |
|
@kshedden I think it's better to leave adding cov as substitute for endog for 0.10. I don't see right away how this will work, when some results will depend on availability of endog. a bit related: I was changing my mind several times about what arguments I need in computing the covariance of a covariance estimator. It's straightforward under normality assumption using cov, but we need a kurtosis estimate or a 4th moment estimate in the more general cases. Aside: You wrote |
I take this back, in the FA in #3294 endog is used only to compute the cov, only endog_names are used in results. |
|
Yes, good point. They are targetting the Frobenius norm version of factor
analysis (least squares element-wise fitting). Once the PA version is in
with a suitable class structure, I will create a PR for the ML and
Frobenius versions.
…On Sat, Dec 9, 2017 at 8:09 PM, Josef Perktold ***@***.***> wrote:
@kshedden <https://github.com/kshedden> I think it's better to leave
adding cov as substitute for 0.10. I don't see right away how this will
work, when some results will depend on availability of endog.
a bit related: I was changing my mind several times about what arguments I
need in computing the covariance of a covariance estimator. It's
straightforward under normality assumption using cov, but we need a
kurtosis estimate or a 4th moment estimate in the more general cases.
https://github.com/statsmodels/statsmodels/pull/4143/files#diff-
4956ab798139dc28ab6854559f7292ecR174
Aside: You wrote stats.correlation_tools.cov_nearest_factor and
cov_nearest_factor_homog. Do they produce a version of factor analysis?
Can they be reused in multivariate?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4156 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACiww627RVma8h1qB_DlWGhVJkqyGmWRks5s-y85gaJpZM4Q6iuc>
.
|
|
The test that fails is due to right padding of some rows of the summary with spaces. Not sure now how to address that. |
|
summary fails for some reason when there is only one factor (a single column data-frame is added to the list of summary tables) |
|
This pull request introduces 1 alert - view on lgtm.com new alerts:
Comment posted by lgtm.com |
|
Travis, appveyor fail because you removed the results class maybe you forgot to add the why do you want to move the results class? |
|
I was struggling with circular imports. Factor needs to import
_ml_factanal and _ml_factanal needs to create a FactorResults. If there
is another way to structure this let me know.
…On Fri, Dec 15, 2017 at 9:07 AM, Josef Perktold ***@***.***> wrote:
Travis, appveyor fail because you removed the results class
from statsmodels.multivariate.factor import Factor
statsmodels/multivariate/factor.py:5: in <module>
from .factor_results import FactorResults
E ImportError: No module named 'statsmodels.multivariate.factor_results'
maybe you forgot to add the factor_results module.
why do you want to move the results class?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4156 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACiww4GYSySTt8h_CurTre0SWHfc15Dnks5tAn0QgaJpZM4Q6iuc>
.
|
|
Remove |
|
I think the MLE factanal code can be merged into the main moduel I don't expect this to get so large that we need to split the main factor analysis over several modules. And as far as I can see it should conceptually all belong together. |
|
Everything is back in one file now. I still have an issue with summary2 not handling a one column matrix (the summary2 code divides by x.dim[1] - 1). |
|
quickfix for summary2 in iolib.summary2 line 249 which causes the exception, add a lower bound of 1 on nsep
It works for an example with one factor, but I haven't looked more closely at it. |
|
I also just ran into the whitespace failure for summary txt after cleaning trailing whitespace for a test that I'm writing. You can replace the last part of The unit test passes then for me locally. |
|
|
||
| def test_exact(): | ||
| """ | ||
| Test if we can recover exact factor-structured matrices with default |
There was a problem hiding this comment.
no docstring for unit tests, use code comment #
|
all green now. I did a brief read through the code, and I didn't see any problems. I think we need to squash the commits heavily to avoid the code movement and separate module in the history. |
1 similar comment
|
Time reduced by half using analytic score function. Possible point of user confusion: fit has maxiter, tol arguments for principal axes method, and also has opt dictionary for ML estimation, where opt may have tol and maxiter as keys. |
| @cache_readonly | ||
| def load_stderr(self): | ||
| """ | ||
| The standard errors of the loadings. |
There was a problem hiding this comment.
This has to specify "of the MLE rotated loadings"
|
|
||
| assert_equal(rslt.df, 4) | ||
|
|
||
| # Smoke test for standard errors |
There was a problem hiding this comment.
better to use a regression test: just plug in current numbers
| if type(par) is np.ndarray: | ||
| uniq, load = self._unpack(par) | ||
| else: | ||
| load, uniq = par[0], par[1] |
There was a problem hiding this comment.
reversing the order of load, uniq might get confusing
|
test failures are most likely a wrong one python 2.7 has huge numbers in a failing score test. I don't see integer division, but adding the future is safer |
… convergence behavior
| # Do the optimization | ||
| if opt is None: | ||
| opt = _opt_defaults | ||
| r = minimize(nloglike, start, jac=nscore, method=opt_method, |
There was a problem hiding this comment.
you could add mle_retvals to the results class, so it's possible to inspect convergence information.
1 similar comment
|
@kshedden If you are finished here, then we can merge this. It's all green now. I'm trying to finish up #4171 with unit tests against Stata, so I can merge it on top of this merged PR. #4164 has some comments on extensions and refactorings. |
|
Yes, I think it's ready to go. Additional issues can be addressed in
follow-up PR's.
…On Wed, Dec 20, 2017 at 1:39 PM, Josef Perktold ***@***.***> wrote:
@kshedden <https://github.com/kshedden> If you are finished here, then we
can merge this. It's all green now.
I'm trying to finish up #4171
<#4171> with unit tests
against Stata, so I can merge it on top of this merged PR.
#4164 <#4164> has some
comments on extensions and refactorings.
AFAICS, it will be better to move this to a full Model-Results pattern,
which means mainly being more careful with attaching attributes and inplace
modifications.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4156 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACiww7vDKaWVW9Qlbtvl2KQgq-DI4gShks5tCVRJgaJpZM4Q6iuc>
.
|
|
merged, factor analysis is starting to look good @kshedden Thank you |
This is an implementation of maximum likelihood factor analysis. It probably would need to be integrated with #3294.
The main reference for me was Bai and Li:
https://arxiv.org/pdf/1205.6617.pdf
They focus more on estimation in high dimensions, less on issues relating to model assessment which are very important in psychometrics. Specialized methods for the latter are not included here.
This uses direct numerical optimization of the log-likelihood. The gradient is not provided but scipy minimize seems to do OK without it here. EM is also common and we could provide that as an optional algorithm.