Created
June 3, 2024 13:30
-
-
Save carlislerainey/9c5f03c6fab4131a793b3b324838fc74 to your computer and use it in GitHub Desktop.
DeclareDesign code from Alex Coppock to simulate SEs from pilot studies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(DeclareDesign) | |
# Original parameters | |
tau <- 1 # treatment effect | |
se <- tau / (qnorm(0.95) + qnorm(0.80)) # standard error for 80% power | |
n_planned <- 500 # sample size per condition in planned full study | |
# calculate required standard deviation to yield 80% power given the above | |
sigma <- se * sqrt(2 * n_planned / 2) | |
# Declare the design | |
design <- | |
declare_model(N = 2*n_planned, | |
potential_outcomes(Y ~ rnorm(N, tau*Z, sigma))) + | |
declare_assignment(Z = complete_ra(N)) + | |
declare_measurement(Y = reveal_outcomes(Y ~ Z)) + | |
declare_estimator(Y ~ Z) | |
# We're after power for a one-tailed test | |
diagnosands <- | |
declare_diagnosands( | |
p_one_tailed_power = mean(pt(statistic, df, lower.tail = FALSE) <= 0.05)) | |
# This function simulates and gives bootstrapped SEs for simulation uncertainty | |
diagnosis <- diagnose_design(design, | |
diagnosands = diagnosands) | |
# compare over sample sizes | |
n_pilot_values <- c(10, 30, 60, 90, 150) | |
pilots <- redesign(design, n_planned = c(10, 30, 60, 90, 150)) | |
simulations <- simulate_designs(pilots) | |
# plot the standard errors | |
ggplot(simulations, aes(std.error)) + | |
geom_histogram() + | |
facet_wrap(vars(n_planned)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment