# How to add customized constraints when making predictions using posterior\_predict

**URL:** <https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677>\
**Category:** brms\
**Tags:** brms\
**Created:** [April 3, 2021, 3:47pm UTC](https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677 "2021-04-03T15:47:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![YidaZhang0628](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yidazhang0628/32/10105_2.png) [@YidaZhang0628](https://discourse.mc-stan.org/u/YidaZhang0628)\
**Post date:** [April 3, 2021, 3:47pm UTC](https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677/1 "2021-04-03T15:47:37Z")

</div>

Hi, below is a simple hierarchical Bayes model that I want to fit. Y\_ij here can be considered as the expression of gene i in cell j measured by technology 1 and x\_ij is the expression measured by technology 2. Y\_ij is count data so I use a ZINB distribution to model it. X\_ij is relative proportion so it is between 0 and 1. Cell size is used as an offset.

 ![Screen Shot 2021-04-03 at 11.07.03 AM](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/3/39db710ce125e4cc0d4b05cf9804feedab4727e2.png)

The key part of the model is the alpha\_i, which represents distortion between two technologies. It is simply a group-level effect, i.e., for all cells from the same gene, they are distorted in the same way. Different genes are distorted in different ways.

Eventually, what I want is that for a new gene (let’s say we measured its expression in 10 cells), I can predict its expression in technology 1 based on its expression in technology 2. And this requires proper prediction/sampling of distortion (alpha\_i) for this new gene. In my case, I want the sampling to work this way:  
(1) when making a prediction of this gene for all 10 cells, I want all the cells to have the same level of distortion, i.e., alpha\_i to be the same for these 10 cells instead of sampling one alpha\_i for each cell.  
(2) if this gene is in the training data, then it means that it has an estimated alpha\_i already. My second constraint is that when making prediction for a gene that is already seen in the training data, instead of randomly sample a new alpha\_i for it, I want to use the same alpha\_i estimated during training.  
(3) In reality, the distortion of each gene may not be random. For example, longer genes may have higher alpha\_i than shorter genes. There may not be a strong correlation between gene length and distortion level so I don’t plan to include this in the model. But when making predictions, I want to assign a higher alpha\_i for a longer gene instead of completely random sampling alpha\_i from its posterior distribution.

My first question is that can posterior\_predict achieve the three constraints when making predictions for new data? In general, can I control how the group-level effect is sampled when making predictions for new data?

My understanding is that for (1) and (2), the default posterior\_predict can already do it. But when I looked at the description of prepare\_predictions, I feel like the default posterior\_predict will sample alpha\_i for each cell separately even for the same gene. And even for a gene that is seen in the training data, when making predictions, it will sample a new alpha\_i instead of using the alpha\_i estimated during training. Is this true? In general, how does posterior\_predict work in terms of sampling new group-level effects?

I tried to look into the source code of the posterior\_predict function. I found the R script of posterior\_predict.R on Github but it doesn’t contain the details. I am trying to write my own code to do this. Here is a description of the procedure:

If we want to make a prediction of a new gene with 10 cells:  
(1) the input is x\_ij and cell size j  
(2) sample alpha\_i from its posterior distribution. Since I am doing it as an independent step, I can control the sampling. For example, I can use the same alpha\_i during training if this gene is in the training set.  
(3) randomly sample other parameters like alpha, theta, and pie from their posterior distribution.  
(4) plug all these values into the model and randomly draw count from the ZINB distribution.

I don’t know much about MCMC sampling so I am doing the prediction directly based on the model. Can anyone tell me if this is correct?

Thank you!

---

<div class="post-metadata">

**Author:** ![YidaZhang0628](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yidazhang0628/32/10105_2.png) [@YidaZhang0628](https://discourse.mc-stan.org/u/YidaZhang0628)\
**Post date:** [April 4, 2021, 5:34pm UTC](https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677/2 "2021-04-04T17:34:03Z")

</div>

A quick version of the post if you don’t have time:

When making posterior predictive prediction on new data using a fitted model with group level effect, is there any way to control how the group level effect is sampled? Can I add constraint to it instead of pure random sampling from posterior predictive distribution?

@paul.buerkner

---

<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:** [April 4, 2021, 6:08pm UTC](https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677/3 "2021-04-04T18:08:44Z")

</div>

some options are documented in ?prepare\_predictions

perhaps that helps already.

---

<div class="post-metadata">

**Author:** ![YidaZhang0628](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yidazhang0628/32/10105_2.png) [@YidaZhang0628](https://discourse.mc-stan.org/u/YidaZhang0628)\
**Post date:** [April 4, 2021, 7:08pm UTC](https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677/4 "2021-04-04T19:08:59Z")

</div>

Thanks for the reply!

Yes I have checked the document of prepare\_predictions. Based on my understanding, what is related to my question is the “sample\_new\_levels” parameter. If I understand correctly, “uncertainty” means that samples from the same new level are randomly selected and they can be from different existing levels (also randomly selected). On the contrary, “old\_levels” means that samples from the same new level are randomly selected from the same existing level and this existing level is randomly selected from all existing levels. Is this correct? If yes, then they cannot do what I want to do here. My goal is for samples from the same new level, they are randomly selected from the same existing level but this existing level is not randomly selected from all existing levels. This existing level is selected based on some constraints. For example, if the new level is already seen in the training data, then the same group level effect estimated during training is selected as the “existing level” in prediction (not randomly sample from existing levels). I was wondering if there is any way to achieve this?

Thank you!

---

<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:** [April 5, 2021, 6:47am UTC](https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677/5 "2021-04-05T06:47:16Z")

</div>

I see. This is unfortunately not yet possible in brms.

---

<div class="post-metadata">

**Author:** ![YidaZhang0628](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yidazhang0628/32/10105_2.png) [@YidaZhang0628](https://discourse.mc-stan.org/u/YidaZhang0628)\
**Post date:** [April 5, 2021, 3:57pm UTC](https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677/6 "2021-04-05T15:57:44Z")

</div>

Sure. I am thinking about implementing it myself. I saw from [this post](https://discourse.mc-stan.org/t/sampling-from-the-posterior-predictive-distribution/1678) that for making predictions on new data, I can sample values for each parameter (in my case they are alpha, alpha\_i, theta, and pie) from their posterior distribution respectively and then sample yrep from the ZINB distribution with sampled alpha, alpha\_i, theta, and pie. Is this how the posterior\_predict function works? If yes, then I can add the constraints to the first step to customize the sampling process. I tried to look for the source code of posterior\_predict but the posterior\_predict.R file on Github doesn’t include all the details…

---

<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:** [April 5, 2021, 3:58pm UTC](https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677/7 "2021-04-05T15:58:45Z")

</div>

This is basically how the posterior\_predict function works, yes.

---

<div class="post-metadata">

**Author:** ![YidaZhang0628](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yidazhang0628/32/10105_2.png) [@YidaZhang0628](https://discourse.mc-stan.org/u/YidaZhang0628)\
**Post date:** [April 5, 2021, 3:59pm UTC](https://discourse.mc-stan.org/t/how-to-add-customized-constraints-when-making-predictions-using-posterior-predict/21677/8 "2021-04-05T15:59:17Z")

</div>

Thanks!
