marginal_effects and offset

82 views
Skip to first unread message

Lukasz Wiklendt

unread,
Apr 9, 2018, 8:05:46 AM4/9/18
to brms-users
Is there a way to remove the offset from the marginal_effects function (or fitted function)?
The following code does a simple regression with poisson family with log-rate of 0.5 + 2 * a + b, with a in {0, 1} and b some random offset:

library(brms)
library
(tidyverse)

set.seed(180409)

icpt
= 0.5
a
= 2

n
= 100
df
= data_frame(a = rbinom(n, 1, 0.5))
df$b
= exp(rnorm(n, mean = 1, sd = 0.5))
df$y
= rpois(n, exp(icpt + a * df$a + log(df$b)))

df$a
= factor(df$a)

if (!exists('fit')) {
  fit
= brm(bf(y ~ 1 + a + offset(log(b)), family=poisson(link='log')), df)
}

plot
(marginal_effects(fit, scale='linear', method='fitted'))

Checking the fit shows the correct estimates:

Population-Level Effects:
          Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
Intercept     0.51      0.06     0.39     0.62       1156 1.00
a1            2.01      0.06     1.90     2.14       1267 1.00

but I can't figure out how to plot the marginal_effects without an offset being reintroduced, which shows estimates at 1.7 and 3.7 instead of 0.5 and 2.5 [noting that log(mean(df$b)) is 1.2].

Lukasz Wiklendt

unread,
Apr 9, 2018, 5:26:26 PM4/9/18
to brms-users
For posterity, the solution is to include the offset as if it is a predictor in the conditions argument of marginal_effects. E.g.

plot(marginal_effects(fit, scale='linear', method='fitted',
                      conditions
=data_frame(b=1)))

Paul Buerkner

unread,
Apr 10, 2018, 4:01:14 AM4/10/18
to Lukasz Wiklendt, brms-users
That is indeed the solution. If you want to achieve the same thing in fitted, predict, etc. use argument newdata.

--
You received this message because you are subscribed to the Google Groups "brms-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brms-users+unsubscribe@googlegroups.com.
To post to this group, send email to brms-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brms-users/71cef20d-d65e-4514-8896-737da8f3ec07%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages