Last active
January 6, 2020 15:26
-
-
Save bgall/755b728a9d0bf429f45dd0006c28c81d to your computer and use it in GitHub Desktop.
Calculate post-hoc power of study of the effect of indoor plants on mental health
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(123) | |
############################################## | |
# Study parameters | |
############################################## | |
# Sample size | |
n <- 63 | |
# Mean of outcome | |
y_pre <- 47.9 | |
y_post <- 46.2 | |
# SD of outcome | |
sd_pre <- 10.6 | |
sd_post <- 11.1 | |
############################################## | |
# Assuming these are the TRUE values, | |
# how often should we see significant | |
# differences? | |
############################################## | |
# Number of simulations | |
sims <- 100000 | |
# Simulate data, test for differences with Welch | |
# two-sample t-test (unequal variances), return | |
# p-values. | |
results <- vector(length = sims, mode = "numeric") | |
for (i in 1:sims) { | |
results[i] <- t.test( | |
rnorm(n = n, mean = y_pre, sd = sd_pre), | |
rnorm(n = n, mean = y_post, sd = sd_post), | |
var.equal = FALSE, | |
conf.level = 0 | |
)$p.value | |
} | |
# Define function to take alpha, calculate power, | |
# and report it back | |
power_calc <- function(alpha) { | |
paste0("For alpha = ", | |
alpha, | |
" study has ", | |
prop.table(table(results < alpha))[2], | |
"% power.") | |
} | |
# Calculate power for different alphas | |
power_calc(0.01) | |
power_calc(0.022) | |
power_calc(0.05) | |
power_calc(0.10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment