## Sample Size Calcuations for a case control study with the following inputs

### Factors Affecting Power

#### Based on these elements, we can come up with a mathematical equation, which relates all the above elements. We will use difference in proportions as the effect size in the formula, which can be found at the url

ZBeta<-0.84 # Assuming 80% power
ZAlphaHalf<-1.96 #Assuming Signifcance Level Alpha =0.05
r<-1 # Assuming equal number of cases and controls ( r is the ratio of control to cases)
pExposedControl<-.20 # 20% exposed in control group, say
OR<-2.0 # you want ot detect an odds ratio of 2.0 or more
# To get proportion of cases exposed
pExposedCase<-(OR*pExposedControl)/((pExposedControl)*(OR-1)+1)
pbar<-(pExposedControl+pExposedCase)/2 # Average proportion exposed
n<-((r+1)/r)*((pbar*(1-pbar))*((ZBeta+ZAlphaHalf)^2))/((pExposedCase-pExposedControl)^2)
n
## [1] 172.48
# therefore total sample size is 2*n, n cases and n control
N<-round (2*n)
N
## [1] 345

# for different prevlance rates

ZBeta<-0.84 # Assuming 80% power
ZAlphaHalf<-1.96 #Assuming Signifcance Level Alpha =0.05
r<-1 # Assuming equal number of cases and controls ( r is the ratio of control to cases)
pExposedControl<-c(.20, .30,0.50,0.60,0.70) # 20% exposed in control group, say
OR<-2.0 # you want ot detect an odds ratio of 2.0 or more
# To get proportion of cases exposed
pExposedCase<-(OR*pExposedControl)/((pExposedControl)*(OR-1)+1)
pbar<-(pExposedControl+pExposedCase)/2 # Average proportion exposed
n<-((r+1)/r)*((pbar*(1-pbar))*((ZBeta+ZAlphaHalf)^2))/((pExposedCase-pExposedControl)^2)

EffectSize<-pExposedCase-pExposedControl
round(n)
## [1] 172 142 137 153 186
# therefore total sample size is N= 2*n, n cases and n control
N<-round (2*n)
N
## [1] 345 283 274 306 373
SampleSizes<-rbind(round(pExposedControl,2),round(pExposedCase,2),round(EffectSize,2),N)
library(knitr)
library("kableExtra")
kable(SampleSizes, caption="Sample Sizes for Various Scenarios")
 0.2 0.3 0.5 0.6 0.7 0.33 0.46 0.67 0.75 0.82 0.13 0.16 0.17 0.15 0.12 N 345 283 274 306 373