Skip to content

REF/ENH/SUMM refactor and enhance Multicomparison.tukeyhsd #4379

Open

Description

@josef-pkt

Multicomparison.tukeyhsd has been written a long time ago and never been updated, besides some fixes and enhancements.

It's pre-pandas and uses structured dtype to build the summary SimpleTable.

I don't want to rewrite it using pandas, e.g. groupby, given that the current numpy based group statistics never needed maintenance (AFAIR).
However, the code structure is not very useful for enhancements. The code is spread over a standalone function, the Multicomparison.tukeyhsd method and the TukeyHSDResults class. Too much is done on the lower level and can be moved into the results class, making it easier to change and add extra results.

TukeyHSDResults is not just a Holder class anymore, given that plotting method was added a long time ago.

The standalone tukeyhsd function is a full test function returning a tuple with many elements, however does not include p-values. #2346 Arguments are summary statistics mean_all, nobs_all, var_all
I'm not sure we should change it much. It is also in the official api through statsmodels.stats.multicomp.
specifically: do we add p-values to the tukeyhsd function and hand it all the way up to TukeyHSDResults or do we directly add the p-values to the results class.

what's the usage pattern? (I'm not sure)

general idea for hypothesis test: support both simple return of numbers for large scale hypothesis testing and returning informative results classes for closer, interactive investigation. I doubt there is much large scale hypothesis testing with tukeyhsd. In that case we are free to just focus on the results class.

internal usage: Which parts of the results class can be reused across different multiple comparison hypothesis tests? #4365 added a pairwise comparison method to the models also with just a basic results class which essentially just holds a dataframe.
Other mutliple comparisons are in various experimental and prototype code, e.g. kruskal-nemenyi. I have no idea yet what we will need there.

issues bugs and enhancements

and more general

  • tukeyhsd for (balanced) two-way ANOVA

proposed solution for now:

  • move summary table creation to results class
  • add attributes or cached attributes to results class
  • maybe add new methods to results class: How do we add numerical integration mvnormal p-values? Those are not the first choice, slower and more fragile than qsturng, but maybe more accurate in some cases and fewer range limitations.
  • add summary_frame so users get a pandas compatible summary to work with (similar to t_test and t_test_pairwise) (e.g. save to csv as in TukeyHSD: No method to get group1 and group2 to match res.std_pairs #2762)

related:
We have different methods to compute multiple comparison p-values. Make it more reusable, e.g. make it simpler to add qsturng pvalues to other hypothesis test. We should get a standard distribution interface to qsturng (sf/pvalue and ppf/critvalue).

Activity

  1. josef-pkt commented on Mar 22, 2018

    @josef-pkt
    MemberAuthor

    on issue: does tukeyhsd need a ddof keyword? I guess yes.

    For balanced two-way ANOVA in #4323, I used a group demeaned endog to take out the second categorical factor. The confidence intervals differ from those in R. Either R uses some other small sample correction, or, more likely, we need to correct the degrees of freedom to account for the extra parameters that were estimated for the group demean. (i.e. similar to ddof in absorbed OLS.)

  2. josef-pkt commented on Mar 22, 2018

    @josef-pkt
    MemberAuthor

    unit tests

    statsmodels\stats\tests\test_multi.py has one test for the standalone function against R that includes reference numbers of p adj
    myres = tukeyhsd(m_r, 6, 110.8, alpha=0.05, df=4)

    statsmodels\stats\tests\test_pairwise.py all unit test are for tukeyhsd, for class and dispatch function
    some against R with pvalues that are only used for reject decision
    test_hochberg_intervals res.halfwidths from _simultaneous_ci tested against matlab

  3. josef-pkt commented on Mar 22, 2018

    @josef-pkt
    MemberAuthor

    other:

    sandbox.stats.multicomp also has simultaneous_ci which IIRC are the standard pairwise version and not the hochberg intervals
    for plotting, Kerby added some plot function for lines (?, check whether we can reuse it here)

    there is also a letter assignment somewhere,
    a more recent version of it is in PR #3674, I wrote it but it doesn't have a usecase yet.

    The MultiComparison class also has allpairtest that just returns a tuple similar to the arguments for TukeyHSDResults. Would be good to add an informative results class to that also.

  4. josef-pkt commented on Mar 22, 2018

    @josef-pkt
    MemberAuthor

    BUG in allpairstest: alpha is hardcoded to alpha=0.05 in the call to the multipletesting correction instead of using the provided alpha.
    (aside allpairstest doesn't have unit tests AFAIR, R uses pooled variance instead of pairwise variance, somewhere there should be comments about the details.)

  5. josef-pkt commented on Mar 25, 2018

    @josef-pkt
    MemberAuthor
  6. sadepu1915 commented on Jun 28, 2019

    @sadepu1915

    Is any body working on this ...Can i go for it ?

  7. josef-pkt commented on Jan 21, 2025

    @josef-pkt
    MemberAuthor

    I guess we don't really need the critical values anymore in tukey_hsd computation.

        if q_crit is None:
            q_crit = get_tukeyQcrit2(n_means, df_total, alpha=alpha)
    

    #9476

  8. josef-pkt commented on Jan 24, 2025

    @josef-pkt
    MemberAuthor

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

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