Plot Survival Curves and Hazard Functions
Plot estimated survival curves, and for parametric survival models, plot
hazard functions. There is an option to print the number of subjects
at risk at the start of each time interval. Curves are automatically
labeled at the points of maximum separation (using the labcurve
function), and there are many other options for labeling that can be
specified with the label.curves
parameter. For example, different
plotting symbols can be placed at constant x-increments and a legend
linking the symbols with category labels can automatically positioned on
the most empty portion of the plot.
For the case of a two stratum analysis by npsurv
,
survdiffplot
plots the difference in two Kaplan-Meier estimates
along with approximate confidence bands for the differences, with a
reference line at zero. The number of subjects at risk is optionally
plotted. This number is taken as the minimum of the number of subjects
at risk over the two strata. When conf='diffbands'
,
survdiffplot
instead does not make a new plot but adds a shaded
polygon to an existing plot, showing the midpoint of two survival
estimates plus or minus 1/2 the width of the confidence interval for the
difference of two Kaplan-Meier estimates.
survplotp
creates an interactive plotly
graphic with
shaded confidence bands. In the two strata case, it draws the 1/2
confidence bands for the difference in two probabilities centered at the
midpoint of the probability estimates, so that where the two curves
touch this band there is no significant difference (no multiplicity
adjustment is made). For the two strata case, the two individual
confidence bands have entries in the legend but are not displayed until
the user clicks on the legend.
When code
was from running npsurv
on a
multi-state/competing risk Surv
object, survplot
plots
cumulative incidence curves properly accounting for competing risks.
You must specify exactly one state/event cause to plot using the
state
argument. survplot
will not plot multiple states on
one graph. This can be accomplished using multiple calls with different
values of state
and specifying add=TRUE
for all but the
first call.
survplot(fit, ...) survplotp(fit, ...) ## S3 method for class 'rms' survplot(fit, ..., xlim, ylim=if(loglog) c(-5, 1.5) else if (what == "survival" & missing(fun)) c(0, 1), xlab, ylab, time.inc, what=c("survival","hazard"), type=c("tsiatis","kaplan-meier"), conf.type=c("log","log-log","plain","none"), conf.int=FALSE, conf=c("bands","bars"), mylim=NULL, add=FALSE, label.curves=TRUE, abbrev.label=FALSE, levels.only=FALSE, lty, lwd=par("lwd"), col=1, col.fill=gray(seq(.95, .75, length=5)), adj.subtitle=TRUE, loglog=FALSE, fun, n.risk=FALSE, logt=FALSE, dots=FALSE, dotsize=.003, grid=NULL, srt.n.risk=0, sep.n.risk=0.056, adj.n.risk=1, y.n.risk, cex.n.risk=.6, cex.xlab=par('cex.lab'), cex.ylab=cex.xlab, pr=FALSE) ## S3 method for class 'npsurv' survplot(fit, xlim, ylim, xlab, ylab, time.inc, state=NULL, conf=c("bands","bars","diffbands","none"), mylim=NULL, add=FALSE, label.curves=TRUE, abbrev.label=FALSE, levels.only=FALSE, lty,lwd=par('lwd'), col=1, col.fill=gray(seq(.95, .75, length=5)), loglog=FALSE, fun, n.risk=FALSE, aehaz=FALSE, times=NULL, logt=FALSE, dots=FALSE, dotsize=.003, grid=NULL, srt.n.risk=0, sep.n.risk=.056, adj.n.risk=1, y.n.risk, cex.n.risk=.6, cex.xlab=par('cex.lab'), cex.ylab=cex.xlab, pr=FALSE, ...) ## S3 method for class 'npsurv' survplotp(fit, xlim, ylim, xlab, ylab, time.inc, state=NULL, conf=c("bands", "none"), mylim=NULL, abbrev.label=FALSE, col=colorspace::rainbow_hcl, levels.only=TRUE, loglog=FALSE, fun=function(y) y, aehaz=FALSE, times=NULL, logt=FALSE, pr=FALSE, ...) survdiffplot(fit, order=1:2, fun=function(y) y, xlim, ylim, xlab, ylab="Difference in Survival Probability", time.inc, conf.int, conf=c("shaded", "bands","diffbands","none"), add=FALSE, lty=1, lwd=par('lwd'), col=1, n.risk=FALSE, grid=NULL, srt.n.risk=0, adj.n.risk=1, y.n.risk, cex.n.risk=.6, cex.xlab=par('cex.lab'), cex.ylab=cex.xlab, convert=function(f) f)
fit |
result of fit ( |
... |
list of factors with names used in model. For fits from |
xlim |
a vector of two numbers specifiying the x-axis range for follow-up time.
Default is |
ylim |
y-axis limits. Default is |
xlab |
x-axis label. Default is |
ylab |
y-axis label. Default is |
time.inc |
time increment for labeling the x-axis and printing numbers at risk.
If not specified, the value
of |
state |
the state/event cause to use in plotting if the fit was for
a multi-state/competing risk |
type |
specifies type of estimates, |
conf.type |
specifies the basis for confidence limits. This argument is
ignored for fits from |
conf.int |
Default is |
conf |
|
mylim |
used to curtail computed |
what |
defaults to |
add |
set to |
label.curves |
default is |
abbrev.label |
set to |
levels.only |
set to |
lty |
vector of line types to use for different factor levels. Default is
|
lwd |
vector of line widths to use for different factor levels. Default is
current |
col |
color for curve, default is |
col.fill |
a vector of colors to used in filling confidence bands |
adj.subtitle |
set to |
loglog |
set to |
fun |
specifies any function to translate estimates and confidence limits
before plotting. If the fit is a multi-state object the default for
|
logt |
set to |
n.risk |
set to |
srt.n.risk |
angle of rotation for leftmost number of subjects at risk (since this number
may run into the second or into the y-axis). Default is |
adj.n.risk |
justification for leftmost number at risk. Default is |
sep.n.risk |
multiple of upper y limit - lower y limit for separating lines of text
containing number of subjects at risk. Default is |
y.n.risk |
When |
cex.n.risk |
character size for number of subjects at risk (when |
cex.xlab |
|
cex.ylab |
|
dots |
set to |
dotsize |
size of dots in inches |
grid |
defaults to |
pr |
set to |
aehaz |
set to |
times |
a numeric vector of times at which to compute cumulative incidence probability estimates to add to curve labels |
order |
an integer vector of length two specifying the order of groups when
computing survival differences. The default of |
convert |
a function to convert the output of
|
survplot
will not work for Cox models with time-dependent covariables.
Use survest
or survfit
for that purpose.
There is a set a system option mgp.axis.labels
to allow x
and y-axes to have differing mgp
graphical parameters (see par
).
This is important when labels for y-axis tick marks are to be written
horizontally (par(las=1)
), as a larger gap between the labels and
the tick marks are needed. You can set the axis-specific 2nd
component of mgp
using mgp.axis.labels(c(xvalue,yvalue))
.
list with components adjust (text string specifying adjustment levels)
and curve.labels
(vector of text strings corresponding to levels
of factor used to distinguish curves). For npsurv
, the returned
value is the vector of strata labels, or NULL if there are no strata.
plots. If par()$mar[4] < 4
, issues par(mar=)
to increment mar[4]
by 2
if n.risk=TRUE
and add=FALSE
. The user may want to reset par(mar)
in
this case to not leave such a wide right margin for plots. You usually
would issue par(mar=c(5,4,4,2)+.1)
.
# Simulate data from a population model in which the log hazard # function is linear in age and there is no age x sex interaction n <- 1000 set.seed(731) age <- 50 + 12*rnorm(n) label(age) <- "Age" sex <- factor(sample(c('male','female'), n, TRUE)) cens <- 15*runif(n) h <- .02*exp(.04*(age-50)+.8*(sex=='female')) dt <- -log(runif(n))/h label(dt) <- 'Follow-up Time' e <- ifelse(dt <= cens,1,0) dt <- pmin(dt, cens) units(dt) <- "Year" dd <- datadist(age, sex) options(datadist='dd') S <- Surv(dt,e) # When age is in the model by itself and we predict at the mean age, # approximate confidence intervals are ok f <- cph(S ~ age, surv=TRUE) survplot(f, age=mean(age), conf.int=.95) g <- cph(S ~ age, x=TRUE, y=TRUE) survplot(g, age=mean(age), conf.int=.95, add=TRUE, col='red', conf='bars') # Repeat for an age far from the mean; not ok survplot(f, age=75, conf.int=.95) survplot(g, age=75, conf.int=.95, add=TRUE, col='red', conf='bars') #Plot stratified survival curves by sex, adj for quadratic age effect # with age x sex interaction (2 d.f. interaction) f <- cph(S ~ pol(age,2)*strat(sex), x=TRUE, y=TRUE) #or f <- psm(S ~ pol(age,2)*sex) Predict(f, sex, age=c(30,50,70)) survplot(f, sex, n.risk=TRUE, levels.only=TRUE) #Adjust age to median survplot(f, sex, logt=TRUE, loglog=TRUE) #Check for Weibull-ness (linearity) survplot(f, sex=c("male","female"), age=50) #Would have worked without datadist #or with an incomplete datadist survplot(f, sex, label.curves=list(keys=c(2,0), point.inc=2)) #Identify curves with symbols survplot(f, sex, label.curves=list(keys=c('m','f'))) #Identify curves with single letters #Plots by quintiles of age, adjusting sex to male options(digits=3) survplot(f, age=quantile(age,(1:4)/5), sex="male") #Plot survival Kaplan-Meier survival estimates for males f <- npsurv(S ~ 1, subset=sex=="male") survplot(f) #Plot survival for both sexes and show exponential hazard estimates f <- npsurv(S ~ sex) survplot(f, aehaz=TRUE) #Check for log-normal and log-logistic fits survplot(f, fun=qnorm, ylab="Inverse Normal Transform") survplot(f, fun=function(y)log(y/(1-y)), ylab="Logit S(t)") #Plot the difference between sexes survdiffplot(f) #Similar but show half-width of confidence intervals centered #at average of two survival estimates survplot(f, conf='diffbands') options(datadist=NULL) ## Not run: # # Time to progression/death for patients with monoclonal gammopathy # Competing risk curves (cumulative incidence) # status variable must be a factor with first level denoting right censoring m <- upData(mgus1, stop = stop / 365.25, units=c(stop='years'), labels=c(stop='Follow-up Time'), subset=start == 0) f <- npsurv(Surv(stop, event) ~ 1, data=m) # Use survplot for enhanced displays of cumulative incidence curves for # competing risks survplot(f, state='pcm', n.risk=TRUE, xlim=c(0, 20), ylim=c(0, .5), col=2) survplot(f, state='death', aehaz=TRUE, col=3, label.curves=list(keys='lines')) f <- npsurv(Surv(stop, event) ~ sex, data=m) survplot(f, state='death', aehaz=TRUE, n.risk=TRUE, conf='diffbands', label.curves=list(keys='lines')) ## End(Not run)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.