The R Project SVN R

Rev

Rev 61168 | Rev 65999 | 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/stats/man/termplot.Rd
2
% Part of the R package, http://www.R-project.org
59337 maechler 3
% Copyright 1995-2012 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
57939 hornik 6
\newcommand{\CRANpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}
7
 
27442 ripley 8
\name{termplot}
56186 murdoch 9
\alias{termplot}
50821 ripley 10
\title{Plot Regression Terms}
27442 ripley 11
\description{
12
  Plots regression terms against their predictors, optionally with
13
  standard errors and partial residuals added.
14
}
15
\usage{
42103 ripley 16
termplot(model, data = NULL, envir = environment(formula(model)),
17
         partial.resid = FALSE, rug = FALSE,
44243 ripley 18
         terms = NULL, se = FALSE,
19
         xlabs = NULL, ylabs = NULL, main = NULL,
27442 ripley 20
         col.term = 2, lwd.term = 1.5,
21
         col.se = "orange", lty.se = 2, lwd.se = 1,
22
         col.res = "gray", cex = 1, pch = par("pch"),
36816 ripley 23
         col.smth = "darkred", lty.smth = 2, span.smth = 2/3,
42103 ripley 24
         ask = dev.interactive() && nb.fig < n.tms,
44243 ripley 25
         use.factor.levels = TRUE, smooth = NULL, ylim = "common",
60976 ripley 26
         plot = TRUE, \dots)
27442 ripley 27
}
28
\arguments{
29
  \item{model}{fitted model object}
30
  \item{data}{data frame in which variables in \code{model} can be
31
    found}
32
  \item{envir}{environment in which variables in \code{model} can be found}
33
  \item{partial.resid}{logical; should partial residuals be plotted?}
34
  \item{rug}{add \link{rug}plots (jittered 1-d histograms) to the axes?}
59337 maechler 35
  \item{terms}{which terms to plot (default \code{NULL} means all
36
    terms); a \code{\link{character}} vector, passed to
60976 ripley 37
    \code{\link{predict}(.., term = "terms", terms = *)}.}
27442 ripley 38
  \item{se}{plot pointwise standard errors?}
39
  \item{xlabs}{vector of labels for the x axes}
40
  \item{ylabs}{vector of labels for the y axes}
41
  \item{main}{logical, or vector of main titles;  if \code{TRUE}, the
42
    model's call is taken as main title, \code{NULL} or \code{FALSE} mean
43
    no titles.}
42961 ripley 44
  \item{col.term, lwd.term}{color and line width for the \sQuote{term curve},
27442 ripley 45
    see \code{\link{lines}}.}
46
  \item{col.se, lty.se, lwd.se}{color, line type and line width for the
42961 ripley 47
    \sQuote{twice-standard-error curve} when \code{se = TRUE}.}
27442 ripley 48
  \item{col.res, cex, pch}{color, plotting character expansion and type
49
    for partial residuals, when \code{partial.resid = TRUE}, see
50
    \code{\link{points}}.}
51
   \item{ask}{logical; if \code{TRUE}, the user is \emph{ask}ed before
52
     each plot, see \code{\link{par}(ask=.)}.}
53
   \item{use.factor.levels}{Should x-axis ticks use factor levels or
54
     numbers for factor terms?}
28044 tlumley 55
   \item{smooth}{\code{NULL} or a function with the same arguments as
56
     \code{\link{panel.smooth}} to draw a smooth through the partial
57
     residuals for non-factor terms}
42103 ripley 58
   \item{lty.smth, col.smth, span.smth}{Passed to \code{smooth}}
59
   \item{ylim}{an optional range for the y axis, or \code{"common"} when
60
     a range sufficient for all the plot will be computed, or
61
     \code{"free"} when limits are computed for each plot.}
60976 ripley 62
   \item{plot}{if set to \code{FALSE} plots are not produced: instead a
63
     list is returned containing the data that would have been plotted.}
42103 ripley 64
   \item{\dots}{other graphical parameters.}
27442 ripley 65
}
66
\details{
60976 ripley 67
  The \code{model} object must have a \code{predict} method that accepts
68
  \code{type = terms}, e.g., \code{\link{glm}} in the \pkg{stats} package,
27442 ripley 69
  \code{\link[survival]{coxph}} and \code{\link[survival]{survreg}} in
57939 hornik 70
  the \CRANpkg{survival} package.
27442 ripley 71
 
60976 ripley 72
  For the \code{partial.resid = TRUE} option \code{model} must have a
73
  \code{\link{residuals}} method that accepts \code{type = "partial"},
27442 ripley 74
  which \code{\link{lm}} and \code{\link{glm}} do.
75
 
76
  The \code{data} argument should rarely be needed, but in some cases
28728 tlumley 77
  \code{termplot} may be unable to reconstruct the original data
78
  frame. Using \code{na.action=na.exclude} makes these problems less likely.
27442 ripley 79
 
59629 ripley 80
  Nothing sensible happens for interaction terms, and they may cause errors.
61433 ripley 81
 
60976 ripley 82
  The \code{plot = FALSE} option is useful when some special action is needed,
83
  e.g. to overlay the results of two different models or to plot
84
  confidence bands.
27442 ripley 85
}
60976 ripley 86
\value{
87
  For \code{plot = FALSE}, a list with one element for each plot which
88
  would have been produced.  Each element of the list is a data frame
89
  with variables \code{x}, \code{y}, and optionally the pointwise
90
  standard errors \code{se}. For continuous predictors \code{x} will
91
  contain the ordered unique values and for a factor it will be a factor
92
  containing one instance of each level.  The list has attribute
93
  \code{"constant"} copied from the predicted terms object.
94
 
95
  Otherwise, the number of terms, invisibly.
96
}
27442 ripley 97
\seealso{For (generalized) linear models, \code{\link{plot.lm}} and
98
  \code{\link{predict.glm}}.}
99
\examples{
41508 ripley 100
require(graphics)
101
 
27442 ripley 102
had.splines <- "package:splines" \%in\% search()
103
if(!had.splines) rs <- require(splines)
104
x <- 1:100
61168 ripley 105
z <- factor(rep(LETTERS[1:4], 25))
42103 ripley 106
y <- rnorm(100, sin(x/10)+as.numeric(z))
61168 ripley 107
model <- glm(y ~ ns(x, 6) + z)
27442 ripley 108
 
61160 ripley 109
par(mfrow = c(2,2)) ## 2 x 2 plots for same model :
27442 ripley 110
termplot(model, main = paste("termplot( ", deparse(model$call)," ...)"))
61160 ripley 111
termplot(model, rug = TRUE)
112
termplot(model, partial.resid = TRUE, se = TRUE, main = TRUE)
113
termplot(model, partial.resid = TRUE, smooth = panel.smooth, span.smth = 1/4)
27442 ripley 114
if(!had.splines && rs) detach("package:splines")
115
}
116
\keyword{hplot}
117
\keyword{regression}