rlogistic-regressiongtsummarysignificant-digits

Format and significant figures of p-values in tbl_regression


I'm using tbl_regression to generate a table of results for a multiple logistic regression model. However, I'm running into trouble formatting the p-values.

When I run this code:

t1 <- tbl_regression(model, tidy_fun = tidyWaldCI, exponentiate = TRUE) %>%
  bold_p()

The p-values are only to one decimal point yet tiny p-values are formatted to 3 decimal points (<0.001)

However, I tried this code to get everything to 3 decimal places yet now the small p-values are just 0.000:

t1 <- tbl_regression(model, tidy_fun = tidyWaldCI, exponentiate = TRUE, pvalue_fun = ~style_sigfig(., digits = 3)) %>%
  bold_p()

How do I get the output to show 3 decimal places for all p-values above 0.001 but show "<0.001" for small p-values?


Solution

  • The function you passed in the pvalue_fun argument just rounds to 3 significant digits. To get proper p-value formatting you can pass a function that will append "<" to small p-values. Example below

    library(gtsummary)
    
    lm(mpg ~ factor(cyl) + am, mtcars) |> 
      tbl_regression(pvalue_fun = label_style_pvalue(digits = 3)) |> 
      as_kable() # convert to kable to table will display on SO
    
    Characteristic Beta 95% CI p-value
    factor(cyl)
    4
    6 -6.2 -9.3, -3.0 <0.001
    8 -10 -13, -7.1 <0.001
    am 2.6 -0.10, 5.2 0.058

    Created on 2025-01-22 with reprex v2.1.1