# Fitted and predicted draws on average

**URL:** <https://discourse.mc-stan.org/t/fitted-and-predicted-draws-on-average/12680>\
**Category:** brms\
**Created:** [January 17, 2020, 4:45pm UTC](https://discourse.mc-stan.org/t/fitted-and-predicted-draws-on-average/12680 "2020-01-17T16:45:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvanUkhov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ivanukhov/32/6888_2.png) [@IvanUkhov](https://discourse.mc-stan.org/u/IvanUkhov)\
**Post date:** [January 17, 2020, 4:45pm UTC](https://discourse.mc-stan.org/t/fitted-and-predicted-draws-on-average/12680/1 "2020-01-17T16:45:25Z")

</div>

I am struggling to wrap my head around the difference between `fitted` and `predict`. In my case, it is about the multinomial family. There are a few relevant posts here and on GitHub. After reading them and `pp_expect.R` with `posterior_predict.R`, my current understanding is that `fitted` gives draws of the expected value of the posterior distribution, while `predict` gives draws from the posterior distribution. How far am I from the truth? If not too far, should one expect the means of the two to match? If I take the average of the draws from `fitted` and the average of those from `predict`, should they not be the same? In my example, they deviate.

---

<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:** [January 17, 2020, 4:48pm UTC](https://discourse.mc-stan.org/t/fitted-and-predicted-draws-on-average/12680/2 "2020-01-17T16:48:05Z")

</div>

You are correct. The deviation are likely because of random error in the posterior predictions.

---

<div class="post-metadata">

**Author:** ![IvanUkhov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ivanukhov/32/6888_2.png) [@IvanUkhov](https://discourse.mc-stan.org/u/IvanUkhov)\
**Post date:** [January 17, 2020, 7:47pm UTC](https://discourse.mc-stan.org/t/fitted-and-predicted-draws-on-average/12680/3 "2020-01-17T19:47:54Z")

</div>

Thank you for the quick reply. I guess I misuse some of the functions then. Let me give a minimal working example:

```
data <- tribble(
    ~a, ~b, ~c, ~total,
  3000, 3000, 7000, 13000,
)
data$counts <- with(data, cbind(a, b, c))
formula <- brmsformula(counts | trials(total) ~ 1)
fit <- brm(formula, data, multinomial(), seed = 42)

data$counts / data$total
# a b c
# [1,] 0.2307692 0.2307692 0.5384615

colMeans(fitted(fit, summary = FALSE)) / data$total
# a b c
# [1,] 0.2309553 0.2307284 0.5383163

colMeans(predict(fit, summary = FALSE)) / data$total
# a b c
# [1,] 0.1364585 0.2725709 0.5909706

```

---

<div class="post-metadata">

**Author:** ![IvanUkhov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ivanukhov/32/6888_2.png) [@IvanUkhov](https://discourse.mc-stan.org/u/IvanUkhov)\
**Post date:** [January 18, 2020, 6:30am UTC](https://discourse.mc-stan.org/t/fitted-and-predicted-draws-on-average/12680/4 "2020-01-18T06:30:04Z")

</div>

This difference cannot arguably be attributed to chance. I have tried with different seeds. In addition, if it was about chance, it would go away with the sample size.

---

<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:** [January 18, 2020, 9:18am UTC](https://discourse.mc-stan.org/t/fitted-and-predicted-draws-on-average/12680/5 "2020-01-18T09:18:49Z")

</div>

I will take a look. Thanks!

---

<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:** [January 18, 2020, 2:50pm UTC](https://discourse.mc-stan.org/t/fitted-and-predicted-draws-on-average/12680/6 "2020-01-18T14:50:03Z")

</div>

Thanks! It turned out to be a slightly embarassing typo as I wrote `pcategorical` instead of `dcategorical` so accidentally used the cumulative distribution function… Should be fixed now on github.

---

<div class="post-metadata">

**Author:** ![IvanUkhov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ivanukhov/32/6888_2.png) [@IvanUkhov](https://discourse.mc-stan.org/u/IvanUkhov)\
**Post date:** [January 18, 2020, 4:06pm UTC](https://discourse.mc-stan.org/t/fitted-and-predicted-draws-on-average/12680/7 "2020-01-18T16:06:07Z")

</div>

That was fast! Thank you so much!
