You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
proportions_chisquare produces erroneous p-value with large sample size #7603
Function Path: from statsmodels.stats.proportion.proportions_chisquare
With large sample sizes, proportions_chisquare produces a p-value of 1, even though the difference in proportions is statistically significantly large, that should yield a p-value close to 0 (which is what we get when we use the scipy.stats.chi2_contingency function with the same input parameters). We present an example of this below.
Say we have 2 groups "a" and "b", with the following trials and successes.
nsucc_groupa = 640
ntrials_groupa = 679
nsucc_groupb = 100
ntrials_groupb = 53000
the BUG is that proportions_chisquare is missing asarray, asarray defaults to float64 casting.
count = np.asarray(count)
nobs = np.asarray(nobs)
However, if the numpy array has dtype with int dtype, then asarray doesn't convert.
In that case we would still need to upcast to at least float, or better float64
@ronitshaw Thanks for reporting. I never thought of checking examples like this.
We are not upcasting by default, because there are usecases where users want lower precision to save memory.
But in these cases, we still need to make sure computations are at full precision, and memory should not be an issue when we use summary statistics like counts.
I haven't been closely following numpy developement in a while.
Over the longer term, casting rules change, and code that initially worked, might stop working correctly.
As defensive programming, we should/could make more explicit upcasting, instead of relying on implicit upcasting.
e.g. use asarray with dtype= at least float64
(we might want to allow complex numbers in some cases by default)
Activity
josef-pkt commentedon Jul 23, 2021
BUG counts or nobs needs to be float, then pvalue is 0
problem is in the helper function, after that it just delegates to scipy stats
using
count, nobs = np.asarray([nsucc_groupa,nsucc_groupb]), np.asarray([ntrials_groupa,ntrials_groupb])josef-pkt commentedon Jul 23, 2021
I guess it's an int overflow, that causes a negative integer. and numpy is not upcasting by default.
using int64 works for the example
josef-pkt commentedon Jul 23, 2021
the BUG is that
proportions_chisquareis missing asarray,asarraydefaults to float64 casting.However, if the numpy array has dtype with int dtype, then asarray doesn't convert.
In that case we would still need to upcast to at least float, or better float64
josef-pkt commentedon Jul 23, 2021
something is still weird
atleast_1d(nobs) is used in proportions_chisquare and it upcasts in my notebook
but not for counts
This problem with
intdtype might affect other functions for count models, when there are no computations early on that upcast the data.josef-pkt commentedon Jul 23, 2021
for whatever reason atleast_1d doesn't upcast
In the actual computation, conversion to float comes too late, e.g. reversing sequence works to force computation to be in float.
edit
sumcould also overflow if table is small int type, so that wouldn't help in those cases.We need a float64 table.
josef-pkt commentedon Jul 23, 2021
aside: since division between ints produces a float, we stopped using
1.*as quick casting trick.Obviously, we still need it for upcasting small integers.
josef-pkt commentedon Jul 23, 2021
@ronitshaw Thanks for reporting. I never thought of checking examples like this.
We are not upcasting by default, because there are usecases where users want lower precision to save memory.
But in these cases, we still need to make sure computations are at full precision, and memory should not be an issue when we use summary statistics like counts.
josef-pkt commentedon Jul 23, 2021
another thought
I haven't been closely following numpy developement in a while.
Over the longer term, casting rules change, and code that initially worked, might stop working correctly.
As defensive programming, we should/could make more explicit upcasting, instead of relying on implicit upcasting.
e.g. use asarray with dtype=
at least float64(we might want to allow complex numbers in some cases by default)
e.g. sum has a dtype option for the accumulator
at least dtype can be implemented with np.promote_types
BUG: proportions_chisquare prevent integer overflow closes statsmodel…
josef-pkt commentedon Aug 24, 2021
bug fix in #7669