Skip to content

Bug in /statsmodels/stats/proportion.py? #8049

Closed

Description

@jiannanlu

Describe the bug

In the _std_2prop_power function, when calculating std_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

from statsmodels.stats import proportion

diff, p2, ratio = 0.1, 0.1, 2
p1 = p2 + diff
p_pooled = (p1 + p2 * ratio) / (1 + ratio)
p1_vnull, p2_vnull = p_pooled, p_pooled

# the current behavior
std_null_statsmodels = proportion._std_diff_prop(p1_vnull, p2_vnull)
# the expected behavior
std_null_correct = proportion._std_diff_prop(p1_vnull, p2_vnull, ratio = ratio)
# their difference
print([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)

Activity

  1. josef-pkt commented on Jan 29, 2022

    @josef-pkt
    Member

    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 value

  2. added this to the 0.14 milestone on Jan 29, 2022
  3. jiannanlu commented on Jan 29, 2022

    @jiannanlu
    Author

    Thanks for the quick reply.

    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$):

    https://www2.ccrb.cuhk.edu.hk/stat/proportion/Casagrande.htm

    You'll see what I am talking about.

  4. josef-pkt commented on Jan 30, 2022

    @josef-pkt
    Member

    some more info:

    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)

  5. josef-pkt commented on Jan 30, 2022

    @josef-pkt
    Member

    summ: 2 bugs

    • power_proportions_2indep does not propagate ratio to _std_2prop_power, and
    • _std_2prop_power does not use ratio in std_null as pointed out in the first message

    and clarify docstring for std

    @jiannanlu Thanks for finding and reporting the bug

  6. added
    type-bug-wrong
    and removed on Jan 30, 2022
  7. josef-pkt commented on Jan 30, 2022

    @josef-pkt
    Member

    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.

  8. josef-pkt commented on Jan 30, 2022

    @josef-pkt
    Member

    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)

  9. josef-pkt commented on Feb 1, 2022

    @josef-pkt
    Member

    @bashtage This needs to go into the next bugfix release

    I can prepare it later today or tomorrow

  10. bashtage commented on Feb 1, 2022

    @bashtage
    Member

    Please get it in. It looks like aside from a release note 0.13.2 is ready.

  11. josef-pkt commented on Feb 1, 2022

    @josef-pkt
    Member

    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 alternative
    H0: prop1 - prop2 - value = 0
    i.e. p2 is control

    proportion.power_proportions_2indep(diff, p2, 154, ratio=2, alpha=0.05)
    <class 'statsmodels.tools.testing.Holder'>
    power = 0.8260347311228263
    p_pooled = 0.13333333333333333
    std_null = 0.41633319989322654
    std_alt = 0.45276925690687087
    nobs1 = 154
    nobs2 = 308
    nobs_ratio = 2
    alpha = 0.05
    
    proportion.power_proportions_2indep(diff, p2, 154 * 2, ratio=0.5, alpha=0.05)
    <class 'statsmodels.tools.testing.Holder'>
    power = 0.7995659211532175
    p_pooled = 0.16666666666666666
    std_null = 0.6454972243679028
    std_alt = 0.5830951894845301
    nobs1 = 308
    nobs2 = 154.0
    nobs_ratio = 0.5
    alpha = 0.05
    
    # stata `power twoproportions 0.1 0.2, n1(154) nratio(2) test(chi2)`
    # r(power) =  .7995659211532177
    
    . qui power twoproportions 0.1 0.2, n1(154) n2(308) test(chi2)
    . di r(power)
    .79956592
    
    . qui power twoproportions 0.1 0.2, n2(154) n1(308) test(chi2)
    . di r(power)
    .82603473
    
    
  12. josef-pkt commented on Feb 1, 2022

    @josef-pkt
    Member

    power increases if the proportion with smaller variance has more nobs (for fixed ratio)

    0.5 * proportion.samplesize_proportions_2indep_onetail(diff, p2, 0.7995659211532175, ratio=0.5)
    154.00004060720644
    proportion.samplesize_proportions_2indep_onetail(-diff, p1, 0.7995659211532175, ratio=2)
    154.0000406072065
    
    proportion.samplesize_proportions_2indep_onetail(diff, p2, 0.7995659211532175, ratio=2)
    143.12694491581271
    0.5 * proportion.samplesize_proportions_2indep_onetail(-diff, p1, 0.7995659211532175, ratio=0.5)
    143.12694491581277
    
    (proportion.samplesize_proportions_2indep_onetail(diff, p2, 0.8, ratio=1),
     proportion.samplesize_proportions_2indep_onetail(-diff, p1, 0.8, ratio=1))
    (198.9634132843311, 198.9634132843311)
    
  13. added a commit that references this issue on Feb 1, 2022
    35225bd
  14. josef-pkt commented on Feb 1, 2022

    @josef-pkt
    Member

    bugfix in PR #8093

  15. added a commit that references this issue on Feb 1, 2022
    2602b81
  16. josef-pkt commented on Feb 2, 2022

    @josef-pkt
    Member

    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.

  17. modified the milestones: 0.14, 0.13.2 on Feb 8, 2022
  18. modified the milestones: 0.13.2, 0.14 on Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-statsprio-hightype-bug-wrong

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions