-
-
Notifications
You must be signed in to change notification settings - Fork 89
Add possibility for left-truncation to ppc_km_overlay() and ppc_km_overlay_grouped() #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
6194c04
1a51b0e
d6e2135
d4274f2
f59e38e
c16fce3
f4b5e0e
fced009
c7d8485
8d59871
1737006
45a6d88
294637a
0044845
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,18 +58,33 @@ | |
| #' \donttest{ | ||
| #' ppc_km_overlay_grouped(y, yrep[1:25, ], group = group, status_y = status_y) | ||
| #' } | ||
| #' # With left-truncation (delayed entry) times: | ||
| #' condition <- y > mean(y) / 2 | ||
| #' left_truncation_y[condition] <- pmin( | ||
| #' runif(sum(condition), min = 0.6, max = 0.99) * y[condition], | ||
| #' min_vals[condition] - 0.001 | ||
| #' ) | ||
| #' \donttest{ | ||
| #' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y, | ||
| #' left_truncation_y = left_truncation_y) | ||
| #' } | ||
|
Comment on lines
61
to
72
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get a warning when I try this example:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The warning was to be expected in case of that example, but I made a cleaner example that does not produce warnings. In addition, I think the new example visualizes the effect of left-truncation a bit better.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In real life, the warning is an indication of an impossible observation or posterior predictive draw, so the warning itself is good. When creating examples, we just have to make sure that each value in left_truncation_y is lower than the corresponding value in y or corresponding values in yrep so we don't get the warning.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok great, thanks, that makes sense. |
||
| NULL | ||
|
|
||
| #' @export | ||
| #' @rdname PPC-censoring | ||
| #' @param status_y The status indicator for the observations from `y`. This must | ||
| #' be a numeric vector of the same length as `y` with values in \{0, 1\} (0 = | ||
| #' right censored, 1 = event). | ||
| #' @param left_truncation_y Optional parameter that specifies left-truncation | ||
| #' (delayed entry) times for the observations from `y`. This must | ||
| #' be a numeric vector of the same length as `y`. If `NULL` (default), | ||
| #' no left-truncation is assumed. | ||
| ppc_km_overlay <- function( | ||
| y, | ||
| yrep, | ||
| ..., | ||
| status_y, | ||
| left_truncation_y = NULL, | ||
Sakuski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| size = 0.25, | ||
| alpha = 0.7 | ||
| ) { | ||
|
|
@@ -79,8 +94,15 @@ ppc_km_overlay <- function( | |
| suggested_package("survival") | ||
| suggested_package("ggfortify") | ||
|
|
||
| stopifnot(is.numeric(status_y)) | ||
| stopifnot(all(status_y %in% c(0, 1))) | ||
| if (!is.numeric(status_y) || length(status_y) != length(y) || !all(status_y %in% c(0, 1))) { | ||
| stop("`status_y` must be a numeric vector of 0s and 1s the same length as `y`.") | ||
| } | ||
|
|
||
| if (!is.null(left_truncation_y)) { | ||
| if (!is.numeric(left_truncation_y) || length(left_truncation_y) != length(y)) { | ||
| stop("`left_truncation_y` must be a numeric vector of the same length as `y`.") | ||
| } | ||
| } | ||
|
|
||
| data <- ppc_data(y, yrep, group = status_y) | ||
|
|
||
|
|
@@ -96,7 +118,12 @@ ppc_km_overlay <- function( | |
| as.numeric(as.character(.data$group)), | ||
| 1)) | ||
|
|
||
| sf_form <- survival::Surv(value, group) ~ rep_label | ||
| if (is.null(left_truncation_y)) { | ||
| sf_form <- survival::Surv(time = data$value, event = data$group) ~ rep_label | ||
| } else { | ||
| sf_form <- survival::Surv(time = left_truncation_y[data$y_id], time2 = data$value, event = data$group) ~ rep_label | ||
| } | ||
|
|
||
| if (!is.null(add_group)) { | ||
| data <- dplyr::inner_join(data, | ||
| tibble::tibble(y_id = seq_along(y), | ||
|
|
@@ -164,6 +191,7 @@ ppc_km_overlay_grouped <- function( | |
| group, | ||
| ..., | ||
| status_y, | ||
| left_truncation_y = NULL, | ||
| size = 0.25, | ||
| alpha = 0.7 | ||
| ) { | ||
|
|
@@ -175,6 +203,7 @@ ppc_km_overlay_grouped <- function( | |
| add_group = group, | ||
| ..., | ||
| status_y = status_y, | ||
| left_truncation_y = left_truncation_y, | ||
| size = size, | ||
| alpha = alpha | ||
| ) | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.