Investigation: Study design and power via simulations

Investigation: Study design and power via simulations#

Simulation evaluates the operating characteristics of a specified design and analysis under explicit assumptions. Power, false-positive rate, interval coverage, bias, and precision are different quantities. Simulation uncertainty must also be reported.

Question. Estimate power and coverage together over a sample-size grid; run null scenarios and compare increasing participants with increasing trials.

Before running: write the result you expect and what would challenge it. Data are simulated unless explicitly identified otherwise.

# Open this notebook from book/notebooks (or book/exercises).
course_path <- if (file.exists("../R/course.R")) "../R/course.R" else "book/R/course.R"
stopifnot(file.exists(course_path))
source(course_path)
set.seed(432)
evaluate_design <- function(n,delta=.4,B=500,alpha=.05) {
  runs <- replicate(B,{
    x <- rnorm(n,delta,1)
    fit <- t.test(x,mu=0,conf.level=1-alpha)
    c(estimate=mean(x),reject=fit$p.value<alpha,covered=fit$conf.int[1]<=delta && fit$conf.int[2]>=delta,width=diff(fit$conf.int))
  })
  rejection <- mean(runs["reject",])
  coverage <- mean(runs["covered",])
  data.frame(n=n,truth=delta,rejection_rate=rejection,rejection_mcse=sqrt(rejection*(1-rejection)/B),coverage=coverage,coverage_mcse=sqrt(coverage*(1-coverage)/B),bias=mean(runs["estimate",])-delta,mean_interval_width=mean(runs["width",]))
}
results <- do.call(rbind,lapply(c(20,40,80),evaluate_design))
null_results <- do.call(rbind,lapply(c(20,40,80),function(n) evaluate_design(n,delta=0)))
results
null_results
A data.frame: 3 × 8
ntruthrejection_raterejection_mcsecoveragecoverage_mcsebiasmean_interval_width
<dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
200.40.4120.0220116330.9480.009929350-0.0063203270.9091630
400.40.6880.0207198460.9500.009746794 0.0043910050.6361965
800.40.9500.0097467940.9320.011258419 0.0091898180.4413624
A data.frame: 3 × 8
ntruthrejection_raterejection_mcsecoveragecoverage_mcsebiasmean_interval_width
<dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
2000.0520.009929350.9480.00992935-0.00217362220.9275219
4000.0520.009929350.9480.00992935-0.00794284160.6368108
8000.0540.010107820.9460.01010782-0.00075987910.4460279

Power is not coverage#

Under the nonzero alternative, rejection frequency estimates power. Under the null, it estimates the false-positive rate. Coverage counts intervals containing the true parameter, in either case. The displayed Monte Carlo SE quantifies precision of the simulation estimate; it does not capture uncertainty about the assumed effect or variance.

# Participant means with trial noise: the subject SD limits what more trials can fix.
cluster_power <- function(subjects,trials,B=300,delta=20,subject_sd=50,trial_sd=100) {
  rejected <- replicate(B,{
    subject_means <- delta+rnorm(subjects,0,subject_sd)+rnorm(subjects,0,trial_sd/sqrt(trials))
    t.test(subject_means)$p.value<.05
  })
  p <- mean(rejected)
  c(power=p,mcse=sqrt(p*(1-p)/B))
}
designs <- expand.grid(subjects=c(20,40,80),trials=c(10,40))
values <- t(mapply(cluster_power,designs$subjects,designs$trials))
cbind(designs,values)
A data.frame: 6 × 4
subjectstrialspowermcse
<dbl><dbl><dbl><dbl>
20100.22000000.02391652
40100.56000000.02865891
80100.83666670.02134288
20400.37333330.02792583
40400.62666670.02792583
80400.91000000.01652271

From demonstration to a study recommendation#

Increase repetitions until Monte Carlo uncertainty is small enough for the decision; do not use ten runs to support a precise sample-size claim. Evaluate plausible effect sizes, dropout, missingness, and analysis failures. For complex models, record convergence and failed-fit counts and specify how failures enter the decision. A BIC threshold has its own operating characteristics and must be calibrated separately. Relative bias is undefined at a zero true effect, so this lab uses absolute bias.

Interpret and verify#

Choose plausible parameter ranges, record failed fits, and report Monte Carlo standard errors alongside a sample-size recommendation.

Failure to consider: More simulation repetitions improve Monte Carlo precision, not the truth of the assumed data-generating process. A BIC selection rule is not automatically a test at a stated alpha.

Report the target, the numerical result with its units, the relevant uncertainty, and one limitation. Distinguish what is established by the simulation from what would require real data.

AI audit: critique the saved faulty claim in the companion exercise before using any live assistant. If you use AI, record the relevant suggestion, your independent check, and the final decision.

Build on explicit generative assumptions and uncertainty; next examine how model search changes evaluation.

sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.7.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] C.UTF-8/C.UTF-8/C.UTF-8/C/C.UTF-8/C.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] fansi_1.0.3         crayon_1.5.1        digest_0.6.29      
 [4] utf8_1.2.2          IRdisplay_1.1       repr_1.1.4         
 [7] lifecycle_1.0.1     jsonlite_1.8.0      evaluate_0.15      
[10] pillar_1.8.0        rlang_1.0.6         cli_3.3.0          
[13] uuid_1.1-0          vctrs_0.4.1         IRkernel_1.3.0.9000
[16] tools_4.2.1         glue_1.6.2          fastmap_1.1.0      
[19] compiler_4.2.1      base64enc_0.1-3     pbdZMQ_0.3-7       
[22] htmltools_0.5.3    

Attribution#

Notebook authored by Ven Popov and edited by Krista Bond, Charles Wu, Patience Stevens, and Amy Sentis.

Revised investigation: 2026 course revision. Original authorship is retained; examples and explanatory text have been updated.