Skip to content

ENH/REF: Signature of power() between parent class Power and child class FTestPower does not fit. #8403

Open

Description

@pzdkn

Describe the bug

When using plot_power() or solve_power with a FTestPower object, one gets an error that positional argument alpha is not specified. This is because, we call the plot_power() function of parent class which, expects arguments for power() in all inheriting classes in the form of effect_size, nobs, alpha. However, inside FTestPower the signature is different.

Code Sample, a copy-pastable example if possible

test = FTestPower()
test.plot_power(dep_var='nobs',
                    nobs=np.range(100),
                    effect_size=0.8,
                    alpha=0.05,
Details

Note: As you can see, there are many issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates.

Note: Please be sure you are using the latest released version of statsmodels, or a recent build of main. If your problem has been fixed in an unreleased version, you might be able to use main until a new release occurs.

Note: If you are using a released version, have you verified that the bug exists in the main branch of this repository? It helps the limited resources if we know problems exist in the current main branch so that they do not need to check whether the code sample produces a bug in the next release.

If the issue has not been resolved, please file it in the issue tracker.

Expected Output

We would expect to see the power analysis plot.

Output of import statsmodels.api as sm; sm.show_versions()

Details

[paste the output of import statsmodels.api as sm; sm.show_versions() here below this line]

INSTALLED VERSIONS

Python: 3.9.12.final.0
OS: Darwin 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:29 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T8101 x86_64
byteorder: little
LC_ALL: None
LANG: None
statsmodels

Installed: 0.13.2 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/statsmodels)
Required Dependencies

cython: Not installed
numpy: 1.22.4 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/numpy)
scipy: 1.9.0 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/scipy)
pandas: 1.4.3 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/pandas)
dateutil: 2.8.2 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/dateutil)
patsy: 0.5.2 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/patsy)
Optional Dependencies

matplotlib: 3.5.3 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/matplotlib)
Backend MacOSX is interactive backend. Turning interactive mode on.
backend: MacOSX
cvxopt: Not installed
joblib: 1.1.0 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/joblib)
Developer Tools

IPython: 8.4.0 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/IPython)
jinja2: 3.0.3 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/jinja2)
sphinx: 3.5.4 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/sphinx)
pygments: 2.13.0 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/pygments)
pytest: 7.1.2 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/pytest)
virtualenv: 20.16.3 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/virtualenv)
INSTALLED VERSIONS

Python: 3.9.12.final.0
OS: Darwin 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:29 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T8101 x86_64
byteorder: little
LC_ALL: None
LANG: None
statsmodels

Installed: 0.13.2 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/statsmodels)
Required Dependencies

cython: Not installed
numpy: 1.22.4 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/numpy)
scipy: 1.9.0 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/scipy)
pandas: 1.4.3 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/pandas)
dateutil: 2.8.2 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/dateutil)
patsy: 0.5.2 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/patsy)
Optional Dependencies

matplotlib: 3.5.3 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/matplotlib)
backend: MacOSX
cvxopt: Not installed
joblib: 1.1.0 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/joblib)
Developer Tools

IPython: 8.4.0 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/IPython)
jinja2: 3.0.3 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/jinja2)
sphinx: 3.5.4 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/sphinx)
pygments: 2.13.0 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/pygments)
pytest: 7.1.2 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/pytest)
virtualenv: 20.16.3 (/Users/pengfei_zhao/miniconda3/envs/witte/lib/python3.9/site-packages/virtualenv)

Activity

  1. josef-pkt commented on Sep 12, 2022

    @josef-pkt
    Member

    Thanks for reporting

    the unit test for plot skips this test
    pytest.skip('skip FTestPower plot_power')

    Based on the unit tests, it looks like I was matching the R pwr package arguments for it without thinking about consistency across our power classes
    pwr.f2.test(u=5, v=19, f2=0.3**2, sig.level=0.1)

    I guess a proper solution would be to change the arguments of methods of FTestPower class which would not be backwards compatible.
    The only backwards compatible fix is to add a specific FTestPower.plot_power methods using copy-paste-adjust.
    I think changing the super class methods is not good, because it messes up the code for a temporary special case fix.

    for the background, I have not looked at ftestpower in a long time AFAIR (I only needed FTestAnovaPower in the last years):
    For which F-test are you computing the power?
    Do you know which effect size measure is used for FTestPower?

    searching around a bit shows:

    The ftestpower function uses
    nc = effect_size**2 * (df_denom + df_num + ncc)
    to compute noncentrality parameter

    related issues for power computation for Models
    #6558 for OLS
    #6759 for GLM
    #6983 for effect size measures for tests applicable to models

    6558 issue mentions:
    (still a bit annoying stats.power.FTestPower defines effect size as sqrt, i.e. effectsize = sqrt(nc / nobs)

    To fix this issue:
    FTestPower is too generic with using df_denom and df_num instead of effectsize or normed noncentrality (nc/nobs) and nobs, and we won't fit it into the standard pattern. (We need additional FTest power classes that use nc/nobs as effect size and nobs as sample size argument.)
    So, we need to add a special cased plot_power.

  2. added this to the 0.15 milestone on Feb 3, 2023
  3. josef-pkt commented on Feb 4, 2023

    @josef-pkt
    Member

    see #8646 (comment)
    for a bug in names df_num, df_denom

    Still need to fix the bug in a backwards compatible way and add class with "standard signature" with nobs as basic arg.
    Current FTestPower class has not enough information to compute nobs from df_denum.

    if effect size is defined as nc / nobs, then there is a different denominator than df_denum.
    It would be consistent if

    in ftest_power function we implicitly use nobs = (df_denom + df_num + ncc) if effect_size**2 is defined as nc / nobs
    nc = effect_size**2 * (df_denom + df_num + ncc)

    I should read up in how the get to this, and how ncc is defined in general.

  4. josef-pkt commented on Feb 4, 2023

    @josef-pkt
    Member

    NCSS PASS has interesting docs https://www.ncss.com/wp-content/themes/ncss/pdf/Procedures/PASS/Multiple_Regression_using_Effect_Size.pdf

    see the parts on conditional effect, i.e. test significant of subset of regressors (using some partial R2 measure)
    df_xxx depend on size of subsets (partition of regressors)
    from this we should get the ncc correction and nobs.
    I still need to check the details.

  5. josef-pkt commented on Feb 4, 2023

    @josef-pkt
    Member

    for rewritten new power class:

    maybe use df_num and ddf_denum as relevant arguments
    where df_denum = nobs - ddf_denum
    and then nc = effect_size * nobs = f2 * nobs

  6. josef-pkt commented on Feb 5, 2023

    @josef-pkt
    Member

    I guess I can add a new FTestRegressionPower class, that uses f^2 and nobs ddf_denom
    That should be pretty quick and can use results from current unit test, and maybe examples from PASS docs

    I think that could be used then directly for results from a regression table (anova_lm), I guess, with eta^2 instead of R2 to compute f2.

  7. josef-pkt commented on Feb 5, 2023

    @josef-pkt
    Member

    Just remembered: Stata has a new topic manual pss for power and sample size https://www.stata.com/manuals/pss.pdf with power and sample size computation based on rsquared.

    AFAIR, it was added later than the version of Stata that I have.

    arguments are more specific to the regression case

    ∗ n(numlist) sample size; required to compute power or effect size
    ∗ ntested(numlist) number of tested covariates
    ∗ ncontrol(numlist) number of control covariates; required for testing a subset of coefficients
    ∗ diff(numlist) difference between the R2 of the full and the reduced model, R2_F − R2_R; specify instead of the R2 of the full model, R2_F , when testing a subset of coefficients

    Stata also has a separate similar function power pcorr using partial correlation.

    we still need to have a generic F-test power class for generic F-tests, or/and maybe something for two-sample comparisons based on F statistic. Main issue is how to handle, nobs, df_num and df_denom for various cases. Power computation itself can be delegated to the function ftest_power (with corrected df names)
    e.g. allow subclasses of a generic F test power that just adds a method that defines the relationship between those 3, nobs, df_num and df_denom,

    This goes back to my original question after adding generic power classes.
    For which special cases do we need to add specific power classes?

    example:
    F-test for ratio of two variances, in Stata power twovariances
    we need nobs1 and ratio, nobs2 = nobs1 * ratio
    Then both df_num and df_denom depend on nobs1
    What's the appropriate effect size? nc / nobs

  8. changed the title [-]Signature of power() between parent class Power and child class FTestPower does not fit.[/-] [+]ENH/REF: Signature of power() between parent class Power and child class FTestPower does not fit.[/+] on Feb 7, 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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions