Learn R Programming

riskRegression (version 2018.04.21)

autoplot.predictCox: Plot predictions from a Cox model

Description

Plot predictions from a Cox model

Usage

# S3 method for predictCox
autoplot(object, type = NULL, ci = FALSE,
  band = FALSE, group.by = "row", reduce.data = FALSE, plot = TRUE,
  digits = 2, alpha = NA, ...)

Arguments

object

object obtained with the function predictCox.

type

the type of predicted value to display. Choices are "hazard" the hazard function, "cumhazard" the cumulative hazard function, or "survival" the survival function.

ci

Logical. If TRUE display the confidence intervals for the predictions.

band

Logical. If TRUE display the confidence bands for the predictions.

group.by

The grouping factor used to color the prediction curves. Can be "row", "strata", or "covariates".

reduce.data

Logical. If TRUE only the covariates that does take indentical values for all observations are displayed.

plot

Logical. Should the graphic be plotted.

digits

integer indicating the number of decimal places

alpha

transparency of the confidence bands. Argument passed to ggplot2::geom_ribbon.

...

not used. Only for compatibility with the plot method.

Examples

Run this code
# NOT RUN {
library(survival)
library(ggplot2)

## predictions ##
d <- sampleData(1e2, outcome = "survival")
m.cox <- coxph(Surv(time,event)~ X1 + X2 + X3,
                data = d, x = TRUE, y = TRUE)
dt.basehaz <- predictCox(m.cox)
ggplot(as.data.table(dt.basehaz), aes(x = time, y = survival)) + geom_point() + geom_line()

pred.cox <- predictCox(m.cox, newdata = d[1:4,],
  times = 1:5, type = "survival", keep.newdata = TRUE)
autoplot(pred.cox)
autoplot(pred.cox, group.by = "covariates")
autoplot(pred.cox, group.by = "covariates", reduce.data = TRUE)


m.cox.strata <- coxph(Surv(time,event)~ strata(X1) + strata(X2) + X3 + X6,
data = d, x = TRUE, y = TRUE)
pred.cox.strata <- predictCox(m.cox.strata, newdata = d[1,,drop=FALSE],
time = 1:5, keep.newdata = TRUE)
autoplot(pred.cox.strata, type = "survival")
autoplot(pred.cox.strata, type = "survival", group.by = "strata")
res <- autoplot(pred.cox.strata, type = "survival",
            group.by = "covariates")

# customize display
res$plot + geom_point(data = res$data, size = 5)

## predictions with confidence interval
pred.cox <- predictCox(m.cox, newdata = d[1,,drop=FALSE],
  times = 1:5, type = "survival", se = TRUE, keep.newdata = TRUE)
autoplot(pred.cox, ci = TRUE)

## predictions with confidence bands
pred.cox <- predictCox(m.cox, newdata = d[1,,drop=FALSE],
  times = 1:5, type = "survival", nsim.band = 500,  band = TRUE, keep.newdata = TRUE)
autoplot(pred.cox, band = TRUE)


# }

Run the code above in your browser using DataLab