3

I want to estimate the needed sample size to compute a Chi Squared (Test for homogenity) test for discrete data using Python and need a hint how to do it.

In general I want to estimate if the failure rates of two production processes differ significantly (alpha = 5%) or not.

I have only found the statsmodels.stats.gof.chisquare_effectsize() function but this seems to work only for a goodness of fit test.

Is there any way how I can determine the needed sample size?

I appreciate every answer.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
2Obe
  • 3,570
  • 6
  • 30
  • 54

1 Answers1

4

You can use statsmodels.stats.GofChisquarePower().solve_power() However, you need to adjust the degrees of freedom (df) to account for the number of variables. You can accomplish this with the n_bins parameter.

>>>import statsmodels.stats.power as smp
>>>n_levels_variable_a = 2
>>>n_levels_variable_b = 3
>>>smp.GofChisquarePower().solve_power(0.346, power=.8, n_bins=(n_levels_variable_a-1)*(n_levels_variable_b-1), alpha=0.05)

115.94688728433769

BirdLaw
  • 572
  • 6
  • 16
  • Do I get it right that: 1. The n_bins is the number of groups I want to compare? In my case n_bins=2 because I have two production lines. 2. The effect size describes the difference between the means of the two production lines. Therefore it is kind of a confidence intervall and the lower this value is set, the higher the sample size will be? – 2Obe Aug 31 '17 at 06:31
  • @2Obe My mistake, I see that I misread your post, and thought you were seeking the test for _independence_. – BirdLaw Aug 31 '17 at 16:37
  • For homogeneity, the math is the same, but degrees of freedom (in this case n_bins) should be (number_of_rows -1)*(number_of_columns-1) where number_of_rows (and columns) are the number of rows and columns in the contingency table. (e.g. if you have two variables, 'gender' and 'political affiliation') you could have (2-1)*(2-1) – BirdLaw Aug 31 '17 at 16:45
  • 2. The effect size is the size of the effect for which you seek a given power level. For instance, you can achieve sufficient power to detect an effect of a certain size with sample size=n. So if you need to reliably (80% of the time) detect an effect of a certain size, while maintaining an acceptable false positive rate (alpha, roughly), it will give you the required sample size – BirdLaw Aug 31 '17 at 16:52