Hi All,
I’m new to brms
and trying to fit a multi-level mediation model for within-person experimental data. I am sure this is a obvious question - but I cant seem to find an answer or example online (some practical details remain unclear). Since it’s for my thesis, any help would be greatly appreciated.
I came across this excellent blog post of Matti Vuorre which outlines exactly the structure I’m aiming for. However, I’m struggling with the choice of priors/distributions for the mediator.
In my posterior check pp_check - the distribution does not fit well at the tails. As far as I have understood, this is a big problem and a sign of an unreliable model- so my questions are:
- If this is a sign of misspecification, how do I go about in addressing this? I read the posts in this forum and tried some other distributions, but none seems to fit better. I am sure that adapting the priors afterwards / based on the posterior distribution is “overfitting”/cheating the model, so I assume fit a mixed-distribution based on the posteriors is also not allowed.
- Do I just use the not so well working tails? I have read tutorial were they then switch to a different distribution, and their problems are gone, but my mediator unfortunatley is not symmetrical - do I just change my mediators distribution (this feels wrong too)?
This is what I have done so far (see code at the end):
My data is within-person experimental data.I have isolated within-person deviations of my mediator and effect contrast coded all binary predictors. My controlvariable is z-standardized.
Within-person design
-
Predictors:
x
-
Mediator:
m
(continuous, raw data ranges ~0–100, never exactly 0 or 100) -
Outcome:
y
(binary) -
Control variable: z-standardized
-
Outcome: Posterior predictive checks look good
-
Mediator: I’ve tried several families:
student
,gaussian
,skew_normal
,beta
(after rescaling),hurdle_lognormal
, etc.
→ Best fit so far: Student Distributio – mean looks good, butpp_check
shows poor fit at the tails.
I have fitted a student distribution for the mediator and a benroulli for the outcome. The outcome model works well. The mean in the student works well with regards to predicting the mean stats and none of the other distributions fitted better (I tried skew_normal(), gaussian, beta distributions when I divide my variable /100, hurdle_log etc.pp.), based on the posterior checks and mean stats.
I’ve already consulted a range of resources (e.g., brms
vignettes, Andrew Heiss, Solomon Kurz, various YouTube tutorials), but examples rarely show full model fitting code or troubleshooting for not working distribution. Any pointers would be greatly appreciated— the right literature, if I have missed something in the forum or sharing practical advice would truly make my month. I assume I might just not recognize the tutorials that are doing what I am looking for.
Thank you for reading this far!
fixefPrior <- c(
set_prior("normal(0, 1)", class = "b", resp = "y"),
m_model<- brms::bf( m~ x+ controlvariable+ (x|p| ID) + (1 | item),family = student())
y_model <- brms::bf(
y~ x+m+ control+ (x+m|p| ID) + (1 | item),
family = bernoulli(link = "logit"))
# Define full file path for the saved model
model_file <- file.path(subfolder, "mediation.rds")
# Fit both models jointly
if(!file.exists(
paste0(mainpath,"/mediation.rds"))){
fit <- brm(m_model+y_model + set_rescor(FALSE),
data = data,
prior = c(fixefPrior),
chains = nChains,
cores = nCores,
iter = nIter,
warmup = nWarmup,
save_pars = save_pars(all = T))
saveRDS(fit,
file = paste0(mainpath,"mediation.rds"))
} else {
m0_full <- readRDS(file = paste0(
mainpath,"mediation.rds"))
}