Update: There are no weights w
that give the correct estimate of the marginal effect with avg_slopes(m_agg, wts = w)
.
You can use one of these two calculations instead.
# Model `m_agg` uses weights. In the original, un-collapsed dataset
# each observation is a subset of sample size of 1.
# avg_slopes(m_agg, newdata = data_01s %>% mutate(n = 1))
#> Term Estimate 2.5 % 97.5 %
#> x 0.000256 0.000205 0.000297
# The sample sizes `n` get in the way of the `posterior_epred` calculation.
# So "turn those off" by setting `n = 1` and use the weights argument instead.
avg_slopes(m_agg, newdata = data_agg %>% mutate(n = 1), wts = data_agg$n)
#> Term Estimate 2.5 % 97.5 %
#> x 0.000256 0.000205 0.000297
More details in this marginaleffects
issue which I submitted (unnecessarily, it turns out as there is no bug).