Skip to content

Instantly share code, notes, and snippets.

@acoppock
Created September 7, 2023 15:18
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/edc71f66b85b87da3e3abec085d56788 to your computer and use it in GitHub Desktop.
Save acoppock/edc71f66b85b87da3e3abec085d56788 to your computer and use it in GitHub Desktop.
power analysis for an experiment with one-sided noncompliance
library(DeclareDesign)
library(tidyverse)
# Declare the design ----------------------------------------
compliance_rate <- 0.5
design <-
declare_model(
N = 1000,
type = sample(x = c("Never-Taker", "Complier"),
size = N,
prob = c(1 - compliance_rate, compliance_rate),
replace = TRUE),
U = rnorm(N),
# potential outcomes of Y with respect to D
# compliers experience a 0.2 sd effect
potential_outcomes(
Y ~ case_when(
type == "Never-Taker" ~ 0.75 - 0.20 * D + U,
type == "Complier" ~ 0.25 + 0.20 * D + U
),
conditions = list(D = c(0, 1))
),
# potential outcomes of D with respect to Z
potential_outcomes(
D ~ if_else(Z == 1 & type == "Complier", 1, 0),
conditions = list(Z = c(0, 1))
)
) +
declare_inquiry(
CACE = mean(Y_D_1[type == "Complier"] -
Y_D_0[type == "Complier"])
) +
declare_assignment(Z = complete_ra(N)) +
declare_measurement(D = reveal_outcomes(D ~ Z),
Y = reveal_outcomes(Y ~ D)) +
declare_estimator(
Y ~ D | Z,
.method = iv_robust,
inquiry = "CACE"
)
# simulate at compliance rate = 0.5 -------------------------
simulations <- simulate_design(design)
simulations |>
summarize(power = mean(p.value <= 0.05))
# redesign over compliance rates ----------------------------
simulations <-
design |>
redesign(compliance_rate = c(0.2, 0.5, 0.8)) |>
simulate_designs()
simulations |>
group_by(compliance_rate) |>
summarize(power = mean(p.value <= 0.05))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment