```{r}
#| label: setup
#| include: false
library(tidyverse)
library(kableExtra)
library(webexercises)
library(patchwork)
library(scales)
```

## Learning Objectives

1. Understand the relationship between Type I and Type II errors
2. Define statistical power and its factors (effect size, sample size, significance level, design)
3. Calculate required sample size for common scenarios (means, proportions, survival)
4. Estimate effect size from pilot data or literature
5. Conduct power analysis for hypothesis tests and confidence interval width
6. Adjust sample size for expected dropout or non-compliance
7. Recognize consequences of inadequate power (false negatives, inflated effect estimates)
8. Interpret non-significant findings in context of post-hoc power

---

## Clinical Hook

::: {.clinical-hook}
**The Underpowered Study:** "I studied 12 patients with the new treatment; they had hemoglobin increase of 0.5 g/dL compared to 0.3 g/dL in controls. The difference was not statistically significant (p = 0.47), so the treatment doesn't work." Wait—with n = 12, the study had only 15% power to detect a clinically meaningful difference (say, 1.0 g/dL). The non-significant finding proves nothing; the study was too small. This is why power matters.
:::

---

## Type I and Type II Errors

::: {.objectives-section}
In hypothesis testing, two types of errors: Type I (α) = rejecting null when it's true (false positive), Type II (β) = failing to reject null when alternative is true (false negative). Power = 1 - β = probability of detecting a true effect. Typical convention: α = 0.05 (5% false positive rate), β = 0.20 (20% false negative rate, so power = 80%). More stringent studies use lower α or higher power.

**Visualizing errors:**
- Null is true: Type I error (reject it)
- Alternative is true: Type II error (fail to reject null)
- Trade-off: decreasing α increases β for fixed n
- Solve by increasing n
:::

---

## What is Statistical Power?

::: {.objectives-section}
Power = probability of detecting a true effect of specified size (if it exists). Example: "Our study has 80% power to detect a difference of 10 mmHg in mean blood pressure." Interpretation: if the true difference is 10 mmHg, 80% of similar studies would find it statistically significant; 20% would miss it (Type II error, false negative).

**Power depends on:**
1. Effect size (larger effect easier to detect; lower power needed)
2. Sample size (more subjects = more power)
3. Significance level α (smaller α = lower power for fixed n)
4. Study design (some designs more efficient than others)
5. One-sided vs. two-sided test (one-sided has more power)
:::

---

## Effect Size Concepts

::: {.objectives-section}
Effect size quantifies the magnitude of treatment effect or difference. Different measures for different scenarios: Cohen's d (difference in means), correlation r, odds ratio, risk difference, hazard ratio. Effect size is a property of the real world, not the sample; needed upfront to calculate required sample size.

**Choosing effect size:**
- Literature review: what differences have been shown?
- Pilot study: observed effect and variability
- Clinical judgment: what difference would matter to patients?
- Regulations: FDA may specify effect size for drug approval
- Standardized benchmarks: Cohen's d = 0.2 (small), 0.5 (medium), 0.8 (large)
:::

---

## Effect Size from Different Sources

::: {.objectives-section}
Estimate effect size by: (1) pilot studies (observational, non-randomized; limited precision), (2) published literature (but may be inflated due to publication bias), (3) clinical judgment (expert opinion on meaningful difference), (4) regulations (e.g., FDA guidance for clinical trials). Sensitivity analysis: plan sample size for range of plausible effect sizes.

**Common mistake:**
- Using effect sizes from small pilot studies (estimates are imprecise; prone to overestimation)
- Solution: inflate expected variability or use conservative effect size estimate
- Sensitivity analysis: plan n for range (e.g., small to medium effect)
:::

---

## Sample Size Calculation: Comparing Two Means

::: {.objectives-section}
Simple scenario: randomize patients to treatment vs. control, measure continuous outcome (blood pressure, hemoglobin, pain score). Formula (for equal group sizes, two-sided test):

n per group = 2 × [(z_α/2 + z_β) / (effect size)]² × (SD)²

Where z-values depend on α and power, effect size is Cohen's d, SD is standard deviation. Walk through worked example with real numbers.

**Factors affecting n:**
- Larger effect size → smaller n needed
- Larger SD → larger n needed
- Higher power (1 - β) → larger n needed
- Two-sided test → larger n needed than one-sided
:::

---

## Sample Size Calculation: Comparing Two Proportions

::: {.objectives-section}
Binary outcome (treatment success/failure, disease yes/no). Formula (equal group sizes):

n per group ≈ 2 × [(z_α/2 + z_β)]² × [p₁(1-p₁) + p₂(1-p₂)] / (p₁ - p₂)²

Where p₁ and p₂ are success probabilities in each group. Critical difference (p₁ - p₂) you want to detect. Worked example: comparing cure rates between treatments.

**Practical considerations:**
- Smaller differences require larger n
- Proportions near 0.5 require larger n than near 0 or 1
- Rule of thumb: need about 3× more subjects for proportions vs. continuous outcomes (for same effect size)
:::

---

## Sample Size for Confidence Interval Width

::: {.objectives-section}
Rather than hypothesis testing, plan sample size to achieve desired precision (margin of error). Goal: 95% CI width ± 5 percentage points for disease prevalence estimate. Formula: n = (z/ME)² × p(1-p), where z is critical value, ME is margin of error (half CI width), p is expected proportion. Simpler approach than power analysis; directly links to estimation goal.

**Estimation study planning:**
- Specify desired margin of error (ME)
- Estimate population proportion (p)
- Calculate n = (1.96 / ME)² × p(1-p)
- Example: estimate disease prevalence to within ± 3%; p = 0.15 → n ≈ 450
:::

---

## Adjustments to Sample Size

::: {.objectives-section}
Calculated n assumes perfect conditions. Adjust for: (1) expected dropout (multiply by 1/(1-dropout rate)), (2) non-compliance (reduce assumed effect size or increase n), (3) multiple comparisons (may require larger n or adjust α), (4) clustering in design (inflate for design effect / intraclass correlation).

**Common adjustments:**
- Expect 20% dropout: calculated n = 100 → recruit 125
- Cluster-randomized trial: inflate by design effect = 1 + (m - 1)ρ, where m = cluster size, ρ = ICC
- Multiple comparisons: stricter α (Bonferroni) requires larger n
- Matching: complex; roughly maintain n but improve efficiency
:::

---

## Interim Analyses and Group Sequential Designs

::: {.objectives-section}
In long trials, interim analyses check efficacy and safety. Risk: if multiple looks at data, Type I error inflates. Group sequential designs control this with pre-specified boundaries (stopping for efficacy, futility, or harm). Requires larger total sample size than fixed design. Emphasis on pre-specification: interim plans must be written before data collection.

**Interim analysis:**
- Stop early for efficacy if effect clearly present
- Stop early for futility if effect unlikely given data so far
- Stop for safety if harm detected
- Requires adjustment to maintain α level
- Must pre-specify decision rules
:::

---

## Power for Different Study Designs

::: {.objectives-section}
Sample size needs differ by design: paired designs have more power (reduced variance); cluster-randomized trials have less power (reduced effective n); survival studies need to account for event count, not just n subjects. Design influences sample size requirement; calculate n appropriate to your design.

**Design-specific calculations:**
- Paired t-test: n = 2 × [(z_α/2 + z_β) / (effect size)]² (single pair SD)
- Cluster-randomized: multiply fixed design n by design effect
- Survival studies: base on number of events, not subjects
- Cross-over designs: more efficient (fewer subjects, more observations)
:::

---

## Post-Hoc Power and Interpretation of Non-Significant Results

::: {.objectives-section}
Post-hoc power: calculating power after study completion, given observed effect size. Controversial; many statisticians caution against it (power and p-value redundant; if p > 0.05, power to detect observed effect is always < 50%). More informative: report effect size and CI (even if not significant). Wide CI suggests imprecision; narrow CI near null is more credible null finding.

**Better practice than post-hoc power:**
- Report effect size (Cohen's d, OR, HR) with 95% CI
- Interpret: is CI consistent with clinically meaningful effect?
- If CI includes both meaningful and trivial effects, uncertainty remains
- If CI excludes meaningful effects, null finding more credible
:::

---

## Planning for Precision (Confidence Interval Approach)

::: {.objectives-section}
Alternative to power analysis: plan n to achieve desired CI width. "I want to estimate treatment effect to within ± 10 percentage points." This directly addresses precision. May be more intuitive than power analysis for clinicians. Requires specifying anticipated effect size and variability.

**Precision-based planning:**
- Specify desired margin of error (CI width / 2)
- Estimate SD or proportion variability
- Calculate n: n = (z/ME)² × (variance)
- No β or "power" concept; focuses on estimation precision
:::

---

## Sensitivity Analysis and Robustness

::: {.objectives-section}
Sample size calculations depend on assumptions (effect size, SD, dropout rate). These may be uncertain. Conduct sensitivity analysis: vary key assumptions and recalculate n. "If effect size is 0.4 instead of 0.5, n = 102 instead of 64." Presents decision-makers with range of possibilities. Conservative approach: use conservative assumptions (smaller effect, larger SD, higher dropout).

**Sensitivity analysis approach:**
- Base case: best guess of parameters
- Vary effect size (optimistic to pessimistic)
- Vary SD / variability
- Vary dropout rate
- Vary other design parameters
- Present table or plot of resulting n
:::

---

## Sample Size Software and Tools

::: {.objectives-section}
Free and commercial software available: G*Power (comprehensive, free), R packages (pwr, powerMedicine), online calculators (nih.gov/stat), Stata/SAS/SPSS built-in functions. Emphasis: understand the concepts; use software for calculations. Different software may give slightly different results (different default assumptions).

**Tools to mention:**
- G*Power (free, comprehensive)
- RStudio: pwr package
- Online: OpenEpi, GraphPad StatMate
- FDA guidance and regulations often have recommended effect sizes
:::

---

## Writing the Sample Size Justification

::: {.objectives-section}
In research proposals and papers, justify sample size: (1) specify primary outcome, (2) cite effect size (pilot study, literature, clinical judgment), (3) specify α and power, (4) show calculation (formula or software output), (5) note adjustments (dropout, multiplicity, design effects). Transparency: reviewers can assess whether study is appropriately powered.

**Sample justification paragraph:**
"We aimed to detect a difference of 10 mmHg in systolic BP (effect size based on prior pilot study showing SD = 15). With α = 0.05 (two-sided) and 80% power, we calculated n = 64 per group (128 total). Assuming 15% dropout, we enrolled 75 per group (150 total). Power analysis was conducted using G*Power version X."
:::

---

## Consequences of Inadequate Power

::: {.objectives-section}
Underpowered studies risk: (1) false negatives (miss true effects), (2) inflated estimates (effect sizes in small studies tend to be larger than truth; survivor bias), (3) inconclusive results (wide CIs), (4) waste of resources (study too small to be informative). Overpowered studies: unnecessary cost and burden. Appropriate power balances Type I and Type II error and resource use.

**Impact on literature:**
- Underpowered studies often published only if significant (selection bias)
- Inflated effect sizes in small studies
- Meta-analyses may overestimate true effect
- Failure to replicate in larger studies
:::

---

## Further Learning

::: {.resources-box}

**Books:**
- Cohen J. *Statistical Power Analysis for the Behavioral Sciences* (2nd ed.) — Classic reference
- Pocock SJ. *Clinical Trials: A Practical Approach* (2nd ed.)
- Altman DG. *Practical Statistics for Medical Research* (chapters on sample size)

**Videos:**
- **StatQuest with Josh Starmer** — Power Analysis and Sample Size
- **Crash Course** — Understanding Power and Type II Errors
- **Khan Academy** — Hypothesis Testing and Type I/II Errors

**Papers:**
- Button KS, et al. "Power failure: why small sample size undermines the reliability of neuroscience." *Nat Rev Neurosci*. 2013.
- Lakens D. "Sample Size Justification." *Psych Bull*. 2022.

**Interactive Resources:**
- [G*Power](https://www.psychologie.hhu.de/arbeitsgruppen/allgemeine-psychologie-und-arbeitspsychologie/gpower) — Free power analysis software
- [rpsychologist Power Calculator](https://rpsychologist.com/d3/NHST/) — Interactive visualization
- [Sensitivity analysis tool](https://www.samplesize.net/) — Explore assumptions

:::

---

## NEET PG Practice MCQs

::: {.neet-practice}
*MCQs to be developed:*

Example question structures:
- Calculating sample size for two-group comparison (means, proportions)
- Calculating sample size for confidence interval width
- Understanding relationship between power, effect size, and sample size
- Adjusting sample size for dropout and design effects
- Estimating effect size from pilot or published data
- Interpreting non-significant findings in context of power
- Choosing α, β, and effect size for study design
- Recognizing inadequate power in published studies
- Calculating power for given sample size and effect size

*Answers with step-by-step calculations to be added.*
:::

---

## Summary

**Key Takeaways:**
- Power = probability of detecting a true effect (1 - β)
- Type I error α = false positive (5%); Type II error β = false negative (20%, so power = 80%)
- Sample size increases with: smaller effect size, larger variability, higher power, lower α, two-sided test
- Effect size must be specified a priori: from literature, pilot, or clinical judgment
- Adjust calculated n for expected dropout, design effects, multiple comparisons
- Non-significant results from small sample are inconclusive; evaluate with CI and expected power
- Underpowered studies risk false negatives and inflated effect estimates
- Precision approach: plan n to achieve desired CI width (alternative to power)
- Sensitivity analysis: vary assumptions to explore required n under different scenarios
- Post-hoc power is misleading; use observed effect size and CI instead

**Next Steps:**
- Calculate sample size for a research question using G*Power or R
- Conduct sensitivity analysis: how does n change with assumptions?
- Evaluate power in a published study; assess adequacy
- Review your own study protocol: is power justified?

---

## References

::: {#refs}
:::
