Dear all,
Sorry if this is somehow a simple question but I have been stuck on this for a while.
I am trying to do a simple mediation analysis. First I fitted a multilevel model in brms (“structural equation model”):
# Define structural equation model
path1 <- bf(bci ~ faith + cort + immunity + age + (1|group/ID))
path2 <- bf(immunity ~ faith + cort + age + (1|group/ID))
path3 <- bf(faitth ~ cort + age + (1|group/ID))
path4 <- bf(cort ~ age + (1|group/ID)) + skew_normal()
sem_immunity_faith <- path1 + path2 + path3 + path4
# Run brms
ncores = detectCores()
options(mc.cores = parallel::detectCores())
model_immunity_faith <-brm(sem_immunity_faith + set_rescor(FALSE),
data = metadata,
warmup = 50000, iter = 100000,
cores=ncores, chains=4, init=1000)
To do a mediation analysis I was trying to follow the instructions in this post:
https://discourse.mc-stan.org/t/mediation-question-using-brms/4260
But apparently the function posterior_samples
is deprecated so I tried to do it with the new as_draws
function, like so:
# Indirect effect of cort on faith meditated by immunity
post_immune_cort <- as_draws_df(model_immunity_faith, variable = "b_immunity_cort")
post_immune_faith <- as_draws_df(model_immunity_faith, variable = "b_immunity_faith")
indirect_cort_faith <- post_immune_cort *post_immune_faith
indirect_cort_faith <- as.data.frame(posterior::summarize_draws(indirect_cort_faith))
It seems to work but I am not sure if I am doing something wrong. Any feedback on this?
Thanks in advance.
Kind regards,
Hugo