# Fitted values differ between fit() and marginal\_effects ()

**URL:** <https://discourse.mc-stan.org/t/fitted-values-differ-between-fit-and-marginal-effects/8975>\
**Category:** brms\
**Created:** [May 24, 2019, 5:26pm UTC](https://discourse.mc-stan.org/t/fitted-values-differ-between-fit-and-marginal-effects/8975 "2019-05-24T17:26:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HirokiYamamoto2531](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/hirokiyamamoto2531/32/4907_2.png) [@HirokiYamamoto2531](https://discourse.mc-stan.org/u/HirokiYamamoto2531)\
**Post date:** [May 24, 2019, 5:26pm UTC](https://discourse.mc-stan.org/t/fitted-values-differ-between-fit-and-marginal-effects/8975/1 "2019-05-24T17:26:51Z")

</div>

Hi,

I recently used fitted () to extract fitted values from brms but it seems that the values I get using fitted () differs from the ones I get using marginal\_effects ()

Below is what I did to get fitted values.

rt\_g = seq(from = 122, to = 4546, length.out = 3432)  
…response time, dependent variable

accessible\_g = rep(c(-1, -1, 1, 1), 858)  
inaccessible\_g = rep(c(-1, 1, -1, 1), 858)  
group\_g = rep(c(-1, -1, -1, -1, 1, 1, 1, 1), 429)  
…these are independent variables, each of which contains two levels (-1 or 1)

subject\_g = rep(c(“Native 1”,“Native 2”,“Native 3”,“Native 4”,“Native 5”,“Native 6”,“Native 7”,“Native 8”,“Native 9”,“Native 10”,“Native 11”,“Native 12”,“Native 13”,“Native 14”,“Native 15”,“Native 16”,“Native 17”,“Native 18”,“Native 19”,“Native 20”,“Native 21”,“Native 22”,“Native 23”,“Native 24”,“Native 25”,“Native 26”,“Native 27”,“Native 28”,“Native 29”,“Native 30”,“Native 31”,“Native 32”,“Native 33”,“Native 34”,“Native 35”,“Native 36”,“Native 37”,“Native 38”,“Native 39”,“Native 40”,“Native 41”,“Native 42”,“Native 43”,“Native 44”,“Native 45”,“Native 46”,“Native 47”,“Native 48”,“Native 49”,“Native 50”,“Native 51”,“Native 52”,“Native 53”,“Native 54”,“Native 55”,“Native 56”,“Native 57”,“Native 58”,“Native 59”,“Native 60”,“Native 61”,“Native 62”,“Native 63”,“Native 64”,“Native 65”,“Native 66”,“Native 67”,“Native 68”,“Native 69”,“Native 70”,“Native 71”,“Native 72”, “Non-Native 1”,“Non-Native 2”,“Non-Native 3”,“Non-Native 4”,“Non-Native 5”,“Non-Native 6”,“Non-Native 7”,“Non-Native 8”,“Non-Native 9”,“Non-Native 10”,“Non-Native 11”,“Non-Native 12”,“Non-Native 13”,“Non-Native 14”,“Non-Native 15”,“Non-Native 16”,“Non-Native 17”,“Non-Native 18”,“Non-Native 19”,“Non-Native 20”,“Non-Native 21”,“Non-Native 22”,“Non-Native 23”,“Non-Native 24”,“Non-Native 25”,“Non-Native 26”,“Non-Native 27”,“Non-Native 28”,“Non-Native 29”,“Non-Native 30”,“Non-Native 31”,“Non-Native 32”,“Non-Native 33”,“Non-Native 34”,“Non-Native 35”,“Non-Native 36”,“Non-Native 37”,“Non-Native 38”,“Non-Native 39”,“Non-Native 40”,“Non-Native 41”,“Non-Native 42”,“Non-Native 43”,“Non-Native 44”,“Non-Native 45”,“Non-Native 46”,“Non-Native 47”,“Non-Native 48”,“Non-Native 49”,“Non-Native 50”,“Non-Native 51”,“Non-Native 52”,“Non-Native 53”,“Non-Native 54”,“Non-Native 55”,“Non-Native 56”,“Non-Native 57”,“Non-Native 58”,“Non-Native 59”,“Non-Native 60”,“Non-Native 61”,“Non-Native 62”,“Non-Native 63”,“Non-Native 64”,“Non-Native 65”,“Non-Native 66”,“Non-Native 67”,“Non-Native 68”,“Non-Native 69”,“Non-Native 70”,“Non-Native 71”), 24)  
…143 individual subjects

item\_g = rep(1:24, each = 143)  
…24 items

nd = tibble(rt = rt\_g, accessible = accessible\_g, inaccessible = inaccessible\_g, group = group\_g, subject = subject\_g, item = item\_g)

ref\_g = fitted(ref\_model, newdata = nd) %\>%  
as\_tibble() %\>%  
bind\_cols(nd)  
…ref\_model contains the results from brms, which is

ref\_model = brm(rt ~ accessible_inaccessible_group + (1+accessible_inaccessible|subject) + (1+accessible_inaccessible\*group|item), ref, family = lognormal(), prior = priors, cores = 4, warmup = 2000, iter = 4000, chains = 4, control = list(adapt\_delta = 0.99))

Below is what I did to plot a three-way interaction using marginal\_effects

conditions = make\_conditions(ref\_model, “group”)  
marginal\_effects(ref\_model, “accessible:inaccessible”, conditions = conditions)

As long as I see the graph plotted by marginal\_effects (), the patterns are similar but the values seem to be different from the fitted values returned by fitted().

Could anyone give me any advice?

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:** [May 27, 2019, 7:24pm UTC](https://discourse.mc-stan.org/t/fitted-values-differ-between-fit-and-marginal-effects/8975/2 "2019-05-27T19:24:00Z")

</div>

That’s possibly because in `fitted` the `robust` argument defaults to `FALSE` while in `marginal_effects` it defaults to `TRUE`. Otherwise, they will return the same values for the same input data (except for some time-series models) `marginal_effects` just calls `fitted` under the hood.

---

<div class="post-metadata">

**Author:** ![HirokiYamamoto2531](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/hirokiyamamoto2531/32/4907_2.png) [@HirokiYamamoto2531](https://discourse.mc-stan.org/u/HirokiYamamoto2531)\
**Post date:** [May 28, 2019, 5:06pm UTC](https://discourse.mc-stan.org/t/fitted-values-differ-between-fit-and-marginal-effects/8975/3 "2019-05-28T17:06:49Z")

</div>

Thank you for the reply.

I included “robust = F” like below but the values are still slightly different.  
Did I miss something?

ref\_g =

- fitted(ref\_model, newdata = nd, robust = T) %\>%
- as\_tibble() %\>%
- bind\_cols(nd)

---

<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, 2019, 5:10pm UTC](https://discourse.mc-stan.org/t/fitted-values-differ-between-fit-and-marginal-effects/8975/4 "2019-05-28T17:10:57Z")

</div>

I don’t know. I would recomment comparing the data frame you put into `fitted` to the one you obtain when looking at

```
me <- marginal_effects(...)
me[[1]]
# etc.

```
