Skip to contents

Add significance stars to estimates with small p-values

Usage

add_significance_stars(
  x,
  pattern = ifelse(inherits(x, c("tbl_regression", "tbl_uvregression")),
    "{estimate}{stars}", "{p.value}{stars}"),
  thresholds = c(0.001, 0.01, 0.05),
  hide_ci = TRUE,
  hide_p = inherits(x, c("tbl_regression", "tbl_uvregression")),
  hide_se = FALSE
)

Arguments

x

(gtsummary)
A 'gtsummary' object with a 'p.value' column

pattern

(string)
glue-syntax string indicating what to display in formatted column. Default is "{estimate}{stars}" for regression summaries and "{p.value}{stars}" otherwise. A footnote is placed on the first column listed in the pattern. Other common patterns are "{estimate}{stars} ({conf.low}, {conf.high})" and "{estimate} ({conf.low} to {conf.high}){stars}"

thresholds

(numeric)
Thresholds for significance stars. Default is c(0.001, 0.01, 0.05)

hide_ci

(scalar logical)
logical whether to hide confidence interval. Default is TRUE

hide_p

(scalar logical)
logical whether to hide p-value. Default is TRUE for regression summaries, and FALSE otherwise.

hide_se

(scalar logical)
logical whether to hide standard error. Default is FALSE

Value

a 'gtsummary' table

Examples

tbl <-
  lm(time ~ ph.ecog + sex, survival::lung) |>
  tbl_regression(label = list(ph.ecog = "ECOG Score", sex = "Sex"))

# Example 1 ----------------------------------
tbl |>
  add_significance_stars(hide_ci = FALSE, hide_p = FALSE)
Characteristic Beta1 SE2 95% CI2 p-value
ECOG Score -58** 19.1 -96, -21 0.003
Sex 52 27.9 -2.5, 107 0.061
1 *p<0.05; **p<0.01; ***p<0.001
2 SE = Standard Error, CI = Confidence Interval
# Example 2 ---------------------------------- tbl |> add_significance_stars( pattern = "{estimate} ({conf.low}, {conf.high}){stars}", hide_ci = TRUE, hide_se = TRUE ) |> modify_header(estimate = "**Beta (95% CI)**") |> modify_footnote(estimate = "CI = Confidence Interval", abbreviation = TRUE)
Characteristic Beta (95% CI)1,2
ECOG Score -58 (-96, -21)**
Sex 52 (-2.5, 107)
1 *p<0.05; **p<0.01; ***p<0.001
2 CI = Confidence Interval
# Example 3 ---------------------------------- # Use ' \n' to put a line break between beta and SE tbl |> add_significance_stars( hide_se = TRUE, pattern = "{estimate}{stars} \n({std.error})" ) |> modify_header(estimate = "**Beta \n(SE)**") |> modify_footnote(estimate = "SE = Standard Error", abbreviation = TRUE) |> as_gt() |> gt::fmt_markdown(columns = everything()) |> gt::tab_style( style = "vertical-align:top", locations = gt::cells_body(columns = label) )
Characteristic Beta
(SE)
1,2
ECOG Score -58.1552930554511** (19.0504299286733)
Sex 52.4310811827553 (27.8957178715894)
1 *p<0.05; **p<0.01; ***p<0.001
2 SE = Standard Error
# Example 4 ---------------------------------- lm(marker ~ stage + grade, data = trial) |> tbl_regression() |> add_global_p() |> add_significance_stars( hide_p = FALSE, pattern = "{p.value}{stars}" )
Characteristic Beta SE1 p-value2
T Stage

0.2
    T1
    T2 0.36 0.168
    T3 0.23 0.183
    T4 0.13 0.171
Grade

0.047*
    I
    II -0.36 0.150
    III -0.08 0.150
1 SE = Standard Error
2 *p<0.05; **p<0.01; ***p<0.001