Plot Predictions From a Cox Model
Plot predictions from a Cox model.
## S3 method for class 'predictCox' autoplot( object, type = NULL, ci = object$se, band = object$band, plot = TRUE, smooth = FALSE, digits = 2, alpha = NA, group.by = "row", reduce.data = FALSE, ylab = NULL, first.derivative = FALSE, ... )
object |
Object obtained with the function |
type |
[character] The type of predicted value to display.
Choices are:
|
ci |
[logical] If |
band |
[logical] If |
plot |
[logical] Should the graphic be plotted. |
smooth |
[logical] Should a smooth version of the risk function be plotted instead of a simple function? |
digits |
[integer] Number of decimal places when displaying the values of the covariates in the caption. |
alpha |
[numeric, 0-1] Transparency of the confidence bands. Argument passed to |
group.by |
[character] The grouping factor used to color the prediction curves. Can be |
reduce.data |
[logical] If |
ylab |
[character] Label for the y axis. |
first.derivative |
[logical] If |
... |
Additional parameters to cutomize the display. |
Invisible. A list containing:
plot: the ggplot object.
data: the data used to create the plot.
predictCox
to compute cumulative hazard and survival based on a Cox model.
library(survival) library(ggplot2) #### simulate data #### set.seed(10) d <- sampleData(1e2, outcome = "survival") seqTau <- c(0,sort(unique(d$time[d$event==1])), max(d$time)) #### Cox model #### m.cox <- coxph(Surv(time,event)~ X1 + X2 + X3, data = d, x = TRUE, y = TRUE) ## display baseline hazard e.basehaz <- predictCox(m.cox) autoplot(e.basehaz, type = "cumhazard") autoplot(e.basehaz, type = "cumhazard", smooth = TRUE) autoplot(e.basehaz, type = "cumhazard", smooth = TRUE, first.derivative = TRUE) ## display baseline hazard with type of event e.basehaz <- predictCox(m.cox, keep.newdata = TRUE) autoplot(e.basehaz, type = "cumhazard") ## display predicted survival pred.cox <- predictCox(m.cox, newdata = d[1:2,], times = seqTau, type = "survival", keep.newdata = TRUE) autoplot(pred.cox) autoplot(pred.cox, smooth = TRUE) autoplot(pred.cox, group.by = "covariates") autoplot(pred.cox, group.by = "covariates", reduce.data = TRUE) ## predictions with confidence interval/bands pred.cox <- predictCox(m.cox, newdata = d[1:2,,drop=FALSE], times = seqTau, type = "survival", band = TRUE, se = TRUE, keep.newdata = TRUE) res <- autoplot(pred.cox, ci = TRUE, band = TRUE, plot = FALSE) res$plot + facet_wrap(~row) res2 <- autoplot(pred.cox, ci = TRUE, band = TRUE, alpha = 0.1, plot = FALSE) res2$plot + facet_wrap(~row) #### Stratified Cox model #### m.cox.strata <- coxph(Surv(time,event)~ strata(X1) + strata(X2) + X3 + X4, data = d, x = TRUE, y = TRUE) ## baseline hazard pred.baseline <- predictCox(m.cox.strata, keep.newdata = TRUE, type = "survival") res <- autoplot(pred.baseline) res$plot + facet_wrap(~strata, labeller = label_both) ## predictions pred.cox.strata <- predictCox(m.cox.strata, newdata = d[1:5,,drop=FALSE], time = seqTau, keep.newdata = TRUE, se = TRUE) res2 <- autoplot(pred.cox.strata, type = "survival", group.by = "strata", plot = FALSE) res2$plot + facet_wrap(~strata, labeller = label_both) + theme(legend.position="bottom") ## smooth version autoplot(pred.cox.strata, type = "survival", group.by = "strata", smooth = TRUE, ci = FALSE)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.