Skip to content

ENH: hypothesis tests, confint, power for rates (poisson, negbin) - #8166

Merged
josef-pkt merged 21 commits into
statsmodels:mainfrom
josef-pkt:rates_inference
Jun 20, 2022
Merged

josef-pkt merged 21 commits into
statsmodels:mainfrom
josef-pkt:rates_inference

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Mar 10, 2022 •

Copy link
Copy Markdown
Member

see #8138

collecting functions
It's not clear yet what will be in here and how it will be structured.
literature references are a bit all over the place.
It still open which options and methods will be available.

current plan

  • 1 sample poisson
    • test
    • confint
  • 2 sample poisson ratio
    • confint to match part of current ratio hypothesis test
    • power, based on PASS, Zhu and Guo (equivalence, one-sided with a margin)
  • 2 sample poisson diff,
    • confint, mainly mover
    • test ???
    • power ???, maybe only basic wald, equality hypothesis similar to power proportion 2indep
  • negbin 2indep
    • power Zhu, analogy to Poisson
    • test, confint, 1 sample ???
  • generic power based on variance functions under null and alternative

also includes

confint for poisson quantile and tolerance interval for predicting new observations. (the same can be added for binomial)

no default method for now.
I'm not sure what should be the default.

@lgtm-com

lgtm-com Bot commented Mar 10, 2022

Copy link
Copy Markdown

This pull request introduces 2 alerts when merging 50d2624 into 581119c - view on LGTM.com

new alerts:

  • 1 for Module is imported more than once
  • 1 for Unused local variable

@josef-pkt

Copy link
Copy Markdown
Member Author

unit tests fail, silly
I left a +1 in the commit that I had used to check that the unit tests run by forcing failures
assert_allclose(pv2, 1+0.05, rtol=1e-10)

@lgtm-com

lgtm-com Bot commented Mar 11, 2022

Copy link
Copy Markdown

This pull request introduces 1 alert when merging 7db7e83 into 84307f1 - view on LGTM.com

new alerts:

  • 1 for Unused local variable

@lgtm-com

lgtm-com Bot commented Mar 11, 2022

Copy link
Copy Markdown

This pull request introduces 1 alert when merging e8a1fb0 into 84307f1 - view on LGTM.com

new alerts:

  • 1 for Unused local variable

@josef-pkt

Copy link
Copy Markdown
Member Author

azure is all green except for an unrelated test failure on one machine

@lgtm-com

lgtm-com Bot commented Mar 12, 2022

Copy link
Copy Markdown

This pull request introduces 1 alert when merging 82672bb into 84307f1 - view on LGTM.com

new alerts:

  • 1 for Unused local variable

@lgtm-com

lgtm-com Bot commented Mar 12, 2022

Copy link
Copy Markdown

This pull request introduces 1 alert when merging f91176f into 84307f1 - view on LGTM.com

new alerts:

  • 1 for Unused local variable

@josef-pkt

josef-pkt commented Mar 18, 2022 •

Copy link
Copy Markdown
Member Author

commit "ENH: add confint_poisson_2indep (only wald, sqrt)" is incomplete, at wherever I stopped last week (before getting lost in readings.)

update: amended commit now "ENH: add diff compare, add confint_poisson_2indep"
includes compare="diff" to test and confint, with some initial methods
I'm still collecting methods across articles

@josef-pkt

Copy link
Copy Markdown
Member Author

R tolerance package has poistol.int, which uses 1 sample confint as in Barker 2002
https://cran.r-project.org/web/packages/tolerance/tolerance.pdf
I guess it will not give us unit tests for 1 sample confint because tolerance intervals are integers.

related to #6979

@lgtm-com

lgtm-com Bot commented Mar 26, 2022

Copy link
Copy Markdown

This pull request introduces 2 alerts when merging b4a5c53 into 408eae8 - view on LGTM.com

new alerts:

  • 1 for Unused local variable
  • 1 for Redundant assignment

@josef-pkt

josef-pkt commented Mar 29, 2022 •

Copy link
Copy Markdown
Member Author

need to clear up E-test and docs

My version is not the original Krishnamoorty and Thomson 2004 version
they use diff as comparison and use a constraint moment estimate for constraint y1 - y2 = d

However, I think equality testing value = ratio_null = 1, then it is the same as value = diff = 0 is the same for both compare, and score is equal to moment (pooled) estimate of y1, y2,
but not if y1 != y2 under the null.

(I don't know what the reference is that I actually used, I read many articles on E and EM tests)

Also, the docstring(s) should mention that etest is for rare events (i.e. small counts).
It's not an efficient implementation for very large poisson means. But in that case other approximate methods work well and there is no need for an e-test or other approximately exact test.

aside: explanation why there are several "exact" tests that don't give the same p-value
https://stats.stackexchange.com/questions/33133/differences-between-poisson-test-and-e-test-when-testing-poisson-parameters/569625#569625

update on etest

etest_poisson_2indep(count1, n1, count2, n2, method='score',
                    compare="diff", value=0)
(3.4174018389996834, 0.0005672617581627942)

etest_poisson_2indep(count1, n1, count2, n2, method='score',
                    compare="ratio", ratio_null=1)
(3.4174018390002145, 0.0005672617581627942)

etest_poisson_2indep(count1, n1, count2, n2, method='score',
                    compare="diff", value=-0.02*count1 / n1) #-0.01)
(3.539607500457262, 0.0004043240473654646)

etest_poisson_2indep(count1, n1, count2, n2, method='score',
                    compare="ratio", ratio_null=0.98)
(3.5142972483834143, 0.00039399254685208653)

and for wald

etd = etest_poisson_2indep(count1, n1, count2, n2, method='wald',
                           compare="diff", value=0)
etr = etest_poisson_2indep(count1, n1, count2, n2, method='wald',
                           compare="ratio", ratio_null=1)
etd, etr
((3.384912556260277, 0.000643144612489779),
 (3.384912556260793, 0.000643144612489779))

etd = etest_poisson_2indep(count1, n1, count2, n2, method='wald',
                           compare="diff", value=-0.02*count1 / n1)
etr = etest_poisson_2indep(count1, n1, count2, n2, method='wald',
                           compare="ratio", ratio_null=0.98)
etd, etr
((3.5136003562348463, 0.0004277279222760968),
 (3.4672482103783544, 0.000483029145414606))

@josef-pkt

Copy link
Copy Markdown
Member Author

another module location issue:
Where do we put helper function for all those inference, hypothesis test, confidence interval functions?

I will add helper functions for MOVER confidence intervals, diff, ratio and sum. Those are useful for both rates and proportions and in future likely also for other types of random variables.

basic ztest, ttest _generic helper functions are currently in weightstats

also we will have issues with circular, lazy imports,
e.g. rates needs proportions. currently lazily imported inside function
both rates and proportions import _zstat_generic2 from weightstats

@lgtm-com

lgtm-com Bot commented Mar 30, 2022

Copy link
Copy Markdown

This pull request introduces 1 alert when merging a082120 into 408eae8 - view on LGTM.com

new alerts:

  • 1 for Redundant assignment

@pep8speaks

pep8speaks commented Mar 30, 2022 •

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 2022-06-20 16:24:32 UTC

@lgtm-com

lgtm-com Bot commented Mar 30, 2022

Copy link
Copy Markdown

This pull request introduces 1 alert when merging 18ba26b into 408eae8 - view on LGTM.com

new alerts:

  • 1 for Redundant assignment

"""
n = exposure # short hand
rate = count / exposure
alpha = alpha / 2 # two-sided

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.

bad redefinition in this case
I spend a second or third time more than an hour bug hunting because I used the wrong alpha

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.

that was for _2indep

@josef-pkt

josef-pkt commented Mar 30, 2022 •

Copy link
Copy Markdown
Member Author

(this is not a small PR anymore)

needs more unit tests
mover poisson diff verifies against Li article,

still to add

  • options for poisson rates ratio conditional confint, especially exact-ci instead of minlike binom-test
  • Negbin power with same pattern as poisson, poisson still needs var_alt options

additional methods: not much anymore, I'm getting tired of finding more methods
skew correction as in ratesci is postponed

maybe:

  • invert confint to get hypothesis test (the opposite direction of computing score confint)
    this might be useful in general for MOVER confint, at least to have equivalence test-confint
  • (unlikely) make similar improvements to old proportion functions
  • weighted average or sum of poisson, standardized incidence rates ? maybe add mover confint (in 5 lines)

update

maybe others based on simple pattern

  • poisson 1 sample equivalence test, is just two onesided calls
  • more power based on generic helper functions

@josef-pkt

josef-pkt commented Mar 31, 2022 •

Copy link
Copy Markdown
Member Author

woking on power again,
I think I have partially reversed nobs1, nobs2, nobs_ratio because I followed PASS docs too closely, e.g. in power_poisson_2indep
#8206 find and stick to our convention

update
checking PASS docs again, Noninferiority tests for the ratio of two poisson rates:

rates ratio R= rate2 / rate1 -- ours is reversed
nobs_ratio = n2 / n1 -- same as ours
target nobs for power : control nobs1 -- ours is treatment nobs1

In my power function for 2indep, I mixed up the relabeling of 1 and 2, but in such a way that the solution is the same as PASS (unit tests pass), but internal names are inconsistent with a standard pattern that we want.

@josef-pkt

Copy link
Copy Markdown
Member Author

improve internal names in power functions (for helper functions)?

std_null is not actual standard deviation its ,sqrt(nobs1) * std_null, i.e. we keep nobs1 as separate term for sample size computation.

Also we don't clearly distinguish between total exposure, nobs and individual exposure
In general total exposure = nobs * exposure_obs but term "exposure" can mean either total or individual exposure. If exposure is not explicitly included then in poisson, nobs is total exposure, and rate is in terms of total exposure,

@josef-pkt

Copy link
Copy Markdown
Member Author

I had forgotten:
power_negbin_2indep already has unit tests against PASSED, (and/or MKMisc) R package
(I even had to install new R version to run those packages)

check for the above:
power for Zhu-Lakkis negbin for "alt" versus "score" has opposite ranking also in R depending on positive or negative "correlation" of rate and sample size.

So the only bug left was nobs_ratio in power_poisson_diff_2indep

almost ready to merge: check and finish api and docstrings

@lgtm-com

lgtm-com Bot commented Jun 12, 2022

Copy link
Copy Markdown

This pull request introduces 1 alert when merging 3062210 into 91423ba - view on LGTM.com

new alerts:

  • 1 for Redundant assignment

@josef-pkt

josef-pkt commented Jun 13, 2022 •

Copy link
Copy Markdown
Member Author

notes on power_poisson_diff_2indep

this uses non-pooled "alt" or "wald" under both null and alternative. v_null = v_alt
This differs from proportion where we use pooled under the null

This follows pass which only has the wald/alt version (plus a sqrt transformed version).
Stucke, Kieser (2013) has both wald/alt and score versions.

(found again while cleaning up docstring)

If I add pooled, then I could as well add full score for nonzero value. I already have _score_diff helper function to compute constrained MLE. Can I get unit tests for it ? no reference numbers available AFAICS.
???

@josef-pkt

Copy link
Copy Markdown
Member Author

another default decision

I currently have method_var="alt" as default, but that might not be good.
e.g. Stucke Kieser recommend "score" for diff and "alt" for ratio for one sided test.
"score" in ratio is conservative. The problem might be that "score" is not well centered, i.e. one-sided test is not as good as the two-sided test. (at least we get that in confidence intervals where score confint is less equal-tailed than eg. midp)

I did not see (or don't remember seeing) any recommendations in other articles to choose between method_var options.

@lgtm-com

lgtm-com Bot commented Jun 13, 2022

Copy link
Copy Markdown

This pull request introduces 2 alerts when merging 7ac3fc3 into 91423ba - view on LGTM.com

new alerts:

  • 1 for Unused local variable
  • 1 for Redundant assignment

@josef-pkt

josef-pkt commented Jun 14, 2022 •

Copy link
Copy Markdown
Member Author

decision on signature of power functions: rate1, rate2, nobs1, nobs_ratio, <keywords>

proportion should get the same prop1, prop2, nobs1, nobs_ratio, ...
Unfortunately, I already used the name power_proportions_2indep for the special case, using diff instead of prop1.
Default in proportion tests is compare='diff', which can be added to power function without breaking backwards compatibility.

another question:
Do I combine old effectsize power functions, e.g. based on proportion_effectsize (or sqrt in Poisson) with the new power functions based on proper variance function?
I don't have any plans for that yet. Hypothesis tests based on variance stabilizing transform might be only appropriate for equality testing (value=0 or value=1 for a ratio)

more design (in)consistency:
The two power_equivalence functions do not return a holder instance, return is 3 values, instead of 1 pow,
power_poisson_ratio_2indep and others return either one value pow or optionally a HolderTuple.

@lgtm-com

lgtm-com Bot commented Jun 14, 2022

Copy link
Copy Markdown

This pull request introduces 2 alerts when merging 482df93 into 96c41cd - view on LGTM.com

new alerts:

  • 1 for Unused local variable
  • 1 for Redundant assignment

Comment thread statsmodels/stats/rates.py Outdated
if return_results:
res = HolderTuple(
power=pow_,
rate_pooled=rate_pooled,

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.

rate_pooled only makes sense if value=0
now value can be also nonzero

we could use rates for "null" estimate given alternatvie, e.g. score restricted MLE.
drop for now ?

@lgtm-com

lgtm-com Bot commented Jun 15, 2022

Copy link
Copy Markdown

This pull request introduces 4 alerts when merging 432a4df into 96c41cd - view on LGTM.com

new alerts:

  • 2 for Unused local variable
  • 2 for Redundant assignment

@josef-pkt

Copy link
Copy Markdown
Member Author

I will merge when green.

basic results look fine
incomplete checking for vectorized usage, another api review before release would be good
enhancements left for later

@josef-pkt

Copy link
Copy Markdown
Member Author

no more LGTM complaints, merging

@josef-pkt
josef-pkt merged commit 34ed0cc into statsmodels:main Jun 20, 2022
@josef-pkt

Copy link
Copy Markdown
Member Author

oops,
stats.rst is outdated, it doesn't have the renamed functions,
e.g. power_poisson_2indep -> power_poisson_ratio_2indep

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