Skip to content

ENH: rates, poisson means two-sample comparison rebased - #6677

Merged
josef-pkt merged 6 commits into
statsmodels:masterfrom
josef-pkt:poisson_2sample_rebased
May 5, 2020
Merged

josef-pkt merged 6 commits into
statsmodels:masterfrom
josef-pkt:poisson_2sample_rebased

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Apr 30, 2020 •

Copy link
Copy Markdown
Member

rebased version of #2723 from 2015

original draft version for stackoverflow is in issue #2718

link again to my initial notes
https://github.com/josef-pkt/misc/wiki/Exact-and-Asymptotic-Tests---Rates,-Poisson

@pep8speaks

pep8speaks commented Apr 30, 2020 •

Copy link
Copy Markdown

Hello @josef-pkt! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-05-05 21:08:15 UTC

Comment thread statsmodels/stats/rates.py Outdated


print('debug')
print(stat_sample, mask.sum(), mean1, mean2)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

remove print

@josef-pkt

Copy link
Copy Markdown
Member Author

for unit test

a brief search for R:
all exact tests seem to be conditional tests

base stats has poisson.test that uses binomial conditional on the sum of events (uses likelihood to define extreme cases, rejection region, according to Faye not consistent with confint)
https://stat.ethz.ch/R-manual/R-devel/library/stats/html/poisson.test.html

package rateratio.test by Faye has exact test that matches the confidence interval.
https://journal.r-project.org/archive/2010/RJ-2010-008/RJ-2010-008.pdf
https://cran.r-project.org/web/packages/rateratio.test/index.html

package exactci also has poisson with exact pvalue and confint
https://rdrr.io/cran/exactci/man/poisson.exact.html

NCSS/PASS https://ncss-wpengine.netdna-ssl.com/wp-content/themes/ncss/pdf/Procedures/PASS/Tests_for_the_Ratio_of_Two_Poisson_Rates.pdf also quotes Gu

I don't find an E-test in R with google search,

another article, that I guess didn't look at

Guogen Shan (2015) Exact unconditional testing procedures for comparing two independent Poisson rates, Journal of Statistical Computation and Simulation, 85:5, 947-955, DOI: 10.1080/00949655.2013.855776
sounds like a Monte Carlo comparing exact unconditional methods
(I don't have access right now)

@josef-pkt

Copy link
Copy Markdown
Member Author

this PR fails in lint check, but not in unit tests

Comment thread statsmodels/stats/rates.py Outdated


# copied from statsmodels.stats.weightstats
def _zstat_generic2(value, std_diff, alternative):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

remove, I only added this to have a standalone script that didn't depend on statsmodels
(for stackoverflow)

@coveralls

coveralls commented Apr 30, 2020 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 87.765% when pulling 4317ff2 on josef-pkt:poisson_2sample_rebased into f6d0bf6 on statsmodels:master.

@josef-pkt josef-pkt left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

looks fine

cleanup, unit tests against R, and merge

Comment thread statsmodels/stats/rates.py Outdated
return zstat, pvalue


def poisson_twosample(count1, exposure1, count2, exposure2, ratio_null=1,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

rename to test_poisson_twosample or test_poisson_2indep

Comment thread statsmodels/stats/rates.py Outdated
return stat, pvalue


def etest_twopoisson(count1, exposure1, count2, exposure2, ratio_null=1,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

rename etest_poisson_2indep ?

rate1 = rate1_mle
rate2 = rate2_mle

# The sampling distribution needs to be based on the null hypotheis

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

typo hypothesis

Comment thread statsmodels/stats/rates.py Outdated
y_total = y1 + y2
stat = None
# TODO: why y2 in here and not y1, check definition of H1 "larger"
pvalue = proportion.binom_test(y1, y_total, prop=bp, alternative=alternative)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this is covered in R stats poisson test

s1, pv1 = poisson_twosample(count1, n1, count2, n2, method='wald')
pv1r = 0.000356
assert_allclose(pv1, pv1r*2, rtol=0, atol=5e-6)
print('wald', s1, pv1 / 2) # one sided in the "right" direction

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

remove print statements.
I guess this was just a example script file

Comment thread statsmodels/stats/rates.py Outdated
threshold = 100
threshold = stats.poisson.isf(1e-13, max(mean1, mean2))
threshold = max(threshold, 100) # keep at least 100
y_grid = np.arange(threshold + 1)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

needs options, kwds, for users to change this, see also #6676
I'm not sure it's the same grid, i.e. here it says y_grid

@josef-pkt
josef-pkt force-pushed the poisson_2sample_rebased branch from 4a6b619 to 04d53e9 Compare May 4, 2020 19:38
@josef-pkt

Copy link
Copy Markdown
Member Author

I don't find my original script file on my computer. I don't find a notebook either.
Seems the only examples are in the script in #2718

@josef-pkt

Copy link
Copy Markdown
Member Author

comparing with GLM Poisson

wald test doesn't match up exactly, score_test does
pv2 is using the function in this PR

st = res.model.score_test(res.params, exog_extra = [1., 0])
st, st[1][0], pv2
((array([11.67863472]), array([0.00063222]), 1),
 0.0006322190895476891,
 0.0006322188820470972)

@josef-pkt
josef-pkt force-pushed the poisson_2sample_rebased branch from 04d53e9 to 8e4071e Compare May 4, 2020 21:00
@josef-pkt

Copy link
Copy Markdown
Member Author

corner case count1 = count2 = 0

In etest, I use a grid that contains the both zero point. This raised RuntimeWarning in the unit test.
I added an small number eps to avoide zero-division, but this case needs to be investigated and get an "official" behavior. (clipping or adding 0.5 like continuity correction in binomial/proportions)

@josef-pkt

Copy link
Copy Markdown
Member Author

bug, missing unit test
In method='exact-cond': default alternative "2-sided" does not have correct spelling for binom test

values are good, agrees with R stats

s2, pv2 = smr.test_poisson_2indep(count1, n1, count2, n2, method='exact-cond',
                                  ratio_null=1., alternative='two-sided')
 -
pv2, 0.0006751826586863219, pv2 - 0.0006751826586863219  # R stats poisson.test
(0.0006751826586863214, 0.0006751826586863219, -5.421010862427522e-19)

@josef-pkt

josef-pkt commented May 4, 2020 •

Copy link
Copy Markdown
Member Author

current exact_cond and cond_midp are "minlike"

library exactci Fay

Note: midp doesn't have conf.int. If midp=FALSE, then conf.int is available

> poisson.exact(c(60, 30), c(51477.5, 54308.7), tsmethod="minlike", midp=TRUE)

        Exact two-sided Poisson test (sum of minimum likelihood method), mid-p
        version

data:  c(60, 30) time base: c(51477.5, 54308.7)
count1 = 60, expected count1 = 43.795646313035, p-value =
0.0005572624066191
alternative hypothesis: true rate ratio is not equal to 1
95 percent confidence interval:
 NA NA
sample estimates:
      rate ratio 
2.10999757175465 
s2, pv2 = smr.test_poisson_2indep(count1, n1, count2, n2, method='cond-midp',
                                  ratio_null=1., alternative='two-sided')
s2, pv2
(None, 0.000557262406619058)

@josef-pkt

Copy link
Copy Markdown
Member Author

The two-sided conditional exact test delegates to proportion.binom_test and then to scipy.stats.binom_test

so "minlike" is inherited from scipy.stats, see also #2735

@josef-pkt

Copy link
Copy Markdown
Member Author

another check tomorrow, then merge
I guess more unit tests for one-sided alternatives and value options.

tost equivalence tests can be easily added.
confint left for another day/PR, I haven't checked yet which confint methods have an analytical form and which methods need iterative inversion of hypothesis tests

@josef-pkt
josef-pkt force-pushed the poisson_2sample_rebased branch from 4185259 to 9c70bd6 Compare May 5, 2020 13:18
@lgtm-com

lgtm-com Bot commented May 5, 2020

Copy link
Copy Markdown

This pull request introduces 4 alerts when merging 9c70bd6 into f6d0bf6 - view on LGTM.com

new alerts:

  • 4 for Variable defined multiple times

@josef-pkt

Copy link
Copy Markdown
Member Author

all green
unit test coverage is only missing some "alternative" options for etest, (plus some exceptions or single lines)

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