Learn R Programming

riskRegression (version 1.4.3)

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, groupBy = "row", reduce.data = FALSE, plot = TRUE,
  digits = 2, alpha = NA, ...)

Arguments

object

object obtained with the function predictCox.

type

the type of predicted value. Choices are

  • "hazard" the baseline hazard function when argument newdata is not used and the hazard function when argument newdata is used.

  • "cumhazard" the cumulative baseline hazard function when argument newdata is not used and the cumulative hazard function when argument newdata is used.

  • "survival" the survival baseline hazard function when argument newdata is not used and the cumulative hazard function when argument newdata is used.

Several choices can be combined in a vector of strings that match (no matter the case) strings "hazard","cumhazard", "survival".

ci

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

band

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

groupBy

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, groupBy = "covariates")
autoplot(pred.cox, groupBy = "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", groupBy = "strata")
res <- autoplot(pred.cox.strata, type = "survival",
            groupBy = "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