Skip to content

Instantly share code, notes, and snippets.

@ddsjoberg
Created January 20, 2021 17:41
Show Gist options
  • Save ddsjoberg/b9bede1218dd82d69503d94c74063329 to your computer and use it in GitHub Desktop.
Save ddsjoberg/b9bede1218dd82d69503d94c74063329 to your computer and use it in GitHub Desktop.
use_significance_stars <- function(x) {
if (!"estimate" %in% names(x$table_body)) return(x)
# extracting old estimate fun
old_est_fun <- switch(
!is.null(x$table_header),
x$table_header %>%
dplyr::filter(column == "estimate") %>%
purrr::pluck("fmt_fun", 1)
)
# updating fun with stars
updated_est_fun <- function(estimate, p.value) {
sty_estimate <- old_est_fun(estimate)
dplyr::case_when(
p.value < 0.001 ~ paste0(sty_estimate, "***"),
p.value < 0.01 ~ paste0(sty_estimate, "**"),
p.value < 0.05 ~ paste0(sty_estimate, "*"),
TRUE ~ sty_estimate
)
}
# browser()
x$table_body$estimate <- updated_est_fun(x$table_body$estimate,
x$table_body$p.value)
x %>%
modify_table_header(
column = "estimate",
fmt_fun = as.character,
footnote = "* p<0.05; ** p<0.01; *** p<0.001"
) %>%
modify_table_header(
column = c("p.value", "ci"),
hide = TRUE
) %>%
modify_table_header(
column = "std.error",
hide = FALSE
)
}
lm(marker ~ grade, trial) %>%
tbl_regression() %>%
use_significance_stars()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment