8.7 What you can now do
Given the above code and workflow, you can now figure out how many subjects you might need to achieve 80% power, assuming a certain effect size (or a range of effect sizes as above) and assuming some specific values for the standard deviations and variance covariance matrices.
You can also study Type I error properties of your model as a function of whether the model is a maximal model or a varying intercepts only model.
Example: Compute power as a function of effect size and sample size. Note that the number of subjects has to be even, otherwise the simulation code will fail! One could put in a test for this in the code: if the number of subjects is divisible by 2, the modulo function should return 0:
10 %% 2
## [1] 0
11 %% 2
## [1] 1
## define a function for computing power
## (as a function of effect size and
## subject sample size:
<- function(b = NULL, nsubjects = 28) {
compute_power if (nsubjects %% 2 != 0) {
stop("No. of subjects must be divisible by 2.")
}<- 100
nsim <- rep(NA, nsim)
sotvals <- rep(0, nsim)
failed for (i in 1:nsim) {
2] <- b
beta[<- gen_sim_lnorm2(
dat_sim nitem = 24,
nsubj = nsubjects,
beta = beta,
Sigma_u = Sigma_u,
Sigma_w = Sigma_w,
sigma_e = sigma_e
)
## no correlations estimated to avoid convergence problems:
## analysis done after log-transforming:
<- lmer(log(rt) ~ so + (so || subj) +
m || item), data = dat_sim, control = lmerControl(calc.derivs = FALSE))
(so ## ignore failed trials
if (any(grepl("failed to converge", m@optinfo$conv$lme4$messages))) {
<- 1
failed[i] else {
} <- summary(m)$coefficients[2, 3]
sotvals[i]
}
}
## power:
paste("Power:", round(mean(abs(sotvals) > 2,
na.rm = TRUE
2), sep = " ")
), }
## usage: this will halt function
## with an error message
# compute_power(b=0.03,nsubjects=29)
compute_power(b = 0.03, nsubjects = 28)
[1] “Power: 0.04”