# Modeling bimodal duration data with categorical predictors with brms

**URL:** https://discourse.mc-stan.org/t/modeling-bimodal-duration-data-with-categorical-predictors-with-brms/28281
**Category:** Modeling
**Tags:** specification, fitting-issues, brms
**Created:** [July 21, 2022, 12:09pm UTC](https://discourse.mc-stan.org/t/modeling-bimodal-duration-data-with-categorical-predictors-with-brms/28281 "2022-07-21T12:09:16Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Patlane](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@Patlane](https://discourse.mc-stan.org/u/Patlane)
#### Post date: [July 21, 2022, 12:09pm UTC](https://discourse.mc-stan.org/t/modeling-bimodal-duration-data-with-categorical-predictors-with-brms/28281/1 "2022-07-21T12:09:16Z")

</div>

Hello!

I have collected some eye-tracking data within a three-factorial experiment design. My response variable is the start time of looking at some object (called as Gaze Onset) during the task of picture description. My Gaze Onsets follow a bimodal distribution for which I actually have found an explanatory variable (Gaze Label, the name of the object my participants look at the picture, see the Figure).

 ![GazeOnset_distributions](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/d/dfe9006e25ad56ee870eeb9fd91a2e3cf005d36c.png)

My problem is that when I specify this explanatory variable as a co-variate in my Bayesian regression model, the sampler still does not detect the two modes of the response variable (see the posterior predictive check below).

 ![GazeOnset_posteriorPredictiveCheck](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/a/af10daed486162d837e4fc3ebd530283aa55a46b.png)

Looking into different examples of models of bimodal distributions (e.g. [https://psyarxiv.com/cdxv3/](https://psyarxiv.com/cdxv3/)), the inclusion of an explanatory variable of the two modes should take care of the bimodal distribution in the sampler. I am new to Bayesian data analysis. Therefore, I am still modelling my data with unrestricted priors. Here’s the specification of my model.

```nohighlight
myBrmsModel = brm(GazeOnset ~ identity*doubleActor*load +
                    GazeLabel +
                    (1+identity*doubleActor*load|participantID) +
                    (1+identity*doubleActor*load|item) +
                    (1|trialIndex),
                  family = shifted_lognormal(),
                  data = test)

```

I have already implemented the shifted\_lognormal family suggested in this post: [Regression Models for Duration Data](https://discourse.mc-stan.org/t/regression-models-for-duration-data/16305). The mixture family did not work for my data as the convergence got really bad. Would you have any more ideas how could I capture the two modes of my response variable, please? Since I’m not a programming queen, I would appreciate the bits and pieces of concrete code.

---

<div class="post-metadata">

### Author: ![Patlane](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@Patlane](https://discourse.mc-stan.org/u/Patlane)
#### Post date: [July 26, 2022, 7:50am UTC](https://discourse.mc-stan.org/t/modeling-bimodal-duration-data-with-categorical-predictors-with-brms/28281/2 "2022-07-26T07:50:46Z")

</div>

Hi guys,

Meanwhile I took the courage and set some uninformative or regularizing priors and switched to the mixture modelling:

```nohighlight
mix <- mixture(exgaussian, exgaussian)
prior <- c(
  prior(normal(0, 1000), Intercept, dpar = mu1),
  prior(normal(1000, 1000), Intercept, dpar = mu2))

```

and I defined a simpler model with my controlled factors only:

```nohighlight
myBrmsModel <- brm(bf(GazeOnset ~ identity*doubleActor*load),
                   data = test,
                   family = mix,
                   prior = prior,
                   control = list(adapt_delta = 0.99),
                   init = 0,
                   iter = 5000)

```

With these changes, I indeed got brms to sample from a bimodal posterior:

 ![GazeOnset_posteriorPredictiveCheck_updated](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/d/d4d45a994fc43f420af5ab73c92dd30b84de8fa5.png)

But now I have another burning question. Could you help me to add in the random effects structure together with the priors? I have uploaded the subset of my data this time as well.

The desired random effects structure:

```nohighlight
(1+identity*doubleActor*load|participantID) +
                        (1+identity*doubleActor*load|item)+
                         (1|trialIndex)

```

The data:  
[22-07-26\_gazeOnsetsSubset.txt](https://discourse.mc-stan.org/uploads/short-url/sz2IsHu7TPA209t9MCw5LvhgPPj.txt) (60.8 KB)

I’m indeed a newbie here and would be extremely thankful for your help!
