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
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.
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.
@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?
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)
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:
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?
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.
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.
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.
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.
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.
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:
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.)
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.
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.
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.
Activity
josef-pkt commentedon Apr 4, 2014
bump one part of this: add p-values to tukeyhsd results
I just saw that they are still missing in summary()
josef-pkt commentedon Apr 4, 2014
and allpairtest doesn't return results instance:
print mc2.allpairtest(stats.ttest_ind, method='b')[0]josef-pkt commentedon Apr 5, 2014
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,previous issue with pandas #824
latest change that uses
groupsuniquein summary table #1472no unittests for summary(), the values have good coverage, I think
alimuldal commentedon Dec 2, 2014
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
kruskalmethod of theMultipleComparisonclass - it's definitely not recapitulatingscipy.stats.kruskalas the docstring suggests.josef-pkt commentedon Dec 3, 2014
Hi @alimuldal
I didn't know about this Dunn's multiple comparison test. My initial intention in the
MultipleComparisoncode 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)
josef-pkt commentedon Dec 3, 2014
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
alimuldal commentedon Dec 3, 2014
@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?
josef-pkt commentedon Dec 3, 2014
Yes, that's how I see it also, comparing the expressions for the test statistics.
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,
qsturngandpsturng.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)
alimuldal commentedon Dec 3, 2014
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:
What would the degrees of freedom parameter correspond to here? The PMCMR docs seem to suggest that
dfought 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?
josef-pkt commentedon Dec 3, 2014
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:
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.
alimuldal commentedon Dec 3, 2014
Thanks, that's really helpful.
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?
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.
josef-pkt commentedon Dec 3, 2014
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.
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.
21 remaining items
mysticaltech commentedon Aug 14, 2020
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
josef-pkt commentedon Aug 14, 2020
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.
mysticaltech commentedon Aug 14, 2020
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.
hillstub commentedon Aug 14, 2020
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).
mysticaltech commentedon Aug 14, 2020
That would be awesome @hillstub ! Interesting results from the emmeans script, a quick search in their docs returned this:
Also, more about this here https://stats.stackexchange.com/questions/449255/how-is-the-classic-dunnett-test-with-strict-assumptions-related-to-dunnett-com
julioasotodv commentedon Jan 18, 2021
I just found about this issue, as I was planning to roll my own Dunnet correction...
Does anyone know the state for this?
mysticaltech commentedon Jan 18, 2021
@juliantaylor I think it didn't move, if you can do it, it would be great!
josef-pkt commentedon Mar 30, 2021
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.
mysticaltech commentedon Mar 31, 2021
I trust your good judgment @josef-pkt! 🙏🏻 ✨
saiwing-yeung commentedon Nov 10, 2021
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
mysticaltech commentedon Aug 10, 2022
@josef-pkt FYI for Dunnett's maximtrp/scikit-posthocs#24 (comment)
josef-pkt commentedon Aug 31, 2022
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.
ENH: Add Brant test, standardized beta coefficients, and Dunn post-ho…
Shikhar-Kesharwani commentedon Sep 13, 2026
Implemented in PR #10248. Added
dunn_test()andDunnResultinstatsmodels.stats.nonparametric(and re-exported instatsmodels.stats.api) supporting pooled and unpooled variance formulations, full tie-adjustments, and family-wise / FDR multiplicity adjustments viamultipletests. Benchmark parity verified against R'sdunn.testpackage.Shikhar-Kesharwani commentedon Sep 13, 2026
Dedicated PR opened in #10251 with isolated diff, full test suite, and benchmarks.