Guest User

Untitled

a guest
Nov 7th, 2026
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.15 KB | None | 0 0
  1. library(ggplot2)
  2. library(pwr)
  3. points <- 1000
  4.  
  5. n <- 120
  6. d <- 0.2
  7. sig.level <- 0.05
  8.  
  9. df <- n - 1
  10. tcrit <- qt(1 - sig.level, df = df)
  11. h1ncp <- d * sqrt(n)
  12.  
  13. h0args <- list(df = df)
  14. h1args <- list(df = df, ncp = h1ncp)
  15.  
  16. power <- pwr.t.test(d = d, n = n, sig.level = sig.level, alternative = "greater")$power
  17.  
  18. ggplot() +
  19.   stat_function(fun = dt, args = h0args,
  20.                 geom = "area", xlim = c(tcrit, 30),
  21.                 fill = "skyblue3", alpha = 0.8) +
  22.   stat_function(fun = dt, args = h1args, n = points,
  23.                 geom = "area", xlim = c(-10, tcrit),
  24.                 fill = "hotpink2", alpha = 0.4) +
  25.   stat_function(fun = dt, args = h1args, n = points,
  26.                 geom = "area", xlim = c(tcrit, 30),
  27.                 fill = "seagreen3", alpha = 0.2) +
  28.   geom_function(fun = dt, args = h0args, n = points) +
  29.   geom_function(fun=dt, args = h1args, n = points) +
  30.   geom_vline(xintercept = tcrit, linetype = "dotted") +
  31.   annotate(
  32.     "text",
  33.     x = tcrit + 0.1 , y = I(0.4),
  34.     hjust = "left",
  35.     label = paste0(round(power*100,2), "%"),
  36.     color = "seagreen4"
  37.   ) +
  38.   xlim(-2 * tcrit, 4 * tcrit) +
  39.   theme_linedraw()
  40.  
Advertisement
Add Comment
Please, Sign In to add comment