Search This Blog

Tuesday, January 24, 2012

Plot Step Functions : plot.stepfun()


plot.stepfun {stepfun}R Documentation

Plot Step Functions

Description

Method of the generic plot for stepfun objects and utility for plotting piecewise constant functions.

Usage

## S3 method for class 'stepfun':
plot(x, xval, xlim, ylim,
     xlab = "x", ylab = "f(x)", main = NULL,
     add = FALSE, verticals = TRUE, do.points = TRUE,
     pch = par("pch"), col.points=par("col"), cex.points=par("cex"),
     col.hor = par("col"), col.vert= par("col"),
     lty = par("lty"), lwd = par("lwd"), ...)

## S3 method for class 'stepfun':
lines(x, ...)

Arguments

x an R object inheriting from "stepfun".
xval numeric vector of abscissa values at which to evaluate x. Defaults to knots(x) restricted to xlim.
xlim,ylim numeric(2) each; range of x or y values to use. Both have sensible defaults.
xlab,ylab labels of x and y axis.
main main title.
add logical; if TRUE only add to an existing plot.
verticals logical; if TRUE, draw vertical lines at steps.
do.points logical; if true, also draw points at the (xlim restricted) knot locations.
pch character; point character if do.points.
col.points character or integer code; color of points if do.points.
cex.points numeric; character expansion factor if do.points.
col.hor color of horizontal lines.
col.vert color of vertical lines.
lty, lwd line type and thickness for all lines.
... further arguments of plot(.), or if(add) segments(.).

Value

A list with two components
t abscissa (x) values, including the two outermost ones.
y y values ‘in between’ the t[].

Author(s)

Martin Maechler maechler@stat.math.ethz.ch, 1990, 1993; ported to R, 1997.

See Also

ecdf for empirical distribution functions as special step functions, approxfun and splinefun.

Examples

y0 <- c(1,2,4,3)
sfun0  <- stepfun(1:3, y0, f = 0)
sfun.2 <- stepfun(1:3, y0, f = .2)
sfun1  <- stepfun(1:3, y0, right = TRUE)

tt <- seq(0,3, by=0.1)
op <- par(mfrow=c(2,2))
plot(sfun0); plot(sfun0, xval=tt, add=TRUE, col.h="bisque")
plot(sfun.2);plot(sfun.2,xval=tt, add=TRUE, col.h="orange")
plot(sfun1);lines(sfun1, xval=tt, col.h="coral")
##-- This is  revealing :
plot(sfun0, verticals= FALSE,
     main = "stepfun(x, y0, f=f)  for f = 0, .2, 1")
for(i in 1:3)
  lines(list(sfun0,sfun.2,stepfun(1:3,y0,f = 1))[[i]], col.h=i, col.v=i)
legend(2.5, 1.9, paste("f =", c(0,0.2,1)), col=1:3, lty=1, y.inter=1); par(op)

# Extend and/or restrict 'viewport':
plot(sfun0, xlim = c(0,5), ylim = c(0, 3.5),
     main = "plot(stepfun(*), xlim= . , ylim = .)")

##-- this works too (automatic call to  ecdf(.)):
plot.stepfun(rt(50, df=3), col.vert = "gray20")

Empirical Cumulative Distribution Function


ecdf {stepfun}R Documentation

Empirical Cumulative Distribution Function

Description

Compute or plot an empirical cumulative distribution function.

Usage

ecdf(x)

## S3 method for class 'ecdf':
plot(x, ..., ylab="Fn(x)", verticals = FALSE,
     col.01line = "gray70")

## S3 method for class 'ecdf':
print(x, digits= getOption("digits") - 2, ...)

Arguments

x numeric vector of “observations” in ecdf; for the methods, an object of class "ecdf", typically.
... arguments to be passed to subsequent methods, i.e., plot.stepfun for the plot method.
ylab label for the y-axis.
verticals see plot.stepfun.
col.01line numeric or character specifying the color of the horizontal lines at y=0 and 1, see colors.
digits number of significant digits to use, see print.

Details

The e.c.d.f. (empirical cumulative distribution function) Fn is a step function with jump 1/n at each observation (possibly with multiple jumps at one place if there are ties).
For observations x= (x1,x2, ... xn), Fn is the fraction of observations less or equal to t, i.e.,
Fn(t) = #{x_i <= t} / n = 1/n sum(i=1,n) Indicator(xi <= t).
The function plot.ecdf which implements the plot method for ecdf objects, is implemented via a call to plot.stepfun; see its documentation.

Value

For ecdf, a function of class "ecdf", inheriting from the "stepfun" class.

Author(s)

Martin Maechler, maechler@stat.math.ethz.ch.

See Also

stepfun, the more general class of step functions, approxfun and splinefun.

Examples

##-- Simple didactical  ecdf  example:
Fn <- ecdf(rnorm(12))
Fn; summary(Fn)
12*Fn(knots(Fn)) == 1:12 ## == 1:12  if and only if there are no ties !

y <- round(rnorm(12),1); y[3] <- y[1]
Fn12 <- ecdf(y)
Fn12
print(knots(Fn12), dig=2)
12*Fn12(knots(Fn12)) ## ~= 1:12  if there where no ties

summary(Fn12)
summary.stepfun(Fn12)
print(ls.Fn12 <- ls(env= environment(Fn12)))
##[1] "f"  "method"  "n"  "ties"   "x"  "y"  "yleft"  "yright"

12 * Fn12((-20:20)/10)

###----------------- Plotting --------------------------

op <- par(mfrow=c(3,1), mgp=c(1.5, 0.8,0), mar= .1+c(3,3,2,1))

F10 <- ecdf(rnorm(10))
summary(F10)

plot(F10)
plot(F10, verticals= TRUE, do.p = FALSE)

plot(Fn12)# , lwd=2) dis-regarded
xx <- unique(sort(c(seq(-3,2, length=201), knots(Fn12))))
lines(xx, Fn12(xx), col='blue')
abline(v=knots(Fn12),lty=2,col='gray70')

plot(xx, Fn12(xx), type='b', cex=.1)#- plot.default
plot(Fn12, col.h='red', add= TRUE)  #- plot method
abline(v=knots(Fn12),lty=2,col='gray70')
plot(Fn12, verticals=TRUE, col.p='blue', col.h='red',col.v='bisque')
par(op)

##-- this works too (automatic call to  ecdf(.)):
plot.ecdf(rnorm(24))

Add Lines or Points to a Survival Plot


lines.survfit {survival}R Documentation

Add Lines or Points to a Survival Plot

Description

Often used to add the expected survival curve(s) to a Kaplan-Meier plot generated with plot.survfit.

Usage

## S3 method for class 'survfit':
lines(x, type="s", mark=3, col=1, lty=1,
lwd=1, mark.time=TRUE , xscale=1,  firstx=0, firsty=1, xmax, fun, conf.int=FALSE,  ...)
## S3 method for class 'survfit':
points(x, ...)

Arguments

x a survival object, generated from the survfit or survexp functions.
type the line type, as described in lines. The default is a step function for survfit objects, and a connected line for survexp objects.
mark vectors giving the mark symbol, color, line type and line width for the added curves.
mark.time controls the labeling of the curves. If FALSE, no labeling is done. If TRUE, then curves are marked at each censoring time. If mark.time is a numeric vector, then curves are marked at the specified time points.
xscale a number used to divide the x values. If time was originally in days, a value of 365.24 would give a plotted scale in years.
firstx,firsty the starting point for the survival curves. If either of these is set to NA or the plot will start at the first time point of the curve.
col,lty,lwd,... passed to lines
xmax the maximum horizontal plot coordinate. This shortens the curve before plotting it, so unlike using the xlim graphical parameter, warning messages about out of bounds points are not generated.
fun an arbitrary function defining a transformation of the survival curve. For example fun=log is an alternative way to draw a log-survival curve (but with the axis labeled with log(S) values). Four often used transformations can be specified with a character argument instead: "log" is the same as using the log=T option, "event" plots cumulative events (f(y) =1-y), "cumhaz" plots the cumulative hazard function (f(y) = -log(y)) and "cloglog" creates a complementary log-log survival plot (f(y) = log(-log(y)) along with log scale for the x-axis).
conf.int if TRUE, confidence bands for the curves are also plotted. If set to "only", then only the CI bands are plotted, and the curve itself is left off. This can be useful for fine control over the colors or line types of a plot.

Value

a list with components x and y, containing the coordinates of the last point on each of the curves (but not of the confidence limits). This may be useful for labeling.

Side Effects

one or more curves are added to the current plot.

See Also

lines, par, plot.survfit, survfit, survexp.

Examples

fit <- survfit(Surv(time, status) ~ sex, pbc,subset=1:312)
plot(fit, mark.time=FALSE, xscale=365.24,
        xlab='Years', ylab='Survival')
lines(fit[1], lwd=2, xscale=365.24)    #darken the first curve and add marks

# add expected survival curves for the two groups,
#   based on the US census data
tdata <- data.frame(age=pbc$age*365.24, sex=pbc$sex +1,
                    year= rep(mdy.date(1,1,1976), nrow(pbc)))
tdata<-tdata[1:312,] ## only the randomised people, with no missing data

efit <- survexp(~ sex+ratetable(sex=sex,age=age,year=year), data=tdata, ratetable=survexp.us, times=(0:24)*182)
temp <- lines(efit, lty=2, xscale=365.24, lwd=2:1)
text(temp, c("Male", "Female"), adj= -.1) #labels just past the ends
title(main="Primary Biliary Cirrhosis, Observed and Expected")

[Package survival version 2.32 Index]

Plot empirical and fitted survival curves

Plot empirical and fitted survival curves

Description

Plot a Kaplan-Meier estimate of the survival probability and compare it with the fitted survival probability from a msm model.

Usage

plot.survfit.msm(x, from=1, to=NULL, range=NULL, covariates="mean",
                 interp=c("start","midpoint"), ci=c("none","normal","bootstrap"), B=100,
                 legend.pos=NULL, xlab="Time", ylab="Survival probability", lwd=1, ...)

Arguments

x Output from msm, representing a fitted multi-state model object.
from State from which to consider survival. Defaults to state 1.
to Absorbing state to consider. Defaults to the highest-labelled absorbing state.
range Vector of two elements, giving the range of times to plot for.
covariates Covariate values for which to evaluate the expected probabilities. This can either be:

the string "mean", denoting the means of the covariates in the data (this is the default),

the number 0, indicating that all the covariates should be set to zero,

or a list of values, with optional names. For example
list (60, 1)
where the order of the list follows the order of the covariates originally given in the model formula, or a named list,
list (age = 60, sex = 1)
ci If "none" (the default) no confidence intervals are plotted. If "normal" or "bootstrap", confidence intervals are plotted based on the respective method in pmatrix.msm. This is very computationally-intensive, since intervals must be computed at a series of times.
B Number of bootstrap or normal replicates for the confidence interval. The default is 100 rather than the usual 1000, since these plots are for rough diagnostic purposes.
interp If interp="start" (the default) then the entry time into the absorbing state is assumed to be the time it is first observed in the data.
If interp="midpoint" then the entry time into the absorbing state is assumed to be halfway between the time it is first observed and the previous observation time. This is generally more reasonable for "progressive" models with observations at arbitrary times.
legend.pos Vector of the x and y position, respectively, of the legend.
xlab x axis label.
ylab y axis label.
lwd Line width. See par.
... Other arguments to be passed to the plot.survfit and lines.survfit functions.

Details

If the data represent observations of the process at arbitrary times, then the first occurrence of the absorbing state in the data will usually be greater than the actual first transition time to that state. Therefore the Kaplan-Meier estimate of the survival probability will be an overestimate.
This currently only handles time-homogeneous models.

See Also

survfit, plot.survfit, plot.prevalence.msm

Plot Step Functions

Plot Step Functions

Description

Method of the generic plot for stepfun objects and utility for plotting piecewise constant functions.

Usage

## S3 method for class 'stepfun':
plot(x, xval, xlim, ylim,
     xlab = "x", ylab = "f(x)", main = NULL,
     add = FALSE, verticals = TRUE, do.points = TRUE,
     pch = par("pch"), col.points=par("col"), cex.points=par("cex"),
     col.hor = par("col"), col.vert= par("col"),
     lty = par("lty"), lwd = par("lwd"), ...)

## S3 method for class 'stepfun':
lines(x, ...)

Arguments

x an R object inheriting from "stepfun".
xval numeric vector of abscissa values at which to evaluate x. Defaults to knots(x) restricted to xlim.
xlim,ylim numeric(2) each; range of x or y values to use. Both have sensible defaults.
xlab,ylab labels of x and y axis.
main main title.
add logical; if TRUE only add to an existing plot.
verticals logical; if TRUE, draw vertical lines at steps.
do.points logical; if true, also draw points at the (xlim restricted) knot locations.
pch character; point character if do.points.
col.points character or integer code; color of points if do.points.
cex.points numeric; character expansion factor if do.points.
col.hor color of horizontal lines.
col.vert color of vertical lines.
lty, lwd line type and thickness for all lines.
... further arguments of plot(.), or if(add) segments(.).

Value

A list with two components
t abscissa (x) values, including the two outermost ones.
y y values ‘in between’ the t[].

Author(s)

Martin Maechler maechler@stat.math.ethz.ch, 1990, 1993; ported to R, 1997.

See Also

ecdf for empirical distribution functions as special step functions, approxfun and splinefun.

Examples

y0 <- c(1,2,4,3)
sfun0  <- stepfun(1:3, y0, f = 0)
sfun.2 <- stepfun(1:3, y0, f = .2)
sfun1  <- stepfun(1:3, y0, right = TRUE)

tt <- seq(0,3, by=0.1)
op <- par(mfrow=c(2,2))
plot(sfun0); plot(sfun0, xval=tt, add=TRUE, col.h="bisque")
plot(sfun.2);plot(sfun.2,xval=tt, add=TRUE, col.h="orange")
plot(sfun1);lines(sfun1, xval=tt, col.h="coral")
##-- This is  revealing :
plot(sfun0, verticals= FALSE,
     main = "stepfun(x, y0, f=f)  for f = 0, .2, 1")
for(i in 1:3)
  lines(list(sfun0,sfun.2,stepfun(1:3,y0,f = 1))[[i]], col.h=i, col.v=i)
legend(2.5, 1.9, paste("f =", c(0,0.2,1)), col=1:3, lty=1, y.inter=1); par(op)

# Extend and/or restrict 'viewport':
plot(sfun0, xlim = c(0,5), ylim = c(0, 3.5),
     main = "plot(stepfun(*), xlim= . , ylim = .)")

##-- this works too (automatic call to  ecdf(.)):
plot.stepfun(rt(50, df=3), col.vert = "gray20")