# March 27, 2024 library(tidyverse) # C for criticism (the mediator) C <- c(3, 2, 4, 2, 2, 3, 1, 2, 3, 3, 4, 4, 3, 2, 4, 4, 5, 3, 4, 4) # S for symptoms (the outcome) S <- c(4, 3, 4, 3, 4, 5, 3, 5, 7, 5, 7, 8, 5, 5, 7, 6, 6, 4, 5, 6) # group is the IV d <- tibble(group = c(rep("tx", 10), rep("ctrl", 10)), C, S) # group means d %>% group_by(group) %>% summarise(M = mean(S)) # contrast coding d <- d %>% mutate(X = ifelse(group == "tx", -1/2, 1/2)) # is there a significant group difference on the outcome? model1 <- lm(S ~ X, d) summary(model1) # check! pathc <- coef(model1)[2] # total effect of X; path c # is there a significant group difference on the mediator? model2 <- lm(C ~ X, d) summary(model2) # check! patha <- coef(model2)[2] # effect of X on C (the mediator); path a # is the effect of treatment reduced holding the mediator constant (i.e., # partialing it out)? model3 <- lm(S ~ X + C, d) coef(model1)[2] coef(model3)[2] # check! pathcprime <- coef(model3)[2] # paths c' and b pathb <- coef(model3)[3] all.equal(patha*pathb, pathc - pathcprime) library(mediation) med <- mediate(model.m = model2, model.y = model3, treat = "X", mediator = "C", boot = TRUE) summary(med) # ACME ("Average of Causal Mediation Effect") = a*b {"indirect effect"} # ADE: "Average Direct Effect" = c' {"direct effect"} # Total effect = c {ACME + ADE} # Proportion Mediated = (a*b)/c