Skip to content

Multiple Comparisons #852

Open

Description

@josef-pkt

In PR #849 I merged Tyler's enhancements to tukeyhsd into master (see also #809 #789 #824 )

some things left to do

  • another round of proofreading docstrings, and cleaner example script
  • add p-values to tukeyhsd results
  • refactoring, some comments in the PRs and issue
  • move out of sandbox
  • unit tests for multicomp module (currently only tukeyhsd is really verified)

further enhancements

Activity

  1. added this to the 0.6 milestone on Apr 4, 2014
  2. josef-pkt commented on Apr 4, 2014

    @josef-pkt
    MemberAuthor

    bump one part of this: add p-values to tukeyhsd results

    I just saw that they are still missing in summary()

  3. josef-pkt commented on Apr 4, 2014

    @josef-pkt
    MemberAuthor

    and allpairtest doesn't return results instance:
    print mc2.allpairtest(stats.ttest_ind, method='b')[0]

  4. josef-pkt commented on Apr 5, 2014

    @josef-pkt
    MemberAuthor

    If groups are a pandas.Series, then I get nan in the result summary table for the group indices.

    everything looks fine except for groupsunique,

    In [56]:  res.groupsunique
    Out[56]:
    Brand
    0        1
    18       2
    29       3
    38       4
    Name: Brand, dtype: int64
    

    previous issue with pandas #824
    latest change that uses groupsunique in summary table #1472

    no unittests for summary(), the values have good coverage, I think

  5. modified the milestones: 0.6, 0.7 on Aug 17, 2014
  6. alimuldal commented on Dec 2, 2014

    @alimuldal

    One thing that's currently missing is a decent post-hoc test for the Kruskal-Wallis ANOVA. I wrote a standalone function that runs a K-W ANOVA followed by Dunn's multiple comparison test: https://gist.github.com/alimuldal/fbb19b73fa25423f02e8. I'm not sure what's currently going on with the kruskal method of the MultipleComparison class - it's definitely not recapitulating scipy.stats.kruskal as the docstring suggests.

  7. josef-pkt commented on Dec 3, 2014

    @josef-pkt
    MemberAuthor

    Hi @alimuldal
    I didn't know about this Dunn's multiple comparison test. My initial intention in the MultipleComparison code was to provide the same kind of statistics as for the ANOVA comparison for data that has been converted to ranks. I never went far enough with it to figure out what I have to adjust or if everything works the same for ranks as for the original data.

    BTW: I think you have a typo in
    https://gist.github.com/alimuldal/fbb19b73fa25423f02e8#file-dunn-py-L111
    The second rank should divide by n[jj] or pre-calculate all the mean ranks. I'm surprised that the multiple comparison version doesn't have tie correction.
    Also, AFAICS the 4th edition was published in 2003 http://www.crcpress.com/product/isbn/9780824740528
    I guess I never looked at that book. (google books says Taylor and Francis 2014, but the 5th edition is 2010 http://www.crcpress.com/product/isbn/9781420077612 )

    a quick google search
    a discussion about the multiple comparison in two packages, and p-value correction option in kruskal
    http://r.789695.n4.nabble.com/Multiple-Comparisons-Kruskal-Wallis-Test-kruskal-agricolae-and-kruskalmc-pgirmess-don-t-yield-the-sa-td4639004.html

    This one mentions kruskal-nemenyi
    http://cran.r-project.org/web/packages/PMCMR/vignettes/PMCMR.pdf
    AFAICS, it has the same test statistic as the gist and uses the studentized range distribution, which is the same as used for Tukey-HSD and would correspond to the joint independent normal distribution (and would be more accurate, less conservative, than the generic p-value correction)

  8. josef-pkt commented on Dec 3, 2014

    @josef-pkt
    MemberAuthor

    a few more links, popularized in machine learning (?)

    using Friedman-Nemenyi for comparison of algorithms in forecast competition
    http://kourentzes.com/forecasting/2014/05/01/critical-values-for-the-nemenyi-test/
    http://kourentzes.com/forecasting/2013/04/19/nemenyi-test/
    http://kourentzes.com/forecasting/2012/04/19/statistical-significance-of-forecasting-methods-an-empirical-evaluation-of-the-robustness-and-interpretability-of-the-mcb-anom-and-friedman-nemenyi-test/
    similar, evaluating neural network algorithms
    http://www.uco.es/ayrna/QRBF/node5.html

    my comment about multiple testing p-value correction versus studentized range distribution
    http://stats.stackexchange.com/questions/73376/how-to-correctly-apply-the-nemenyi-post-hoc-test-after-the-friedman-test/126381#126381

    and Wikipedia has an entry for the test
    http://en.wikipedia.org/wiki/Nemenyi_test with reference to Hollander Wolfe

  9. alimuldal commented on Dec 3, 2014

    @alimuldal

    @josef-pkt Thanks for pointing out my typo - I haven't tested it for unequal sample sizes yet. You're also right about Gibbons & Chakraborti (turns out I was using the fifth edition anyway). I've fixed both of these issues in the gist.

    If I understand correctly, the (only?) difference between the Dunn and Nemenyi tests is that whereas Dunn compares the value of the test statistic to the upper standard normal quantile, Nemenyi compares it to the upper studentized range quantile (divided by sqrt(2)). What are the implications of using the studentized range distribution rather than the standard normal?

  10. josef-pkt commented on Dec 3, 2014

    @josef-pkt
    MemberAuthor

    If I understand correctly, the (only?) difference between the Dunn and Nemenyi tests is that whereas Dunn compares the value of the test statistic to the upper standard normal quantile, Nemenyi compares it to the upper studentized range quantile (divided by sqrt(2)).

    Yes, that's how I see it also, comparing the expressions for the test statistics.

    What are the implications of using the studentized range distribution rather than the standard normal?

    As I tried to explain in my answer on stats.stackexchange, studentized range statistic has the multiple comparison "build in". Essentially it also looks at the normal quantiles, but for all comparisons at the same time.
    Correction: I don't remember the details but studentized range distribution could be simultaneous t-quantiles, instead of normal.

    We use it for the p-values in tukey_hsd in statsmodels for the simultaneous/multiple comparison, qsturng and psturng.

    The sqrt(2) comes from the relationship between studentized range statistic (max - min values IIRC) and the t-statistic. They differ by a factor of 2 (but I don't remember which way)

  11. alimuldal commented on Dec 3, 2014

    @alimuldal

    As I tried to explain in my answer on stats.stackexchange, studentized range statistic has the multiple comparison "build in". Essentially it also looks at the normal quantiles, but for all comparisons at the same time.

    I see. So if I were to use the Nemenyi test rather than the Dunn, am I right in thinking that I could do something like this to get a corrected p-value for each pair of groups:

    Zij = (np.abs(R[ii] / n[ii] - R[jj] / n[jj]) /
           np.sqrt((1. / 12.) * N * (N + 1.) * (1. / n[ii] + 1. / n[jj])))
    p_corrected = psturng(Zij * np.sqrt(2), ncomp, df)
    

    What would the degrees of freedom parameter correspond to here? The PMCMR docs seem to suggest that df ought to just be some very large number.

    How would you expect the Nemenyi test to compare with doing the Dunn test, then correcting the p-values for multiple comparisons using Bonferroni, Holm etc.? Does using the studentized range buy me more power, and what is the cost in terms of additional assumptions that I have to make?

  12. josef-pkt commented on Dec 3, 2014

    @josef-pkt
    MemberAuthor

    df = inf would correspond to using the normal distribution. I need to look up how we used psturng and if there are the values for the limit. In most cases df larger than 100 is very close to normal (based on some references, some probabilities still differ at that dfs)
    In terms of small sample correction, it is not clear whether the t-distribution, i.e. a finite df, is not better than the normal distribution.

    Overall, I never tried a Monte Carlo for tukey-hsd or read about a Monte Carlo for the differences in the various tests. So, based on general principles:

    • Bonferroni should be pretty conservative since it doesn't take into account that the pairs are based on comparing the same samples. studentized range should have better size and more power.
    • IIRC, the main assumption for the studentized range statistic is that the underlying samples are independent or uncorrelated. The large sample normal or t-approximation is the same. I don't remember whether there are auxiliary assumptions, I guess nothing additional or different from the Dunn's version of the test. (The small sample result for tukey-hsd assumes normal distributed samples, but that's irrelevant for large sample, asymptotic results.)
    • Other multiple testing p-value corrections like Holms will be less conservative than Bonferroni and have more power, but they are still not targeted to the specific pairwise comparison problem and should have less power than studentized range distribution.
    • FDR: False discovery rate p-value correction (especially two stage) might be better than studentized range for a different hypothesis, for the case when we expect to have some rejections.
      Basic idea: Studentized range calculates the p-values for the simultaneous hypothesis that all samples are equal, all pair differences equal to zero. FDR could allow some fraction of pairs to be different under the Null.
      (I don't remember whether I read about this for the case of pairwise comparisons, or whether it's just an "educated guess".)

    The above is in analogy to tukey-hsd.
    The only thing that bothers me from the theory is that the ranks are based on the ranks in the pooled sample. I don't know if this doesn't affect the independent/uncorrelated sample assumption of the studentized range distribution. My guess is that it doesn't make a difference under the Null of equal mean/median, but that it could affect the behavior under the alternative and the power when using the studentized range distribution. This might not be relevant in practice.

  13. alimuldal commented on Dec 3, 2014

    @alimuldal

    Thanks, that's really helpful.

    df = inf would correspond to using the normal distribution

    Could you possibly explain the relationship between the studentized range for some particular sample size and df, and the standard normal distribution? Am I correct in thinking that for some pair of values for sample size and df for which the studentized range is equivalent to the standard normal distribution?

    Bonferroni should be pretty conservative since it doesn't take into account that the pairs are based on comparing the same samples. studentized range should have better size and more power.

    Interesting. For the datasets I'm currently working on, the Nemenyi test seems to give consistently larger corrected p-values than doing a Dunn test followed by a Bonferroni correction, which makes me think I must be doing something wrong! Here's my implementation of the Nemenyi test, and here's an updated version of the Dunn test.

  14. josef-pkt commented on Dec 3, 2014

    @josef-pkt
    MemberAuthor

    If you have R, then you could test against the PMCMR package.
    Also you could run a small Monte Carlo with normal random variables to see the size, and the small sample behavior, how often do the different versions reject under the null.

    I have currently no "prime suspect" to explain that Bonferroni has lower p-values than Nemenyi - studentized range, either code or I don't remember things correctly.

    Could you possibly explain the relationship between the studentized range for some particular sample size and df, and the standard normal distribution? Am I correct in thinking that for some pair of values for sample size and df for which the studentized range is equivalent to the standard normal distribution?

    The main difference is multivariate versus univariate probabilities. Studentized range distribution looks at the joint multivariate distribution of all pairwise differences, while Dunn's version looks at the univariate distribution of a single comparison at a time. The df are unrelated to this.
    The second difference is in normal versus t distribution, studentized range is based on the multivariate t-distribution (normal distributed observations divided by estimate of standard deviation) and with df going to infinity it approaches the distribution based on the multivariate normal distribution. (AFAIR).

    Related aside: I never looked at the variance assumptions that underlie the rank based tests. I'm not sure there isn't a variance homogeneity assumption build in, like in the contrast t-test versus Welch t-test.

  15. 21 remaining items

  16. mysticaltech commented on Aug 14, 2020

    @mysticaltech

    For single-step Dunnett, there is also a matlab m implementation, that is based on a very old piece of Fortran code available in one of the original papers talking about it (I have it too if you need it). https://www.mathworks.com/matlabcentral/fileexchange/38157-dunnett-m

  17. josef-pkt commented on Aug 14, 2020

    @josef-pkt
    MemberAuthor

    One problem is to find the license for those fortran codes, e.g. the matlab fileexchange version is BSD, but it is a translation of an older fortran code where I don't see what the license is.

    The second issue is that the fortran code as translated into matlab is not very maintainable and efficient in Python.
    E.g. I guess implementing it using scipy integrate might be faster and simpler. but it would be more difficult to control the integration error.

  18. mysticaltech commented on Aug 14, 2020

    @mysticaltech

    True, the license issue is not clear, but the paper by Kwong (cited above) can be used to code the necessary methods in Python without licence issues. I mean, if someone blocks on something, taking a quick look as to how it was done in R (here https://rdrr.io/cran/DunnettTests/) should not be a problem. Same thing for Fortran / C++, they come with the book and are book examples. Maybe just good as a helper to better understand the computations.

  19. hillstub commented on Aug 14, 2020

    @hillstub

    Good to hear that more folks are enthusiastic about this! Thanks for all the references - it doesn't seem too complicated. I don't think we have to rely on the different scripts; they can just serve as a source of inspiration. I'll look into this some more and will try to give it a stab.

    Btw, there are also other R packages that have a Dunnett test:

    For my dataset (~2,500 samples, ~200 groups) the multcomp package was very slow (~15 minutes), whereas the emmeans was pretty quick (~10 seconds).

  20. mysticaltech commented on Aug 14, 2020

    @mysticaltech

    That would be awesome @hillstub ! Interesting results from the emmeans script, a quick search in their docs returned this:

    "dunnettx" Uses our ownad hoc approximation to the Dunnett distribution for a family of estimates having pairwise correlations of 0.5 (as is true when comparing treatments with a control
    with equal sample sizes). The accuracy of the approximation improves with the number of simultaneous estimates, and is much faster than "mvt". (Available for two-sided cases only.)
    

    Also, more about this here https://stats.stackexchange.com/questions/449255/how-is-the-classic-dunnett-test-with-strict-assumptions-related-to-dunnett-com

  21. julioasotodv commented on Jan 18, 2021

    @julioasotodv

    I just found about this issue, as I was planning to roll my own Dunnet correction...

    Does anyone know the state for this?

  22. mysticaltech commented on Jan 18, 2021

    @mysticaltech

    @juliantaylor I think it didn't move, if you can do it, it would be great!

  23. josef-pkt commented on Mar 30, 2021

    @josef-pkt
    MemberAuthor

    As a workaround for the lack of specific distribution, we can use the general multivariate t-distribution.
    That replicates tukey-hsd pvalues, and should also work for Dunnet.

    We could add that also for arbitrary contrast matrices, with the caveat that our multivariate t cdf/probabiliy computation is not very fancy.

  24. mysticaltech commented on Mar 31, 2021

    @mysticaltech

    I trust your good judgment @josef-pkt! 🙏🏻 ✨

  25. saiwing-yeung commented on Nov 10, 2021

    @saiwing-yeung

    It looks like there is a typo in various places in the doc. Instead of "Dunnet", it should be "Dunnett" (two t's). For example: https://en.wikipedia.org/wiki/Dunnett%27s_test

  26. mysticaltech commented on Aug 10, 2022

    @mysticaltech
  27. josef-pkt commented on Aug 31, 2022

    @josef-pkt
    MemberAuthor

    Related to rank base multiple comparisons like kruskal.nemenyi test

    new more specific issue for k-sample statistics #6994
    Note, newer literature all recommends against using joint ranking as in nemenyi because it does not keep strong FWR.
    The recommended tests use pairwise ranks, e.g. mann whiteney or brunner munzel.

    Also all pairwise comparisons might not satisfy transitivity (?) if distribution functions of the k samples intersect.

  28. added a commit that references this issue on Sep 13, 2026
    5659791
  29. Shikhar-Kesharwani commented on Sep 13, 2026

    @Shikhar-Kesharwani

    Implemented in PR #10248. Added dunn_test() and DunnResult in statsmodels.stats.nonparametric (and re-exported in statsmodels.stats.api) supporting pooled and unpooled variance formulations, full tie-adjustments, and family-wise / FDR multiplicity adjustments via multipletests. Benchmark parity verified against R's dunn.test package.

  30. Shikhar-Kesharwani commented on Sep 13, 2026

    @Shikhar-Kesharwani

    Dedicated PR opened in #10251 with isolated diff, full test suite, and benchmarks.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions