Skip to content

Tukey HSD posthoc test for group interactions in two-way ANOVA #6799

Open

Description

@reneshbedre

Hi,

I would like to know how to perform a pairwise comparison for group interactions using Tukey HSD. I tried to use pairwise_tukeyhsd from statsmodels.stats.multicomp, but it does not support pairwise comparison for group interactions.

In R it can be done like this (see https://rpubs.com/tmcurley/twowayanova)

TukeyHSD(len.aov, which = "supp:dose")

Let me know if there is a solution to achieve this. If not, it will be a good addition to the statsmodels package.

Activity

  1. josef-pkt commented on Jun 12, 2020

    @josef-pkt
    Member

    statsmodels only has oneway pairwise comparisons.

    I never looked at references for two- or multiway pairwise comparisons

    One possibility is to recode two way comparison to be a oneway comparison for all crossed cells.
    Based on a very brief look, this might be what TukeyHSD(len.aov, which = "supp:dose") is doing in your link.
    I don't know what the best way is to merge two categoricals into a single categorical for all crossed cells using pandas. I have some code doing it the plain numpy way.

  2. reneshbedre commented on Jun 14, 2020

    @reneshbedre
    Author

    Okay, thank you very much for your reply. Can you please show some example of how to do that?

    Suppose I have this model
    model = ols('value ~ C(Genotype) + C(years) + C(Genotype):C(years)', data=df).fit()

    df.head()
    Genotype years value
    0 A 1_year 1.53
    1 A 1_year 1.83
    2 A 1_year 1.38
    3 B 1_year 3.60
    4 B 1_year 2.94

    How can I analyze TukeyHSD for the interaction?

    for individual factor, I can do like this
    pairwise_tukeyhsd(endog=df['value'], groups=df['Genotype'], alpha=0.05)`
    pairwise_tukeyhsd(endog=df['value'], groups=df['years'], alpha=0.05)

    Thank you.

  3. seralouk commented on Apr 20, 2022

    @seralouk

    Any news about it?

    I would also want to apply the pairwise_tukeyhsd on a 2-way anova test.

  4. seralouk commented on Apr 20, 2022

    @seralouk

    Okay, thank you very much for your reply. Can you please show some example of how to do that?

    Suppose I have this model model = ols('value ~ C(Genotype) + C(years) + C(Genotype):C(years)', data=df).fit()

    df.head() Genotype years value 0 A 1_year 1.53 1 A 1_year 1.83 2 A 1_year 1.38 3 B 1_year 3.60 4 B 1_year 2.94

    How can I analyze TukeyHSD for the interaction?

    for individual factor, I can do like this pairwise_tukeyhsd(endog=df['value'], groups=df['Genotype'], alpha=0.05)` pairwise_tukeyhsd(endog=df['value'], groups=df['years'], alpha=0.05)

    Thank you.

    I believe the last 2 code lines are fine and can be used to test how Genotype and Years affect the Value. Haven't found a better way to do this either.

  5. MuhammadIKaleem commented on Apr 16, 2023

    @MuhammadIKaleem

    I want to run tukey HSD on three factors, each with 12 groups (THREE way anova,) has there been any update that does it now?

  6. josef-pkt commented on Apr 16, 2023

    @josef-pkt
    Member

    No, I still have not figured out what that is theoretically supposed to do.

    The only way I looked at two-way interaction is to define each cell as a group unit/level, so there is only a single (combined) group.

    models have a wald_pairwise option but it's also only for one factor and not for multi-factor effect.

  7. Samyaktg commented on Mar 2, 2024

    @Samyaktg

    Hello! If I am not mistaken the MultiComparison allows tukey test of several factors easily

    import statsmodels.stats.multicomp as mc

    tukey=mc.MultiComparison(df['value'],df['Genotype']+df['years'])
    tukey_test=tuk.tukeyhsd()

    I believe this will provide you with the solution

  8. josef-pkt commented on Jan 9, 2025

    @josef-pkt
    Member

    a R tutorial that only looks at main effects (i.e. two one-way effects in the regression case)
    https://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-two-way-anova-with.html

    aside:
    https://rpubs.com/tmcurley/twowayanova uses a balanced dataset, the design matrix should have orthogonal interaction effects in this case.
    Checking only interaction effects will not work in general in the unbalanced case where interaction effects are not orthogonal to the main effects.

    maybe:
    check whether interaction coding, only interaction effects, works with formulas to get pairwise cell comparisons.
    However, string matching might not work to get the corresponding columns and terms.
    I guess we need to inspect the formula design_info to match terms to columns for the two factors.

    For string matching we would need regular expression to get interaction names for two factors, so that level indicators in names are ignored.
    or use multiple in string search if factor1 in name and factor2 in name.

    (aside: in one-way: does constant + column correspond to cell means independent of coding ? maybe not for helmert, sum or similar)

    What comparisons do we need if the hypothesis is that there are no interaction effects but there are main effects?
    (in general unbalanced case)

    another question or option:
    if we have two or more main effects and we make pairwise comparisons for each main effect:
    Should the p-value multiplicity correction treat each pairwise comparison separately (k_i * (k_i - 1)) / 2 pvalues for each i, or
    take total number of tests into account and consider all p-values, i.e. sum_i (k_i * (k_i - 1)) / 2 pvalues.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions