Trunc() and conditional_effects()

Thanks @paul.buerkner.

For those interested, the relevant function in the link appears to be posterior_epred_trunc_gaussian(). Here’s how I used elements from that function to replicate the behavior of fitted() by working directly with the posterior samples.

# extract the posterior samples
posterior_samples(m1) %>% 
  # wrangle
  expand(nesting(b_Intercept, b_x, sigma),
         x = seq(from = min(d_t_1e3$x), to = max(d_t_1e3$x), length.out = 30)) %>% 
  mutate(mu = b_Intercept + b_x * x) %>% 
  mutate(lb = -1,
         ub = Inf) %>% 
  mutate(zlb = (lb - mu) / sigma,
         zub = (ub - mu) / sigma) %>% 
  mutate(trunc_zmean = (dnorm(zlb) - dnorm(zub)) / (pnorm(zub) - pnorm(zlb))) %>% 
  mutate(trunc_mu = mu + trunc_zmean * sigma) %>% 
  group_by(x) %>% 
  tidybayes::mean_qi(trunc_mu) %>% 
  
  # plot!
  ggplot(aes(x = x)) +
  geom_point(data = d_t_1e3,
             aes(y = y),
             size = 1/4) +
  geom_ribbon(aes(ymin = .lower, ymax = .upper),
              alpha = 1/4) +
  geom_line(aes(y = trunc_mu),
            color = "blue")

1 Like