# Posterior predictions rstan - unintended computations, artifacts

**URL:** <https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379>\
**Category:** General\
**Created:** [February 16, 2022, 6:17pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379 "2022-02-16T18:17:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![marciero](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@marciero](https://discourse.mc-stan.org/u/marciero)\
**Post date:** [February 16, 2022, 6:17pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379/1 "2022-02-16T18:17:31Z")

</div>

I am trying to generate posterior predictions with new data using a separate stan file and just data and quantities blocks. I am using the UCB gender bias example in McElreath’s Lecture 9 2022 video. (Versions of this example appear in the text also, section 11.1.4.) I generated the posterior logit probabilities- 8000 for each of 12 combinations of gender and 6 departments. The model is

Y\_{gid, dept\_id} \sim bernoulli(a(gid, dept\_id))

with the matrix a(i,j) containing the logit probabilities.  
I am generating probabilities of acceptance for each of 4526 “applicants”. So this is merely taking each applicant’s gender and dept and computing the inv\_logit for the 8000 samples in the column of a for corresponding dept/gender. After compiling the code below, I use sample() 1 chain, and 1 iteration So there should be no mcmc and no “sampling” per se. But it is taking several minutes rather than seconds, and generates a 9.5 GB file. My machine bogs down when I try to access the samples.

```nohighlight
data {
   int gid[4526]; // vector of gender ids, 0/1, for each applicant
  int dept_id[4526]; // vector of department ids 1,2,...,6, for each applicant
  matrix[8000, 12] a; // matrix of posterior logit samples for each dept/gender 
}
parameters {
}
model {
}
generated quantities {
  matrix[8000, 4526] y_pred;
  for(n in 1:4526) {
    for(i in 1:8000) {
      y_pred[i, n] = inv_logit(a[i, 2*dept_id[n] - gid[n]]); // "male" for department j is column 2j-1
    }
  }
}

```

I sampled in R using

```nohighlight
sampling(... chains = 1, 
                     iter = 1, 
                     algorithm = 'Fixed_param')

```

What I am trying to do should not be this computationally or data storage intensive. Is there something I am missing regarding the computations going on here?  
Thanks

Mike

---

<div class="post-metadata">

**Author:** ![marciero](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@marciero](https://discourse.mc-stan.org/u/marciero)\
**Post date:** [February 18, 2022, 1:04pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379/2 "2022-02-18T13:04:06Z")

</div>

In fact, this is what the link() function does in the rethinking R package. It takes about a second to do this so there must be inefficiency in my code or it is doing something not intended.

---

<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, 2022, 2:03pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379/3 "2022-02-18T14:03:42Z")

</div>

You say that it shouldn’t be data storage intensive, but you’re iterating and storing 8000 x 4526 = 36,208,000 estimates, which is far from trivial. Are these dimensions correct/necessary?

---

<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, 2022, 2:57pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379/4 "2022-02-18T14:57:05Z")

</div>

For posterior predictive checks for this model, check out this example with `brms`: [11 God Spiked the Integers | Statistical rethinking with brms, ggplot2, and the tidyverse: Second edition](https://bookdown.org/content/4857/god-spiked-the-integers.html#aggregated-binomial-graduate-school-admissions).

---

<div class="post-metadata">

**Author:** ![marciero](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@marciero](https://discourse.mc-stan.org/u/marciero)\
**Post date:** [February 18, 2022, 3:06pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379/5 "2022-02-18T15:06:10Z")

</div>

Thanks for the reference. That is the example in 11.1.4, though I did not see the counterfactual analysis in the book so may not be here. It was in the video. Essentially I am trying reverse-engineer what the link() function does in R or stan. This reference by Kurz is essentially that for brms. The idea was to use the model posterior samples arising from the actual data to generate stratified/counterfactual predictions- in this case pretending all 4526 applicants were male, with number of applicants to each department equal to total applicants in that department in the actual data. Hence the 4526 predictions. I’ll certainly check the Kurz reference for guidance.

---

<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, 2022, 3:14pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379/6 "2022-02-18T15:14:48Z")

</div>

For your actual example, the `link()` function is faster than your implementation because it’s not operating on all of the posterior samples like you are. The link function generates predictions at each iteration for the given model specification.

This means that you instead need to only iterate over the posterior distributions for the coefficients, generating your model predictions by specifying the covariate value of interest (i.e. male). It’s also in this same step that you would apply the inverse-logit function

---

<div class="post-metadata">

**Author:** ![marciero](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@marciero](https://discourse.mc-stan.org/u/marciero)\
**Post date:** [February 18, 2022, 3:25pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379/7 "2022-02-18T15:25:55Z")

</div>

Thanks @andrjohns for the insights! I am a little unclear though on how iterating “over the posterior distributions for the coefficients” would not involve iterating over each of the posterior samples. Maybe I just need to reread carefully a few times?

---

<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, 2022, 3:33pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379/8 "2022-02-18T15:33:11Z")

</div>

Ah I see what you mean, I missed the counterfactual aspect! This section has a counterfactual example that might be helpful: [11 God Spiked the Integers | Statistical rethinking with brms, ggplot2, and the tidyverse: Second edition](https://bookdown.org/content/4857/god-spiked-the-integers.html#cb1347)

---

<div class="post-metadata">

**Author:** ![marciero](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@marciero](https://discourse.mc-stan.org/u/marciero)\
**Post date:** [February 18, 2022, 3:38pm UTC](https://discourse.mc-stan.org/t/posterior-predictions-rstan-unintended-computations-artifacts/26379/9 "2022-02-18T15:38:55Z")

</div>

Thanks again will check that out!
