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
(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
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.)
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.
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
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
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.)
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
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.
Activity
josef-pkt commentedon Mar 11, 2020
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_testandwald_testreturns.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
SSHdirectly, we might just need the ratioSSH / 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?
josef-pkt commentedon Mar 14, 2020
related #6496 for anova
incomplete overview:
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
josef-pkt commentedon May 27, 2020
a preliminary try, I still need to get this organized
reproduces oneway anova with 2 samples, based on #6526
Using element wise
*instead of matrix multiplication@in computing qr produces vectorized result corresponding tot_testinstead of joint test as inwald_testdiff_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.FTestPowerdefines effect size as sqrt, i.e.effectsize = sqrt(nc / nobs)This assumes residual variance
scaleis 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.
josef-pkt commentedon May 27, 2020
first version how to compute power anova versus OLS wald_test:
https://gist.github.com/josef-pkt/28ce6104d1d70aa010431f8d55a07abc
josef-pkt commentedon May 28, 2020
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
deltais the Cohen's f=sqrt(f2) effect size, equal to sqrt of myes_f2 = smo.effectsize_oneway(means, vars_, nobs, use_var="equal") # at estimated paramsjosef-pkt commentedon May 28, 2020
solving for sample size to achieve power, unbalanced sample sizes
josef-pkt commentedon May 28, 2020
location for code: effectsize and power for model results wald tests?
stats.powercontains mainly generic functions and classes, not application to specific modelsstats.contrastscontains 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.basehas not inference code outside of Results classesstats.stattoolsis a "misc" module and doesn't really fit in the topic structure anymore. (it predates most topic modules in stats)stats._power_xxx?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 powerjosef-pkt commentedon May 29, 2020
something not nice yet:
FTestPoweruses 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.)
josef-pkt commentedon Jun 30, 2020
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.
josef-pkt commentedon May 8, 2021
GPower example for rsquared in OLS
https://stackoverflow.com/questions/67433118/communicate-with-gpower-from-python
josef-pkt commentedon Nov 13, 2024
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.