Skip to content

Commit 23e00b5

Browse files
authored
Merge pull request #347 from Sakuski/ppc_km_overlay-left_truncation
Add possibility for left-truncation to ppc_km_overlay() and ppc_km_overlay_grouped()
2 parents 0d891a2 + 0044845 commit 23e00b5

9 files changed

+556
-9
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# bayesplot (development version)
22

3+
* Add possibility for left-truncation to `ppc_km_overlay()` and `ppc_km_overlay_grouped()` by @Sakuski
34
* Added `ppc_loo_pit_ecdf()` by @TeemuSailynoja
45

56
# bayesplot 1.12.0

R/ppc-censoring.R

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,35 @@
5858
#' \donttest{
5959
#' ppc_km_overlay_grouped(y, yrep[1:25, ], group = group, status_y = status_y)
6060
#' }
61+
#' # With left-truncation (delayed entry) times:
62+
#' min_vals <- pmin(y, apply(yrep, 2, min))
63+
#' left_truncation_y <- rep(0, length(y))
64+
#' condition <- y > mean(y) / 2
65+
#' left_truncation_y[condition] <- pmin(
66+
#' runif(sum(condition), min = 0.6, max = 0.99) * y[condition],
67+
#' min_vals[condition] - 0.001
68+
#' )
69+
#' \donttest{
70+
#' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y,
71+
#' left_truncation_y = left_truncation_y)
72+
#' }
6173
NULL
6274

6375
#' @export
6476
#' @rdname PPC-censoring
6577
#' @param status_y The status indicator for the observations from `y`. This must
6678
#' be a numeric vector of the same length as `y` with values in \{0, 1\} (0 =
6779
#' right censored, 1 = event).
80+
#' @param left_truncation_y Optional parameter that specifies left-truncation
81+
#' (delayed entry) times for the observations from `y`. This must
82+
#' be a numeric vector of the same length as `y`. If `NULL` (default),
83+
#' no left-truncation is assumed.
6884
ppc_km_overlay <- function(
6985
y,
7086
yrep,
7187
...,
7288
status_y,
89+
left_truncation_y = NULL,
7390
size = 0.25,
7491
alpha = 0.7
7592
) {
@@ -79,8 +96,15 @@ ppc_km_overlay <- function(
7996
suggested_package("survival")
8097
suggested_package("ggfortify")
8198

82-
stopifnot(is.numeric(status_y))
83-
stopifnot(all(status_y %in% c(0, 1)))
99+
if (!is.numeric(status_y) || length(status_y) != length(y) || !all(status_y %in% c(0, 1))) {
100+
stop("`status_y` must be a numeric vector of 0s and 1s the same length as `y`.")
101+
}
102+
103+
if (!is.null(left_truncation_y)) {
104+
if (!is.numeric(left_truncation_y) || length(left_truncation_y) != length(y)) {
105+
stop("`left_truncation_y` must be a numeric vector of the same length as `y`.")
106+
}
107+
}
84108

85109
data <- ppc_data(y, yrep, group = status_y)
86110

@@ -96,7 +120,12 @@ ppc_km_overlay <- function(
96120
as.numeric(as.character(.data$group)),
97121
1))
98122

99-
sf_form <- survival::Surv(value, group) ~ rep_label
123+
if (is.null(left_truncation_y)) {
124+
sf_form <- survival::Surv(time = data$value, event = data$group) ~ rep_label
125+
} else {
126+
sf_form <- survival::Surv(time = left_truncation_y[data$y_id], time2 = data$value, event = data$group) ~ rep_label
127+
}
128+
100129
if (!is.null(add_group)) {
101130
data <- dplyr::inner_join(data,
102131
tibble::tibble(y_id = seq_along(y),
@@ -164,6 +193,7 @@ ppc_km_overlay_grouped <- function(
164193
group,
165194
...,
166195
status_y,
196+
left_truncation_y = NULL,
167197
size = 0.25,
168198
alpha = 0.7
169199
) {
@@ -175,6 +205,7 @@ ppc_km_overlay_grouped <- function(
175205
add_group = group,
176206
...,
177207
status_y = status_y,
208+
left_truncation_y = left_truncation_y,
178209
size = size,
179210
alpha = alpha
180211
)

man/PPC-censoring.Rd

Lines changed: 36 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/ppc-censoring/ppc-km-overlay-default-2.svg

Lines changed: 71 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)