pingouin.power_ttest2n

pingouin.power_ttest2n(nx, ny, d=None, power=None, alpha=0.05, tail='two-sided')[source]

Evaluate power, effect size or significance level of an independent two-samples T-test with unequal sample sizes.

Parameters
nx, nyint

Sample sizes. Must be specified. If the sample sizes are equal, you should use the power_ttest() function instead.

dfloat

Cohen d effect size

powerfloat

Test power (= 1 - type II error).

alphafloat

Significance level (type I error probability). The default is 0.05.

tailstr

Indicates whether the test is “two-sided” or “one-sided”.

Notes

Exactly ONE of the parameters d, power and alpha must be passed as None, and that parameter is determined from the others.

Notice that alpha has a default value of 0.05 so None must be explicitly passed if you want to compute it.

This function is a mere Python translation of the original pwr.t2n.test function implemented in the pwr package. All credit goes to the author, Stephane Champely.

Statistical power is the likelihood that a study will detect an effect when there is an effect there to be detected. A high statistical power means that there is a low probability of concluding that there is no effect when there is one. Statistical power is mainly affected by the effect size and the sample size.

The first step is to use the Cohen’s d to calculate the non-centrality parameter \(\delta\) and degrees of freedom \(v\). In case of two independent groups with unequal sample sizes, this is:

\[\delta = d * \sqrt{\frac{n_i * n_j}{n_i + n_j}}\]
\[v = n_i + n_j - 2\]

where \(d\) is the Cohen d, \(n\) the sample size, \(n_i\) the sample size of the first group and \(n_j\) the sample size of the second group,

The critical value is then found using the percent point function of the T distribution with \(q = 1 - alpha\) and \(v\) degrees of freedom.

Finally, the power of the test is given by the survival function of the non-central distribution using the previously calculated critical value, degrees of freedom and non-centrality parameter.

scipy.optimize.brenth() is used to solve power equations for other variables (i.e. sample size, effect size, or significance level). If the solving fails, a nan value is returned.

Results have been tested against GPower and the R pwr package.

Examples

  1. Compute achieved power of a T-test given d, n and alpha

>>> from pingouin import power_ttest2n
>>> print('power: %.4f' % power_ttest2n(nx=20, ny=15, d=0.5,
...                                     tail='one-sided'))
power: 0.4164
  1. Compute achieved d given n, power and alpha level

>>> print('d: %.4f' % power_ttest2n(nx=20, ny=15, power=0.80, alpha=0.05))
d: 0.9859
  1. Compute achieved alpha level given``d``, n and power

>>> print('alpha: %.4f' % power_ttest2n(nx=20, ny=15, d=0.5,
...                                     power=0.80, alpha=None))
alpha: 0.5366