Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. library(ggplot2)
  2. library(pwr)
  3.  
  4. #funcs and params-----------------------------
  5. pwrf <- function(ef,pow){
  6. normal <- pwr.t.test(d = ef, type = "two.sample",
  7. alternative = "two.sided",
  8. power = pow)$n
  9. half <- pwr.t.test(d = (ef/2), type = "two.sample",
  10. alternative = "two.sided",
  11. power = pow)$n
  12. return(half/normal)
  13. }
  14. pwrfANOVA <- function(ef,pow){
  15. normal <- pwr.anova.test(f = ef, k = 4,
  16. power = pow)$n
  17. half <- pwr.anova.test(f = (ef/2), k = 4,
  18. power = pow)$n
  19. return(half/normal)
  20. }
  21.  
  22. #t.test---------------------------------------
  23. esTest <- seq(0.001,2,by = 0.001)
  24. df <- data.frame(es = NA, ratio = NA, pow = NA)
  25. for (pw in seq(0.5,0.95, by = 0.05)){
  26. p <- sapply(esTest, function(x) pwrf(x,pw))
  27. d <- data.frame(es = esTest,
  28. ratio = p,
  29. pow = rep(pw, length(p))
  30. )
  31. df <- rbind(df, d)
  32.  
  33. }
  34.  
  35. df <- df[2:dim(df)[1],]
  36. df$pow <- factor(df$pow)
  37.  
  38.  
  39. ggplot(df, aes(x = es, y = ratio, color = pow)) + geom_line() +
  40. theme_minimal() + xlab("Effect Size") + ylab("Sample Ration when es/2") +
  41. ggtitle("Sample Size Ratio for Two Sample T-Test when es is halved")
  42.  
  43. #ANOVA---------------------------------------
  44. esTest <- seq(0.001,1,by = 0.001)
  45. df <- data.frame(es = NA, ratio = NA, pow = NA)
  46. for (pw in seq(0.5,0.95, by = 0.05)){
  47. p <- sapply(esTest, function(x) pwrfANOVA(x,pw))
  48. d <- data.frame(es = esTest,
  49. ratio = p,
  50. pow = rep(pw, length(p))
  51. )
  52. df <- rbind(df, d)
  53.  
  54. }
  55.  
  56. df <- df[2:dim(df)[1],]
  57. df$pow <- factor(df$pow)
  58.  
  59.  
  60. ggplot(df, aes(x = es, y = ratio, color = pow)) + geom_line() +
  61. theme_minimal() + xlab("Effect Size") + ylab("Sample Ration when es/2") +
  62. ggtitle("Sample Size Ratio for ANOVA when es is halved")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement