data(pwrsim)
## Power simulation 1: goodness-of-fit test, H0: a = u ------------------
s <- mptspec(
E.1 = c * r,
E.2 = (1 - c) * u^2,
E.3 = 2 * (1 - c) * u * (1 - u),
E.4 = c * (1 - r) + (1 - c) * (1 - u)^2,
F.1 = a,
F.2 = 1 - a
)
## Before you use par2prob(), carefully check position of parameters!
s$par
s$par2prob(c(c = 0.5, r = 0.5, u = 0.4, a = 0.6)) # evaluate model eqns
dataGen <- function(nn, d) {
structure(list( # stub mpt object
treeid = s$treeid,
n = setNames((nn * c(2, 1)/3)[s$treeid], s$treeid), # 2:1 ratio
pcat = s$par2prob(c(c = 0.5, r = 0.5,
u = 0.5 - d/2, a = 0.5 + d/2))
), class = "mpt") |>
simulate()
}
testFun <- function(nn, d) {
y <- dataGen(nn, d) # generate data with effect
m1 <- mpt(s, y)
m2 <- mpt(update(s, .restr = list(a = u)), y)
anova(m2, m1)$"Pr(>Chi)"[2] # test H0, return p value
}
pwrsim1 <- expand.grid(d = seq(0, 0.5, 0.1), n = 30 * 2^(0:5))
if (FALSE) {
pwrsim1$pval <-
mapply(function(nn, d) replicate(500, testFun(nn, d)),
nn = pwrsim1$n, d = pwrsim1$d, SIMPLIFY = FALSE)
pwrsim1$pwr <- sapply(pwrsim1$pval, function(p) mean(p < .05))
}
## Power simulation 2: age differences in retrieval ---------------------
s <- mptspec("SR", .replicates = 2)
dataGen <- function(nn, d) {
structure(list(
treeid = s$treeid,
n = setNames((nn/2 * c(2, 1, 2, 1)/3)[s$treeid], s$treeid),
pcat = s$par2prob(c(c1 = 0.5, r1 = 0.4 + d/2, u1 = 0.3, # young
c2 = 0.5, r2 = 0.4 - d/2, u2 = 0.3)) # old
), class = "mpt") |>
simulate(m)
}
testFun <- function(nn, d) {
y <- dataGen(nn, d)
m1 <- mpt(s, y)
m2 <- mpt(update(s, .restr = list(r1 = r2)), y)
anova(m2, m1)$"Pr(>Chi)"[2]
}
pwrsim2 <- expand.grid(d = seq(0, 0.4, 0.1), n = 120 * 2^(0:2))
if (FALSE) {
pwrsim2$pval <-
mapply(function(nn, d) replicate(500, testFun(nn, d)),
nn = pwrsim2$n, d = pwrsim2$d, SIMPLIFY = FALSE)
pwrsim2$pwr <- sapply(pwrsim2$pval, function(p) mean(p < .05))
}
Run the code above in your browser using DataLab