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

Customize reference level for tbl_regression() #628

Closed
tjmeyers opened this issue Aug 19, 2020 · 10 comments
Closed

Customize reference level for tbl_regression() #628

tjmeyers opened this issue Aug 19, 2020 · 10 comments

Comments

@tjmeyers
Copy link

Hi Daniel! Thank you for all your work on gtsummary; it's an amazing tool! One feature I would find useful is to customize the reference level indicator for regression models. That is, instead of printing an emdash for the reference level in a categorical variable, I would like to print "1" in the estimate column and "Reference" in the confidence interval column. Do you have any plans to develop this feature? Thank you.

@ddsjoberg
Copy link
Owner

Hey hey!

Can you give an example model and a resulting table?

I can easily add an option to print Reference instead of an emdash.

@tjmeyers
Copy link
Author

tjmeyers commented Aug 19, 2020

An example from your Table Gallery vignette (this paged helped me a lot):

gt_r1 <- glm(response ~ age + trt, trial, family = binomial) %>%
tbl_regression(exponentiate = TRUE)

gives: https://user-images.githubusercontent.com/35614530/90699607-94294500-e238-11ea-8c58-9215838ec64e.png

I had in mind "1" in the OR column and "Reference" in the 95% CI column, in place of the emdashes.
Thank you!

@ddsjoberg
Copy link
Owner

Ok I see.

I don't have any plans for implementing something like this at the moment. I wrote you a small wrapper that will produce the table you're looking for.

as_gt_tjmeyers <- function(x) {
  # adding either 0 or 1 value as reference 
  ref_value <- ifelse(x$inputs$exponentiate, 1, 0)
  x$table_body$estimate <- 
    ifelse(x$table_body$row_ref== TRUE & is.na(x$table_body$estimate), 
           ref_value, 
           x$table_body$estimate)

  # adding "Reference" to CI ref row
  gtsummary::as_gt(x) %>%
    gt::fmt_missing(columns = vars(ci), rows = row_ref== TRUE & is.na(ci), "Reference")
}

glm(response ~ stage, trial, family = binomial()) %>%
  tbl_regression(exponentiate = TRUE) %>%
  as_gt_tjmeyers()

image

This is a little trickier to implement broadly, because it straddles making updates to the underlying gtsummary object and the printing method--as_gt() in this case.

But we do have a new initiative to support formats for various journals (http://www.danieldsjoberg.com/gtsummary/reference/theme_gtsummary.html). If there is a journal with published reporting guidelines that explicitly asks for reference rows to be reported like this, I can begin to ponder how something like this could be integrated into the package.

Happy Programming! 🕺

@tjmeyers
Copy link
Author

Thank you. This is clever and I'm sure others will find this helpful. I should've mentioned that my preferred printing method is as_flex_table() for MS Word. I'm trying to revise your wrapper accordingly but no luck so far. If you have any advice, that would be great. I'll go ahead and close the issue because I appreciate your time in answering my question.

@ddsjoberg
Copy link
Owner

Hmmm, after you run as_flex_table(), you can access the underlying data set. I think something like this would work for you.

tt <- tbl_summary(trial) %>% as_flex_table()

tt$body$dataset <- 
  tt$body$dataset %>%
  mutate( *add code here to change the reference label* )

# print the table
tt

@tjmeyers
Copy link
Author

I'm able to modify the variables in tt$body$dataset but the table did not contain the updated data.

tt <- glm(response ~ age + trt, trial, family = binomial) %>%
tbl_regression(exponentiate = TRUE) %>%
  as_flex_table()

tt$body$dataset <- 
  tt$body$dataset %>%
  mutate(estimate = ifelse(grepl("NA", p.value), "1.00", estimate)) %>%
  mutate(ci = ifelse(grepl("NA", p.value), "Reference", ci)) %>%
  mutate(p.value = ifelse(grepl("NA", p.value),"", p.value))

tt

@ddsjoberg
Copy link
Owner

The code you are applying to tt$body$dataset doesn't look correct and it doesn't modify the data frame. Check the data frame after you apply your changes

@ddsjoberg
Copy link
Owner

Hi @tjmeyers ,

FYI, I've made updates internally to let you accomplish your goal. Example below:

list("tbl_regression-lgl:add_ref_est" = TRUE,
     "tbl_regression-str:ref_row_text" = "Reference") %>%
  set_gtsummary_theme()

mtcars %>%
  mutate(cyl = factor(cyl)) %>%
  select("am", "hp", "cyl") %>%
  tbl_uvregression(
    method = glm,
    y = am,
    formula = "{y} ~ {x}",
    method.args = list(family = binomial),
    # label = "cyl" ~ "No. Cylinders",
    hide_n = TRUE,
    exponentiate = TRUE
  )

image

The update is in this pull request #606

After the update has gone through code review, it'll be merged into master branch, and eventually released on CRAN

@tjmeyers
Copy link
Author

tjmeyers commented Sep 2, 2020 via email

@ddsjoberg
Copy link
Owner

If you'd like, you can open another issue, and we can eventually add a theme for the journals you submit to that prefer this format. You'd then be able to apply this option, and other options the journal guidelines state, in a single line.

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

2 participants