BRMS longitudinal mediation analysis

Hi all!

I am glad to have found this community to discuss tricky issues with the experts!
I carefully read another post on longitudinal mediation using BRMS (Longitudinal mediation analysis), and came up with an additional question.

But since that post dates back quite some time, I thought it would be beneficial to create a new post!

So, I am trying to estimate a longitudinal mediation where the mediator itself is time-varying.
I built the code based on the suggestion from the previous post!(Longitudinal mediation analysis)

a <- bf(health ~ gp(time) + gp(time, by = status_dum) + gp(time, by = poverty)+ (1 | id)) 

b <- bf(status_dum ~ gp(time) + gp(time, by = poverty) + (1 | id))

brm(a + b + set_rescor(FALSE), data = dts)

In my model using panel data that were annually surveyed, health is the dependent variable (continuous), poverty is the independent variable (continuous), and status_dum is the mediator (dichotomous).

While in the previous post, it was recommended to use either s (spline) or gp (gaussian) as the estimator (?), since my mediator is a dichotomous variable, I believe both these options are not appropriate.

Actually, ChatGPT… suggested estimating the model with the following code:
model ← brm(
formula = c(a, b),
family = c(“gaussian”, “bernoulli”),
data = your_data
)

However, since GPT’s suggestion differs too much from the expert suggestion from the previous post, I am opening a new post , seeking advice from the experts!

I would really appreciate the help from the community.
Thank you so much!!

Updated (Feb.12)
I built the following code by trying to include the family inside bf(), but I am not at all sure if it makes sense… I would really appreciate if someone could review it…!

a ← bf(health~ time + status_dum+ poverty+ (1 | id), family = “gaussian”)

b ← bf(status_dum~ time + poverty+ (1 | id), family = “bernoulli”)

brm(a + b + set_rescor(FALSE), data = dts)

  • Operating System: R
  • brms Version: latest

Hi

Based on the info you’ve provided the two formulas at the bottom of your post might be OK. It would be helpful to know more about your data and question in order to provide a more definitive answer. (A reproducible example would be very helpful.)

You might find this helpful: Multivariate and multilevel models

But also

2 Likes

Dear Matti,

I am extremely grateful for your reply!!
And I will closely look at and study the reference materials you kindly shared.

Below, I tried to make a reproducible example in R as best as I could!
When I ran the code in R, it unfortunately gave me the error message : ‘Argument ‘family’ is invalid.’
(maybe trying to add ‘family’ into the formula a, and b may not be the best approach?)

I would really appreciate your advice on the code!
And again, thank you so much for your help.

library(brms)
library(bmlm)
#use dataset from 'bmlm' package for a reproducible example
df <- BLch9

#create a binary mediator and covariate
df$mediator_dum <- rbinom(nrow(df), 1, 0.5)
df$covariate_gender <- rbinom(nrow(df), 1, 0.5)

#implement longitudinal mediation with a covariate, that addresses autoregressive error (AR(1))

#Dependent variable (time varying, continuous) = fwkstrs
#Independent variable (time varying, continuous) = fwkdis
#Mediator (time varying, dichotomous) = mediator_dum
#Covariate (time invariant, dichotomous) = covariate_gender

a<-bf(fwkstrs ~ time + mediator_dum + fwkdis + covariate_gender 
      (1 + covariate_gender|id, family = "gaussian"))

b<-bf(mediator_dum ~ time + fwkdis + covariate_gender 
      (1 + covariate_gender|id, family = "bernoulli"))

#temporal autocorrelation included ('AR(1)')
brm(a + b + set_rescor(FALSE), data = df,
    ar(time = time, gr = id, p=1))

Two syntax problems here, 1) you need a + to add your fixed and random effects, 2) you need to pass family as an argument to bf, which your use of parentheses prevents. So:

a<-bf(fwkstrs ~ time + mediator_dum + fwkdis + covariate_gender +
      (1 + covariate_gender|id), family = "gaussian")

b<-bf(mediator_dum ~ time + fwkdis + covariate_gender +
      (1 + covariate_gender|id), family = "bernoulli")
1 Like

Thank you very much for your help! 🙌