Skip to content

Instantly share code, notes, and snippets.

@jwbowers
Created January 31, 2020 18:26
Show Gist options
  • Save jwbowers/5749bc5cce4fa4eb1d33190e9784dbc9 to your computer and use it in GitHub Desktop.
Save jwbowers/5749bc5cce4fa4eb1d33190e9784dbc9 to your computer and use it in GitHub Desktop.
## Example of simulating data from a design with one-sided non-compliance
types <- c("Always-Taker", "Never-Taker", "Complier", "Defier")
direct_effect_of_encouragement <- 0.0
proportion_defiers <- 0.0
design <- declare_population(
N = 500,
type = sample( types, N, replace = TRUE,
prob = c(0.1, 0.1, 0.8 - proportion_defiers, proportion_defiers)
),
noise = rnorm(N)
) +
declare_potential_outcomes(
D ~ case_when(
Z == 0 & type %in% c("Never-Taker", "Complier") ~ 0,
Z == 1 & type %in% c("Never-Taker", "Defier") ~ 0,
Z == 0 & type %in% c("Always-Taker", "Defier") ~ 1,
Z == 1 & type %in% c("Always-Taker", "Complier") ~ 1
)
) +
declare_potential_outcomes(
Y ~ 0.5 * (type == "Complier") * D +
0.25 * (type == "Always-Taker") * D +
0.75 * (type == "Defier") * D +
direct_effect_of_encouragement * Z + noise,
assignment_variables = c("D", "Z")
) +
declare_estimand(CACE = mean((Y_D_1_Z_1 + Y_D_1_Z_0) / 2 -
(Y_D_0_Z_1 + Y_D_0_Z_0) / 2),
subset = type == "Complier") +
declare_estimand(ITT = mean((Y_D_1_Z_1 + Y_D_1_Z_0) / 2 -
(Y_D_0_Z_1 + Y_D_0_Z_0) / 2)) +
##declare_estimand(ITT2 = mean((Y_D_1_Z_1 + Y_D_0_Z_1) / 2 -
## (Y_D_1_Z_0 + Y_D_1_Z_0) / 2)) +
declare_assignment(prob = 0.5) +
declare_reveal(D, assignment_variable = "Z") +
declare_reveal(Y, assignment_variables = c("D", "Z")) +
declare_estimator(Y ~ D | Z, model = iv_robust, estimand = "CACE",label='2SLS') +
declare_estimator(Y ~ Z, model=lm_robust, estimand = "ITT",label='OLS')
designs <- redesign(
design,
proportion_defiers = 0, # seq(0, 0.3, length.out = 5),
direct_effect_of_encouragement = seq(0, 0.3, length.out = 5)
)
plan(multiprocess,workers=8)
bookdesigns_diag <- diagnose_design(designs, bootstrap_sims=0,sims = 1000)
bookdesigns_diag
res <- reshape_diagnosis(bookdesigns_diag)
res %>% select(direct_effect_of_encouragement,'Estimand Label','Estimator Label','Term','Bias','SD Estimate','Mean Estimand')
plan(sequential)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment