24
24
# ' @section Plot Descriptions:
25
25
# ' \describe{
26
26
# ' \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`.
32
33
# ' }
33
34
# ' \item{`ppc_km_overlay_grouped()`}{
34
35
# ' The same as `ppc_km_overlay()`, but with separate facets by `group`.
40
41
# ' @template reference-km
41
42
# '
42
43
# ' @examples
44
+ # ' \donttest{
43
45
# ' color_scheme_set("brightblue")
44
- # ' y <- example_y_data()
46
+ # '
45
47
# ' # For illustrative purposes, (right-)censor values y > 110:
48
+ # ' y <- example_y_data()
46
49
# ' status_y <- as.numeric(y <= 110)
47
50
# ' y <- pmin(y, 110)
51
+ # '
48
52
# ' # In reality, the replicated data (yrep) would be obtained from a
49
53
# ' # model which takes the censoring of y properly into account. Here,
50
54
# ' # for illustrative purposes, we simply use example_yrep_draws():
51
55
# ' yrep <- example_yrep_draws()
52
56
# ' dim(yrep)
53
- # ' \donttest{
57
+ # '
58
+ # ' # Overlay 25 curves
54
59
# ' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y)
55
- # ' }
60
+ # '
61
+ # ' # With extrapolation_factor = 1 (no extrapolation)
62
+ # ' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y, extrapolation_factor = 1)
63
+ # '
64
+ # ' # With extrapolation_factor = Inf (show all posterior predictive draws)
65
+ # ' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y, extrapolation_factor = Inf)
66
+ # '
56
67
# ' # With separate facets by group:
57
68
# ' group <- example_group_data()
58
- # ' \donttest{
59
69
# ' ppc_km_overlay_grouped(y, yrep[1:25, ], group = group, status_y = status_y)
60
- # ' }
70
+ # '
61
71
# ' # With left-truncation (delayed entry) times:
62
72
# ' min_vals <- pmin(y, apply(yrep, 2, min))
63
73
# ' left_truncation_y <- rep(0, length(y))
66
76
# ' runif(sum(condition), min = 0.6, max = 0.99) * y[condition],
67
77
# ' min_vals[condition] - 0.001
68
78
# ' )
69
- # ' \donttest{
70
79
# ' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y,
71
80
# ' left_truncation_y = left_truncation_y)
72
81
# ' }
78
87
# ' be a numeric vector of the same length as `y` with values in \{0, 1\} (0 =
79
88
# ' right censored, 1 = event).
80
89
# ' @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.
90
+ # ' (delayed entry) times for the observations from `y`. This must be a numeric
91
+ # ' vector of the same length as `y`. If `NULL` (default), no left-truncation
92
+ # ' is assumed.
93
+ # ' @param extrapolation_factor A numeric value (>=1) that controls how far the
94
+ # ' plot is extended beyond the largest observed value in `y`. The default
95
+ # ' value is 1.2, which corresponds to 20 % extrapolation. Note that all
96
+ # ' posterior predictive draws may not be shown by default because of the
97
+ # ' controlled extrapolation. To display all posterior predictive draws, set
98
+ # ' `extrapolation_factor = Inf`.
99
+ # '
84
100
ppc_km_overlay <- function (
85
101
y ,
86
102
yrep ,
87
103
... ,
88
104
status_y ,
89
105
left_truncation_y = NULL ,
106
+ extrapolation_factor = 1.2 ,
90
107
size = 0.25 ,
91
108
alpha = 0.7
92
109
) {
@@ -97,15 +114,25 @@ ppc_km_overlay <- function(
97
114
suggested_package(" ggfortify" )
98
115
99
116
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`." )
117
+ stop(" `status_y` must be a numeric vector of 0s and 1s the same length as `y`." , call. = FALSE )
101
118
}
102
119
103
120
if (! is.null(left_truncation_y )) {
104
121
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`." )
122
+ stop(" `left_truncation_y` must be a numeric vector of the same length as `y`." , call. = FALSE )
106
123
}
107
124
}
108
125
126
+ if (extrapolation_factor < 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
+ )
134
+ }
135
+
109
136
data <- ppc_data(y , yrep , group = status_y )
110
137
111
138
# Modify the status indicator:
@@ -149,6 +176,10 @@ ppc_km_overlay <- function(
149
176
fsf $ is_y_size <- ifelse(fsf $ is_y_color == " yrep" , size , 1 )
150
177
fsf $ is_y_alpha <- ifelse(fsf $ is_y_color == " yrep" , alpha , 1 )
151
178
179
+ max_time_y <- max(y , na.rm = TRUE )
180
+ fsf <- fsf %> %
181
+ dplyr :: filter(is_y_color != " yrep" | time < = max_time_y * extrapolation_factor )
182
+
152
183
# Ensure that the observed data gets plotted last by reordering the
153
184
# levels of the factor "strata"
154
185
fsf $ strata <- factor (fsf $ strata , levels = rev(levels(fsf $ strata )))
@@ -194,6 +225,7 @@ ppc_km_overlay_grouped <- function(
194
225
... ,
195
226
status_y ,
196
227
left_truncation_y = NULL ,
228
+ extrapolation_factor = 1.2 ,
197
229
size = 0.25 ,
198
230
alpha = 0.7
199
231
) {
@@ -207,7 +239,8 @@ ppc_km_overlay_grouped <- function(
207
239
status_y = status_y ,
208
240
left_truncation_y = left_truncation_y ,
209
241
size = size ,
210
- alpha = alpha
242
+ alpha = alpha ,
243
+ extrapolation_factor = extrapolation_factor
211
244
)
212
245
213
246
p_overlay +
0 commit comments