# Use of \`sample\_prior='only'\` in brms

**URL:** <https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339>\
**Category:** brms\
**Created:** [May 28, 2018, 1:41pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339 "2018-05-28T13:41:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [May 28, 2018, 1:41pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/1 "2018-05-28T13:41:20Z")

</div>

(Using brms v2.3.0)

Hi,

If I fit a model using `sample_prior='only'`, i.e.,

> fit ← brm(formula = tp ~ 1 + technique + category + subject,  
> data = d,  
> prior = c(set\_prior(“normal(0,1)”, class=“Intercept”),  
> set\_prior(“normal(0,1)”, class=“b”)),  
> family = poisson(link=log),  
> sample\_prior = “only” #To ignore the likelihood and check if the model is sane  
> )

I would expect that I later could do:  
`ppc_dens_overlay(y = d$tp, yrep = posterior_predict(fit, draws=25))`

but unfortunately, I get:  
`Error in validate_yrep(yrep, y) : NAs not allowed in 'yrep'.`

Fake data to reproduce this can be found here:

> **[data.csv](https://www.dropbox.com/s/9s99krkhpzjznaj/data.csv?dl=0)**
>
> Shared with Dropbox

I have a nagging feeling I’ve completely missed something here…

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 28, 2018, 5:06pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/2 "2018-05-28T17:06:10Z")

</div>

I do think that approach should work in general. Is posterior\_predict returning NA’s? If the error message is right then it seems like there are NA predictions when there shouldn’t be. If there are NAs, are there just a few or are all the predictions NA? If not then it could be a bayesplot issue (incorrect error message).

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [May 28, 2018, 5:10pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/3 "2018-05-28T17:10:08Z")

</div>

Quite many NAs…

> yrep\_fit\_posterior ← posterior\_predict(fit\_prio\_only)  
> There were 47 warnings (use warnings() to see them)  
> sum(is.na(yrep\_fit\_posterior))  
> [1] 7536  
> length(yrep\_fit\_posterior)  
> [1] 280000

But none when I disregard `sample_prior='only`

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [May 28, 2018, 5:11pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/4 "2018-05-28T17:11:02Z")

</div>

I just tested it and think I found the reason. The priors are so wide that they imply large numbers which cause `exp(<larger number>)` to overflow causing NAs to be predicted.

As a side issue it should be `nsamples` not `draws`, but that is not the problem here.

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [May 28, 2018, 5:12pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/5 "2018-05-28T17:12:39Z")

</div>

The `nsamples` I got to know about just a few minutes back by reading the docs ;)

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [May 28, 2018, 5:20pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/6 "2018-05-28T17:20:05Z")

</div>

You mean that my N(0,1) and cauchy(0,1) are too wide? And I thought they were fairly tight…

---

<div class="post-metadata">

**Author:** ![Zachary\_Horne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/zachary_horne/32/5830_2.png) [@Zachary\_Horne](https://discourse.mc-stan.org/u/Zachary_Horne)\
**Post date:** [May 28, 2018, 5:56pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/7 "2018-05-28T17:56:49Z")

</div>

I’m also confused that you said normal(0,1) is wide

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [May 28, 2018, 7:31pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/8 "2018-05-28T19:31:21Z")

</div>

Sorry, I should have put that into more context. The scales of the predictors are so large that the chosen priors lead to very a large prior on the intercept, which is parameterised in a special way. See `?set_prior` for details. I see the following output:

```
Population-Level Effects: 
          Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
Intercept -0.13 18.23 -36.15 35.96 4000 1.00
technique -0.02 1.02 -1.99 2.01 4000 1.00
category -0.01 1.00 -1.96 1.95 4000 1.00
subject 0.01 1.00 -2.01 1.98 4000 1.00

```

Thus, the intercept is the problem. You may put a prior on the intercept directly via

```
fit2 <- brm(formula = tp ~ 0 + intercept + technique + category + subject,
           data = dat,
           prior = set_prior("normal(0,1)", class="b"),
           family = poisson(link=log),
           sample_prior = "only" #To ignore the likelihood and check if the model is sane
)

```

In this case, the `normal(0, 1)` prior really affects the intercept instead of the transformed intercept.

---

<div class="post-metadata">

**Author:** ![Zachary\_Horne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/zachary_horne/32/5830_2.png) [@Zachary\_Horne](https://discourse.mc-stan.org/u/Zachary_Horne)\
**Post date:** [May 28, 2018, 7:34pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/9 "2018-05-28T19:34:05Z")

</div>

This is helpful. Thank you

---

<div class="post-metadata">

**Author:** ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)\
**Post date:** [May 28, 2018, 7:44pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/10 "2018-05-28T19:44:23Z")

</div>

@paul.buerkner already solved the problem with the intercept, but before that I was thinking about this

> [@torkar](#):
>
> You mean that my N(0,1) and cauchy(0,1) are too wide?

For poisson(link=log) they can be too wide, and compared to your data they seem to be too wide. If I read your model right, the intended model has four additive terms for log intensity with each having a prior N(0,1), and thus the prior for the log intensity would be N(0,4^2), which corresponds to assuming that there is 5% probability that the intensity is larger than 720. This may or may not be against your prior information, but at least it seems to be 36 times larger than the largest count in your data. See also @anon75146577’s [blog post](http://andrewgelman.com/2018/04/03/justify-my-love/)

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [May 28, 2018, 8:06pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/11 "2018-05-28T20:06:19Z")

</div>

Thanks @paul.buerkner and @avehtari, much clearer now and much appreciated. And thanks to @jonah et al. for Bayesplot!

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [May 29, 2018, 8:37am UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/12 "2018-05-29T08:37:07Z")

</div>

It might be worth adding that the model @paul.buerkner provides in his answer still generates NAs when running `posterior_predict()`. However, as indicated by Paul and @avehtari, setting the prior to, e.g., `set_prior("normal(0,0.1)", class="b")`, removes the NAs.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [May 29, 2018, 9:13pm UTC](https://discourse.mc-stan.org/t/use-of-sample-prior-only-in-brms/4339/13 "2018-05-29T21:13:15Z")

</div>

> [@avehtari](#):
>
> This may or may not be against your prior information, but at least it seems to be 36 times larger than the largest count in your data. See also @Daniel\_Simpson’s [blog post](http://andrewgelman.com/2018/04/03/justify-my-love/)

Also related to the prior predictive checking section of the [visualization paper](https://arxiv.org/abs/1709.01449) (Dan Simpson is everywhere)
