How to use random assign as a instrumenal var in the analysis of cace in a rct
# Simulate data set.seed(1) n <- 1000 R <- rbinom(n, 1, 0.5) # Random assignment U <- rnorm(n) # Unobserved confounder Z <- R*0.7 + U*0.3 + rnorm(n,0,0.1) # Treatment received Y <- Z*0.5 + U + rnorm(n) # Outcome # Intention-to-treat estimate summary(lm(Y ~ R)) # Naive per-protocol estimate summary(lm(Y ~ Z)) # IV estimate using 2SLS library(ivreg) iv_fit <- ivreg(Y ~ Z | R) summary(iv_fit)