Power Curves for Test of Association

Keren Xu

2019/10/12

A two by two table

Power and Sample size for test of association

When n1 = n2 and \(\pi_1 > \pi_2\), Power = \(P(z > \frac{z_{1-\alpha/2}\sqrt{2\bar{p}(1-\bar{p})}-\sqrt{n}(\pi_1 - \pi_2)}{\sqrt{\pi_1(1-\pi_1)+\pi_2(1-\pi_2)}} | H_1 true)\)

pi2 <- c(0.01, seq(from=0.05, to=0.50, by=0.001))

RR <- seq(from=1.0, to=3.0, by=0.5)

computePower <- function(RR, pi2, alpha, n) {
  pi1 = RR*pi2
  Pe = pi2
  p_hat <- (pi1 + pi2)/2 
  power <- 1 - pnorm(
      (qnorm(1-alpha/2)*(2*p_hat*(1-p_hat))^(1/2) - n^(1/2)*(pi1 - pi2))/(pi1*(1-pi1) + pi2*(1-pi2))^(1/2)
    )
  
  df <- data.frame(Pe, RR, power)
  return(df) 
}

mylist <- sapply(RR,computePower, pi2, alpha = 0.05, n = 100, simplify=FALSE)
mydataframe <- do.call(rbind.data.frame, mylist)


library(ggplot2)
ggplot(mydataframe, aes(x = Pe, y = power, color=as.factor(RR))) + 
  geom_line(size=1.5)+scale_y_continuous(limits = c(0.7,1))+ scale_x_continuous(limits = c(0,0.5)) + theme_classic()