Skip to content

Commit f597d98

Browse files
change to pull the subset argument (#267)
* change to pull the subset argument * doc updates * Updated construction of survdiff() call * unit test for the subset arg --------- Co-authored-by: Daniel D. Sjoberg <danield.sjoberg@gmail.com>
1 parent ec80a76 commit f597d98

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Version: 1.2.0.9000
44
Authors@R: c(
55
person("Daniel D.", "Sjoberg", , "danield.sjoberg@gmail.com", role = c("aut", "cre", "cph"),
66
comment = c(ORCID = "0000-0003-0862-2018")),
7-
person("Shreya", "Sreeram", , "shreyasreeram27@gmail.com", role = "aut"),
7+
person("Shreya", "Sreeram", , "shreyasreeram27@gmail.com", role = "aut"),
88
person("Mark", "Baillie", , "bailliem@gmail.com", role = "aut"),
99
person("Charlotta", "Fruechtenicht", , "charlotta.fruechtenicht@roche.com", role = "aut",
1010
comment = c(ORCID = "0000-0002-6689-6423")),

R/survfit2_p.R

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,25 @@ survfit2_p <- function(x, pvalue_fun = format_p, prepend_p = TRUE, rho = 0) {
4242
cli_abort("The {.fun survfit2_p} does not support multi-state models.")
4343
}
4444

45-
survival::survdiff(
46-
formula = .extract_formula_from_survfit(x),
47-
data = .extract_data_from_survfit(x),
48-
rho = rho
49-
) %>%
45+
# call survdiff
46+
survdiff_args <-
47+
list(
48+
formula = .extract_formula_from_survfit(x),
49+
data = .extract_data_from_survfit(x),
50+
subset = as.list(x$call)[["subset"]],
51+
rho = rho
52+
) %>%
53+
# remove NULL entries
54+
{Filter(Negate(is.null), x = .)} # styler: off
55+
56+
survdiff_result <-
57+
do.call(
58+
what = survival::survdiff,
59+
args = survdiff_args,
60+
envir = x$.Environment
61+
)
62+
63+
survdiff_result %>%
5064
broom::glance() %>%
5165
dplyr::pull("p.value") %>%
5266
pvalue_fun() %>%

man/add_risktable_strata_symbol.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-survfit2_p.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,39 @@ test_that("survfit2_p() throws error", {
3838
add_pvalue()
3939
)
4040
})
41+
42+
test_that("survfit2_p() works with subset argument", {
43+
#create survfit object with subset argument
44+
sf_subset <- survfit2(Surv(time, status) ~ sex, data = df_lung, subset = age >= 75)
45+
46+
#calculate expectes p val directly with survdiff using the same subset
47+
expected_p <- survival::survdiff(
48+
Surv(time, status) ~ sex,
49+
data = df_lung,
50+
subset = age >= 75
51+
) %>%
52+
broom::glance() %>%
53+
dplyr::pull(p.value) %>%
54+
format_p()
55+
56+
#testing that survfit2_p respects the subset argument
57+
expect_equal(
58+
survfit2_p(sf_subset, prepend_p = FALSE),
59+
expected_p
60+
)
61+
62+
#the subset p-value should be different from full data p-value
63+
sf_full <- survfit2(Surv(time, status) ~ sex, data = df_lung)
64+
65+
expect_false(
66+
survfit2_p(sf_subset, prepend_p = FALSE) == survfit2_p(sf_full, prepend_p = FALSE)
67+
)
68+
69+
#testing that subset using filter() gives same result as subset argument
70+
sf_filtered <- survfit2(Surv(time, status) ~ sex, data = df_lung %>% dplyr::filter(age >= 75))
71+
72+
expect_equal(
73+
survfit2_p(sf_subset, prepend_p = FALSE),
74+
survfit2_p(sf_filtered, prepend_p = FALSE)
75+
)
76+
})

0 commit comments

Comments
 (0)