Skip to content

Instantly share code, notes, and snippets.

@acoppock
Last active September 13, 2021 19:07
Show Gist options
  • Save acoppock/ca5491e243ac70422f75864b777f5416 to your computer and use it in GitHub Desktop.
Save acoppock/ca5491e243ac70422f75864b777f5416 to your computer and use it in GitHub Desktop.
Interaction Power Plot
library(DeclareDesign)
library(tidyverse)
set.seed(343)
design <-
declare_population(N = N) +
declare_potential_outcomes(
# Difference 1: 0.6 - 0.4 = 0.2
Y_Z_1 = draw_binary(prob = 0.4, N = N),
Y_Z_2 = draw_binary(prob = 0.6, N = N),
# Difference 2: 0.6 - 0.5 = 0.1
Y_Z_3 = draw_binary(prob = 0.5, N = N),
Y_Z_4 = draw_binary(prob = 0.6, N = N)
# Difference-in-differences (also called the interaction effect): 0.2 - 0.1 = 0.1
) +
declare_assignment(conditions = 1:4) +
declare_reveal(Y, Z) +
declare_step(handler = mutate,
Z1 = as.numeric(Z %in% c(2, 4)),
Z2 = as.numeric(Z %in% c(3, 4))) +
declare_estimator(Y ~ Z1 + Z2 + Z1 * Z2,
model = lm_robust,
term = "Z1:Z2")
diagnosis <-
design %>%
redesign(N = c(500, 1000, 3000, 5000)) %>%
diagnose_design(sims = 500, bootstrap_sims = FALSE)
diagnosis %>%
get_diagnosands() %>%
ggplot(aes(N, power)) +
geom_line() +
geom_hline(yintercept = 0.8, linetype = "dashed") +
theme_bw() +
ggtitle("Power for the interaction term in a 2x2 factorial experiment",
"When the difference in the effect of factor 1 depending on the level of factor 2 is 10pp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment