Skip to content

Commit 4724f3b

Browse files
committed
Add message if user doesn't change extrapolation_factor default
1 parent 228434c commit 4724f3b

File tree

3 files changed

+60
-44
lines changed

3 files changed

+60
-44
lines changed

R/ppc-censoring.R

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
#' @section Plot Descriptions:
2525
#' \describe{
2626
#' \item{`ppc_km_overlay()`}{
27-
#' Empirical CCDF estimates of each dataset (row) in `yrep` are overlaid,
28-
#' with the Kaplan-Meier estimate (Kaplan and Meier, 1958) for `y` itself on
29-
#' top (and in a darker shade). This is a PPC suitable for right-censored
30-
#' `y`. Note that the replicated data from `yrep` is assumed to be
31-
#' uncensored.
27+
#' Empirical CCDF estimates of each dataset (row) in `yrep` are overlaid, with
28+
#' the Kaplan-Meier estimate (Kaplan and Meier, 1958) for `y` itself on top
29+
#' (and in a darker shade). This is a PPC suitable for right-censored `y`.
30+
#' Note that the replicated data from `yrep` is assumed to be uncensored. Left
31+
#' truncation (delayed entry) times for `y` can be specified using
32+
#' `left_truncation_y`.
3233
#' }
3334
#' \item{`ppc_km_overlay_grouped()`}{
3435
#' The same as `ppc_km_overlay()`, but with separate facets by `group`.
@@ -40,32 +41,33 @@
4041
#' @template reference-km
4142
#'
4243
#' @examples
44+
#' \donttest{
4345
#' color_scheme_set("brightblue")
44-
#' y <- example_y_data()
46+
#'
4547
#' # For illustrative purposes, (right-)censor values y > 110:
48+
#' y <- example_y_data()
4649
#' status_y <- as.numeric(y <= 110)
4750
#' y <- pmin(y, 110)
51+
#'
4852
#' # In reality, the replicated data (yrep) would be obtained from a
4953
#' # model which takes the censoring of y properly into account. Here,
5054
#' # for illustrative purposes, we simply use example_yrep_draws():
5155
#' yrep <- example_yrep_draws()
5256
#' dim(yrep)
53-
#' \donttest{
57+
#'
58+
#' # Overlay 25 curves
5459
#' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y)
55-
#' }
60+
#'
5661
#' # With extrapolation_factor = 1 (no extrapolation)
57-
#' \donttest{
5862
#' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y, extrapolation_factor = 1)
59-
#' }
63+
#'
6064
#' # With extrapolation_factor = Inf (show all posterior predictive draws)
61-
#' \donttest{
6265
#' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y, extrapolation_factor = Inf)
63-
#' }
66+
#'
6467
#' # With separate facets by group:
6568
#' group <- example_group_data()
66-
#' \donttest{
6769
#' ppc_km_overlay_grouped(y, yrep[1:25, ], group = group, status_y = status_y)
68-
#' }
70+
#'
6971
#' # With left-truncation (delayed entry) times:
7072
#' min_vals <- pmin(y, apply(yrep, 2, min))
7173
#' left_truncation_y <- rep(0, length(y))
@@ -74,7 +76,6 @@
7476
#' runif(sum(condition), min = 0.6, max = 0.99) * y[condition],
7577
#' min_vals[condition] - 0.001
7678
#' )
77-
#' \donttest{
7879
#' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y,
7980
#' left_truncation_y = left_truncation_y)
8081
#' }
@@ -102,9 +103,9 @@ ppc_km_overlay <- function(
102103
...,
103104
status_y,
104105
left_truncation_y = NULL,
106+
extrapolation_factor = 1.2,
105107
size = 0.25,
106-
alpha = 0.7,
107-
extrapolation_factor = 1.2
108+
alpha = 0.7
108109
) {
109110
check_ignored_arguments(..., ok_args = "add_group")
110111
add_group <- list(...)$add_group
@@ -113,17 +114,23 @@ ppc_km_overlay <- function(
113114
suggested_package("ggfortify")
114115

115116
if (!is.numeric(status_y) || length(status_y) != length(y) || !all(status_y %in% c(0, 1))) {
116-
stop("`status_y` must be a numeric vector of 0s and 1s the same length as `y`.")
117+
stop("`status_y` must be a numeric vector of 0s and 1s the same length as `y`.", call. = FALSE)
117118
}
118119

119120
if (!is.null(left_truncation_y)) {
120121
if (!is.numeric(left_truncation_y) || length(left_truncation_y) != length(y)) {
121-
stop("`left_truncation_y` must be a numeric vector of the same length as `y`.")
122+
stop("`left_truncation_y` must be a numeric vector of the same length as `y`.", call. = FALSE)
122123
}
123124
}
124125

125126
if (extrapolation_factor < 1) {
126-
stop("`extrapolation_factor` must be greater than or equal to 1.")
127+
stop("`extrapolation_factor` must be greater than or equal to 1.", call. = FALSE)
128+
}
129+
if (extrapolation_factor == 1.2) {
130+
message(
131+
"Note: `extrapolation_factor` now defaults to 1.2 (20%).\n",
132+
"To display all posterior predictive draws, set `extrapolation_factor = Inf`."
133+
)
127134
}
128135

129136
data <- ppc_data(y, yrep, group = status_y)
@@ -218,9 +225,9 @@ ppc_km_overlay_grouped <- function(
218225
...,
219226
status_y,
220227
left_truncation_y = NULL,
228+
extrapolation_factor = 1.2,
221229
size = 0.25,
222-
alpha = 0.7,
223-
extrapolation_factor = 1.2
230+
alpha = 0.7
224231
) {
225232
check_ignored_arguments(...)
226233

man/PPC-censoring.Rd

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

tests/testthat/test-ppc-censoring.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ test_that("ppc_km_overlay errors if bad extrapolation_factor value", {
6969
)
7070
})
7171

72+
test_that("ppc_km_overlay messages if extrapolation_factor left at default value", {
73+
skip_if_not_installed("ggfortify")
74+
expect_message(
75+
ppc_km_overlay(y, yrep, status_y = status_y),
76+
"To display all posterior predictive draws, set `extrapolation_factor = Inf`.",
77+
)
78+
})
79+
7280
# Visual tests -----------------------------------------------------------------
7381

7482
test_that("ppc_km_overlay renders correctly", {

0 commit comments

Comments
 (0)