-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ph.R
252 lines (214 loc) · 6.8 KB
/
test_ph.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#' Test Proportional Hazards Assumption
#'
#' Assesses the proportional hazards assumption for survival data using a
#' Cox proportional hazards model and related tests.
#'
#' @param data A data frame containing the survival data.
#' @param time The name of the column in \code{data} containing the
#' time-to-event information.
#' @param event The name of the column in \code{data} indicating whether the
#' event of interest occurred.
#' @param group The name of the column in \code{data} defining the grouping
#' variable.
#' @param plot_theme The theme to be used for the plots.
#'
#' @returns A list containing plots and test results related to the assessment
#' of the proportional hazards assumption.
#'
#' \item{cloglog_plot}{A plot of the log cumulative hazard function. If the
#' lines are roughly parallel, this suggests that the proportional hazards
#' assumption holds."}
#' \item{coxph_model}{The coefficients from the Cox proportional hazards
#' model. The exp(coef) column shows the hazard ratio.}
#' \item{survdiff}{The results of the log-rank test for differences in
#' survival curves between groups. A p-value less than 0.05 suggests that
#' survival differences between groups are statistically significant.}
#' \item{coxph_test}{The results of the proportional hazards assumption test.
#' A p-value less than 0.05 suggests that the proportional hazards assumption
#' may be violated.}
#' \item{schoenfeld_plot}{A plot of the Schoenfeld residuals. A flat smoothed
#' line close to zero supports the proportional hazards assumption. A non-flat
#' smoothed line with a trend suggests the proportional hazards assumption is
#' violated.}
#'
#' @export
#'
#' @importFrom ggsurvfit survfit2
#' @importFrom stats as.formula
#' @importFrom survival cox.zph coxph survdiff Surv
#'
#' @examples
#' ph_results <- test_ph(
#' data = easysurv::easy_bc,
#' time = "recyrs",
#' event = "censrec",
#' group = "group"
#' )
#'
#' ph_results
test_ph <- function(data,
time,
event,
group,
plot_theme = theme_easysurv()) {
if (!is.data.frame(data)) {
cli::cli_abort(c(
"The {.var data} argument must have class {.cls data.frame}.",
"x" = "You've provided an object of class: {.cls {class(data)}}"
))
}
required_cols <- c(time, event, group)
if (!all(required_cols %in% colnames(data))) {
cli::cli_abort(
paste0(
"test_ph did not find the following columns in `data`: ",
paste(setdiff(required_cols, colnames(data)),
collapse = ", "
)
)
)
}
group_list <- levels(droplevels(as.factor(data[[group]])))
if (length(group_list) <= 1) {
cli::cli_abort(c(
paste0(
"The {.field group} variable must contain multiple groups if ",
"looking to assess proportional hazards."
),
"x" = "You've provided a group with the levels: {.var {group_list}}."
))
}
ph_formula <- stats::as.formula(paste0(
"survival::Surv(time = ", time,
", event = ", event,
") ~ ", group
))
km_all <- ggsurvfit::survfit2(
formula = ph_formula,
conf.int = 0.95,
data = data,
type = "kaplan-meier"
)
cloglog_plot <- plot_cloglog(km_all,
plot_theme = plot_theme
)
the_coxph <- survival::coxph(
formula = ph_formula,
data = data
)
coxph_model <- summary(the_coxph)
coxph_test <- survival::cox.zph(the_coxph)
survdiff <- survival::survdiff(
formula = ph_formula,
data = data
)
schoenfeld_residuals <- get_schoenfeld(coxph_test)
# removing plot_theme for now, since theme_easysurv() strips right axis line
schoenfeld_plot <-
plot_schoenfeld(
residuals = schoenfeld_residuals
)
out <- list(
cloglog_plot = cloglog_plot,
coxph_model = coxph_model,
survdiff = survdiff,
coxph_test = coxph_test,
schoenfeld_plot = schoenfeld_plot
)
# Assign a class
class(out) <- c(class(out), "test_ph")
out
}
#' Print methods for \code{test_ph()}
#'
#' @param x An object of class \code{test_ph}
#' @param ... Additional arguments
#'
#' @returns A print summary of the \code{test_ph} object.
#'
#' @importFrom cli cli_h1 cli_h2 cli_h3 cli_text
#' @importFrom cli cli_ul cli_li cli_div cli_end cli_par
#' @importFrom cli cli_alert cli_alert_info cli_alert_warning cli_rule
#' @importFrom cli cat_line qty
#'
#' @export
#'
#' @examples
#' ph_results <- test_ph(
#' data = easysurv::easy_bc,
#' time = "recyrs",
#' event = "censrec",
#' group = "group"
#' )
#'
#' ph_results
print.test_ph <- function(x, ...) {
cli::cli_h1("Testing Survival Curve Differences")
divid <- cli::cli_div(theme = list(.val = list(digits = 3)))
cli::cli_alert_info(c(
"{.fn survival::survdiff} found a p-value of {.val {x$survdiff$pvalue}}."
))
if (x$survdiff$pvalue > 0.05) {
cli::cli_alert_warning(c(
"suggests survival differences between groups are ",
"{.strong NOT} statistically significant."
))
} else {
cli::cli_alert_success(c(
"suggests survival differences between groups are ",
"statistically significant."
))
}
cli::cli_end(divid)
cli::cli_h1("Testing Proportional Hazards Assumption")
cli::cli_h2("Cox Proportional Hazards Model")
cli::cli_text("{.fn survival::coxph} output:")
cli::cat_line()
coeff_table <- as.data.frame(x$coxph_model$coefficients)
print(coeff_table)
cli::cli_par() # to add space, cat_line didn't seem to work here.
cli::cli_end()
divid <- cli::cli_div(theme = list(.val = list(digits = 3)))
cli::cli_text(c(
"The exp(coef) column shows the hazard ",
"{cli::qty(nrow(coeff_table))}ratio{?s} {?was/were} ",
"{.val {coeff_table$`exp(coef)`}}."
))
cli::cli_end(divid)
p_vals <- as.data.frame(x$coxph_test$table)[3]
global_p_val <- p_vals[nrow(p_vals), ]
cli::cli_par() # to add space, cat_line didn't seem to work here.
cli::cli_end()
divid <- cli::cli_div(theme = list(.val = list(digits = 3)))
cli::cli_alert_info(c(
"{.fn survival::cox.zph} found a p-value of {.val {global_p_val}}."
))
if (global_p_val > 0.05) {
cli::cli_alert_success(c(
"suggests the PH assumption {.strong may be} valid."
))
} else {
cli::cli_alert_warning(c(
"suggests the PH assumption {.strong may not be} valid."
))
}
cli::cli_end(divid)
cli::cli_h2("Plots")
cli::cli_alert_info(c(
"Schoenfeld residuals and log cumulative hazard plots ",
"have been printed."
))
cli::cli_alert_info(c(
"PH tests may not always agree, so ",
"consider the results of all tests and plots in totality."
))
cli::cli_rule()
cli::cli_alert(c(
"For more information, run {.code View()} ",
"on saved {.fn test_ph} output."
))
# Actually print at the end to avoid cli being too fast.
print(x$schoenfeld_plot)
print(x$cloglog_plot)
invisible(x)
}