Skip to content

Commit

Permalink
change simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
arborzhang committed May 26, 2024
1 parent 519fa94 commit 6dc3841
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions sessions/causal-mediation-analysis-estimation.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,11 @@ Let's use g-computation!
A1Data <- A0Data <- ObsData
# In one world A equals 1 for everyone, in the other one it equals 0 for everyone
# The rest of the data stays as is (for now)
A1Data$A <- 1; A0Data$A <- 0
head(ObsData); head(A1Data); head(A0Data)exp(Q$coef[2])
A1Data$A <- 1
A0Data$A <- 0
head(ObsData)
head(A1Data)
head(A0Data)
# Predict Y if A=1
Y_A1 <- predict(Q, A1Data, type="response")
# Predict Y if A=0
Expand All @@ -622,19 +625,20 @@ Y_A0 <- predict(Q, A0Data, type="response")
data.frame(Y_A1=head(Y_A1), Y_A0=head(Y_A0), TRUE_Y=head(ObsData$Y)) |> round(digits = 2)
# Mean outcomes in the two worlds
pred_A1 <- mean(Y_A1); pred_A0 <- mean(Y_A0)
pred_A1 <- mean(Y_A1)
pred_A0 <- mean(Y_A0)
# Marginal odds ratio
MOR_gcomp <- (pred_A1*(1 - pred_A0))/((1 - pred_A1)*pred_A0)
# ATE (risk difference)
RD_gcomp <- pred_A1 - pred_A0
c(MOR_gcomp, RD_gcomp) |> round(digits=3)
c(MOR_gcomp, RD_gcomp) |> round(digits=2)
```

The demo shows how to estimate total effect using g-computation. If you
would like to estimate PNDE, TNIT, PNIE, TNDE, then you also need to
predict two potential mediators observed in a hypothetical world in
which all individuals are intervention (A=1) or non-intervention (A=0).
We recommend take the advatange of R packages to do the work :)
We recommend take the advantage of R packages to do the work :)

```{r}
set.seed(1)
Expand Down

0 comments on commit 6dc3841

Please sign in to comment.