# Help with model fitting?

**URL:** <https://discourse.mc-stan.org/t/help-with-model-fitting/36942>\
**Category:** Modeling\
**Tags:** brms\
**Created:** [October 16, 2024, 8:29pm UTC](https://discourse.mc-stan.org/t/help-with-model-fitting/36942 "2024-10-16T20:29:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kshtj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/kshtj/32/19247_2.png) [@kshtj](https://discourse.mc-stan.org/u/kshtj)\
**Post date:** [October 16, 2024, 8:29pm UTC](https://discourse.mc-stan.org/t/help-with-model-fitting/36942/1 "2024-10-16T20:29:25Z")

</div>

Hi I am having trouble in fitting a model in the `brms` which has mixed distribution or two peaks!  
here is what the variables distribution look like :  
 ![first_reward](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/9/3/93ba9dce0eb1170deae4930791e44ee4fbacafc5.png)  
 ![Patch_count](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/e/5/e59a741a4f78c0c5b99fc559f01457a524ae62cf.png)  
 ![RR](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/2/e/2e20e35401aeeabea074a5791d22d0f9ea59b46b.png)

and this the formula I am using:

```no-highlight
m_b2_zip <- brm(
  Patch_count ~ first_Reward + RR + (1 | Name),
  data = data_b2,
  family = zero_inflated_poisson()
)

```

the pp\_check looks like this:  
 ![model_ppc](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/5/4/54c2f1cf5a6147f74ceb09eafb93ad8318b5954d.png)

Can someone please help ?

---

<div class="post-metadata">

**Author:** ![jd\_c](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jd_c/32/16759_2.png) [@jd\_c](https://discourse.mc-stan.org/u/jd_c)\
**Post date:** [October 17, 2024, 3:24pm UTC](https://discourse.mc-stan.org/t/help-with-model-fitting/36942/2 "2024-10-17T15:24:13Z")

</div>

Howdy!  
Just as you use histograms to visualize the data, you could use histograms to visualize your posterior predictive distribution. So, to start with, try using the `pp_check()` function with `type='hist'`. Note, that you may need to adjust the binwidth in the histogram. Since you are using a zero-inflated Poisson, you might also want to check the proportion of zeros. You might also use `type='bars'` or `bars_grouped`. In any case, you don’t want to use a density plot on discrete spaces.

```no-highlight
pp_check(m_b2_zip, type="hist")

prop_zero <- function(Patch_count) mean(Patch_count == 0)
prop_zero_check <- pp_check(m_b2_zip, type = "stat", stat = "prop_zero")

pp_check(m_b2_zip, type="bars")

```

Once you have a more appropriate visualization, then you can better check the model.

Note - if you don’t mind coding in base R, even better plots, in my opinion, are like those that Michael Betancourt does in his case studies, similar to the code I used to make the plot in this post [Improving PPC Fit in MELS model (brms) when using Multimodal Data - #5 by jd\_c](https://discourse.mc-stan.org/t/improving-ppc-fit-in-mels-model-brms-when-using-multimodal-data/36417/5) . Betancourt has some functions to do this type of plot here [GitHub - betanalpha/mcmc\_visualization\_tools: Markov Chain Monte Carlo Visualization Tools](https://github.com/betanalpha/mcmc_visualization_tools) but I personally haven’t had a chance yet to use all of them for plotting purposes.

---

<div class="post-metadata">

**Author:** ![kshtj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/kshtj/32/19247_2.png) [@kshtj](https://discourse.mc-stan.org/u/kshtj)\
**Post date:** [October 17, 2024, 6:52pm UTC](https://discourse.mc-stan.org/t/help-with-model-fitting/36942/3 "2024-10-17T18:52:32Z")

</div>

So I ran mixture family using this :  
mix ← mixture(skew\_normal, gaussian, skew\_normal, order = ‘mu’)

the density plots seem fine but i get this warning:  
Warning messages:  
1: There were 1000 divergent transitions after warmup. See  
[https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup](https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup)  
to find out why this is a problem and how to eliminate them.  
2: There were 3000 transitions after warmup that exceeded the maximum treedepth. Increase max\_treedepth above 10. See  
[https://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded](https://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded)  
3: There were 1 chains where the estimated Bayesian Fraction of Missing Information was low. See  
[https://mc-stan.org/misc/warnings.html#bfmi-low](https://mc-stan.org/misc/warnings.html#bfmi-low)  
4: Examine the pairs() plot to diagnose sampling problems

5: The largest R-hat is 4.42, indicating chains have not mixed.  
Running the chains for more iterations may help. See  
[https://mc-stan.org/misc/warnings.html#r-hat](https://mc-stan.org/misc/warnings.html#r-hat)  
6: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.  
Running the chains for more iterations may help. See  
[https://mc-stan.org/misc/warnings.html#bulk-ess](https://mc-stan.org/misc/warnings.html#bulk-ess)  
7: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.  
Running the chains for more iterations may help. See  
[https://mc-stan.org/misc/warnings.html#tail-ess](https://mc-stan.org/misc/warnings.html#tail-ess)
