|
17 | 17 | #' @template args-y-yrep
|
18 | 18 | #' @param size,alpha Passed to the appropriate geom to control the appearance of
|
19 | 19 | #' the `yrep` distributions.
|
20 |
| -#' @param ... Currently unused. |
| 20 | +#' @param ... Currently only used internally. |
21 | 21 | #'
|
22 | 22 | #' @template return-ggplot
|
23 | 23 | #'
|
|
30 | 30 | #' `y`. Note that the replicated data from `yrep` is assumed to be
|
31 | 31 | #' uncensored.
|
32 | 32 | #' }
|
| 33 | +#' \item{`ppc_km_overlay_grouped()`}{ |
| 34 | +#' The same as `ppc_km_overlay()`, but with separate facets by `group`. |
| 35 | +#' } |
33 | 36 | #' }
|
34 | 37 | #'
|
35 | 38 | #' @templateVar bdaRef (Ch. 6)
|
|
50 | 53 | #' \donttest{
|
51 | 54 | #' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y)
|
52 | 55 | #' }
|
| 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 | +#' } |
53 | 61 | NULL
|
54 | 62 |
|
55 | 63 | #' @export
|
56 | 64 | #' @rdname PPC-censoring
|
57 | 65 | #' @param status_y The status indicator for the observations from `y`. This must
|
58 | 66 | #' be a numeric vector of the same length as `y` with values in \{0, 1\} (0 =
|
59 | 67 | #' right censored, 1 = event).
|
60 |
| -ppc_km_overlay <- |
61 |
| - function(y, |
62 |
| - yrep, |
63 |
| - ..., |
64 |
| - status_y, |
65 |
| - size = 0.25, |
66 |
| - alpha = 0.7) { |
67 |
| - check_ignored_arguments(...) |
| 68 | +ppc_km_overlay <- function( |
| 69 | + y, |
| 70 | + yrep, |
| 71 | + ..., |
| 72 | + status_y, |
| 73 | + size = 0.25, |
| 74 | + alpha = 0.7 |
| 75 | +) { |
| 76 | + check_ignored_arguments(..., ok_args = "add_group") |
| 77 | + add_group <- list(...)$add_group |
68 | 78 |
|
69 |
| - if(!requireNamespace("survival", quietly = TRUE)){ |
70 |
| - abort("Package 'survival' required.") |
71 |
| - } |
72 |
| - if(!requireNamespace("ggfortify", quietly = TRUE)){ |
73 |
| - abort("Package 'ggfortify' required.") |
74 |
| - } |
| 79 | + if(!requireNamespace("survival", quietly = TRUE)){ |
| 80 | + abort("Package 'survival' required.") |
| 81 | + } |
| 82 | + if(!requireNamespace("ggfortify", quietly = TRUE)){ |
| 83 | + abort("Package 'ggfortify' required.") |
| 84 | + } |
75 | 85 |
|
76 |
| - stopifnot(is.numeric(status_y)) |
77 |
| - stopifnot(all(status_y %in% c(0, 1))) |
| 86 | + stopifnot(is.numeric(status_y)) |
| 87 | + stopifnot(all(status_y %in% c(0, 1))) |
78 | 88 |
|
79 |
| - data <- ppc_data(y, yrep, group = status_y) |
| 89 | + data <- ppc_data(y, yrep, group = status_y) |
80 | 90 |
|
81 |
| - # Modify the status indicator: |
82 |
| - # * For the observed data ("y"), convert the status indicator back to |
83 |
| - # a numeric. |
84 |
| - # * For the replicated data ("yrep"), set the status indicator |
85 |
| - # to 1 ("event"). This way, the Kaplan-Meier estimator reduces |
86 |
| - # to "1 - ECDF" with ECDF denoting the ordinary empirical cumulative |
87 |
| - # distribution function. |
88 |
| - data <- data %>% |
89 |
| - dplyr::mutate(group = ifelse(.data$is_y, |
90 |
| - as.numeric(as.character(.data$group)), |
91 |
| - 1)) |
| 91 | + # Modify the status indicator: |
| 92 | + # * For the observed data ("y"), convert the status indicator back to |
| 93 | + # a numeric. |
| 94 | + # * For the replicated data ("yrep"), set the status indicator |
| 95 | + # to 1 ("event"). This way, the Kaplan-Meier estimator reduces |
| 96 | + # to "1 - ECDF" with ECDF denoting the ordinary empirical cumulative |
| 97 | + # distribution function. |
| 98 | + data <- data %>% |
| 99 | + dplyr::mutate(group = ifelse(.data$is_y, |
| 100 | + as.numeric(as.character(.data$group)), |
| 101 | + 1)) |
92 | 102 |
|
93 |
| - sf <- survival::survfit( |
94 |
| - survival::Surv(value, group) ~ rep_label, |
95 |
| - data = data |
96 |
| - ) |
97 |
| - fsf <- fortify(sf) |
| 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 | + } |
| 111 | + sf <- survival::survfit( |
| 112 | + sf_form, |
| 113 | + data = data |
| 114 | + ) |
| 115 | + names(sf$strata) <- sub("add_group=", "add_group:", names(sf$strata)) # Needed to split the strata names in ggfortify:::fortify.survfit() properly. |
| 116 | + 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 | + } |
98 | 122 |
|
99 |
| - fsf$is_y_color <- as.factor(sub("\\[rep\\] \\(.*$", "rep", sub("^italic\\(y\\)", "y", fsf$strata))) |
100 |
| - fsf$is_y_size <- ifelse(fsf$is_y_color == "yrep", size, 1) |
101 |
| - fsf$is_y_alpha <- ifelse(fsf$is_y_color == "yrep", alpha, 1) |
| 123 | + fsf$is_y_color <- as.factor(sub("\\[rep\\] \\(.*$", "rep", sub("^italic\\(y\\)", "y", fsf$strata))) |
| 124 | + fsf$is_y_size <- ifelse(fsf$is_y_color == "yrep", size, 1) |
| 125 | + fsf$is_y_alpha <- ifelse(fsf$is_y_color == "yrep", alpha, 1) |
102 | 126 |
|
103 |
| - # Ensure that the observed data gets plotted last by reordering the |
104 |
| - # levels of the factor "strata" |
105 |
| - fsf$strata <- factor(fsf$strata, levels = rev(levels(fsf$strata))) |
| 127 | + # Ensure that the observed data gets plotted last by reordering the |
| 128 | + # levels of the factor "strata" |
| 129 | + fsf$strata <- factor(fsf$strata, levels = rev(levels(fsf$strata))) |
106 | 130 |
|
107 |
| - ggplot(data = fsf, |
108 |
| - mapping = aes_(x = ~ time, |
109 |
| - y = ~ surv, |
110 |
| - color = ~ is_y_color, |
111 |
| - group = ~ strata, |
112 |
| - size = ~ is_y_size, |
113 |
| - alpha = ~ is_y_alpha)) + |
114 |
| - geom_step() + |
115 |
| - hline_at( |
116 |
| - c(0, 0.5, 1), |
117 |
| - size = c(0.2, 0.1, 0.2), |
118 |
| - linetype = 2, |
119 |
| - color = get_color("dh") |
120 |
| - ) + |
121 |
| - scale_size_identity() + |
122 |
| - scale_alpha_identity() + |
123 |
| - scale_color_ppc_dist() + |
124 |
| - scale_y_continuous(breaks = c(0, 0.5, 1)) + |
125 |
| - xlab(y_label()) + |
126 |
| - yaxis_title(FALSE) + |
127 |
| - xaxis_title(FALSE) + |
128 |
| - yaxis_ticks(FALSE) + |
129 |
| - bayesplot_theme_get() |
130 |
| - } |
| 131 | + ggplot(data = fsf, |
| 132 | + mapping = aes_(x = ~ time, |
| 133 | + y = ~ surv, |
| 134 | + color = ~ is_y_color, |
| 135 | + group = ~ strata, |
| 136 | + size = ~ is_y_size, |
| 137 | + alpha = ~ is_y_alpha)) + |
| 138 | + geom_step() + |
| 139 | + hline_at( |
| 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, |
| 148 | + linetype = 2, |
| 149 | + color = get_color("dh") |
| 150 | + ) + |
| 151 | + scale_size_identity() + |
| 152 | + scale_alpha_identity() + |
| 153 | + scale_color_ppc_dist() + |
| 154 | + scale_y_continuous(breaks = c(0, 0.5, 1)) + |
| 155 | + xlab(y_label()) + |
| 156 | + yaxis_title(FALSE) + |
| 157 | + xaxis_title(FALSE) + |
| 158 | + yaxis_ticks(FALSE) + |
| 159 | + bayesplot_theme_get() |
| 160 | +} |
| 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