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
Results of n-way MANOVA with interactions differ significantly from results from R's stats::manova #8713
i.e. the F values are completely different in these two implementations (and thus the Pr > F is also different). Why is it the case? The results are the same for statsmodels'and R's implementations when there is no interaction between the independent variables - only the analysis with interactions yield different results. Can I specify some parameters that would make the results the same as in R?
I'm working with version 0.13.5 in WSL2. Python version 3.10.6.
I don't know what causes those very large differences.
Here is a long issue on difference between statsmodels and SPSS #6464
In unbalanced anovas, both encoding of categorical and type of anova can have a large effect on the results. But I'm not really familiar with most of the MANOVA details or variants.
One guess would be that R stats manova computes a type 1 (?) anova, where the main effects are significant on their own but not when including the interaction effect.
the model is created correctly using MANOVA.from_formula, but calling fit.mv_test() results in ValueError: zero-size array to reduction operation maximum which has no identity.
I don't know what to think here, since I can perform the analysis in R without any issues:
The bug happens because all eigenvalues are close to zero (below 1e-8 threshold) and the test matrices are empty when testing the intercept.
My guess is that for MANOVA we can skip testing the intercept.
More generally, we need to handle the edge case with empty test matrices, or figure out why all eigenvalues are zero
(Aside: I don't remember why intermediate results are complex, maybe because of eigenvalues of asymmetric matrix)
from debugger session using the latest example as test case.
The source of the problem is that endogs in the last example have mean zero. (around 1e-18)
The estimated intercept are array([-2.73761049e-18, 1.00830802e-17, 6.45100293e-18, 5.63785130e-18])
So all hypothesis test computation are essentially on zeros, so there will not be nonzero eigenvalues.
(i.e. it's mostly floating point noise, but we ignore eigenvalues below 1e-8 threshold)
aside:
params are the first element of the _fittedmod attribute params, df_resid, inv_cov, sscpr = mod._fittedmod
This is what I get if I change the code to skip testing the intercept:
These results are consistent with what I get with R's MANOVA. Since you marked resolving this issue as a milestone for 0.15, I guess I'll just use rpy2 to implement what I need into the software I'm developing and switch to statsmodels once it gives me the same results. Thanks for helping me!
Should I close the issue for now or leave it open for you?
leave it open. It will stay open as a FAQ. (After some time I won't remember what the issue between statsmodels and R is.)
(issue #8722 is for enhancements and refactoring, not for the problem with empty matrices)
I will fix the exception with the intercept for 0.14, but maybe just with an option to skip the intercept test. I guess we don't skip it by default.
I don't know what we should return in case of empty test matrices in general, other than raising a more explicit exception.
Activity
josef-pkt commentedon Mar 3, 2023
I don't know what causes those very large differences.
Here is a long issue on difference between statsmodels and SPSS #6464
In unbalanced anovas, both encoding of categorical and type of anova can have a large effect on the results. But I'm not really familiar with most of the MANOVA details or variants.
One guess would be that R stats manova computes a type 1 (?) anova, where the main effects are significant on their own but not when including the interaction effect.
josef-pkt commentedon Mar 3, 2023
Can you add the R results without interaction effect?
mikrzol commentedon Mar 3, 2023
Sure, it would be:
In statsmodels the results are as follows:
josef-pkt commentedon Mar 3, 2023
I don't really understand what's going on. The data looks balance with 3 observations per cell
If I use Helmert coding, I get very different answers, same as R at floating point precision
josef-pkt commentedon Mar 3, 2023
That's it.
R stats manova uses by default the Helmert encoding.
I got the same result in R before and after setting the contrast option with helmert.
With helmert coding the design matrix is orthogonal
I guess we should add explaining Helmert coding to a docstring or docs
mikrzol commentedon Mar 6, 2023
That indeed seems to solve the issue for this data, but I encounter an error when I try to use Helmert contrasts to my actual test data:
example.csv
When I use:
the model is created correctly using MANOVA.from_formula, but calling fit.mv_test() results in ValueError: zero-size array to reduction operation maximum which has no identity.
I don't know what to think here, since I can perform the analysis in R without any issues:
m_e$contrasts also shows that R uses 'contr.treatment', not 'contr.helmert' in this case. Weird.
josef-pkt commentedon Mar 7, 2023
using the debugger, I found that it breaks when testing the intercept. I don't know why or why it is doing that
However it doesn't happen with patsy's standard coding for categorical
This is what I get if I remove the explicit intercept
josef-pkt commentedon Mar 7, 2023
The bug happens because all eigenvalues are close to zero (below 1e-8 threshold) and the test matrices are empty when testing the intercept.
My guess is that for MANOVA we can skip testing the intercept.
More generally, we need to handle the edge case with empty test matrices, or figure out why all eigenvalues are zero
(Aside: I don't remember why intermediate results are complex, maybe because of eigenvalues of asymmetric matrix)
from debugger session using the latest example as test case.
josef-pkt commentedon Mar 7, 2023
This is what I get if I change the code to skip testing the intercept:
josef-pkt commentedon Mar 8, 2023
The source of the problem is that endogs in the last example have mean zero. (around 1e-18)
The estimated intercept are
array([-2.73761049e-18, 1.00830802e-17, 6.45100293e-18, 5.63785130e-18])So all hypothesis test computation are essentially on zeros, so there will not be nonzero eigenvalues.
(i.e. it's mostly floating point noise, but we ignore eigenvalues below 1e-8 threshold)
aside:
params are the first element of the
_fittedmodattributeparams, df_resid, inv_cov, sscpr = mod._fittedmodmikrzol commentedon Mar 9, 2023
These results are consistent with what I get with R's MANOVA. Since you marked resolving this issue as a milestone for 0.15, I guess I'll just use rpy2 to implement what I need into the software I'm developing and switch to statsmodels once it gives me the same results. Thanks for helping me!
Should I close the issue for now or leave it open for you?
josef-pkt commentedon Mar 9, 2023
leave it open. It will stay open as a FAQ. (After some time I won't remember what the issue between statsmodels and R is.)
(issue #8722 is for enhancements and refactoring, not for the problem with empty matrices)
I will fix the exception with the intercept for 0.14, but maybe just with an option to skip the intercept test. I guess we don't skip it by default.
I don't know what we should return in case of empty test matrices in general, other than raising a more explicit exception.
josef-pkt commentedon Apr 5, 2023
for the record: my initial unit test example was
I have it replaced with an example with random numbers.
ENH/BUG: add option skip_intercept_test to manova, mv_test see statsm…