Skip to content

Instantly share code, notes, and snippets.

@acoppock
Created March 5, 2021 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acoppock/402eec86b3c11547289f349e18bd8566 to your computer and use it in GitHub Desktop.
Save acoppock/402eec86b3c11547289f349e18bd8566 to your computer and use it in GitHub Desktop.
library(DeclareDesign)
library(tidyverse)
design <-
declare_model(
N = 100,
type =
rep(c("Always-Taker", "Never-Taker", "Complier", "Defier"),
c(0.2, 0.2, 0.6, 0.0)*N),
U = rnorm(N),
potential_outcomes(
Y ~ case_when(
type == "Always-Taker" ~ -0.25 - 0.50 * D + U,
type == "Never-Taker" ~ 0.75 - 0.25 * D + U,
type == "Complier" ~ 0.25 + 0.50 * D + U,
type == "Defier" ~ -0.25 - 0.50 * D + U
),
conditions = list(D = c(0, 1))
),
potential_outcomes(
D ~ case_when(
Z == 1 & type %in% c("Always-Taker", "Complier") ~ 1,
Z == 1 & type %in% c("Never-Taker", "Defier") ~ 0,
Z == 0 & type %in% c("Never-Taker", "Complier") ~ 0,
Z == 0 & type %in% c("Always-Taker", "Defier") ~ 1
),
conditions = list(Z = c(0, 1))
)
) +
declare_inquiry(
ATE = mean(Y_D_1 - Y_D_0),
CACE = mean(Y_D_1[type == "Complier"] - Y_D_0[type == "Complier"])) +
declare_assignment(Z = conduct_ra(N = N, prob = 0.5), handler = fabricate) +
declare_measurement(D = fabricatr::reveal_outcomes(D ~ Z),
Y = fabricatr::reveal_outcomes(Y ~ D)) +
declare_estimator(
Y ~ D | Z,
model = iv_robust,
inquiry = c("ATE", "CACE"),
label = "2SLS"
) +
declare_estimator(
Y ~ D,
model = lm_robust,
inquiry = c("ATE", "CACE"),
label = "As treated"
) +
declare_estimator(
Y ~ D,
model = lm_robust,
inquiry = c("ATE", "CACE"),
subset = D == Z,
label = "Per protocol"
)
run_design(design)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment