Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #========================================================#
  2. # Setup
  3. #========================================================#
  4.  
  5. library(dplyr)
  6. library(ggplot2)
  7. library(here)
  8. library(pwr)
  9. library(scales)
  10. library(stringr)
  11.  
  12. #========================================================#
  13. # Functions
  14. #========================================================#
  15.  
  16. #============================
  17. # power_prop_test()
  18. #============================
  19. # a simplification of one of
  20. # the function from pwr
  21. # for reuse later
  22. power_prop_test <- function(h, n_group) {
  23. pwr.2p.test(
  24. h = h,
  25. n = n_group,
  26. sig.level = 0.01,
  27. alternative = 'two.sided'
  28. )$power
  29. }
  30.  
  31. #============================
  32. # power_prop_test_grid()
  33. #============================
  34. # function to calculate power for
  35. # prop.test from:
  36. # n_group: sample size in each group
  37. # prop_control: proportion in the control group
  38. # prop_effect: effect in the treatment group
  39. power_prop_test_grid <- function(n_group, prop_control, prop_effect) {
  40. # get all possible combinations of
  41. # n_group, prop_control, and prop_effect
  42. expand.grid(list(
  43. n_group = n_group,
  44. prop_control = prop_control,
  45. prop_effect = prop_effect
  46. )) %>%
  47. mutate(
  48. prop_treatment = prop_control + prop_effect,
  49. # standardised effect size (see pwr docs)
  50. h = pwr::ES.h(prop_control, prop_treatment),
  51. # calculate the power
  52. power = power_prop_test(h, n_group)
  53. )
  54. }
  55.  
  56. #========================================================#
  57. # Power curve
  58. #========================================================#
  59.  
  60. #============================
  61. # power curve data
  62. #============================
  63.  
  64. # proportions to try for the control group
  65. prop_control <- seq(0.05, 0.5, 0.025)
  66.  
  67. # effect sizes to try
  68. prop_effect <- seq(0, 0.15, by = 0.001)
  69.  
  70. # samples sizes for each group to try
  71. n_group <- c(500, 1000, 2500, 5000)
  72.  
  73. # get the power for all combinations
  74. df_prop_test_power <- power_prop_test_grid(n_group, prop_control, prop_effect)
  75.  
  76. #============================
  77. # power curve plot
  78. #============================
  79.  
  80. # create a plot theme
  81. theme_datasci <-
  82. theme_minimal(
  83. base_size = 12,
  84. base_family = "mono") +
  85. theme(
  86. panel.grid.minor = element_blank(),
  87. panel.grid.major.x = element_blank(),
  88. legend.title = element_text(size = 8),
  89. legend.text = element_text(size = 7))
  90.  
  91. # set the theme
  92. theme_set(theme_datasci)
  93.  
  94. # plot
  95. ggplot(df_prop_test_power) +
  96. aes(
  97. x = prop_effect,
  98. y = power,
  99. color = prop_control,
  100. group = prop_control) +
  101. geom_line(show.legend = T) +
  102. # coordinates and scales ------------
  103. coord_cartesian(xlim = c(0, 0.15)) +
  104. scale_x_continuous(
  105. breaks = seq(0, 0.15, 0.025),
  106. labels = percent_format()) +
  107. scale_color_viridis_c(
  108. option = 'plasma',
  109. end = 0.9,
  110. labels = percent_format()) +
  111. # facet -----------------------------
  112. facet_wrap(
  113. ~ n_group,
  114. ncol = 2,
  115. # add 'N per group:' at the start of the facet labels
  116. labeller = as_labeller(
  117. function(x) { str_c('N per group: ', x)})) +
  118. # labels & theme --------------------
  119. theme(legend.position = c(0.9, 0.2)) +
  120. labs(
  121. x = 'Difference between treatment and control groups',
  122. y = 'Probability of detecting an effect',
  123. color = 'Baseline % \nin control group',
  124. title = 'Power curves for a prop.test',
  125. subtitle = 'By sample size, base-rate and treatment effect'
  126. )
  127.  
  128. # save
  129. ggsave(here('figures', 'power_curve.png'), width = 8, height = 6, dpi = 'retina')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement