Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(ggplot2)
- library(pwr)
- points <- 1000
- n <- 120
- d <- 0.2
- sig.level <- 0.05
- df <- n - 1
- tcrit <- qt(1 - sig.level, df = df)
- h1ncp <- d * sqrt(n)
- h0args <- list(df = df)
- h1args <- list(df = df, ncp = h1ncp)
- power <- pwr.t.test(d = d, n = n, sig.level = sig.level, alternative = "greater")$power
- ggplot() +
- stat_function(fun = dt, args = h0args,
- geom = "area", xlim = c(tcrit, 30),
- fill = "skyblue3", alpha = 0.8) +
- stat_function(fun = dt, args = h1args, n = points,
- geom = "area", xlim = c(-10, tcrit),
- fill = "hotpink2", alpha = 0.4) +
- stat_function(fun = dt, args = h1args, n = points,
- geom = "area", xlim = c(tcrit, 30),
- fill = "seagreen3", alpha = 0.2) +
- geom_function(fun = dt, args = h0args, n = points) +
- geom_function(fun=dt, args = h1args, n = points) +
- geom_vline(xintercept = tcrit, linetype = "dotted") +
- annotate(
- "text",
- x = tcrit + 0.1 , y = I(0.4),
- hjust = "left",
- label = paste0(round(power*100,2), "%"),
- color = "seagreen4"
- ) +
- xlim(-2 * tcrit, 4 * tcrit) +
- theme_linedraw()
Advertisement
Add Comment
Please, Sign In to add comment