From 6dc384168b87767109f0b797a253abe5688bd545 Mon Sep 17 00:00:00 2001 From: Jie_evezhang Date: Sun, 26 May 2024 09:54:28 +0200 Subject: [PATCH] change simulation --- sessions/causal-mediation-analysis-estimation.qmd | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sessions/causal-mediation-analysis-estimation.qmd b/sessions/causal-mediation-analysis-estimation.qmd index ba5408c..d0682bc 100644 --- a/sessions/causal-mediation-analysis-estimation.qmd +++ b/sessions/causal-mediation-analysis-estimation.qmd @@ -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 @@ -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)