# Simulation using brms

**URL:** <https://discourse.mc-stan.org/t/simulation-using-brms/20865>\
**Category:** General\
**Tags:** brms\
**Created:** [February 18, 2021, 3:24pm UTC](https://discourse.mc-stan.org/t/simulation-using-brms/20865 "2021-02-18T15:24:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgellar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgellar/32/2917_2.png) [@jgellar](https://discourse.mc-stan.org/u/jgellar)\
**Post date:** [February 18, 2021, 3:24pm UTC](https://discourse.mc-stan.org/t/simulation-using-brms/20865/1 "2021-02-18T15:24:26Z")

</div>

I am conducting a simulation, and for each iteration of the simulation I am going to run a Stan model using brms. The only thing changing from iteration to iteration is the data, so I’d like to pre-compile the model and use `update.brmsfit(newdata = new_data)` to do the sampling.

My only question is regarding that initial model fit when the model gets compiled. Is there any reason that I can’t do something like set `chains=1, iter=1` to pretty much skip the sampling step? This wouldn’t affect the resulting model fits that occur after calling `update()`, right?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![JLC](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jlc/32/12520_2.png) [@JLC](https://discourse.mc-stan.org/u/JLC)\
**Post date:** [February 18, 2021, 4:32pm UTC](https://discourse.mc-stan.org/t/simulation-using-brms/20865/2 "2021-02-18T16:32:37Z")

</div>

I typically run a prior only model first and then use the resulting posterior to generate new data (with varying numbers of subjects, trials, etc), to feed into `update.brms`.

I use multiple chains and iterations to create a relatively large posterior from which to draw simulated data.

Something like:

```
Sim_Mod <- brm(Sim_fmla,
               Sim_Data,
               family = binomial(),
               prior = Sim_priors,
               inits = "random",
               iter = 2000,
               warmup = 1000,
               chains = 4,
               cores = ncores,
               sample_prior = "only",               
               save_pars = save_pars(all = TRUE),
               control = list(adapt_delta = 0.99,
                              max_treedepth = 12)
)

```

Then generate simulated data with help from `tidybayes`:

```
Sim_Preds <- add_predicted_draws(
        Sim_Data,
        Sim_Mod,
        prediction = "Prediction",
        n = 500
)

```

---

<div class="post-metadata">

**Author:** ![jgellar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgellar/32/2917_2.png) [@jgellar](https://discourse.mc-stan.org/u/jgellar)\
**Post date:** [February 18, 2021, 7:49pm UTC](https://discourse.mc-stan.org/t/simulation-using-brms/20865/3 "2021-02-18T19:49:45Z")

</div>

Thanks for weighing in, unfortunately that solution doesn’t apply to my use case. I am testing how my model performs under various data-generating processes.

Can you confirm whether or not my suggested solution makes sense, and whether it’s okay to run the initial model with only 1 iteration?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [February 18, 2021, 9:50pm UTC](https://discourse.mc-stan.org/t/simulation-using-brms/20865/4 "2021-02-18T21:50:29Z")

</div>

Its fine to run the initial model for only one or two iterations, since you’re just creating the model to update and not making inferences. One thing to be careful of here is that the default priors will be set using data from the initial model creation and then they don’t get updated when the model is run with new data. So it’s good practice to pre-specify all priors in the model otherwise you get data leakage from the first dataset
