Skip to content

ENH: add t_test_pairwise to LikelihoodModelResults - #4365

Merged
josef-pkt merged 7 commits into
statsmodels:masterfrom
josef-pkt:t_test_pairwise
Mar 21, 2018
Merged

josef-pkt merged 7 commits into
statsmodels:masterfrom
josef-pkt:t_test_pairwise

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Mar 20, 2018 •

Copy link
Copy Markdown
Member

this is related to #4361, and #4323 as a model analogue of tukeyhsd (with general multipletesting p-value correction)

pairwise comparison for the main effect of a categorical exog.
Also, this should be similar to Stata pairwise option, but I haven't compared yet.

This uses now the encoding contrast matrix from design_info. Therefore it should be independent of the contrast/categorical encoding.
I checked Treatment encoding with and without reference in my examples and it looks good.

Only one minimal unit test comparing t_test_pairwise with t_test with explicitly provided constraints.

t_test_pairwise is added as a method of LikelihoodModelResults, but actual function is in stats.contrasts.py

todo for now

  • check api, maybe privatize some method
  • more unit tests, (I have more examples in my developing notebook)

todo later

  • improved results, more than just returning dataframe
  • generalize to other target restrictions than pairwise, e.g. deviation from mean (?)
  • generalize to other params besides main effect
  • add api and interface for non-formula exog
  • related: add multiple pvalue correction in t_test ContrastResults (it's easy to do by users for dataframe as done in here)
  • maybe: add tukeyhsd pvalues for special of balanced, orthogonal contrasts FAQ/ENH: two-way Tukey HSD #4323 (which triggered this round)

@josef-pkt

Copy link
Copy Markdown
Member Author

usage example

res0 = smf.ols('Cleaness ~ Stain + DETERGENT', data=melted_df).fit()
resp0cm = res0.t_test_pairwise("DETERGENT", method='hommel', factor_labels='A B C D'.split())

print(resp0cm.result_frame)

         coef  std err         t     P>|t|  Conf. Int. Low  Conf. Int. Upp.  \
B-A  2.000000  1.44658  1.382572  0.216055       -1.539653         5.539653   
C-A  4.666667  1.44658  3.226001  0.018001        1.127014         8.206319   
D-A -3.666667  1.44658 -2.534715  0.044396       -7.206319        -0.127014   
C-B  2.666667  1.44658  1.843429  0.114831       -0.872986         6.206319   
D-B -5.666667  1.44658 -3.917286  0.007826       -9.206319        -2.127014   
D-C -8.333333  1.44658 -5.760715  0.001193      -11.872986        -4.793681   

     pvalue-hommel reject-hommel  
B-A       0.216055         False  
C-A       0.072003         False  
D-A       0.133189         False  
C-B       0.216055         False  
D-B       0.039132          True  
D-C       0.007157          True  

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 1 alert when merging c1d78dc into f5b0ee6 - view on lgtm.com

new alerts:

  • 1 for Iterable can be either a string or a sequence

Comment posted by lgtm.com

@coveralls

coveralls commented Mar 20, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 82.598% when pulling ba959b6 on josef-pkt:t_test_pairwise into f5b0ee6 on statsmodels:master.

@josef-pkt

josef-pkt commented Mar 20, 2018 •

Copy link
Copy Markdown
Member Author

appveyor fails at
if isinstance(idx_start, int)
where idx_start = 2L

instance check too strict, (somewhere in numpy or scipy there was recently an issue about how to check for all possible kinds of integers)
here https://stackoverflow.com/questions/48458438/why-is-numpy-int32-not-recognized-as-an-int-type/48467151 are answers that I looked at recently, either numbers.Integral or operator.index

However, at the cost of a messier signature, I added an explicit index option. (I don't have a case yet where I need a non-consecutive slice/index)

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 1 alert when merging 9515fd3 into f5b0ee6 - view on lgtm.com

new alerts:

  • 1 for Iterable can be either a string or a sequence

Comment posted by lgtm.com

@josef-pkt

josef-pkt commented Mar 20, 2018 •

Copy link
Copy Markdown
Member Author

travis has one unrelated error in test_webuse()
error: [Errno 104] Connection reset by peer

appveyor has a patsy error with long integer 2L in name, in python 2.7 but only in one of the 2.7, the other one is green (no idea why one is green, both use patsy 0.5)

           PatsyError: unrecognized token in constraint
E               C(Weight)[T.2]
E               ^

the variable name is C(Weight)[T.2L]'

AFAIR, we had this somewhere before already, but where?
#4108 it's related to pandas
pandas-0.22.0 causes test error, but no failure/error with pandas-0.20.3
fix by thequackdaddy was to cast to int: data = data.astype(int)

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 1 alert when merging c51b9d5 into f5b0ee6 - view on lgtm.com

new alerts:

  • 1 for Iterable can be either a string or a sequence

Comment posted by lgtm.com

@codecov-io

codecov-io commented Mar 20, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #4365 into master will increase coverage by 0.03%.
The diff coverage is 94.4%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4365      +/-   ##
==========================================
+ Coverage   79.92%   79.96%   +0.03%     
==========================================
  Files         560      560              
  Lines       83985    84144     +159     
  Branches     9525     9534       +9     
==========================================
+ Hits        67124    67283     +159     
+ Misses      14645    14642       -3     
- Partials     2216     2219       +3
Impacted Files Coverage Δ
statsmodels/base/model.py 82.73% <100%> (+0.08%) ⬆️
statsmodels/stats/tests/test_anova.py 100% <100%> (ø) ⬆️
statsmodels/stats/tests/test_contrast.py 100% <100%> (ø) ⬆️
statsmodels/base/tests/test_generic_methods.py 96.22% <100%> (+0.86%) ⬆️
statsmodels/stats/contrast.py 80.26% <87.83%> (+4.28%) ⬆️
statsmodels/stats/descriptivestats.py 24.13% <0%> (ø) ⬆️
statsmodels/sandbox/stats/multicomp.py 35.06% <0%> (+1.43%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f5b0ee6...ba959b6. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member Author

I'd like to stop here and merge.

@kshedden Do you have any general comments?
It's the first time that we actually use the factor information that patsy provides for the formulas. I think there will be more applications where this will be useful, e.g. it might be easier to create artificial datasets for predict, predict_at or margins.
(I haven't looked at interaction effects yet, nor other cases like nested factors.)

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 1 alert when merging bd91e53 into f5b0ee6 - view on lgtm.com

new alerts:

  • 1 for Iterable can be either a string or a sequence

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member Author

all green, unit tests are missing for one function in contrasts that is currently unused, written for my previous version.

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 1 alert when merging ea51b59 into f5b0ee6 - view on lgtm.com

new alerts:

  • 1 for Iterable can be either a string or a sequence

Comment posted by lgtm.com

@josef-pkt

Copy link
Copy Markdown
Member Author

now also has alpha keyword for reject boolean

merge when green

@josef-pkt

Copy link
Copy Markdown
Member Author

This pull request introduces 1 alert when merging ba959b6 into f5b0ee6 - view on lgtm.com

new alerts:

  • 1 for Iterable can be either a string or a sequence

Comment posted by lgtm.com

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