Skip to content

Instantly share code, notes, and snippets.

@shahnikba
Created December 27, 2024 11:08
Show Gist options
  • Select an option

  • Save shahnikba/b9593a55823f797727a2c590d2ca1f97 to your computer and use it in GitHub Desktop.

Select an option

Save shahnikba/b9593a55823f797727a2c590d2ca1f97 to your computer and use it in GitHub Desktop.
Structureal Causal Model
=100000
p_g = 0.1
p_s1 = 0.2
p_s2 = 0.4
p_l1 = 0.05
p_l2 = 0.20
p_l3 = 0.30
U_G = rbinom(n,1,p_g)
U_S1 = rbinom(n,1,p_s1)
U_S2 = rbinom(n,1,p_s2)
U_L1 = rbinom(n,1,p_l1)
U_L2 = rbinom(n,1,p_l2)
U_L3 = rbinom(n,1,p_l3)
G = U_G
S = U_S1 * (1-G) + U_S2 * G
L = U_L1 * (1-G)*(1-S) + U_L2*((1-G)*S + (1-S)*G) + U_L3 * G * S
L0 = U_L1*(1-G) + U_L2*G
L1 = U_L2*(1-G) + U_L3*G
df <- data.frame(G, S, L)
cod1 = sum(df[df$S==1,]$L)/sum(df$S)
cod0 = sum(df[df$S==0,]$L)/(n-sum(df$S))
print("causal effect of smoking")
print((sum(L1) - sum(L0))/n)
print("difference of contional probabilities")
print(cod1-cod0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment