# Sample Size Calculation with R

Background Story:

One day, my boss asked me to check if the data has a certain number of events to perform an efficacy analysis. I was curious how did he come up with the number, later I know he must have done the Sample Size Calculation. Today we will go over the basics and R applications for sample size calculation. #### Five components of Sample Size:

Sample Size: N

Type I error rate: α- level (2-sided 0.05)

Mean under the null and alternative: μ0 and μa

Variance: σ²

Power: 1-β We can use any four of these five factors to calculate the fifth one.

#### Two methods to calculate the sample size:

1. Hypothesis testing: a specific null and alternative hypothesis
2. Confidence interval: an estimated interest

#### Hypothesis testing approach:

1. State the null and the alternative hypothesis
2. Specify standard deviation
3. Decide the power and alpha level
Power=0.8, Alpha=0.05 for two-sided test
4. State the test
5. R/ SAS program

```1. H0: mean=80, Ha: mean1= 70
2. standard deviation =20, d=(80-70)/20
3. Power=0.8 , Alpha=0.05 for two-sided test

pwr.t.test(d =0.5 , sig.level =0.05, power =0.8 , type = "one.sample", alternative="two.sided")

#General formula:
pwr.t.test(n=, d = , sig.level =, power =, type = ("one.sample","two.sample", "piared"), alternative=("two.sided", "less", "great"))```
```    One-sample t test power calculation

n = 33.36713
d = 0.5
sig.level = 0.05
power = 0.8
alternative = two.sided
```

• Two Sample T-test
• Comparison of proportions

`pwr.t.test(d=0.7882 , sig.level =0.05, power =0.8 , type = "two.sample", alternative="two.sided")`

```     Two-sample t test power calculation

n = 26.26343
d = 0.7882
sig.level = 0.05
power = 0.8
alternative = two.sided

NOTE: n is number in *each* group```

#### Summary of Sample Size variations: 1. Variance σ² increase, the sample size N increase
2. Difference between groups increases (μ1-μ2), sample size N decrease
3. Type I error rate (α) increase, sample size N decrease
4. Power (1-ß) increases sample size N increase.

Thanks 77 for sharing the Havard Catalyst class material!

Happy Studying!