Skip to content

ENH: Add games howell test - #7332

Open
ameliajtaylor wants to merge 4 commits into
statsmodels:mainfrom
ameliajtaylor:add-games-howell-test
Open

ameliajtaylor wants to merge 4 commits into
statsmodels:mainfrom
ameliajtaylor:add-games-howell-test

Conversation

@ameliajtaylor

@ameliajtaylor ameliajtaylor commented Feb 19, 2021 •

Copy link
Copy Markdown
  • closes #xxxx
  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

Adding the Games Howell Post Hoc Test. This is not currently implemented in R or JMP (though there is an add one can get) and AFAIK no other packages. However, as I note in the tests, you can easily use this R pub to code up the same test (the pub suggests there is a package one can use, but it does not work, it's better to use the code snippet) and verify the test values. I added screenshots in the details section below of the results in R using the code linked for the test I included in the PR and the results of using R version of connecting letters for the test added for that.

Because this test is an alternative to Tukey HSD but does not assume equal variances and sample sizes, I followed the code for that statistical test (as per a discussion in the google group) as a model for where to put functions/add them to classes, etc. as well as for the tests. This may not be optimal, but follows existing patterns while making sure all new code is well documented and uses black for style.

Details R Games Howell results

Screen Shot 2021-02-19 at 3 21 22 AM

R connecting letters results (the R package calls it multcompLetters)
Screen Shot 2021-02-19 at 3 29 43 AM

Notes:

  • It is essential that you add a test when making code changes. Tests are not
    needed for doc changes.
  • When adding a new function, test values should usually be verified in another package (e.g., R/SAS/Stata).
  • When fixing a bug, you must add a test that would produce the bug in master and
    then show that it is fixed with the new code.
  • New code additions must be well formatted. Changes should pass flake8. If on Linux or OSX, you can
    verify you changes are well formatted by running
    git diff upstream/master -u -- "*.py" | flake8 --diff --isolated
    
    assuming flake8 is installed. This command is also available on Windows
    using the Windows System for Linux once flake8 is installed in the
    local Linux environment. While passing this test is not required, it is good practice and it help
    improve code quality in statsmodels.
  • Docstring additions must render correctly, including escapes and LaTeX.

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be some unnecessary imports.

from collections import defaultdict

import numpy as np
import networkx as nx

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

networkx is not a dependency of statsmodels.

@ameliajtaylor ameliajtaylor Feb 19, 2021 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I forgot to add that/bring this up. As you note below, it is only used for visualizing the results of the test. I see 4 options

  1. add the dependency
  2. leave out the visualization,
  3. keep the visualization and re-implement the underlying algorithm for finding cliques in a graph (or following this paper, which boils down to cliques in a graph).
  4. follow the model of plot_simultaneous in TukeyHSDResults in statsmodels.sandbox.stats.multicomp.py which uses matplotlib but the module itself does not import it and there is not test for it and thus the visualization support is there, but statsmodels does not have matplotlib as a dependency.

Maybe #4 is best? I'm reluctant to give up the visualization for the reasons I put in the other comment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number 4 is fine, just import in the function
and make unit test conditional on availability of nx.
We do the same for matplotlib, where we have mostly only smoke test for plot functions and plot methods.
cvxopt is another optional dependency that has imports and unit tests conditional on availability

"""
return pd.DataFrame(self._results)

def connecting_letters(self, sig_level = 0.05):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this important here? It is the only use of nx. Doesn't seem to be related to the test.

@ameliajtaylor ameliajtaylor Feb 19, 2021 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good discussion to have. You are correct, we can add the statistical test without this function. However, both JMP and R have such a report for Tukey and it makes using the results much much easier.

And if we agree to keep it, we should discuss the name of this function, since I'm not sure we can use the same name as what JMP calls it (I meant to rename before posting the PR and forgot) and discuss where it should live --- in this implementation it can only be applied to Games Howell results, but with a bit of refactoring of the Tukey code and some abstraction here it could be used to visualize results from either test and is really helpful for such visualization (I decided not to do that before posting as it requires some other minor, but non-zero refactoring of existing code and thought that might be better as a follow-up PR, and it is arguable it should be done as part of this PR --- I can do it without changing impacting the existing API for either pairwise_tukeyhsd or pairwise_games_howell).

I use networkx because the algorithm for this is to compute cliques in a graph. To remove that dependency and keep this visualization of the results would require re-implementing that algorithm.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It possibly could be added as a soft dependency. Would need to consult @josef-pkt . In this case, networkx would only be imported in the function where it is needed in a try, except ImportError, and if not available a "friendly" error message would be given to users to tell them that networkx is required but not available.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked at the details yet. I've never seen a graph representation for this.
I have a function (in a PR ?) that assigns letters to individual groups, for a standard linear display (list of letters as grouping).

Maybe we can just return some structure that users can feed directly to nx instead of us doing it.
Otherwise we make it optional and build graph if nx is available.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josef-pkt can you point me to that function. I think it is doing something fundamentally different from this, but without looking I'm not sure. The issue here is the algorithm --- I use a dataframe to display the results/return it to the user. I'm using networkx to find the cliques in the graph that tell you how the groups are connected. I need to look again at what the raw return is from the find_cliques function in nx is to see how user friendly, or not that might be.

I personally, like @bashtage's suggestion to use a try, except, ImportError with a graceful/friendly fail to help users and understand if that's not considered an option. Note that plot_simultaneous in TukeyHSDResults requires matplotlib and has none of this --- I'm not saying we should follow that pattern, rather to say we should set the pattern here we want in other cases, like that, for the future.

@ameliajtaylor ameliajtaylor changed the title Add games howell test ENH: Add games howell test Feb 19, 2021
@josef-pkt

Copy link
Copy Markdown
Member

piepo grouping letter assignment is in #3674
I wrote it, but didn't want to think about where to put it and how to connect it.

@ameliajtaylor

Copy link
Copy Markdown
Author

@josef-pkt That looks to be the same thing. I will do some testing on it with my examples that I know caused problems for a JMP extension. And then, assuming all goes well when I dig in, I'll likely remove that feature from this PR and do the work on your code to figure out where to put it and how to connect it (I have an idea that is better than what I did here).

The idea being that I am cribbing off of the fact that you can use clique algorithms to do this, but you implemented it yourself (the part I didn't want to do), so now we can combine forces!

@josef-pkt josef-pkt added this to the 0.14 milestone Oct 3, 2021
@josef-pkt josef-pkt mentioned this pull request Feb 14, 2022
16 of 20 tasks

def groupdemean(self):
"""groupdemean"""
def groupedmean(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a grouped_mean
it's group_demean, i.e. data is demeaned by groups (i.e. residual of a group indicator model)

@josef-pkt

Copy link
Copy Markdown
Member

games-howell has been merged in #9487 as an extension to tukey-hsd (instead of using a separate implementation)

still extra in this PR: unit test case and letter codes

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants