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