Skip to content

SUMM/ENH/REF multiple comparison - overview, open, roadmap #8396

Open

Description

@josef-pkt

I don't have a good overview what the current status is and the roadmap for it should be.
We have several open issues and prs, enhancement requests, code that needs refactoring and more testing and likely bug fixes.
I put it on the 0.15 roadmap #8217

some of it also applies to statsmodels.base, e.g. extension of t_test_pairwise

an older generic issue Multiple Comparisons #852 includes discussion on rank based Nemenyi and dunnet's comparison to a control

(still incomplete, there are likely open issues and prs missing from this list)

Activity

  1. added this to the 0.15 milestone on Sep 5, 2022
  2. josef-pkt commented on Sep 5, 2022

    @josef-pkt
    MemberAuthor

    aside naming

    Maybe I should not use the term "contrasts", and should use "constraints" or "restrictions" instead.
    Standard definition of contrasts in statistics has a summation requirement (sum to zero?), that we don't always use.
    We can match terminology in wald_test and fit_constrained.

  3. josef-pkt commented on Sep 6, 2022

    @josef-pkt
    MemberAuthor

    Monte Carlo for continuous, compares tukey-hsd, games howell (unequal variance) and others

    disadvantage:
    They don't show the actual size for unequal variance case, table has adjusted alpha so that size is around 0.05.
    They simulate different distributions but assume all samples come from the same distribution family with and without equal variance.
    Maybe there are more recent monte carlo studies.

    Philip H. Ramsey , Kyrstle Barrera , Pri Hachimine-Semprebom & Chang-Chia Liu (2011) Pairwise comparisons of means under realistic nonnormality, unequal variances, outliers and equal sample sizes, Journal of Statistical Computation and Simulation, 81:2, 125-135, DOI: 10.1080/00949650903219935

  4. josef-pkt commented on Jan 21, 2025

    @josef-pkt
    MemberAuthor

    We need some cleanup to make MultiComparison class fully public and move out of sandbox.

    • fix return of allpairstest (and possibly rename method)
    • fix or remove "kruskal" method, currently it has no use BUG/ENH kruskal as MultiComparison #2663
    • decide what to do about rank transformation. We might not need it if we have rank based test functions that we can use with allpairstest (*)

    one possibility is to deprecate allpairstest and replace it with test_allpairs with new return (is backwards compatible in case anyone already uses allpairstest)

    (*)
    Use brunner munzel rank_compare_2indep and scipy kruskal
    Do we still need rank specific methods for e.g. nemenyi #852 #799 ?
    One case where we will need support and methods in MultiComparison class is when we want to pool information across all groups (e.g. ranks based on full sample instead of each pair separately as in allpairstest.)
    (I don't remember any details.)
    Additionally, current GroupStats class ignores ties when computing ranks, i.e. it is not mid-rank as in brunner munzel.
    (It is supposed to be faster than mid-rank computation, but there might not be much difference anymore with newer scipy.)
    update for this:
    MutliComparison does not compute ranks in __init__. Method getranks computes rankdata and attaches as attributes.
    getranks is called by kruskal method, kruskal method prints some results, and returns, AFAICS, uncorrected pvalues.

    Do we need convenience functions for 2-sample tests ?
    e.g. brunner mumzel rank_compare has test method in results instance, i.e. rank_compare_2indep(x1, x2).test_prob_superior() (*1)

    update (thinking some more)

    We might still want to have pre-made pairwise tests instead of user provided callback function in allpairstest, mainly for user convenience and for adding specific returns, similar to tukey_hsd including games howell.

    Performance ?
    I guess the overhead for group handling in setting up a multi comparison instance would not all be needed for specific tests.
    Flexibility versus performance?

    (*1)

    from statsmodels.stats.nonparametric import (
        rank_compare_2indep
        )
    ​
    ​
    apth = mc.allpairtest(lambda *args: rank_compare_2indep(*args).test_prob_superior(), method="holm")
    
    print(apth[0])
    Test Multiple Comparison <lambda> 
    FWER=0.05 method=holm
    alphacSidak=0.02, alphacBonf=0.017
    =======================================================
      group1      group2     stat    pval  pval_corr reject
    -------------------------------------------------------
    b'medical'   b'mental' -17.8885    0.0       0.0   True
    b'medical' b'physical'  -1.7605 0.1041    0.1238  False
     b'mental' b'physical'   2.0375 0.0619    0.1238  False
    -------------------------------------------------------
    

    update 2

    naming convention, is not (newer) standard, no underline separation, for example group statistics

    mc.groupstats.groupnobs, mc.groupstats.groupmean, mc.groupstats.groupvarwithin()
    (array([ 9,  7, 10], dtype=int64),
     array([2.11111111, 4.        , 3.        ]),
     array([0.61111111, 0.33333333, 1.77777778]))
    

    It would likely be better to privatize (leading underscore) some attributes or methods.
    Current MultiComparison class is only or mainly to support tukey-hsd, the rest is not API reviewed.

    also groupstats should have an option to return table or pandas dataframe for quick overview

    Also, old question/issue:
    It might be better to use pandas groupby instead of our own grouping methods.

  5. josef-pkt commented on Jan 21, 2025

    @josef-pkt
    MemberAuthor

    other kinds of hypotheses, in analogy to two sample hypothesis tests

    • pairwise comparisons for TOST ? should already work with allpairstest
    • one-sided options: only relevant for ordered categories or all to one comparison, I think it wouldn't make sense for allpairs.
      multiple comparison after trend tests (which we don't have yet)
      ENH: k-sample trend tests #1130 (comment) mentions multiple testing for trend tests
    • multiplicity corrected confidence intervals where it makes sense, e.g. brunner munzel superior probability, ranks.
    • transformation (to normality): do we need it or leave it to userr

    types of data
    repeated measures, post-hoc for friedman repeated measures anova ?
    I never looked at those.

    count, poisson ?
    We can leave some of these just to the regression models and t_test_pairwise or generic pvalue correction after t_test.

    (*) tost example

    apth = mc.allpairtest(lambda *args: rank_compare_2indep(*args).tost_prob_superior(low=0.45, upp=0.55), method="holm")
    print(apth[0])
    Test Multiple Comparison <lambda> 
    FWER=0.05 method=holm
    alphacSidak=0.02, alphacBonf=0.017
    =======================================================
      group1      group2     stat    pval  pval_corr reject
    -------------------------------------------------------
    b'medical'   b'mental' -16.0102    1.0       1.0  False
    b'medical' b'physical'  -1.3542 0.8996       1.0  False
     b'mental' b'physical'   1.6053 0.9341       1.0  False
    -------------------------------------------------------
    

    we don't reject anything
    I guess small sample and we cannot assert that group means in the pairs are equivalent, not enough power (?)
    Two of the point hypothesis for equality in pairs are not rejected, but we cannot "accept the null" we don't have enough evidence to argue that pairwise means are the same/equivalent.

  6. josef-pkt commented on Jan 21, 2025

    @josef-pkt
    MemberAuthor

    aside:
    Do we have contrast/constraint matrix for pairwise comparison? instead of using the explicit all pairs loop.

    Working with contrast/constraint matrix on group means (and associated standard errors or cov) will make it easier to switch to other sets of comparisons.
    For example for ordered case like trend test, we want to compare consecutive cases.
    Similarly, Dunnett's test for each treatments versus control/reference.
    #4361

    I would like to have a quick way to check consecutive diff or trend to try out one-sided alterantives.
    We could even add a margin, non-zero value to get sequence of noninferiority or superiority tests.

    Note, in the generic version we would have to use pvalue correction.
    Specific pvalues as in tukey-hsd require pre-specified simple comparisons as in Dunnett's treatments versus control.

    see discussion starting here for Dunnet's test #852 (comment)

    alternatively
    We can add list of pairs to compare as an option to a helper function, instead of using predefined "allpairs".
    Then we can still reuse callback to a 2-sample test function, e.g. we don't have Satterthwaite df in regression models.
    (sounds better)

    allpairstest already uses list of pairs defined outside method, so it's generic

    for i, j in zip(*self.pairindices):
                res.append(testfunc(self.datali[i], self.datali[j]))
    

    add pair_indices as keyword, and if it is None, then use the attribute.
    make new method test_pairs which does not necessarily mean all pairs.

    Disadvantage of using callback to standard test function is that we compute some statistics several times, e.g. mean an variance for a group is computed in each pair test separately.
    We would need "from stats" functions, like the tukey_hsd function in sandbox multicomp, that reuses already computed summary, descriptive statistics.

    aside:
    trend contrasts like Helmert or inverse Helmert (compare to mean of "later" levels) have the problem that they are not pairwise comparisons, That is, we compare one group mean to a linear combination of other groups means.
    (Similarly I never looked in details at the literature comparing each group mean versus the average group mean.)

  7. josef-pkt commented on Jan 24, 2025

    @josef-pkt
    MemberAuthor

    the module in the sandbox still has a if __name__ == "__main__":

    it includes, among others, an example and code for pairwise kruskal rank comparisons, kruskal-wallis multiple-comparison

  8. josef-pkt commented on Jan 26, 2025

    @josef-pkt
    MemberAuthor

    in #9487 I had an example with zero within group variance, which causes zero division error and nan in the results.

    One way out would be to use semi-pooling as in Small Area estimation.
    #8397 issue for oneway

    With only categorical variables we don't have a measure of distance between cases, there are no neighbors every group is in the same neighborhood.
    So, the only option is to partially pool the specific group variance with the fully pooled variance, all others pooled or quantiles of those.
    For example, replace zero variance by variance of bottom, e.g. 10th, percentile of groups with nonzero variance, with a minimum percentile size if we only have a small number of groups.
    (The only "neigboring" distance is in endog, i.e. the results of the multi-comparison. This would be appropriate if variance/heteroscedasticity is related to the mean, as in GLM, Poisson data.)

    Another option would be to shrink all variances of small groups towards the pooled target.

    This would be games-howell partially moved towards tukey-hsd. Which could improve estimation if there are groups with small sample sizes and noisy variance estimates.

    Another option as variation of games-howell
    Instead of pooling by sample sizes, we take a simple average variance,, i.e each groups has the same weight in estimating the pooled variance. (I guess this will remove some of the bias in the size of the tests which depend on whether variance is positively or negatively correlated with sample size.)
    We use something similar, AFAIR, in meta-analysis.

    My guess is that some kind of shrinkage like this will improve Games-Howell in very small samples.

  9. josef-pkt commented on Jan 31, 2025

    @josef-pkt
    MemberAuthor

    one more to which variance to use unequal versus equal variance

    • pooled over entire sample
    • pooled over pairs
    • not pooled

    Another issue that I just realized is which mean is used in the variance computation:

    • wald: use combination of within variances x_ij - mean_i
    • score type: assume means are related as specified in the null, e.g. for equality x_ij - mean, when mean it total mean or pairwise mean.

    kruskal uses total mean when computing the variance, terms like ((N + 1)/2)**2 for squared mean of all ranks without ties.
    IIRC, In tukey-hsd we use wald mean for the variance, ie. deviation from groupmeans

    Wald variance will always be smaller than score variance, so Wald type tests reject more often than Score type tests.

    Another option to manipulate results.

    for multicomparison in ranks

    AFAICS Steel-Dwass-Critchlow-Fligner (SDCF) method
    uses common mean in pairs, but standard error of mean (mean rank) differ because of different sample sizes. (score in pairs)
    AFAIR Games-Howell uses wald in pairs.

    So, we can make games-howell more conservative by using unequal score variance in pairs (unequal variance but common mean under Null).

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