Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a python (scipy) function to determine parameters needed to obtain a target power?

Tags:

In R there is a very useful function that helps with determining parameters for a two sided t-test in order to obtain a target statistical power.

The function is called power.prop.test.

http://stat.ethz.ch/R-manual/R-patched/library/stats/html/power.prop.test.html

You can call it using:

power.prop.test(p1 = .50, p2 = .75, power = .90) 

And it will tell you n the sample size needed to obtain this power. This is extremely useful in deterring sample sizes for tests.

Is there a similar function in the scipy package?

like image 645
Matt Alcock Avatar asked Mar 04 '13 14:03

Matt Alcock


People also ask

How do you conduct a power analysis?

In order to do a power analysis, you need to specify an effect size. This is the size of the difference between your null hypothesis and the alternative hypothesis that you hope to detect. For applied and clinical biological research, there may be a very definite effect size that you want to detect.

How do you calculate effect size for power analysis?

Generally, effect size is calculated by taking the difference between the two groups (e.g., the mean of treatment group minus the mean of the control group) and dividing it by the standard deviation of one of the groups.

What is a power analysis in research?

A power analysis is the calculation used to estimate the smallest sample size needed for an experiment, given a required significance level, statistical power, and effect size. It helps to determine if a result from an experiment or survey is due to chance, or if it is genuine and significant.


2 Answers

I've managed to replicate the function using the below formula for n and the inverse survival function norm.isf from scipy.stats

enter image description here

from scipy.stats import norm, zscore  def sample_power_probtest(p1, p2, power=0.8, sig=0.05):     z = norm.isf([sig/2]) #two-sided t test     zp = -1 * norm.isf([power])      d = (p1-p2)     s =2*((p1+p2) /2)*(1-((p1+p2) /2))     n = s * ((zp + z)**2) / (d**2)     return int(round(n[0]))  def sample_power_difftest(d, s, power=0.8, sig=0.05):     z = norm.isf([sig/2])     zp = -1 * norm.isf([power])     n = s * ((zp + z)**2) / (d**2)     return int(round(n[0]))  if __name__ == '__main__':      n = sample_power_probtest(0.1, 0.11, power=0.8, sig=0.05)     print n  #14752      n = sample_power_difftest(0.1, 0.5, power=0.8, sig=0.05)     print n  #392 
like image 72
Matt Alcock Avatar answered Oct 02 '22 01:10

Matt Alcock


Some of the basic power calculations are now available in statsmodels

http://statsmodels.sourceforge.net/devel/stats.html#power-and-sample-size-calculations http://jpktd.blogspot.ca/2013/03/statistical-power-in-statsmodels.html

The blog article does not yet take the latest changes to the statsmodels code into account. Also, I haven't decided yet how many wrapper functions to provide, since many power calculations just reduce to the basic distribution.

>>> import statsmodels.stats.api as sms >>> es = sms.proportion_effectsize(0.5, 0.75) >>> sms.NormalIndPower().solve_power(es, power=0.9, alpha=0.05, ratio=1) 76.652940372066908 

In R stats

> power.prop.test(p1 = .50, p2 = .75, power = .90)       Two-sample comparison of proportions power calculation                 n = 76.7069301141077              p1 = 0.5              p2 = 0.75       sig.level = 0.05           power = 0.9     alternative = two.sided   NOTE: n is number in *each* group  

using R's pwr package

> library(pwr) > h<-ES.h(0.5,0.75) > pwr.2p.test(h=h, power=0.9, sig.level=0.05)       Difference of proportion power calculation for binomial distribution (arcsine transformation)                 h = 0.5235987755982985               n = 76.6529406106181       sig.level = 0.05           power = 0.9     alternative = two.sided   NOTE: same sample sizes  
like image 39
Josef Avatar answered Oct 02 '22 03:10

Josef