Skip to content

Instantly share code, notes, and snippets.

@markmfredrickson
Created August 2, 2011 23:54
Show Gist options
  • Save markmfredrickson/1121546 to your computer and use it in GitHub Desktop.
Save markmfredrickson/1121546 to your computer and use it in GitHub Desktop.
A quick simulation of block size effect on estimated treatment effects
library(RItools)
# set up the potential outcomes, n = 20
Yc <- rnorm(20)
Yt <- Yc + 1 # additive effect of 2
# "randomly" sample 1/2 of the data, this works because we know how Yc is created
Z <- rep(c(T,F), 10)
R <- ifelse(Z, Yt, Yc)
# blocks are meaningless, i.e. effect size same across blocks
B1 <- rep(c(0,1), each = 10) # two blocks of size 10
B2 <- c(rep(0, 2), rep(1, 18)) # a very small block
summary(lm(R ~ Z))
summary(lm(R ~ Z + B1))
summary(lm(R ~ Z + B2))
summary(lm(R ~ Z + B2, weights = c(rep(2, 2), rep(18, 18))))
# now we make it so that blocks do matter
# people in B2_0 get 1 if treated, those in B2_1 get 3 if treated
Yt <- Yc + 1 + 2 * B2
R <- ifelse(Z, Yt, Yc)
summary(lm(R ~ Z + B2))
summary(lm(R ~ Z + B2, weights = c(rep(2, 2), rep(18, 18))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment