Skip to content

Added from_data and summary methods in weightstats CompareMeans - #2754

Merged
josef-pkt merged 4 commits into
statsmodels:masterfrom
ValeryTyumen:CompareMeansSummaryAndFromData
Jan 20, 2016
Merged

josef-pkt merged 4 commits into
statsmodels:masterfrom
ValeryTyumen:CompareMeansSummaryAndFromData

Conversation

@ValeryTyumen

Copy link
Copy Markdown
Contributor

@josef-pkt, take a look, please.

@josef-pkt

Copy link
Copy Markdown
Member

@ValeryTyumen Thank you

most of it looks good, but IIRC the class also works for vectorized data with several columns. In that case the current string in summary will not work.
I think it would be better to use the params table summary_params as it is used in statsmodels.stats.contrast, which is the result after a t-test in the model results.
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/stats/contrast.py#L146
I think that should work in this case also.

@josef-pkt

Copy link
Copy Markdown
Member

Here's an example for the broadcasted version, it looks like it works as expected

(I didn't set a seed, so numbers themselves are not replicable.)

>>> import statsmodels.stats.weightstats as smw
>>> import numpy as np
>>> x = np.random.rand(100, 5)

# compare each of 4 with 1   (e.g. 1 control and 4 treatments)

>>> cm = smw.CompareMeans(smw.DescrStatsW(x[:, :4], weights=None), smw.DescrStatsW(x[:, -1], weights=None))

>>> cm.ttest_ind()
(array([ 1.33455275,  0.55038118,  0.59626587,  0.35984888]), array([ 0.18355543,  0.58267834,  0.55167869,  0.71934367]), 198.0)

>>> cm.tconfint_diff()
(array([-0.02560674, -0.05640965, -0.05422727, -0.06344948]), array([ 0.1328238 ,  0.10008721,  0.10123266,  0.09177433]))

# compare each of 4 with 2: -> error doesn't broadcast

>>> cm = smw.CompareMeans(smw.DescrStatsW(x[:, :4], weights=None), smw.DescrStatsW(x[:, -2:], weights=None))
>>> cm.ttest_ind()
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    cm.ttest_ind()
  File "m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\stats\weightstats.py", line 717, in ttest_ind
    stdm = self.std_meandiff_pooledvar
  File "m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\tools\decorators.py", line 193, in __get__
    val = self.getter(obj)
  File "m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\stats\weightstats.py", line 656, in std_meandiff_pooledvar
    var_pooled = ((d1.sumsquares + d2.sumsquares) /
ValueError: operands could not be broadcast together with shapes (4,) (2,) 


# compare 2 with 2 corresponding to matching columns

>>> cm = smw.CompareMeans(smw.DescrStatsW(x[:, :2], weights=None), smw.DescrStatsW(x[:, -2:], weights=None))
>>> cm.ttest_ind()
(array([ 0.97875698,  0.55038118]), array([ 0.32889443,  0.58267834]), 198.0)

check:
>>> smw.CompareMeans(smw.DescrStatsW(x[:, 0], weights=None), smw.DescrStatsW(x[:, -2], weights=None)).ttest_ind()
(0.97875698190450655, 0.32889442634853316, 198.0)
>>> smw.CompareMeans(smw.DescrStatsW(x[:, 1], weights=None), smw.DescrStatsW(x[:, -1], weights=None)).ttest_ind()
(0.55038117643820628, 0.58267834068182323, 198.0)

@ValeryTyumen

Copy link
Copy Markdown
Contributor Author

I guess it should look this way

>>> from weightstats import CompareMeans
>>> import numpy as np
>>> x = np.random.rand(100, 5)
>>> cm = CompareMeans.from_data(x[:, :4], x[:, -1])
>>> print(cm.summary())
                          A test for means equility                           
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
subset #1           0      0.040     -0.272      0.786      -0.090       0.069
subset #2           0      0.041      0.024      0.981      -0.079       0.081
subset #3           0      0.041      0.609      0.543      -0.056       0.106
subset #4           0      0.042     -0.483      0.630      -0.102       0.062
==============================================================================

@ValeryTyumen

Copy link
Copy Markdown
Contributor Author

@josef-pkt , could you check it, please?

Comment thread statsmodels/stats/weightstats.py Outdated

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.

typo, also reverse phrase sounds better IMO: 'Test for equality of means'

@josef-pkt

Copy link
Copy Markdown
Member

Thanks, looks good, I only so some style issues.

Also, please add it as a smoke test to a unit test. There should be one already where you can just add a call to summary(). (We don't have proper tests for the string output of summary yet)

one more: I need to check coef in summary again

@josef-pkt

Copy link
Copy Markdown
Member

I think value, "coef" in the summary table, should be the actual restriction that is tested
mean1 - mean2 - value
(I don't remember in which direction the - go.)

actually: I think value in the argument could be a vector corresponding to the number of columns of the data. (I doubt that is unit tested already)

@ValeryTyumen

Copy link
Copy Markdown
Contributor Author

Thanks for the review, I'll fix that. But I have a couple of questions.
Just in case, I thought pep8 approves "*" without spaces https://www.python.org/dev/peps/pep-0008/#other-recommendations, doesn't it?
I had a thought about processing value as an array too, it should work, but docstring in ttest_ind and ztest_ind says value is a float object, and I use these methods in summary, so I decided not to add this functionality yet. Is it OK if I add changes to ttest_ind and ztest_ind docs in this PR?

@josef-pkt

Copy link
Copy Markdown
Member

we are following an old pep-8, or the old policy in numpy and scipy. We might update the rules for operators in tight formulas with one or two letter names, but I find the missing spaces very distracting and difficult to read when we have nice informative names. thisisalongname*np.sqrt(abitshorter)/nobs

You can change docstring to clarify or improve them, that's very helpful.

As background:
Often things work just automatically in vectorized form because of numpy broadcasting. We don't exclude them by raising a ValueError, but we also don't advertise them when we don't have really checked or unit tested whether all the different possibilities work correctly.
(Example: early in the development of statsmodels we had a discussion of restricting the model Results.t_test to a single test after we realized that it works in vectorized form. It finally turned out that vectorization in that case is a great feature for many different use cases.)

@ValeryTyumen

Copy link
Copy Markdown
Contributor Author

Considering the above, I decided to leave the documentation as it is. I also decided to make the whole new test for CompareMeans convenient interface, hope it's OK.

Comment thread statsmodels/stats/weightstats.py Outdated

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.

no empty line after header in docstings

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copied that from ContrastResults.summary, it has the same style problem.

@josef-pkt

Copy link
Copy Markdown
Member

some corrections to the docstring, otherwise this looks ready to merge.

@josef-pkt

Copy link
Copy Markdown
Member

Because nobody is reading my PRs, and I'm often too impatient to read everything again before merging, the docstrings are not always clean.
(I had to deprecate two or three functions a while ago because I didn't realize that I had misspelled the family name in the function name.)

@ValeryTyumen Thank you, I will merge after TravisCI reports back

@ValeryTyumen

Copy link
Copy Markdown
Contributor Author

Great! Can I have your e-mail? I have got some questions.

@josef-pkt

Copy link
Copy Markdown
Member

[email protected] But I prefer general statsmodels related questions on the mailing list, I partially ignore private emails or forget about them when they drop of the front page of my mail.

@josef-pkt

Copy link
Copy Markdown
Member

All green as expected, merging

josef-pkt added a commit that referenced this pull request Jan 20, 2016
…Data

ENH: Add from_data and summary methods in weightstats CompareMeans
@josef-pkt
josef-pkt merged commit aa3c3a3 into statsmodels:master Jan 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants