Skip to content

ENH: power calculation for OLS/WLS #6558

Open

Description

@josef-pkt

(I didn't find an issue. There might be an issue or comments on power under local alternatives.)
update
#1758, #2217 issues for local power in likelihood models

I never got started with trying this out.

Here is a relatively simple starting point:

Stata power onway uses the OLS analogue to get the noncentrality parameter

reference
O’Brien, R. G., and K. E. Muller. 1993. Unified power analysis for t-tests through multivariate hypotheses. In Applied
Analysis of Variance in Behavioral Science, ed. L. K. Edwards, 297–344. New York: Dekker.
section 8.3 p. 21 looks pretty clear (brief skimming), with several examples using SAS

I found a copy of that chapter with google search.

getting the anova or t-test analogue for power computation would make an easier case to get started and compare with non-regression results.

update
basic function will be in stats.contrast, example for OLS oneway anova in unit test.
2e5f7f2

Activity

  1. josef-pkt commented on Mar 11, 2020

    @josef-pkt
    MemberAuthor

    section 8.3 can be implemented, maybe reusing existing models with constructed example data.
    (they use a factorial 2-way design as an example case, looks possible to replicate as test case)

    requires sum of squares for the contrasts to compute non-centrality parameter
    This might currently not be available in results t_test and wald_test returns.

    O'Brien and Muller use F test, but it should translate to chisquare tests, and to t- and z-tests

    F version divides by resid_variance, which is not used for chisquare.

    Maybe we don't need sum of squares of the hypothesis SSH directly, we might just need the ratio SSH / sigma^2.
    (I guess we can follow the current implementation in wald_test and find a test statistic for a standard sample size that can be used for replicated datasets, i.e. multiply or divide by nobs. Then we can compute power for different sample sizes, similar to effect size cases.)

    see also #5896 for effect sizes

    bonus question:
    Can we get a cov_type sandwich version of the noncentrality parameter or effect size?

  2. josef-pkt commented on Mar 14, 2020

    @josef-pkt
    MemberAuthor

    related #6496 for anova

    incomplete overview:

    • GPower, and R pwr used mostly Rsquared or partial Rsquared for regression
    • in individual test (t_test, oneway f-test) we get standardized effect sizes, Cohen's d and F
    • regression approaches based on contrasts or linear restriction, and in O'Brien and Muller

    I haven't found an R package that implements the last approach.
    Also, searching for power for ancova in R comes up empty
    https://stackoverflow.com/questions/27372662/ancova-simulationpower-analysis

  3. josef-pkt commented on May 27, 2020

    @josef-pkt
    MemberAuthor

    a preliminary try, I still need to get this organized
    reproduces oneway anova with 2 samples, based on #6526

    def offset_constraint(r_matrix, params_est, params_alt):
        """offset to the value of a linear constraint for new params
        
        usage:
        (cm, v) is original constraint
        
        vo = offset_constraint(cm, res2.params, params_alt)
        fs = res2.wald_test((cm, v + vo))
        nc = fs.statistic * fs.df_num
    
        """
        diff_est = r_matrix @ params_est
        diff_alt = r_matrix @ params_alt
        return diff_est - diff_alt
        
    
    def wald_test_noncent(params_alt, r_matrix, value, results):
        diff_est = r_matrix @ results.params - value
        diff_alt = r_matrix @ params_alt - value
        qf = diff_alt @ np.linalg.solve(results.cov_params(r_matrix=r_matrix), diff_alt)
        return qf
        
    def wald_test_noncent(params_alt, r_matrix, value, results, diff_alt=None):
        """noncentrality parameter for a wald test
        """
        if diff_alt is None:
            # diff_est = r_matrix @ results.params - value  # at estimated parameters
            diff_alt = r_matrix @ params_alt - value  # at parameter under alternative
        qf = diff_alt @ np.linalg.solve(results.cov_params(r_matrix=r_matrix), diff_alt)
        return qf
    

    Using element wise * instead of matrix multiplication @ in computing qr produces vectorized result corresponding to t_test instead of joint test as in wald_test
    diff_alt * np.linalg.solve(res2.cov_params(r_matrix=r_matrix), diff_alt)

    The rest is handling of nobs and df to be able to compute the power.

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

    This assumes residual variance scale is given by prototype dataset. However, because I use cov_params, the scale options in fit can adjust or fix the scale.
    Robust sandwich cov_params are used automatically, but it requires that prototype dataset is representative for estimating the robust cov, e.g. serial correlation.

    another version: compute it base on summary statistic, i.e. users or callers specify cov_params directly.

  4. josef-pkt commented on May 27, 2020

    @josef-pkt
    MemberAuthor
  5. josef-pkt commented on May 28, 2020

    @josef-pkt
    MemberAuthor

    Stata use O'Brien and Muller for contrast in oneway power analysis, and it looks similar in two-way anova
    I have Stata 14 which includes power

    example that replicates my example from notebook
    Note: Stata takes a single pooled variance e.g. I use it from OLS results.scale
    delta is the Cohen's f=sqrt(f2) effect size, equal to sqrt of my
    es_f2 = smo.effectsize_oneway(means, vars_, nobs, use_var="equal") # at estimated params

    . power oneway 1.30335762 1.20487945 1.14442353 0.5264676, varerror(0.3148019220604916) n1(10) n2(11) n3(12) n4(13)
    
    Estimated power for one-way ANOVA
    F test for group effect
    Ho: delta = 0  versus  Ha: delta != 0
    
    Study parameters:
    
            alpha =    0.0500
                N =        46
        Average N =   11.5000
               N1 =        10
               N2 =        11
               N3 =        12
               N4 =        13
            delta =    0.5594
              N_g =         4
               m1 =    1.3034
               m2 =    1.2049
               m3 =    1.1444
               m4 =    0.5265
            Var_m =    0.0985
            Var_e =    0.3148
    
    Estimated power:
    
            power =    0.8737
    
    . return list
    
    scalars:
                  r(Var_m) =  .0985015721564008
                  r(Var_e) =  .3148019220605
               r(balanced) =  0
                     r(N1) =  10
                     r(N2) =  11
                     r(N3) =  12
                     r(N4) =  13
                  r(N_avg) =  11.5
                    r(N_g) =  4
                    r(N_a) =  46
                     r(m1) =  1.30335762
                     r(m2) =  1.20487945
                     r(m3) =  1.14442353
                     r(m4) =  .5264676
                      r(N) =  46
                  r(power) =  .8736665915821515
                   r(beta) =  .1263334084178485
                  r(delta) =  .5593747949205236
               r(onesided) =  0
            r(nfractional) =  0
                  r(alpha) =  .05
    
    macros:
                   r(type) : "test"
                 r(method) : "oneway"
    
  6. josef-pkt commented on May 28, 2020

    @josef-pkt
    MemberAuthor

    solving for sample size to achieve power, unbalanced sample sizes

    . power oneway 1.30335762 1.20487945 1.14442353 0.5264676, varerror(0.3148019220604916) nfractional grweights(10 11 12 13)
    
    Performing iteration ...
    
    Estimated sample size for one-way ANOVA
    F test for group effect
    Ho: delta = 0  versus  Ha: delta != 0
    
    Study parameters:
    
            alpha =    0.0500
            power =    0.8000
            delta =    0.5594
              N_g =         4
               m1 =    1.3034
               m2 =    1.2049
               m3 =    1.1444
               m4 =    0.5265
            Var_m =    0.0985
            Var_e =    0.3148
    
    Estimated sample sizes:
    
                N =   38.9813
        Average N =    9.7453
               N1 =    8.4742
               N2 =    9.3216
               N3 =   10.1690
               N4 =   11.0165
    
    
    smpwr.FTestAnovaPower().solve_power(power=0.8, alpha=0.05, effect_size=np.sqrt(es_f2), nobs=None, k_groups=k)
    38.9809511029454
    
    # solving for df_num (
    smpwr.FTestPower().solve_power(power=0.8, effect_size=np.sqrt(es_f2), df_denom=k-1, df_num=None, alpha=0.05) + k
    38.980951252562825
    
  7. josef-pkt commented on May 28, 2020

    @josef-pkt
    MemberAuthor

    location for code: effectsize and power for model results wald tests?

    • stats.power contains mainly generic functions and classes, not application to specific models
    • stats.contrasts contains function imported into the base models, e.g. t_test_pairwise. However, it doesn't have any standalone functions that are intended for the user.
    • statsmodels.base has not inference code outside of Results classes
    • stats.stattools is a "misc" module and doesn't really fit in the topic structure anymore. (it predates most topic modules in stats)
    • new stats._power_xxx ?
    • stats.oneway that's my current PR, I use oneway case to verify OLS effect size and power, but oneway is just a special case.

    future: I should be able to get the same noncentrality and power computation for score/LM test and possibly for LR tests, so this will expand a little bit, but not enough for a separate module.
    More elaborate would be if we get full design of experiments DOE support, e.g. help in specifying a prototype dataset.

    open question: Do we get more power simulation code? I added a function mainly for internal use in stats.oneway. Those might go mostly with the corresponding inference/estimation functions or models.

    aside: still open: add effectsizes to anova_lm, and check if we can use those for power

  8. josef-pkt commented on May 29, 2020

    @josef-pkt
    MemberAuthor

    something not nice yet:
    FTestPower uses and solves for df_denom instead of nobs as sample size.
    results need to be corrected by caller nobs = df_denom + k

    (I still hate df_num, df_denom naming, I'm still mixing them up all the time.)

  9. josef-pkt commented on Jun 30, 2020

    @josef-pkt
    MemberAuthor

    A version of the above experimental functions for non-centrality from Results.wald_test have been merged with #6789 located in stats.constrasts.

    unit tests for one-way anova, no other usage of it so far.
    only f-test version, no chisquare version yet. (mainly because my analogous oneway examples are all f-test)

    unit tests for chisquare can possibly be written against R package asypow.

    > library(asypow)
    > # Three Sample Poisson Example :
    > # Three independent Poisson processes produce events at
    > # mean rates of 1, 2 and 3 per day.
    > # Find the information matrix
    > pois.mean <- c(1,2,3)
    > info.pois <- info.poisson.kgroup(pois.mean,group.size=3)
    > # Create the constraints matrix
    > constraints <- matrix(c(2,1,2,2,2,3),ncol=3,byrow=TRUE)
    > # Calculate noncentrality parameter, degrees of freedom and parameter
    > # value estimates under the null hypothesis for the test.
    
    > poisson.object <- asypow.noncent(pois.mean,info.pois,constraints)
    > poisson.object
    $w
              [,1]
    [1,] 0.3636364
    
    $df
    [1] 2
    
    $theta.ho
    [1] 1.636364 1.636364 1.636364
    
    > constraints
         [,1] [,2] [,3]
    [1,]    2    1    2
    [2,]    2    2    3
    > info.pois
              [,1]      [,2]      [,3]
    [1,] 0.3333333 0.0000000 0.0000000
    [2,] 0.0000000 0.1666667 0.0000000
    [3,] 0.0000000 0.0000000 0.1111111
    
    > asypow.power(poisson.object, 47, 0.05)
    [1] 0.9674541
    
  10. josef-pkt commented on Nov 13, 2024

    @josef-pkt
    MemberAuthor

    https://gist.github.com/josef-pkt/efc5e8bfd6136a2f5e72eba5f8818f8c

    notebook for power in OLS
    oneway case, compared to stats.oneway

    one messy part
    FTestAnovaPower solves for nobs
    FTestPowerF2 solves for df_denom (but I called it nobs)
    FTestPowerF2 doc string has example with nobs = df2 + df1 + ncc, but that has to be done by user.
    (wald_test return includes df_denom, but not how it is related to nobs)
    How does TTestPower handle df_xxx?

    normalizations of noncentrality and effect sizes are still confusing.
    I did not use helper functions like oneway._fstat2effectsize.

    We also need notebook that include chisquare based wald test.
    I have draft version for generic Chisquare power class in another notebook.
    chisquare will not have df_denom versus nobs problem.

    Also no example for power computation for t_test in OLS yet.
    Although, it should work with existing code, except it uses Cohen's d instead of normalized noncentrality, i.e. population standard deviation instead of the standard deviation used in the test statistic.

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions