Search code examples
rdplyrgtsummary

Number of decimals of p-values using tbl_svysummary


I am trying to report the p-values from a t-test using the following dataset:

if (!require(srvyr)) install.packages('srvyr'); require(srvyr)
if (!require(gtsummary)) install.packages('gtsummary'); require(gtsummary)

library(srvyr)
library(gtsummary)

example <- structure(
  list(
    id = c(
      1, 2, 3, 4, 5, 
      6, 7, 8, 9, 10, 
      11, 12, 13, 14, 15, 
      16, 17, 18, 19, 20, 
      21, 22, 23, 24, 25
    ), strata = c(
      10, 20, 30, 10, 20,
      20, 10, 20, 30, 30, 
      10, 30, 30, 20, 10, 
      20, 20, 20, 10, 20, 
      20, 30, 30, 20, 30
    ), weight = c(
      10, 8, 17, 15, 9, 
      10, 25, 8, 8, 13, 
      17, 24, 12, 15, 3, 
      12, 16, 17, 24, 12,
      3, 2, 8, 14, 4
    ), popgroup = c(
      "A", "B", "A", "A", "A", 
      "A", "B", "B", "B", "A", 
      "A", "B", "A", "B", "A", 
      "A", "B", "A", "A", "B", 
      "A", "B", "B", "B", "B"
    ), inc_01 = c(
      1500, 1200, 130, 500, 750, 
      2000, 10000, 1500, 1050, 400, 
      360, 490, 250, 400, 2500, 
      1300, 800, 540, 690, 520, 
      600, 700, 700, 600, 400
    ), inc_02 = c(
      360, 450, 120, 300, 900, 
      560, 450, 280, 720, 360, 
      1000, 900, 530, 820, 640, 
      520, 130, 140, 150, 650,
      240, 130, 200, 300, 500
    )
  ), class = c(
    "tbl_df", 
    "tbl", 
    "data.frame"
  ), row.names = c(NA, -25L)
)


table_01 <- example |>
  srvyr::as_survey_design(
    strata  = strata, 
    weights = weight
  ) |>
  dplyr::select(popgroup, inc_01, inc_02) |>
  dplyr::filter(!is.na(popgroup)) |>
  gtsummary::tbl_svysummary(
    by   = popgroup,
    type = list(
      inc_01 ~ "continuous",
      inc_02 ~ "continuous"
    ), 
    statistic = list(c(inc_01, inc_02) ~ "{mean} ({mean.std.error})"),
    missing   = "no",
    digits    = list(c(inc_01, inc_02) ~ c(4, 4)),
  ) |>
  gtsummary::add_p(
    test = list(
      all_continuous() ~ "svy.t.test",
      pvalue_fun = function(x) gtsummary::style_pvalue(x, digits = 4)
    )
  ) |>
  gtsummary::modify_fmt_fun(
    update = statistic ~ function(x) gtsummary::style_number(x, digits = 6)
  )

...but the number of decimals remains rounded to the tenth place. Any help will be appreciated. Thanks in advance!


Solution

  • You were very close. The problems are here—you should (1) not wrap test in a list; and (2) the digits parameter of gtsummary::style_pvalue also cannot exceed 3.

    ...
      gtsummary::add_p(
        test = list(
          all_continuous() ~ "svy.t.test",
          pvalue_fun = function(x) gtsummary::style_pvalue(x, digits = 4)
        )
      )
    ...
    

    Here's the correct syntax:

    table_01 <- example |>
      srvyr::as_survey_design(
        strata  = strata, 
        weights = weight
      ) |>
      dplyr::select(popgroup, inc_01, inc_02) |>
      dplyr::filter(!is.na(popgroup)) |>
      gtsummary::tbl_svysummary(
        by   = popgroup,
        type = list(
          inc_01 ~ "continuous",
          inc_02 ~ "continuous"
        ), 
        statistic = list(c(inc_01, inc_02) ~ "{mean} ({mean.std.error})"),
        missing   = "no",
        digits    = list(c(inc_01, inc_02) ~ c(4, 4)),
      ) |>
      
      # You shouldn't be wrapping these arguments in a list
      gtsummary::add_p(
        test       = all_continuous() ~ "svy.t.test",
        pvalue_fun = function(x) gtsummary::style_pvalue(x, digits = 3)  # keep digits from 1–3
      ) |>
      
      gtsummary::modify_fmt_fun(
        update = statistic ~ function(x) gtsummary::style_number(x, digits = 6)
      )
    
    table_01