Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement