purpleblade98
purpleblade98

Reputation: 87

tbl_summary() and add_p() -- Pull the name of the test (as it would appear in footnote) Into a Column

I would like to be able to pull a reader-friendly name of the type of test used for p-value calculation into its own column. Theoretically, I would think it should be possible to use the same object that the footnotes use (but how has been difficult).

Sample data:

library(gtsummary)
df <- data.frame(rnorm(10),rnorm(10), sample(c("orange","pink","green"), size = 10, replace = TRUE),sample(c(TRUE, FALSE), size = 10, replace = TRUE))

names(df) <- c("a","b","c","outcome")
preds <- c("a","b","c")

gt <- df %>% select(all_of(preds), outcome) %>%
  tbl_summary(by = outcome, percent = 'row') %>%
add_p() 

If I do

gt %>% modify_header(statistic ~ "**Test Statistic**", 
  test_name ~ "**Test Used**")

I get the statistic value (great!) and the test used, but the test used is formatted in function name.

I want it to be more reader-friendly. e.g. instead of "fisher.test" I'd like it to print "Fisher's exact test" the same way that the footnotes do. Can this be done?

Upvotes: 0

Views: 778

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11680

The code below illustrates how you can access the test names from the internals of a gtsummary table. That said, there is no guarantee that internals do not change at some point in the future, and these would not be considered breaking changes as they are not features surfaced by the gtsummary package.

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

tbl <- 
  trial |> 
  tbl_summary(
    by = trt, 
    include = age, 
    missing = "no"
  ) |> 
  add_p()

# access the names from the gtsummary internals
tbl$meta_data$test_result[[1]]$df_result
#> # A tibble: 1 × 4
#>   statistic p.value method                 alternative
#>       <dbl>   <dbl> <chr>                  <chr>      
#> 1      4323   0.718 Wilcoxon rank sum test two.sided

Created on 2023-10-05 with reprex v2.0.2

Upvotes: 0

Related Questions