Skip to content

Instantly share code, notes, and snippets.

@turgeonmaxime
Last active June 20, 2024 13:59
Show Gist options
  • Save turgeonmaxime/76f13fea43c3f0e0da0124fc0581b537 to your computer and use it in GitHub Desktop.
Save turgeonmaxime/76f13fea43c3f0e0da0124fc0581b537 to your computer and use it in GitHub Desktop.
Post-hoc power analysis
mu <- 0.1
sigma <- 1
nsamp <- 36
B <- 1000
real_power <- power.t.test(delta = mu, sd = sigma, n = nsamp,
alternative = "one.sided",
type = "one.sample")$power
res <- replicate(B, {
norm_samp <- rnorm(nsamp, mean = mu, sd = sigma)
mu_hat <- mean(norm_samp)
sigma_hat <- sd(norm_samp)
power.t.test(delta = mu_hat, sd = sigma_hat, n = nsamp,
alternative = "one.sided",
type = "one.sample")$power
})
hist(res, 50, main = 'Post-hoc power estimates', xlab = 'Power')
abline(v = real_power, lty = 2)
@turgeonmaxime
Copy link
Author

posthoc_power_analysis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment