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)

留言

這個網誌中的熱門文章

可轉移性、普遍性、代表性和外部有效性

頻率學派 vs 貝氏學派

貝氏分析計算器