Skip to contents

Calculate and add a p-value to stratified tbl_survfit() tables.

Usage

# S3 method for class 'tbl_survfit'
add_p(
  x,
  test = "logrank",
  test.args = NULL,
  pvalue_fun = label_style_pvalue(digits = 1),
  include = everything(),
  quiet,
  ...
)

Arguments

x

(tbl_survfit)
Object of class "tbl_survfit"

test

(string)
string indicating test to use. Must be one of "logrank", "tarone", "survdiff", "petopeto_gehanwilcoxon", "coxph_lrt", "coxph_wald", "coxph_score". See details below

test.args

(named list)
named list of arguments that will be passed to the method specified in the test argument. Default is NULL.

pvalue_fun

(function)
Function to round and format p-values. Default is label_style_pvalue(). The function must have a numeric vector input, and return a string that is the rounded/formatted p-value (e.g. pvalue_fun = label_style_pvalue(digits = 2)).

include

(tidy-select)
Variables to include in output. Default is everything().

quiet

[Deprecated]

...

These dots are for future extensions and must be empty.

test argument

The most common way to specify test= is by using a single string indicating the test name. However, if you need to specify different tests within the same table, the input in flexible using the list notation common throughout the gtsummary package. For example, the following code would call the log-rank test, and a second test of the G-rho family.

... |>
  add_p(test = list(trt ~ "logrank", grade ~ "survdiff"),
        test.args = grade ~ list(rho = 0.5))

See also

Other tbl_survfit tools: add_nevent.tbl_survfit()

Examples

library(survival)

gts_survfit <-
  list(
    survfit(Surv(ttdeath, death) ~ grade, trial),
    survfit(Surv(ttdeath, death) ~ trt, trial)
  ) |>
  tbl_survfit(times = c(12, 24))

# Example 1 ----------------------------------
gts_survfit |>
  add_p()
Characteristic Time 12 Time 24 p-value1
Grade

0.072
    I 97% (93%, 100%) 51% (41%, 65%)
    II 82% (74%, 92%) 47% (37%, 61%)
    III 86% (78%, 95%) 33% (23%, 47%)
Chemotherapy Treatment

0.2
    Drug A 91% (85%, 97%) 47% (38%, 58%)
    Drug B 86% (80%, 93%) 41% (33%, 52%)
1 Log-rank test
# Example 2 ---------------------------------- # Pass `rho=` argument to `survdiff()` gts_survfit |> add_p(test = "survdiff", test.args = list(rho = 0.5))
Characteristic Time 12 Time 24 p-value1
Grade

0.070
    I 97% (93%, 100%) 51% (41%, 65%)
    II 82% (74%, 92%) 47% (37%, 61%)
    III 86% (78%, 95%) 33% (23%, 47%)
Chemotherapy Treatment

0.2
    Drug A 91% (85%, 97%) 47% (38%, 58%)
    Drug B 86% (80%, 93%) 41% (33%, 52%)
1 G-rho test (ρ = 0.5)