R-Code for Power in 2×2 Cross-over [🇷 for BE/BA]
❝ I am trying to adapt your example of power calculation with plot replesentation, but for the calculation of the sample size. My objetive is to plot a graphicala representation with ratio in X axis and sample size in Y axis and setting the CV and power as a fixed value.
I suggest this goody first. May sound picky but we can’t get the exact desired power, only one which is at least that high.*
Try this one:
library(PowerTOST)
design <- "2x2x2" # any in known.designs()
target <- 0.80 # target (desired) power
x.min <- 0.85 # minimum T/R-ratio (> 0.8)
x.res <- 250 # 'resolution' of the x-axis
y.min <- 0.15 # minimum CV
y.max <- 0.30 # maximum CV
y.res <- 0.05 # step size of CVs
# T/R-ratios equally spaced in log-scale
theta0 <- exp(seq(log(x.min), log(1 / x.min), length.out = x.res))
CV <- seq(y.min, y.max, y.res)
res <- data.frame(CV = rep(CV, each = length(theta0)),
target = target, theta0 = theta0,
power = NA_real_, n = NA_integer_)
for (j in 1:nrow(res)) {
tmp <- sampleN.TOST(CV = res$CV[j], theta0 = res$theta0[j],
targetpower = target, design = design,
print = FALSE) # no output to the console
if (tmp[["Sample size"]] >= 12) { # full throttle
res[j, 4:5] <- tmp[8:7]
} else {
res$n[j] <- 12 # acc. to the guidelines
res$power[j] <- power.TOST(CV = res$CV[j], theta0 = res$theta0[j],
design = design, n = res$n[j])
}
}
# theta0 on log-axis to demonstrate the symmetry
plot(res$theta0, res$n, type = "n", log = "x", las = 1,
ylim = c(12, max(res$n)), font.main = 1,
main = paste(design, "design\n",
sprintf("target power \u2265 %.f%%", 100 * target)),
xlab = expression(theta[0]), ylab = expression(italic(n)))
grid()
clr <- colorRampPalette(c("blue", "red"))(length(CV))
for (j in seq_along(CV)) {
lines(res$theta0[res$CV == CV[j]],
res$n[res$CV == CV[j]], lwd = 2, type = "s", col = clr[j],
lend = 2, ljoin = 1)
}
legend("top", bg = "white", inset = 0.02, box.lty = 0,
legend = sprintf("%.f%%", 100 * CV),
title = expression(italic(CV)), lwd = 2, col = clr,
seg.len = 2.5, ncol = 2)
The sample size is a staircase function and due to the symmetry in \(\small{\log_{e}}\)-scale we require the same sample size for any given \(\small{\theta_0}\) and \(1/\small{\theta_0}\) (see also there).
- OK, if we would be able to dose fractional subjects, then yes. I can’t. Amazingly some software packages report sumfink like \(\small{n=23.45}\) and leave the rounding up to the user. Crazy.
Diving deeper into the matter: Power is a hypersurface depending on \(\small{\theta_0}\), \(\small{n}\), and \(\small{CV}\). We can only show spatial projections at certain \(\small{CV}\text{-}\)values (the panels) with \(\small{\log_{e}\theta_0}\) (x-axis), \(\small{n}\) (y-axis), and power (z-axis).
\(\small{n=12-292}\) and \(\small{\theta_0=0.80-1.25}\) to demonstrate that at the limits of the acceptance range power (which is there the Type I Error) is ≤0.05.
The intersections with the horizontal plane (the target power) show the U-shaped dependency of \(\small{n}\) on \(\small{\theta_0}\) as in the plot above. The slices at certain \(\small{\theta_0}\)- and \(\small{n}\)-values give the plots like in the very old post above.
Don’t try to imagine four-dimensional objects. Even a simple hypercube (tesseract) may fry you brain.
Dif-tor heh smusma 🖖🏼 Довге життя Україна!
![[image]](https://static.bebac.at/pics/Blue_and_yellow_ribbon_UA.png)
Helmut Schütz
![[image]](https://static.bebac.at/img/CC by.png)
The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes
Complete thread:
- R-Code for Power in 2×2 Cross-over Helmut 2006-12-28 21:33
- Power in Diletti's sample size table Helmut 2007-01-01 23:59
- Approximate Power Helmut 2007-01-06 15:17
- Power Calculation for replicate crossover design mathews 2008-04-03 13:19
- Power Calculation for replicate crossover design Helmut 2008-04-03 13:49
- Power Calculation for replicate crossover design mathews 2008-04-04 13:21
- Power Calculation for replicate crossover design Helmut 2008-04-03 13:49
- Exact Power Helmut 2009-09-25 19:20
- Exact Power ElMaestro 2009-09-27 23:27
- Software validation Helmut 2009-09-28 15:21
- All the important answers are here! ElMaestro 2009-09-28 19:12
- All the important answers are here! Helmut 2009-09-28 19:52
- All the important answers are here! ElMaestro 2009-09-28 20:18
- All the important answers are here! yjlee168 2009-09-28 21:30
- All the important answers are here! Helmut 2009-09-28 23:18
- 42! SASophylistic power oracle d_labes 2009-09-29 13:37
- Al Gore Rhythms Helmut 2009-09-29 14:52
- Al Gore Rhythms ElMaestro 2009-09-29 18:14
- Al Gore Rhythms yjlee168 2009-09-29 23:55
- Al Gore Rhythms Helmut 2009-09-29 14:52
- 42! SASophylistic power oracle d_labes 2009-09-29 13:37
- All the important answers are here! yjlee168 2009-09-29 23:49
- All the important answers are here! Helmut 2009-09-28 23:18
- All the important answers are here! Helmut 2009-09-28 19:52
- All the important answers are here! ElMaestro 2009-09-28 19:12
- Software validation Helmut 2009-09-28 15:21
- Exact Power ElMaestro 2009-09-27 23:27
- Power Calculation for replicate crossover design mathews 2008-04-03 13:19
- Approximate Power Helmut 2007-01-06 15:17
- R-Code for Power in 2×2 Cross-over KDA 2019-01-23 04:24
- R-Code for Power in 2×2 Cross-over Ohlbe 2019-01-27 18:24
- R-Code for Power in 2×2 Cross-over Brus 2021-12-02 15:00
- R-Code for Power in 2×2 Cross-over ElMaestro 2021-12-02 15:15
- R-Code for Power in 2×2 Cross-over Helmut 2021-12-02 17:30
- R-Code for Power in 2×2 Cross-overHelmut 2021-12-02 16:57
- R-Code for Power in 2×2 Cross-over ElMaestro 2021-12-02 15:15
- Power in Diletti's sample size table Helmut 2007-01-01 23:59