Fitted values differ between fit() and marginal_effects ()

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 ~ accessibleinaccessiblegroup + (1+accessibleinaccessible|subject) + (1+accessibleinaccessible*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.

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.

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)

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.