Skip to content

Instantly share code, notes, and snippets.

@chelseaparlett
Created August 5, 2019 17:17
Show Gist options
  • Save chelseaparlett/f2f157c43870ea8dda5504b78d7a2f97 to your computer and use it in GitHub Desktop.
Save chelseaparlett/f2f157c43870ea8dda5504b78d7a2f97 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(pwr)
#funcs and params-----------------------------
pwrf <- function(ef,pow){
normal <- pwr.t.test(d = ef, type = "two.sample",
alternative = "two.sided",
power = pow)$n
half <- pwr.t.test(d = (ef/2), type = "two.sample",
alternative = "two.sided",
power = pow)$n
return(half/normal)
}
pwrfANOVA <- function(ef,pow){
normal <- pwr.anova.test(f = ef, k = 4,
power = pow)$n
half <- pwr.anova.test(f = (ef/2), k = 4,
power = pow)$n
return(half/normal)
}
#t.test---------------------------------------
esTest <- seq(0.001,2,by = 0.001)
df <- data.frame(es = NA, ratio = NA, pow = NA)
for (pw in seq(0.5,0.95, by = 0.05)){
p <- sapply(esTest, function(x) pwrf(x,pw))
d <- data.frame(es = esTest,
ratio = p,
pow = rep(pw, length(p))
)
df <- rbind(df, d)
}
df <- df[2:dim(df)[1],]
df$pow <- factor(df$pow)
ggplot(df, aes(x = es, y = ratio, color = pow)) + geom_line() +
theme_minimal() + xlab("Effect Size") + ylab("Sample Ration when es/2") +
ggtitle("Sample Size Ratio for Two Sample T-Test when es is halved")
#ANOVA---------------------------------------
esTest <- seq(0.001,1,by = 0.001)
df <- data.frame(es = NA, ratio = NA, pow = NA)
for (pw in seq(0.5,0.95, by = 0.05)){
p <- sapply(esTest, function(x) pwrfANOVA(x,pw))
d <- data.frame(es = esTest,
ratio = p,
pow = rep(pw, length(p))
)
df <- rbind(df, d)
}
df <- df[2:dim(df)[1],]
df$pow <- factor(df$pow)
ggplot(df, aes(x = es, y = ratio, color = pow)) + geom_line() +
theme_minimal() + xlab("Effect Size") + ylab("Sample Ration when es/2") +
ggtitle("Sample Size Ratio for ANOVA when es is halved")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment