Skip to content

Results Class for tukeyhsd, added confidence interval plotting functionality - #809

Closed
tyleha wants to merge 27 commits into
statsmodels:masterfrom
tyleha:master
Closed

tyleha wants to merge 27 commits into
statsmodels:masterfrom
tyleha:master

Conversation

@tyleha

@tyleha tyleha commented May 4, 2013

Copy link
Copy Markdown
Contributor

In response to issue #789, created a results class TukeyHSDResults that more concisely contains the output of a tukey honesty significant difference test from the MultiComparison class. This class now contains all the raw output that MultiComparison.tukeyhsd() used to, plus a couple extras. The Hochberg interval plotting i discussed is also contained within this new class, such that the expected workflow would be something like:

mc = MultiComparison(data, groups)
results = mc.tukeyhsd()
print results #the summary table we were used to before
results.compute_plot_intervals()

The current workflow is also compatible with the pairwise_tukey function.

@josef-pkt

Copy link
Copy Markdown
Member

Thank you
a quick look only so far, the structure looks good, and I will go over the details soon

about the plots:

  • matplotlib is optional so we need to protect import or import only in function
  • we add an ax argument, if users want to attach the plot to an existing figure
  • we return the fig instance

examples for this pattern are in statsmodels graphics,
I also just made a similar protected plot here
josef-pkt@23774f1#L0R249
with smoke unit tests in a later commit (with skip if matplotlib is not installed)

tyleha added 4 commits May 6, 2013 23:42
…lotlib internally imported, allowed attachemnt of ax argument, and returned associated fig instance.
…nts, and made comparison_name optional so you could just plot all groups in black without preference. for pull request statsmodels#809
@tyleha

tyleha commented May 9, 2013

Copy link
Copy Markdown
Contributor Author

Looks like the Travis build is failing in the TestTuckeyHSD2 tests since the method now returns a results object instead of summtab, res. I can potentially see about fixing the tests this weekend but if anyone with better knowledge of the statsmodels unittesting wants to take a crack at it, be my guest

@josef-pkt

Copy link
Copy Markdown
Member

The actual test methods are inherited from CheckTuckeyHSDMixin(object)

In the methods, test_multicomptukey, test_group_tukey and test_shortcut_function, the double indexed results need to be replaced with the corresponding attributes of the new results class.

@josef-pkt

Copy link
Copy Markdown
Member

The docstrings need to be converted to the numpy documentation standard https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
so that they are correctly parsed for the sphinx documentation.

@josef-pkt

Copy link
Copy Markdown
Member

We also need to add unit tests for the new functionality, especially the confidence intervals.

Tyler, can you get benchmark/ verified results for those for example for the datasets that are used in the unittests in the TestTuckeyHSD?

@josef-pkt

Copy link
Copy Markdown
Member

Tyler, thanks again for working on this.

I'm busy until the middle of next week, but I would like to get this merged later next week. Then, I can also look into any loose ends, and make any additional changes that will still be necessary.

@tyleha

tyleha commented May 9, 2013

Copy link
Copy Markdown
Contributor Author

If we assume MATLAB results as the gold standard, I can certainly compute intervals on a couple datasets in Matlab and compare results against python, running them in test_pairwise.py. I figure 2-3 unique datasets should be fine.

If it's just converting res elements to TukeyHSDResults attributes, I can probably tackle that while I'm in there.

@josef-pkt

Copy link
Copy Markdown
Member

If the definitions for the confidence intervals are pretty standard, then MATLAB will be good. In some other cases, we had to go over several other statistical packages to figure out what the different versions were doing (popular difference is in degrees of freedom corrections), and to decide what we support.
But I don't expect that this is the case here.

@tyleha

tyleha commented May 10, 2013

Copy link
Copy Markdown
Contributor Author

Well, seeing as I essentially just translated the Matlab multcomp function into python, these had better be the same confidence intervals. I've done some personal confirmation of this on my own and everything checks out, but unittests are still wise. The math behind them comes from Hochberg et al. p94 (also in the docstring) if I need to be any more specific.

Unfortunately, Matlab is the only place I have encountered these specific interval calculations, so I don't know of any alternative methods of confirmation. But I have complete faith in the Matlab implementation.

@josef-pkt

Copy link
Copy Markdown
Member

Did you actually translate license incompatible matlab code for this?
The code didn't look matlab style, but I haven't looked closely yet.

http://en.wikipedia.org/wiki/Tukey's_range_test#Confidence_limits
looks pretty generic

checking again: R has also confidence interval, and the tukeyhsd function has confidence intervals that are already tested against R
Do your new confidence interval differ from the existing ones?

(I'm just looking at bits and pieces, I don't have a full overview yet.)

@tyleha

tyleha commented May 10, 2013

Copy link
Copy Markdown
Contributor Author

Ok. I feel as though I have not done a good job explaining this pull request. Let's go for the full overview.

A little Matlab background

Matlab has an excellent package for multiple comparisons called multcompare. It allows you to perform tukey hsd multiple comparisons, among a variety others. In addition to computing inter-mean significance, standard error, and the confidence intervals for the differences between each mean (the tukey range test CI you linked to above), multcompare also returns a plot displaying the means each group, and a _single confidence interval for each group_.

What is unique about this plot is that the confidence interval for each mean is universal; each CI allows simultaneous visual comparison of significance to any other group mean. That is to say, if you look at the width of one group CI and it does not overlap with another, those two means will be found to be significantly different at the p=0.05 level using the tukey hsd test. Whereas in your initial blog example, you needed to plot a difference of means and its CI for each pair of groups (which would be 6 CIs for 4 groups, 10 for 5 groups and so on exponentially), this plot requires only a single CI for each group (4 CIs for 4 groups, 5 for 5 and so on) to visually identify the same information. It's quite nice once you get used to it. These CIs were first proposed by Hochberg et al. and are based off knowledge of the q_crit value from tukey's hsd and the RMSE of all group means. (And just to reiterate, these are NOT the same CIs as tukey confidence limits, R's confidence intervals, or even the CIs outputted by statsmodels's tukeyhsd().)

On to Python

To my knowledge, python has no such implementation of these "Hochberg" intervals. As far as I know, nor does Pandas or R. Seeing as I very much like this plot, and it's used a lot in my department for a quick way to compare all group means, I coded up this pull request to emulate the multcompare plot functionality, attempting to mimic the format, coloring, and scheme of the plot as best I could. For that reason, I said I "translated" the Matlab code to python. It would have been better to say I attempted to emulate the Matlab plot, and checked my math against the raw multcompare code to ensure we were computing the same intervals.

Hopefully this clears a few things up! I am happy to try and keep contributing.

tl;dr: no, this code is not tranlsated (bad word choice on my part) from Matlab and no, the confidence intervals computed in my pull request are not the same as tukey range test CIs.

@josef-pkt

Copy link
Copy Markdown
Member

Thank you for spelling this out very explicitly, I think I understand.
I don't have the Hochberg and Tamhane book, and only very briefly looked at the available appendix. I will figure out your (Hochberg's, matlabs) confidence interval next week.

And thanks for clarifying the "translation", it's not what we usually understand under translating code, and we need to be careful with license restricted or incompatible code.

I didn't want to sound critical, I just needed some clarification. I think this pull request is a good improvement and enhancement.

…ng passed into stats.tukeyhsd() instead of the list of independent group nobs. This meant that df_total and thus the crit_int interval widths were being calculated incorrectly. Matters for pull request statsmodels#809 so that now the interval widths of plot_intervals will match the results_table.
@tyleha

tyleha commented May 16, 2013

Copy link
Copy Markdown
Contributor Author

So I see the comment in tukeyhsd() that there's an error in the variance calculation when nobs_all is scalar, missing 1/n. What was that error? I have switched the call to tukeyhsd() to now use the scalar self.groupstats.groupnobs, seeing as group independent measure of variance is how the hochberg CIs are calculated (see commit message above). When comparing the computed intervals using scalar nobs_all to the same intervals calculated in Matlab, they check out (are equivalent). In other words, I don't know what the error in the variance calculation is/was.

@josef-pkt

Copy link
Copy Markdown
Member

I don't remember the details for the comment. There are many combinations of whether nobs are scalar or vectors and whether vars are pooled and I don't remember for which case(s) it failed. (Also I don't remember whether that was supposed to work for the single sample case).
I need to see if I can dig out some old examples that I used.

About your change with self.groupstats.groupnobs:
Do you have an example for tukeyhsd where you get different results from matlab?
Do you have an unbalance (unequal nobs per group) example?

tukeyhsd has several test cases against R, but I might not have paid attention whether there are unequal sample size cases. There should be some, but from a quick look at test_pairwise I cannot see that.

We need to fix the tests and add new unit tests to confirm what works and whether something doesn't work correctly.

@josef-pkt

Copy link
Copy Markdown
Member

usage of the plot functions (based on reading the code again, without playing with an example)

current:
compute_plot_intervals differs from plot_intervals by qcrit as argument and calculating halfwidths.
plot_intervals cannot be called without first calling another function.

Having two functions internally is fine to separate calculation code from pure plotting code (but not necessary). However I think it would be less confusing to have only one user-facing plot function, and make any other methods private (prefix with underline).

AFAICS, we have to use cases calling plots:

  1. call the the plot method with a new qcrit (alpha) and recalculate the confidence interval
  2. repeatedly call the plot method to display different comparison categories.

Using qcrit as argument looks inconvenient to me, since the user needs to find qcrit (or is there a special use for it?) Allowing user to specify alpha instead to change it from the original call to tukeyhsd looks more convenient to me.

suggestion:

only one user facing method plot_interval(_hochberg)
add alpha=None to this method

  • if alpha is None, reuse existing alpha/qcrit and calculate halfwidth if needed, and create plot
  • if alpha is not None, check if alpha has changed, recalculate if necessary, and create plot

naming of functions:

I think we should also add the pairwise confidence interval plots (as in the other statistical packages) (maybe later)
Then, it would be better to have names that distinguish the two types of plots
plot_interval, plot_interval_hochberg, plot_hochberg, ?
plot_pairs, plot_interval_pairs ?

"interval" as qualifier looks a bit redundant in the name

@tyleha

tyleha commented May 19, 2013

Copy link
Copy Markdown
Contributor Author

Good suggestions. You are correct that those two use cases were the ones I was anticipating. For 1), I agree that q_crit is an unnecessary arg. However, adding alpha is too, and I think breaks the separate workflow of a MultiComparison class (to do the math) and Results class. If a user wants to modify the FWER, they can re-compute tukeyhsd() and create a new results class to act on. As for 2), I think we're in good shape as we're checking if halfwidths exits and computing if not. So now:

Organization of methods: Make compute_intervals an internal function. I think it might be best to make a public sandbox version of the method outside of the results class in case users want to compute these intervals for their own purposes.

Renaming methods: I went back over Hochberg's text real quick to see what he called these intervals - IMO, "hochberg" is meaningless to users. He calls them "simultaneous confidence intervals". One of plot_simultaneous, plot_simultaneous_ci, or plot_simultaneous_comparisons seems best.

@josef-pkt

Copy link
Copy Markdown
Member

just a note on TravisCI: it fails these days sometimes with setup network problems unrelated to our code.
The tests passed though on python 3.2

your suggestion are fine with me, I also thought about dropping alpha/qcrit, but don't have a strong preference.

plot_simultaneous might be enough as a name, both ci and comparison are a bit redundant in this context, I think.
we could add then later a plot_pairs as complement

I don't know if there would be any plots that don't plot confidence intervals (groupings, ...?), which would make adding _confint or _ci more informative

plot_simultaneous_confint would be a nice full name, if we rely on tab completion.

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.

the class docstring can have an Attributes section, where we can add the documentation for attributes that don't have their own docstring

#844

same for MultiComparison (I can also add that later)

(partially duplicated comment because I made the other comment on the changeset instead of the pull request)

@josef-pkt

Copy link
Copy Markdown
Member

I think this looks pretty much ready to merge.
I will do a doc build to see if it works correctly, but I didn't see any mistakes in docstring formatting.

@josef-pkt

Copy link
Copy Markdown
Member

@tylerhartley Do you still have any changes that you haven't pushed yet?

I will try to rebase the changes in this pull request today, and then it won't be easy to get additional changes from this pull request. (except cherry-picking)

@tyleha

tyleha commented May 24, 2013

Copy link
Copy Markdown
Contributor Author

No - this is the full working copy. I think adding some plotting functionality to allpairtests etc. might be fun, but it should probably be in a separate pull request. So this is ready on my end.

@josef-pkt

Copy link
Copy Markdown
Member

Thank you Tyler.

There are still many places that can be improved in this area of statsmodels. I just merged a pull request #784 with some cleanup changes for some tests in statsmodels.stats, but haven't touched results classes yet, nor plotting support.

If you want to work directly related to this pull request, then you should wait until I have merged this, so you can get a master with the changes. I should manage to do the merge in the next few days.

josef-pkt pushed a commit to josef-pkt/statsmodels that referenced this pull request May 25, 2013
…lotlib internally imported, allowed attachemnt of ax argument, and returned associated fig instance.
josef-pkt pushed a commit to josef-pkt/statsmodels that referenced this pull request May 25, 2013
…nts, and made comparison_name optional so you could just plot all groups in black without preference. for pull request statsmodels#809
josef-pkt pushed a commit to josef-pkt/statsmodels that referenced this pull request May 25, 2013
josef-pkt pushed a commit to josef-pkt/statsmodels that referenced this pull request May 25, 2013
josef-pkt pushed a commit to josef-pkt/statsmodels that referenced this pull request May 25, 2013
josef-pkt pushed a commit to josef-pkt/statsmodels that referenced this pull request May 25, 2013
josef-pkt pushed a commit to josef-pkt/statsmodels that referenced this pull request May 25, 2013
@josef-pkt josef-pkt mentioned this pull request May 25, 2013
@josef-pkt

Copy link
Copy Markdown
Member

rebased version is in PR #849
I will do some (cosmetic) changes.
I don't know yet if I will do some refactoring of sandbox.stats.multicomp in that PR, or in a new one.

@josef-pkt josef-pkt closed this in 2ea8555 May 25, 2013
@josef-pkt

Copy link
Copy Markdown
Member

@tylerhartley I merged the rebased branch, Thank you for working on this.

PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
…lotlib internally imported, allowed attachemnt of ax argument, and returned associated fig instance.
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
…nts, and made comparison_name optional so you could just plot all groups in black without preference. for pull request statsmodels#809
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
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.

2 participants