-1

When making a regression table using {gtsummary}, p-value can be added through add_glance_table(p.value):

  library(gtsumary)
  trial %>%
  na.exclude() %>%
  lm(ttdeath ~ age + marker + response,.) %>%
  tbl_regression() %>%
  add_glance_table(p.value)

enter image description here

I would like to add significance star(s) to the p-value in glance table. I have tried add_significance_stars(), but it only replaces the original p-values created by tbl_regression() to the stars.

enter image description here

Is there any way I can put significance stars on p-values in the glance table?

Edit: I would like to apologize my initial question which lacked information and clarity. Edited for more information.

r_noobie
  • 127
  • 6
  • I'm voting to reopen this now that you've made it more complete, but a sample of data would make it better – camille Dec 31 '21 at 15:37
  • I have voted to leave this question closed as it is still unclear exactly where you want the p-values to appear. It is also unclear what you mean by "original p-value". Do you mean overall significance? [From Review](https://stackoverflow.com/review/reopen/30711131) – Ian Campbell Dec 31 '21 at 16:41
  • This is a clear question specifically asking about the p-value added from the `add_glance_table()` function (the p-value at the bottom of the table). The post has also been updated to include fully executable code using the trial dataset that comes from the gtsummary package. Please vote to re-open. – Daniel D. Sjoberg Dec 31 '21 at 17:44

1 Answers1

1

The add_significance_stars() function can add stars to the model covariate p-values. We can add a function to format the model p-value with significance stars as well. Example below

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.0'

# write a function to round p-values and add stars
style_pvalue_stars <- function(x) {
  dplyr::case_when(
    x < 0.001 ~ paste0(style_pvalue(x), "***"),
    x < 0.01 ~ paste0(style_pvalue(x), "**"),
    x < 0.05 ~ paste0(style_pvalue(x), "*"),
    TRUE ~ style_pvalue(x)
  )
}

style_pvalue_stars(c(0.0001, 0.04, 0.50))
#> [1] "<0.001***" "0.040*"    "0.5"

tbl <-
  lm(ttdeath ~ age + marker + response, trial) %>%
  tbl_regression() %>%
  # add significance stars to the covariate p-values
  add_significance_stars(pattern = "{p.value}{stars}", 
                         hide_ci = FALSE, hide_p = FALSE) %>%
  add_glance_table(p.value) %>%
  # add stars to the model p-value
  modify_fmt_fun(
    update = estimate ~ style_pvalue_stars,
    rows = row_type == "glance_statistic" & label == "p-value"
  )

enter image description here Created on 2022-01-01 by the reprex package (v2.0.1)

Happy Programming!

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28