Created
February 8, 2015 12:44
-
-
Save johnjosephhorton/4d4e561a52c86b74150a to your computer and use it in GitHub Desktop.
showing_stratification_advantage
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(ggplot2) | |
# simulate experiments using stratification and randomization and report | |
# absolute difference in estimates from true treatment effect, tau. | |
simExp <- function(n, tau, beta = 3){ | |
x <- sort(runif(n)) | |
W.rand <- rbinom(n,1,0.5) | |
W.strat <- rep(c(0,1), n/2) | |
epsilon <- rnorm(n, 0, 0.5) | |
y.rand <- beta * x + tau * W.rand + epsilon | |
y.strat <- beta * x + tau * W.strat + epsilon | |
m.strat <- lm(y.strat ~ W.strat) | |
m.rand <- lm(y.rand ~ W.rand) | |
c(abs(tau - coef(m.strat)[2]), abs(tau - coef(m.rand)[2])) | |
} | |
# compare w/ a t-test | |
results <-data.frame(t(sapply(1:1000, function(x) simExp(100, 3)))) | |
with(results, t.test(W.strat, W.rand)) | |
deltaABS <- function(sample.size){ | |
results <-data.frame(t(sapply(1:1000, function(x) simExp(sample.size, 3)))) | |
mean(results$W.rand) - mean(results$W.strat) | |
} | |
sizes <- c(20, 40, 80, 160, 320, 640) | |
improvement <- sapply(sizes, deltaABS) | |
df <- data.frame(size = sizes, improvement = improvement) | |
g <- ggplot(data = df, aes(x = sizes, y = improvement)) + geom_point() + geom_line() + | |
xlab("Sample size") + | |
ylab("Stratification Improvement in Absolute Distance from True Treatment Effect") + | |
theme_bw() | |
png("strat_results.png") | |
print(g) | |
dev.off() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment