Skip to content

BUG/ENH: proportion_confint: impose bounds, support asarray and pandas - #4410

Merged
josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:bug_prop_confint_bounds_2742
Mar 30, 2018
Merged

josef-pkt merged 4 commits into
statsmodels:masterfrom
josef-pkt:bug_prop_confint_bounds_2742

Conversation

@josef-pkt

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

Copy link
Copy Markdown
Member

closes #2742, see discussion there

also closes #2609

fixes nan and out of bounds issue
adds asarray, but returns pandas if pandas was used

no additional unit tests yet

@coveralls

coveralls commented Mar 29, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.009%) to 82.682% when pulling 43cbd7e on josef-pkt:bug_prop_confint_bounds_2742 into 935ceaa on statsmodels:master.

@codecov-io

codecov-io commented Mar 29, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #4410 into master will increase coverage by <.01%.
The diff coverage is 91.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4410      +/-   ##
==========================================
+ Coverage   80.03%   80.04%   +<.01%     
==========================================
  Files         560      560              
  Lines       84277    84319      +42     
  Branches     9545     9554       +9     
==========================================
+ Hits        67452    67490      +38     
- Misses      14602    14603       +1     
- Partials     2223     2226       +3
Impacted Files Coverage Δ
statsmodels/stats/tests/test_proportion.py 99.3% <100%> (+0.05%) ⬆️
statsmodels/stats/proportion.py 86.09% <80.95%> (-0.45%) ⬇️
statsmodels/stats/descriptivestats.py 24.13% <0%> (ø) ⬆️

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 935ceaa...33016f2. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member Author

There is a disagreement for jeffreys for proportions close to or at the boundary between statsmodels and R binom.
I'm skipping those cases currently, (in the next commit)

E               AssertionError:
E               Arrays are not almost equal to 6 decimals
E               (1, 30)jeffreys
E               (mismatch 100.0%)
E                x: array([ 0.00362 ,  0.145418])
E                y: array([  6.903016e-05,   1.231438e-01])

and

E               AssertionError:
E               Arrays are not almost equal to 6 decimals
E               (30, 30)jeffrey
E               (mismatch 100.0%)
E                x: array([ 0.920322,  0.999984])
E                y: array([ 0.938483,  1.      ])

same at 0/30 and 1/30

I don't know what R binom is doing. We just use a beta distribution based on a prior that corresponds to a (0.5, 0.5) presample observation.

@josef-pkt
josef-pkt force-pushed the bug_prop_confint_bounds_2742 branch from fb56dfd to 9cc4146 Compare March 29, 2018 23:53
@josef-pkt

josef-pkt commented Mar 29, 2018 •

Copy link
Copy Markdown
Member Author

(silly indexing mistake, and I was running the wrong tests function locally,
canceled builds, amended and force pushed)

@josef-pkt

josef-pkt commented Mar 30, 2018 •

Copy link
Copy Markdown
Member Author

ok about jeffreys/bayes

R binom binom.confint uses a highest density interval for bayes by default, while I use 'central'

> binom.bayes(x = 0, n = 30, tol = 1e-8, type ="central")
  method x  n shape1 shape2       mean lower      upper  sig
1  bayes 0 30    0.5   30.5 0.01612903     0 0.06151672 0.05

> binom.bayes(x = 1, n = 30, tol = 1e-8, type ="central")
  method x  n shape1 shape2      mean       lower     upper  sig
1  bayes 1 30    1.5   29.5 0.0483871 0.003620464 0.1454178 0.05

compared to

>>> smprop.proportion_confint(np.asarray([0, 1, 29, 30]), 30, alpha=0.05, method='jeffr')
(array([  1.62318612e-05,   3.62046437e-03,   8.54582223e-01,  9.20321826e-01]), 
array([ 0.07967817,  0.14541778,  0.99637954,  0.99998377]))

There is still a discrepancy at 0/30, where R binom seems to shift the weight to the other tails

>>> c, n = 0, 30; stats.beta.ppf(0.95, c + 0.5, n - c + 0.5)
0.061516724557300909

I ignore this for now, maybe mention it in docstring.


I just found a bug in pandas handling while trying these example. list has an index method, so we cannot check hasattr or getattr index to identify dataframe_likes, maybe also check that index is not callable? callable(li.index)

>>> dir(li)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

no unit tests for using lists, so this broke.

@josef-pkt

Copy link
Copy Markdown
Member Author

all green

docstring needs some work and updating, otherwise ready to merge

@josef-pkt

Copy link
Copy Markdown
Member Author

What I haven't done here is to replace the binom.test reference numbers by those from binom.bayes with centered confint type.

R's binom has a few more confidence intervals, but we should be able to replicate those with GLM by choosing the corresponding link function. Likelihood Profile confidence intervals are not yet merged, AFAIR.
What we don't have is GLM confidence intervals with observation adjustment, e.g. Firth's.

Nevertheless, I want to stop for this round and merge when green. (and open a new issue for another round)

@josef-pkt

Copy link
Copy Markdown
Member Author

I made a mistake in the docstring, maybe.

binom.bayes corresponds to jeffreys in which binom.test uses highest density, and adjusts at boundary if centered. I added it in the description of Clopper-pearson exact ci.
see #2624 for midp exact.

Both jeffreys and beta are centered and don't shift probability at the boundary from one tail to the other.
So the comments in the docstring Notes apply to both.

@josef-pkt

Copy link
Copy Markdown
Member Author

all green except for a test_simulate hang on one of the appveyor's

(I will force push an amended commit to adjust the docstring, then merge right away. No need to run ci testing again on PR.)

@josef-pkt
josef-pkt force-pushed the bug_prop_confint_bounds_2742 branch from 43cbd7e to c16ef02 Compare March 30, 2018 14:37
@josef-pkt
josef-pkt merged commit 5a01211 into statsmodels:master Mar 30, 2018
@josef-pkt
josef-pkt deleted the bug_prop_confint_bounds_2742 branch April 18, 2018 14:12
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.

BUG: proportion confint returns nan, negative and larger than 1 BUG corner case proportion_confint at zero and one

3 participants