Skip to content

Instantly share code, notes, and snippets.

@martinctc
Created January 9, 2023 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinctc/06c324d7dabfed1c029a9251d803052b to your computer and use it in GitHub Desktop.
Save martinctc/06c324d7dabfed1c029a9251d803052b to your computer and use it in GitHub Desktop.
[Power analysis and sample size estimation with R] #R
# See <https://rpubs.com/mbounthavong/sample_size_power_analysis_R>
library(pwr)
# Sample size estimations for two proportions
# `pwr::ES.h()` computes effect size for two proportions
# n provides required sample size
p0 <- pwr.2p.test(h = ES.h(p1 = 0.60, p2 = 0.50), sig.level = 0.05, power = .80)
plot(p0)
# Power analysis for two proportions
p1 <- seq(0.5, 1.0, 0.05) # proportion of responders
power1 <-pwr.2p.test(h = ES.h(p1 = p1, p2 = 0.50),
n = 388,
sig.level = 0.05)
powerchange <- data.frame(p1, power = power1$power * 100)
plot(powerchange$p1,
powerchange$power,
type = "b",
xlab = "Proportion of Responders in Treatment A",
ylab = "Power (%)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment