有诸如 simr 之类的包可以为您完成所有这些工作,甚至更多(并且也可以处理不平衡的设计),但是这里有一个简单的方法来模拟混合模型的数据,然后您可以从头开始用于功率分析:
有几个重要的参数需要考虑:
- 整体数据大小
- 数据是否平衡
- 参加人数
- 固定效果
- 随机效应方差
- 剩余方差
这里我们使用平衡设计,这使得设置更容易,但将其扩展到不平衡设计也不会太难:
library(lme4)
set.seed(15)
N <- 10 # number of unique values of a and b for each participant
N.participant <- 20 # number of participants
betas <- c(10, 1, 2, 3, 4, 5, 6, 7) # fixed effects
s <- 0.5 # variance of random effects
e <- 1 # residual variance
# form the balanced data frame
dt <- expand.grid(a = rnorm(N), b = rnorm(N), c = c(0,1), d = c(0,1), time = c(0,1,2), group = c(0,1), participant = LETTERS[1:N.participant])
# form the model matrix for fixed effects
X <- model.matrix(~ a + b + c + d + time + group + time:group, data = dt)
# set a vector of 1s for the outcome. This is needed so we can easily compute the
# model matrix for random effects (in this case just the random intercetps)
dt$y <- 1
# The final model formula
myFormula <- "y ~ a + b + c + d + time + group + time:group + (1 | participant)"
# obtain the model matrix for random effects
foo <- lFormula(eval(myFormula), dt)
Z <- t(as.matrix(foo$reTrms$Zt))
# simulate the random effects
u <- rnorm(N.participant , 0, s)
# simulate y using the general mixed model equation and residual variance e
dt$y <- X %*% betas + Z %*% u + rnorm(nrow(dt), 0, e)
# fit the model
m0 <- lmer(myFormula, dt)
summary(m0)
您将为每组参数运行多次,当然使用不同的种子,并计算相关的蒙特卡罗估计值,以及每组达到的功效。