The R Project SVN R

Rev

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