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
In the _std_2prop_power function, when calculatingstd_null, the parameter ratio (sample size ratio, nobs2 = ratio * nobs1) is not correctly passed to _std_diff_prop, Instead, default value of one is used.
This potential bug would impact both samplesize_proportions_2indep_onetail and power_proportions_2indep, when the two samples are unequally sized.
Code Sample
fromstatsmodels.statsimportproportiondiff, p2, ratio=0.1, 0.1, 2p1=p2+diffp_pooled= (p1+p2*ratio) / (1+ratio)
p1_vnull, p2_vnull=p_pooled, p_pooled# the current behaviorstd_null_statsmodels=proportion._std_diff_prop(p1_vnull, p2_vnull)
# the expected behaviorstd_null_correct=proportion._std_diff_prop(p1_vnull, p2_vnull, ratio=ratio)
# their differenceprint([std_null_statsmodels, std_null_correct])
Details
See formula (4) of this paper for technical details:
Fleiss, Joseph L., Alex Tytun, and Hans K. Ury. A simple approximation for calculating sample sizes for comparing independent proportions. Biometrics (1980): 343-346.
Basically, the current behavior incorrectly assumes ratio = 1, for the first term in the numerator.
If the issue has not been resolved, please file it in the issue tracker.
Expected Output
Change this line to std_null = _std_diff_prop(p1_vnull, p2_vnull, ratio=nobs_ratio)
std_null = _std_diff_prop(p1_vnull, p2_vnull)
I need to check the normalization/scaling here (nobs or nobs1)
The variance in both samples are the same in this null case. (but the null could have a nonzero value
In the null case, p1 = p2 = p_pooled, so I agree that p1(1 - p1) = p2(1 - p2) = p_pooled(1 - p_pooled). However, variance of the proportion estimate for sample 2 should have been be scaled by ratio = nobs2 / nobs1, which was not done.
If you check the paper I referenced (in details above), or other sample size calculators such as this one (see the r+1 term for $m^\prime$):
nonzero value is not supported,
Would be better to raise ValueError if value != 0,
or implement it? (AFAIR, other packages don't allow nonzero value. and we don't have power for tost_proportions_2indep)
value : float
currently only `value=0`, i.e. equality testing, is supported
std_null
standard error of difference under the null hypothesis (without
sqrt(nobs))
std_alt
standard error of difference under the alternative hypothesis
(without sqrt(nobs))
AFAICS, this should be sqrt(nobs1) instead of sqrt(nobs)
i.e. AFAIR everything is normalized by nobs1 which is also the target of the sample size computation
all unit tests in test_power_2indep (against R package ? and Stata use ratio=1)
related aside: #6675 original merged PR
I don't see anything about using the power_proportions_2indep function with the power classes to solve for nobs1 and similar.
Solving for nobs-ratio, requires that it is properly propagated during rootfinding.
Finding effect_size is also "non-standard". We need to make sure std_alt adjusts as we change the alt value (diff) or effect size.
It looks like they have the one-tail sample size computation with a non-zero null "value" (either diff or risk ratio)
(I didn't read their discussion for the estimator, e.g. Miettinen and Nurminen MLE under Null diff restriction.)
Farrington, Conor P., and Godfrey Manning. "Test statistics and sample size formulae for comparative binomial trials with null hypothesis of non‐zero risk difference or non‐unity relative risk." Statistics in medicine 9, no. 12 (1990): 1447-1454.
if nobs ratio = 1, then p1 and p2 can be enterchanged.
but not if nobs ratio != 1.
Stata has the reversed convention from ours (using Stata 14)
update Stata defines the nobs ratio in the same way as we do ratio = nobs2 / nobs1
However, Stata has reversed comparison compared to us
Stata: risk difference, p2-p1; synonym for diff() (p1 is control)
while we have difference between proportion 1 and 2 under the alternative H0: prop1 - prop2 - value = 0
i.e. p2 is control
Stata has an option for continuity correction, but does not allow computing sample size for fisher.
Continuity correction in chi2 test makes rejection rate closer to fisher, so we can use continuity correction to get an approximation to the sample size for fisher.
I usually don't add continuity correction to proportion tests because they become too conservative, but it would be useful as a fast approximation to fisher's test.
Activity
josef-pkt commentedon Jan 29, 2022
power_proportions_2indep doesn't propagate any keywords (although alpha and value are unused arguments in the function)
p_pooled, std_null, std_alt = _std_2prop_power(diff, prop2, ratio=1, alpha=0.05, value=0)std_null = _std_diff_prop(p1_vnull, p2_vnull)I need to check the normalization/scaling here (nobs or nobs1)
The variance in both samples are the same in this null case. (but the null could have a nonzero
valuejiannanlu commentedon Jan 29, 2022
Thanks for the quick reply.
In the null case,
p1 = p2 = p_pooled, so I agree thatp1(1 - p1) = p2(1 - p2) = p_pooled(1 - p_pooled). However, variance of the proportion estimate for sample 2 should have been be scaled byratio = nobs2 / nobs1, which was not done.If you check the paper I referenced (in details above), or other sample size calculators such as this one (see the$m^\prime$ ):
r+1term forhttps://www2.ccrb.cuhk.edu.hk/stat/proportion/Casagrande.htm
You'll see what I am talking about.
josef-pkt commentedon Jan 30, 2022
some more info:
nonzero
valueis not supported,Would be better to raise ValueError if value != 0,
or implement it? (AFAIR, other packages don't allow nonzero value. and we don't have power for tost_proportions_2indep)
AFAICS, this should be sqrt(nobs1) instead of sqrt(nobs)
i.e. AFAIR everything is normalized by
nobs1which is also the target of the sample size computationall unit tests in
test_power_2indep(against R package ? and Stata use ratio=1)josef-pkt commentedon Jan 30, 2022
summ: 2 bugs
and clarify docstring for std
@jiannanlu Thanks for finding and reporting the bug
josef-pkt commentedon Jan 30, 2022
related aside:
#6675 original merged PR
I don't see anything about using the power_proportions_2indep function with the power classes to solve for nobs1 and similar.
Solving for nobs-
ratio, requires that it is properly propagated during rootfinding.Finding effect_size is also "non-standard". We need to make sure std_alt adjusts as we change the alt value (diff) or effect size.
josef-pkt commentedon Jan 30, 2022
brief search and skimming
It looks like they have the one-tail sample size computation with a non-zero null "value" (either diff or risk ratio)
(I didn't read their discussion for the estimator, e.g. Miettinen and Nurminen MLE under Null diff restriction.)
Farrington, Conor P., and Godfrey Manning. "Test statistics and sample size formulae for comparative binomial trials with null hypothesis of non‐zero risk difference or non‐unity relative risk." Statistics in medicine 9, no. 12 (1990): 1447-1454.
see also #4828 (comment)
josef-pkt commentedon Feb 1, 2022
@bashtage This needs to go into the next bugfix release
I can prepare it later today or tomorrow
bashtage commentedon Feb 1, 2022
Please get it in. It looks like aside from a release note 0.13.2 is ready.
josef-pkt commentedon Feb 1, 2022
after fixing the above bugs
if nobs ratio = 1, then p1 and p2 can be enterchanged.
but not if nobs ratio != 1.
Stata has the reversed convention from ours (using Stata 14)
update Stata defines the nobs ratio in the same way as we do ratio = nobs2 / nobs1
However, Stata has reversed comparison compared to us
Stata:
risk difference, p2-p1; synonym for diff()(p1 is control)while we have
difference between proportion 1 and 2 under the alternativeH0: prop1 - prop2 - value = 0i.e. p2 is control
josef-pkt commentedon Feb 1, 2022
power increases if the proportion with smaller variance has more nobs (for fixed ratio)
BUG: use nobs ratio in power and samplesize proportions_2indep closes s…
josef-pkt commentedon Feb 1, 2022
bugfix in PR #8093
BUG: use nobs ratio in power and samplesize proportions_2indep closes s…
josef-pkt commentedon Feb 2, 2022
followup here:
The online calculator uses continuity correction https://www2.ccrb.cuhk.edu.hk/stat/proportion/Casagrande.htm
Stata has an option for continuity correction, but does not allow computing sample size for
fisher.Continuity correction in chi2 test makes rejection rate closer to fisher, so we can use continuity correction to get an approximation to the sample size for fisher.
I usually don't add continuity correction to proportion tests because they become too conservative, but it would be useful as a fast approximation to fisher's test.