Listening experiment
Assumptions
Hypothesis
n <- 110; m <- 110
pval <- replicate(2000, {
x <- rnorm(n, mean = 1000 + 4, sd = 10) # Participant 1 responses
y <- rnorm(m, mean = 1000, sd = 10) # Participant 2 responses
t.test(x, y, mu = 0, var.equal = TRUE)$p.value
})
mean(pval < 0.05)
## [1] 0.8345
Turn into a function of n and effect size
pwrFun <- function(n = 30, d = 4, sd = 10,
nrep = 50) {
n <- n; m <- n
pval <- replicate(nrep, {
x <- rnorm(n, mean = 1000 + d, sd = sd)
y <- rnorm(m, mean = 1000, sd = sd)
t.test(x, y, mu = 0, var.equal = TRUE)$p.value
})
mean(pval < 0.05)
}
Set up conditions and call power function
cond <- expand.grid(d = 0:5,
n = c(50, 75, 100, 125))
system.time(
cond$pwr <- mapply(pwrFun, n = cond$n, d = cond$d,
MoreArgs = list(nrep = 500))
)
## User System verstrichen
## 1.435 0.007 1.441
## Plot results
lattice::xyplot(pwr ~ d, cond, groups = n, type = c("g", "b"),
auto.key = list(corner = c(0, 1)))
n <- 100
out <- replicate(2000, {
x <- rnorm(n, mean = 1000 + 4, sd = 10)
y <- rnorm(n, mean = 1000, sd = 10)
t <- t.test(x, y, mu = 0, var.equal = TRUE)
c(
effect = -as.numeric(diff(t$estimate)),
sd.pool = t$stderr * sqrt(2*n) / 2
)
})
rowMeans(out)
## effect sd.pool
## 3.981564 9.997941
boxplot(t(out))
Effect of fertilizers
In an experiment, two fertilizers (A and B, each either low or high dose) will be combined and the yield of peas (Y) in kg be observed. Goal is to detect an increase of the Fertilizer-A effect by an additional 12 kg when combined with a high dose of Fertilizer B (interaction effect).
Assumptions
Hypothesis
set.seed(1704)
n <- 96
dat <- data.frame(
A = factor(rep(1:2, each = n/2), labels = c("low", "high")),
B = factor(rep(rep(1:2, each = n/4), 2), labels = c("low", "high"))
)
X <- model.matrix(~ A*B, dat)
unique(X)
## (Intercept) Ahigh Bhigh Ahigh:Bhigh
## 1 1 0 0 0
## 25 1 0 1 0
## 49 1 1 0 0
## 73 1 1 1 1
beta <- c(mu = 30, a2 = 30, b2 = 5, ab22 = 12)
means <- X %*% beta
lattice::xyplot(I(means + rnorm(n, sd = 10)) ~ A, dat, groups = B,
type = c("g", "p", "a"), auto.key = TRUE, ylab = "Yield (kg)")
out <- replicate(2000, {
y <- means + rnorm(n, sd = 10) # y = mu + a + b + ab + e
m <- aov(y ~ A*B, dat)
c(coef(m), sigma = sigma(m))
})
boxplot(t(out))
pval <- replicate(2000, {
y <- means + rnorm(n, sd = 10)
m <- aov(y ~ A*B, dat)
summary(m)[[1]]$"Pr(>F)"[3] # test of interaction
})
mean(pval < 0.05)
## [1] 0.817