Skip to content

FAQ/ENH: Discrepencies in two-way MANOVA between statsmodels and SPSS #6464

Open

Description

@JMansuy

I am using Statsmodels (version 0.10.0) to compute a two-way MANOVA.

My model looks like this:

type_gend= MANOVA.from_formula('type_home_collection + type_kerbside_collection + type_staffed_dop ~ C(PROD_TYPE) * C(GEND) -1', data=df)
print(type_gend.mv_test())

When working on it, I realized some weird things

  • Results differ depending on which factor I place first in the model. This is true also when I write the full formula (type_home_collection + type_kerbside_collection + type_staffed_dop ~ C(PROD_TYPE) + C(GEND) + C(PROD_TYPE):C(GEND) -1)
  • Results are different from what I get in SPSS: the interaction effect is similar, but main effects are not

I checked and my input data is exactly the same in both cases (I actually exported my DataFrame from python).

Unfortunately I'm neither good enough in statistics nor in programming to solve that, but I hope this may help...

Activity

  1. josef-pkt commented on Jan 24, 2020

    @josef-pkt
    Member

    Thanks for reporting. This is not enough information. I don't have SPSS and cannot check this.

    Did you try the same with the constant, no -1 in the formula?

    My guess is that there might be different encodings between statsmodels/patsy and SPSS, so that we are actually test different hypothesis.
    AFAIR, the hypothesis test are constructed on the parameters, and with different encodings the null hypothesis will differ unless they are transformed to encoding independent constrast matrices.

    The other possibility is that we define things differently from SPSS for non-orthogonal two way designs. (I don't know what SPSS does,)

    The best would be if you can come up with a replicable example and the expected output from SPSS.
    Otherwise, you can add the output from spss and statsmodels to this issue, and we see if we have a guess based on it.

    I'm not very familiar with our MANOVA and will need to look at it again. I never went through the details of the formula handling in it.
    (The original author of our MANOVA is not active in statsmodels anymore.)

  2. josef-pkt commented on Jan 24, 2020

    @josef-pkt
    Member

    unit tests: AFAICS,

    • we don't have unit tests for 2-way MANOVA (test_manova.py)
    • the underlying code with mv_test has at least one two-way unit test in test_multivariate_ols.py

    So, my guess is that the underlying mv_test method is fine, but MANOVA constructs different contrast matrices depending on the encoding of the two-way factors in exog.

    @JMansuy Does SPSS show which contrast matrices they use for the different hypothesis?

  3. josef-pkt commented on Jan 24, 2020

    @josef-pkt
    Member

    AFAICS from the SPSS computation, they do a reparameterization to cell means in the MANOVA case, and something similar in the MANCOVA case.
    The details in the SPSS algorithm docs are not very clear to me. It looks like a traditional ANOVA style analysis and not going through the linear model directly. There might be problems with non-orthogonal designs in the SPSS case, i.e. incomplete block design.

    edit it looks like WLS on cell means with W = "D is the diagonal matrix of cell frequencies".
    They also (optionally) incorporate the contrasts L in the reparameterization before estimation.
    Then they mention just simple unit tests like H0 : theta_h = 0.
    We and AFAIR, SAS compute parameters B from the original parameterization and then test hypothesis L * params * M = C in mv_test

    @JMansuy Can you compare a cell parameterization to SPSS?
    formula
    'type_home_collection + type_kerbside_collection + type_staffed_dop ~ C(PROD_TYPE) : C(GEND) -1'

  4. josef-pkt commented on Jan 24, 2020

    @josef-pkt
    Member

    AFAIR, and based on unit tests, we were using SAS docs when MANCOVA was added.

  5. JMansuy commented on Jan 24, 2020

    @JMansuy
    Author

    Hello Josef, thanks for your answer!

    Please find below the answers to your suggestions. Unfortunately I'm not very familiar with MANOVA nor python (I learned both for my paper), and even less with SPSS, so apologies in advance if my feedback is not very helpful.

    On the MANOVA above, here is what Statsmodels and SPSS send me:

    Statsmodel:
    image

    SPSS:
    image

    I did some tests with other factors and it looks like Statsmodels and SPSS are giving the same F Values (and p-values) for the interaction effect.

    Removing the constant does not change neither the F Value nor the the p-value.

    I'm not familiar with the contrast concept yet, so do not hesitate to ask me for further precisions if I do not explain well on this part. In SPSS the constrast is set to "None" for both variables, but you can select several other options (deviation, simple, difference, Helmert, repeated and polynomial) for each variable separately. I've also read about a multivariate contrast which is kind of "hidden". I do not think I have access to the contrast matrix in use.

    When doing the cell parametrization I have that:
    image

    Hope this helps a bit...
    Do not hesitate to tell me if you want me to perform other tasks.

  6. JMansuy commented on Jan 24, 2020

    @JMansuy
    Author

    FYI I got the same results in Jamovi (coded in R) as compared to SPSS:
    image

  7. josef-pkt commented on Jan 24, 2020

    @josef-pkt
    Member

    One more check: leave out the interaction effect in the statmodels version, and see what results you get.

    In standard anova tables, R, leaves out the interaction effect when evaluating the main effect, while we don't do that. Maybe they do something similar in MANOVA.

    Please add your commands and package for the R results in your last comment. If R agrees with SPSS in this case, then it will be easier to investigate.

  8. josef-pkt commented on Jan 24, 2020

    @josef-pkt
    Member

    Aside: Yesterday I was reading or skimming articles from the 1970s in the psychology literature about using linear models instead of anova for unbalanced design and they argued for a while which type of anova to use (1, 2 or 3). The types all agree for a balanced design but differ for unbalanced (different cell frequency) designs.

  9. JMansuy commented on Jan 24, 2020

    @JMansuy
    Author

    When I leave out the interaction effect, I get the same (more or less) main effects as in SPSS indeed:
    image

    Regarding R, the package is jmv (their GitHub: https://github.com/jamovi/jamovi)

  10. JMansuy commented on Jan 24, 2020

    @JMansuy
    Author

    In my case I have an unbalanced design, and I've indeed seen the difference using type I, II or III with the anova_lm method for my dependent variables separately.

  11. josef-pkt commented on Jan 24, 2020

    @josef-pkt
    Member

    Did you include the constant in the no-interaction effect model in your previous to last comment?

    degrees of freedom Den DF difference is pretty large, but also the Num DF in C(PROD_TYPE)

    It looks like SPSS and R have the same DF2/Den DF for prod_type main effect as for for the interaction. That might mean that they are still using information from the full model, when computing the main effect.

  12. JMansuy commented on Jan 24, 2020

    @JMansuy
    Author

    No indeed, I did not include the constant. Here are the results with the intercept:

    image

  13. josef-pkt commented on Jan 24, 2020

    @josef-pkt
    Member

    Good, that's getting close.

    Now Den DF differs by just around 2 or 2 to 4

    I have no guess where that might come from.

  14. JMansuy commented on Jan 24, 2020

    @JMansuy
    Author

    FYI, I removed the intersect also in the SPSS model I posted, but it did not change the results.

    The SPSS output with interesect:
    image

    The difference in Den DF is surprising indeed, I have no idea either. I'll continue learning about MANOVA, and I'll keep you updated if I find an idea on that.

    Thanks for the support on this issue!

  15. josef-pkt commented on Jan 24, 2020

    @josef-pkt
    Member

    our implementation refers to SAS docs for details for the mv_test statistics and df
    https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_introreg_sect012.htm

    So, I guess SPSS is doing something slightly different than SAS. I don't think that's specific to two-way effects.
    (I never tried to understand how df are justified in this case, this uses F distribution and not asymptotic chi2.)

    SPSS has slightly smaller df_denom, which means we have slightly small pvalues and slightly larger rejection rates.
    We have fractional df_denm in Hotelling-Lawley trace, so my SAS and our df are better. (?)

    @JMansuy One more to compare to SPSS.
    Can you get the results for a one-way MANOVA, i.e. use only one of the main effects?

  16. changed the title [-]Discrepencies in two-way MANOVA between statsmodels and SPSS[/-] [+]FAQ/ENH: Discrepencies in two-way MANOVA between statsmodels and SPSS[/+] on Jan 24, 2020
  17. JMansuy commented on Jan 25, 2020

    @JMansuy
    Author

    Sure, here is the output for one-way MANOVA for SPSS:
    image

    And here for Statsmodels:
    image

  18. josef-pkt commented on Jan 25, 2020

    @josef-pkt
    Member

    I found one more thing to check:

    According to the SPSS manova docs (in IBM_SPSS_Statistics_Command_Syntax_Reference.pdf) there is a METHOD=UNIQUE option that sounds like type 3 ANOVA, but AFAIU it is the default.

    Can you check whether adding this option makes a difference in the SPSS two-way manova?

    Otherwise, AFAICS, there is only the difference in degrees of freedom, especially Hotelling's trace

  19. JMansuy commented on Jan 25, 2020

    @JMansuy
    Author

    We can indeed change the method in SPSS. Here is what happens when I select type 2 (METHOD=SSTYPE(2)):
    image

    And here is the table with type 3 selected (METHOD=SSTYPE(3)):
    image

    We can also select type 1 and type 4. Not sure which one applies best to my case (I randomly assigned PROD_TYPE to my respondents), but all the previously sent tables are made with a type 2 model.

  20. josef-pkt commented on Jan 25, 2020

    @josef-pkt
    Member

    @JMansuy one last one, then I think we have all main comparisons between SPSS and statsmodels.

    show the results of your original two-way interaction model in statsmodels but with a constant included (no -1)

    Then we have the direct comparison to the different types of SPSS manovas

  21. JMansuy commented on Jan 25, 2020

    @JMansuy
    Author

    Sure, here it is:
    image

  22. josef-pkt commented on Jan 25, 2020

    @josef-pkt
    Member

    with the intercept we have now the same DF in statsmodels as in SPSS.

    The actual test statistics for the main effects don't agree with SPSS type 3 either. So no idea how what those tests condition on.
    (I'm not completely sure our manova does pure type 3 in the sense of testing the main effect only even when an interaction effect is in the model.)

    The interaction effect results (f-value, p-value agrees even though our reported Den DF is different.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions