Skip to content

Commit 91b55b6

Browse files
committed
Added ppc_km_overlay_grouped().
1 parent cbc442f commit 91b55b6

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

R/ppc-censoring.R

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#' @template args-y-yrep
1818
#' @param size,alpha Passed to the appropriate geom to control the appearance of
1919
#' the `yrep` distributions.
20-
#' @param ... Currently unused.
20+
#' @param ... Currently only used internally.
2121
#'
2222
#' @template return-ggplot
2323
#'
@@ -30,6 +30,9 @@
3030
#' `y`. Note that the replicated data from `yrep` is assumed to be
3131
#' uncensored.
3232
#' }
33+
#' \item{`ppc_km_overlay_grouped()`}{
34+
#' The same as `ppc_km_overlay()`, but with separate facets by `group`.
35+
#' }
3336
#' }
3437
#'
3538
#' @templateVar bdaRef (Ch. 6)
@@ -50,6 +53,11 @@
5053
#' \donttest{
5154
#' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y)
5255
#' }
56+
#' # With separate facets by group:
57+
#' group <- example_group_data()
58+
#' \donttest{
59+
#' ppc_km_overlay_grouped(y, yrep[1:25, ], group = group, status_y = status_y)
60+
#' }
5361
NULL
5462

5563
#' @export
@@ -65,7 +73,8 @@ ppc_km_overlay <- function(
6573
size = 0.25,
6674
alpha = 0.7
6775
) {
68-
check_ignored_arguments(...)
76+
check_ignored_arguments(..., ok_args = "add_group")
77+
add_group <- list(...)$add_group
6978

7079
if(!requireNamespace("survival", quietly = TRUE)){
7180
abort("Package 'survival' required.")
@@ -91,11 +100,25 @@ ppc_km_overlay <- function(
91100
as.numeric(as.character(.data$group)),
92101
1))
93102

103+
sf_form <- survival::Surv(value, group) ~ rep_label
104+
if(!is.null(add_group)){
105+
data <- dplyr::inner_join(data,
106+
tibble::tibble(y_id = seq_along(y),
107+
add_group = add_group),
108+
by = "y_id")
109+
sf_form <- update(sf_form, . ~ . + add_group)
110+
}
94111
sf <- survival::survfit(
95-
survival::Surv(value, group) ~ rep_label,
112+
sf_form,
96113
data = data
97114
)
115+
names(sf$strata) <- sub("add_group=", "add_group:", names(sf$strata)) # Needed to split the strata names in ggfortify:::fortify.survfit() properly.
98116
fsf <- fortify(sf)
117+
if(any(grepl("add_group", levels(fsf$strata)))){
118+
strata_split <- strsplit(as.character(fsf$strata), split = ", add_group:")
119+
fsf$strata <- as.factor(sapply(strata_split, "[[", 1))
120+
fsf$group <- as.factor(sapply(strata_split, "[[", 2))
121+
}
99122

100123
fsf$is_y_color <- as.factor(sub("\\[rep\\] \\(.*$", "rep", sub("^italic\\(y\\)", "y", fsf$strata)))
101124
fsf$is_y_size <- ifelse(fsf$is_y_color == "yrep", size, 1)
@@ -114,8 +137,14 @@ ppc_km_overlay <- function(
114137
alpha = ~ is_y_alpha)) +
115138
geom_step() +
116139
hline_at(
117-
c(0, 0.5, 1),
118-
size = c(0.2, 0.1, 0.2),
140+
0.5,
141+
size = 0.1,
142+
linetype = 2,
143+
color = get_color("dh")
144+
) +
145+
hline_at(
146+
c(0, 1),
147+
size = 0.2,
119148
linetype = 2,
120149
color = get_color("dh")
121150
) +
@@ -129,3 +158,32 @@ ppc_km_overlay <- function(
129158
yaxis_ticks(FALSE) +
130159
bayesplot_theme_get()
131160
}
161+
162+
#' @export
163+
#' @rdname PPC-censoring
164+
#' @template args-group
165+
ppc_km_overlay_grouped <- function(
166+
y,
167+
yrep,
168+
group,
169+
...,
170+
status_y,
171+
size = 0.25,
172+
alpha = 0.7
173+
) {
174+
check_ignored_arguments(...)
175+
176+
p_overlay <- ppc_km_overlay(
177+
y = y,
178+
yrep = yrep,
179+
add_group = group,
180+
...,
181+
status_y = status_y,
182+
size = size,
183+
alpha = alpha
184+
)
185+
186+
p_overlay +
187+
facet_wrap("group") +
188+
force_axes_in_facets()
189+
}

0 commit comments

Comments
 (0)