The R Project SVN R

Rev

Rev 61433 | Rev 85904 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/graphics/man/cdplot.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
59039 ripley 3
% Copyright 1995-2007 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
35517 hornik 6
\name{cdplot}
56186 murdoch 7
\alias{cdplot}
35517 hornik 8
\alias{cdplot.default}
9
\alias{cdplot.formula}
10
\title{Conditional Density Plots}
11
\description{
12
  Computes and plots conditional densities describing how the
13
  conditional distribution of a categorical variable \code{y} changes over a
14
  numerical variable \code{x}.
15
}
16
\usage{
17
cdplot(x, \dots)
36139 ripley 18
 
35517 hornik 19
\method{cdplot}{default}(x, y,
41355 hornik 20
  plot = TRUE, tol.ylab = 0.05, ylevels = NULL,
35517 hornik 21
  bw = "nrd0", n = 512, from = NULL, to = NULL,
22
  col = NULL, border = 1, main = "", xlab = NULL, ylab = NULL,
35599 hornik 23
  yaxlabels = NULL, xlim = NULL, ylim = c(0, 1), \dots)
36139 ripley 24
 
35517 hornik 25
\method{cdplot}{formula}(formula, data = list(),
41355 hornik 26
  plot = TRUE, tol.ylab = 0.05, ylevels = NULL,
35517 hornik 27
  bw = "nrd0", n = 512, from = NULL, to = NULL,
28
  col = NULL, border = 1, main = "", xlab = NULL, ylab = NULL,
35600 hornik 29
  yaxlabels = NULL, xlim = NULL, ylim = c(0, 1), \dots,
30
  subset = NULL)
35517 hornik 31
}
32
\arguments{
52994 hornik 33
  \item{x}{an object, the default method expects a single numerical
34
    variable (or an object coercible to this).}
35517 hornik 35
  \item{y}{a \code{"factor"} interpreted to be the dependent variable}
36
  \item{formula}{a \code{"formula"} of type \code{y ~ x} with a single dependent
61433 ripley 37
    \code{"factor"} and a single numerical explanatory variable.}
35517 hornik 38
  \item{data}{an optional data frame.}
39
  \item{plot}{logical. Should the computed conditional densities be plotted?}
40
  \item{tol.ylab}{convenience tolerance parameter for y-axis annotation.
41
    If the distance between two labels drops under this threshold, they are
42
    plotted equidistantly.}
41355 hornik 43
  \item{ylevels}{a character or numeric vector specifying in which order
44
    the levels of the dependent variable should be plotted.}
35517 hornik 45
  \item{bw, n, from, to, \dots}{arguments passed to \code{\link{density}}}
46
  \item{col}{a vector of fill colors of the same length as \code{levels(y)}.
61433 ripley 47
    The default is to call \code{\link{gray.colors}}.}
48
  \item{border}{border color of shaded polygons.}
35517 hornik 49
  \item{main, xlab, ylab}{character strings for annotation}
35533 hornik 50
  \item{yaxlabels}{character vector for annotation of y axis, defaults to
51
    \code{levels(y)}.}
35599 hornik 52
  \item{xlim, ylim}{the range of x and y values with sensible defaults.}
35600 hornik 53
  \item{subset}{an optional vector specifying a subset of observations
54
    to be used for plotting.}
35517 hornik 55
}
56
\details{
57
  \code{cdplot} computes the conditional densities of \code{x} given
58
  the levels of \code{y} weighted by the marginal distribution of \code{y}.
59
  The densities are derived cumulatively over the levels of \code{y}.
60
 
61
  This visualization technique is similar to spinograms (see \code{\link{spineplot}})
62
  and plots \eqn{P(y | x)} against \eqn{x}. The conditional probabilities
40281 ripley 63
  are not derived by discretization (as in the spinogram), but using a smoothing
35517 hornik 64
  approach via \code{\link{density}}.
61433 ripley 65
 
66
  Note, that the estimates of the conditional densities are more reliable for
35517 hornik 67
  high-density regions of \eqn{x}. Conversely, the are less reliable in regions
68
  with only few \eqn{x} observations.
69
}
70
\value{
71
  The conditional density functions (cumulative over the levels of \code{y})
72
  are returned invisibly.
73
}
74
\seealso{
75
  \code{\link{spineplot}}, \code{\link{density}}
76
}
77
\references{
78
  Hofmann, H., Theus, M. (2005), \emph{Interactive graphics for visualizing
79
  conditional distributions}, Unpublished Manuscript.
80
}
81
\author{
82
  Achim Zeileis \email{Achim.Zeileis@R-project.org}
83
}
84
\examples{
85
## NASA space shuttle o-ring failures
44236 ripley 86
fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1,
87
                 1, 2, 1, 1, 1, 1, 1),
35517 hornik 88
               levels = 1:2, labels = c("no", "yes"))
44236 ripley 89
temperature <- c(53, 57, 58, 63, 66, 67, 67, 67, 68, 69, 70, 70,
90
                 70, 70, 72, 73, 75, 75, 76, 76, 78, 79, 81)
35517 hornik 91
 
92
## CD plot
93
cdplot(fail ~ temperature)
94
cdplot(fail ~ temperature, bw = 2)
95
cdplot(fail ~ temperature, bw = "SJ")
96
 
97
## compare with spinogram
98
(spineplot(fail ~ temperature, breaks = 3))
99
 
41355 hornik 100
## highlighting for failures
101
cdplot(fail ~ temperature, ylevels = 2:1)
102
 
35517 hornik 103
## scatter plot with conditional density
104
cdens <- cdplot(fail ~ temperature, plot = FALSE)
105
plot(I(as.numeric(fail) - 1) ~ jitter(temperature, factor = 2),
106
     xlab = "Temperature", ylab = "Conditional failure probability")
107
lines(53:81, 1 - cdens[[1]](53:81), col = 2)
108
}
109
\keyword{hplot}