Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to exponentiate model coefficients with brms models #1568

Closed
pgseye opened this issue Nov 8, 2023 · 4 comments
Closed

How to exponentiate model coefficients with brms models #1568

pgseye opened this issue Nov 8, 2023 · 4 comments

Comments

@pgseye
Copy link

pgseye commented Nov 8, 2023

I have started dipping my toes into Bayesian models and as I use tbl_regression for presenting all of my (frequentist) model results, naturally tried to do the same with the brm model. It works to present the coefficient but is there a way to exponentiate them if required (e.g. a logistic model)

If I try this I get:
! exponentiate = TRUE is not valid for this type of model and was ignored.

@larmarange
Copy link
Collaborator

Are you referring to brm package or brms?

Could you provide a reproductive example?

@pgseye
Copy link
Author

pgseye commented Nov 8, 2023

Thanks - I think I have managed to put together a small example and supporting code:

small_dat <- structure(list(id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
                                   11L, 12L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 18L, 19L, 20L, 21L, 
                                   22L, 23L, 24L, 25L, 26L, 27L, 27L, 27L, 27L, 28L, 29L, 30L, 31L, 
                                   32L, 33L, 33L, 34L, 35L, 36L, 36L, 37L, 38L, 39L, 39L, 40L, 41L, 
                                   42L), x = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                         2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                         2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 
                                                         2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L), levels = c("low", "high"), class = "factor"), 
                            y = c(1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 
                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 
                                                                                            -50L), class = c("tbl_df", "tbl", "data.frame"))


library(lme4)
library(brms)
library(gtsummary)
mod <- glmer(y ~ x + (1|id), data = small_dat, family = "binomial")
summary(mod)
tbl_regression(mod, exp = T) # this exponentiates

mod2 <- brm(y ~ x + (1|id), data = small_dat, family = "bernoulli")
summary(mod2)
tbl_regression(mod2, exp = T) # this doesn't exponentiate

@larmarange
Copy link
Collaborator

First, the correct syntax is tbl_regression(mod2, exponentiate = TRUE).

Second, broom.mixed fails to tidy mod2. There is therefore a bug to be reported in this package: https://github.com/bbolker/broom.mixed/issues

> rlang::last_trace()
<error/dplyr:::mutate_error>
Error in `mutate()`:
ℹ In argument: `exp`.
Caused by error:
! `exp` must be a vector, not a primitive function.
---
Backtrace:
     ▆
  1. ├─broom.mixed::tidy(mod2, exponentiate = TRUE)
  2. ├─broom.mixed:::tidy.brmsfit(mod2, exponentiate = TRUE)
  3. │ └─out %>% mutate(across(contains(vv)), exp) %>% ...
  4. ├─dplyr::mutate(., across(std.error, ~. * estimate))
  5. ├─dplyr::mutate(., across(contains(vv)), exp)
  6. ├─dplyr:::mutate.data.frame(., across(contains(vv)), exp)
  7. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
  8. │   ├─base::withCallingHandlers(...)
  9. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
 10. │     └─mask$eval_all_mutate(quo)
 11. │       └─dplyr (local) eval()
 12. └─dplyr:::dplyr_internal_error("dplyr:::mutate_not_vector", `<named list>`)
Run rlang::last_trace(drop = FALSE) to see 5 hidden frames.

@ddsjoberg
Copy link
Owner

Thank you @pgseye and @larmarange !

@pgseye please re-open this issue of there is anything more to attend to after the fix in broom.mixed is live.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants