# Replicating conditional\_effects

**URL:** <https://discourse.mc-stan.org/t/replicating-conditional-effects/34068>\
**Category:** brms\
**Created:** [February 9, 2024, 9:53am UTC](https://discourse.mc-stan.org/t/replicating-conditional-effects/34068 "2024-02-09T09:53:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elef](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/elef/32/4280_2.png) [@Elef](https://discourse.mc-stan.org/u/Elef)\
**Post date:** [February 9, 2024, 9:53am UTC](https://discourse.mc-stan.org/t/replicating-conditional-effects/34068/1 "2024-02-09T09:53:31Z")

</div>

As part of understanding better the (Bayesian) stats I would like to replicate the outcome of the conditional\_effects by hand (ggplot)

By using the following model

```no-highlight
job::job({
fit3 <- brm(time | cens(censored) ~ age * sex + disease + (1|patient),
            data = kidney, family = lognormal(),
       cores = 4, chains = 4, seed = 60224)
})

```

I can get this plot by conditioning on disease == other

```no-highlight
cond <- make_conditions(kidney, vars = c("disease")) %>% filter(disease == 'other')

p_null <- conditional_effects(fit3,
                    effects = 'age:sex',
                    conditions = cond,
                    re_formula = ~(1|patient))

```

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/1/0/102b140f3d94101c3381b9f132be18b69d8ca31c.png)

Now I’m trying to replicate this plot by

1. Creating a grid of data I want to plot (age and sex keeping constant disease == ‘orther’),
2. Adding the epred drwas using re\_formula = ~(1|patient) (as before),
3. Ploting it

The data grid:

```no-highlight
## Function to create a sequence from min to max based on a variable in a data.frame 
seqf <- function(x, var , length.out = 21){
  seq(min(x[, var]), 
      max(x[, var]),
      length.out = 21
      )
}

dat_grid1 <- expand_grid(
         age = seqf(x=kidney, var= 'age'),
         sex = kidney$sex %>% unique %>% sort,
         disease = 'other',
         patient = kidney$patient %>% unique
  )

```

The epred draws and the plot

```no-highlight
dat_grid1 %>%
  add_epred_draws(fit3, re_formula = ~(1|patient), allow_new_levels = F) %>%
  ggplot(aes(x = age , 
             y = .epred, 
             color = sex, group = sex, fill = sex)) +
  stat_lineribbon(.width = 0.0, alpha = 1) +
  stat_ribbon(.width = 0.75, alpha = .3) +
    theme_bw() 

```

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/4/b/4bf50cc5398612bb8cc5d26dda0f66bb78ab65b3.png)

Now, something is wrong, the outcomes are different and “more” annoyingly my plot take 5 times more time to be generated.

Of course,  
My data has ~1600 rows,  
The data from conditional\_effects is like 300 rows - `plot(p_null, plot = F)[[1]]$data ` but I don’t undestand how it can accept the argument `re_formula = ~(1|patient)` when in the data the patient level has `NA`

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/e/9/e9fa60d232912a93d1df3fc803cd447ea245d5de.png)
