2
$\begingroup$

I want to calculate the sample size for my AB-Test with the power.prop.test() in R. Let's say I expect an uplift in an acceptance rate from 0.33% to 0.34% and want to know how big my sample size has to be. I use the power.prop.test()

power.prop.test(n = NULL,
               p1 = 0.33, 
               p2 = 0.34, 
               sig.level = 0.05, 
               power = 0.8, 
               alternative = c("two.sided"),
               strict=T)

and get

Two-sample comparison of proportions power calculation 

          n = 34969.42
         p1 = 0.33
         p2 = 0.34
  sig.level = 0.05
      power = 0.8
alternative = two.sided

NOTE: n is number in *each* group

So I would need 34970 cases in each group. When I just use half of it - so 17485 cases - and the corresponding acceptance rates of 0.33 and 0.34 I get the following:

d1<-data.frame("acceptance"=c(17485*0.33,  17485*0.34),
               "no acceptance"=c(17485-17485*0.33,  17485-17485*0.34))
chisq.test(d1,correct=F)

which gives the following result:

Pearson's Chi-squared test with Yates' continuity correction

data:  d1
X-squared = 3.8863, df = 1, p-value = 0.04868

So it is significant, although I only took half of the sample size... What did I get wrong here? Thanks for your help!

$\endgroup$

1 Answer 1

3
$\begingroup$

You're misunderstanding what the power analysis is telling you: that when the true, unobserved, probabilities of acceptance are 0.33 & 0.34, an experiment with a sample size of 34970 in each group has an 80% chance (approximately) of resulting in a test-statistic significant at the 5% level. So the way to check this is to simulate the data-generating process—in each simulation the estimated, observed probabilities will differ somewhat from 0.33 & 0.34. Try out the following function:

calc.power <- function(p1, p2, n, alpha, no.simulns){
  p.values <- numeric(no.simulns)# make a vector of p-values
  for (i in 1:no.simulns){
    rbinom(1, n, p1) -> no.accepted.1 # simulate for group 1 by drawing from binomial distribution
    rbinom(1, n, p2) -> no.accepted.2 # simulate again for 2nd group
    d1 <- data.frame("acceptance"=c(no.accepted.1,  no.accepted.2),
                     "no acceptance"=c(n-no.accepted.1,  n - no.accepted.2))
    chisq.test(d1, correct=F)$p.value -> p.values[i] # perform Pearson's chi-squared test on simulated data
  }
  sum(p.values <= alpha)/no.simulns # calculate proportion of tests significant at specified alpha level
}
$\endgroup$
3
  • $\begingroup$ Close: (1) "80% of the tests where the test group is better by 0.34 vs 0.33", to be more precise (more than 80% of course when the test group is better still), & (2) remember you've used a two-tailed test, so it's a 5% chance of deciding for the wrong group when their performance is the same. If you calculate the power for the smaller sample size calc.power(0.34,0.33, 17485, 0.05, 1000) you should find it falls to about 50%, i.e. a 50% chance that you fail to reject the null hypothesis (of equal probabilities) when the true probabilities are 0.34 for test & 0.33 for control. $\endgroup$
    – Scortchi
    Commented Apr 19, 2017 at 12:13
  • $\begingroup$ yes, right, 50% is what I got there.. Never thought about all this, this way! ok so using the power.prop.test() for calculating the needed sample size is the right approach, right? :) $\endgroup$
    – Lena743
    Commented Apr 19, 2017 at 12:33
  • $\begingroup$ I guess I deleted my comment earlier. For those who try to follow the conversation, this was my first comment: Thanks for your fast answer and nice function! But I'm not sure if I got it.. The power.prop.test() states how many observations I need in each group to decide for the testgroup in 80% of the tests where the testgroup is better and only in 5% of the tests where the control group is better. Right? So this is my needed sample size, isn't it? Does this mean that my chisq.test() with only half of the sample size just has a higher probability to make the wrong decision? $\endgroup$
    – Lena743
    Commented Apr 19, 2017 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.