| plot.effects {effects} | R Documentation |
Plots of Effects and Predictor Effects
Description
plot methods for predictoreff, predictorefflist, eff, efflist and effpoly objects created by calls other methods in the effects package. The plot arguments were substantially changed in mid-2017. For more details and many examples, see the Predictor Effects Graphics Gallery vignette.
Usage
## S3 method for class 'eff'
plot(x, x.var,
main=paste(effect, "effect plot"),
symbols=TRUE, lines=TRUE, axes, confint,
partial.residuals, id, lattice, ...,
# legacy arguments:
multiline, z.var, rug, xlab, ylab, colors, cex, lty, lwd,
ylim, xlim, factor.names, ci.style,
band.transparency, band.colors, type, ticks,
alternating, rotx, roty, grid, layout,
rescale.axis, transform.x, ticks.x, show.strip.values,
key.args, use.splines,
residuals.color, residuals.pch, residuals.cex, smooth.residuals,
residuals.smooth.color, show.fitted, span)
## S3 method for class 'efflist'
plot(x, selection, rows, cols, ask=FALSE, graphics=TRUE, lattice, ...)
## S3 method for class 'predictoreff'
plot(x, x.var,
main = paste(names(x$variables)[1], "predictor effect plot"), ...)
## S3 method for class 'predictorefflist'
plot(x, selection, rows, cols, ask = FALSE,
graphics = TRUE, lattice, ...)
## S3 method for class 'effpoly'
plot(x, x.var=which.max(levels),
main=paste(effect, "effect plot"),
symbols=TRUE, lines=TRUE, axes, confint, lattice, ...,
# legacy arguments:
type, multiline, rug, xlab, ylab, colors, cex, lty, lwd,
factor.names, show.strip.values,
ci.style, band.colors, band.transparency, style,
transform.x, ticks.x, xlim,
ticks, ylim, rotx, roty, alternating, grid,
layout, key.args, use.splines)
## S3 method for class 'mlm.efflist'
plot(x, ...)
Arguments
x |
an object of class |
x.var |
the index (number) or quoted name of the covariate or factor to place on the horizontal axis of each panel of the effect plot. The default is the predictor with the largest number of levels or values. This argument is ignored with |
main |
the title for the plot, printed at the top; the default title is constructed from the name of the effect. |
symbols |
|
lines |
|
axes |
an optional list of specifications for the x and y axes; if not given, axis properties take generally reasonable default values. See Details for more information. |
confint |
an optional list of specifications for plotting confidence regions and intervals; if not given, generally reasonable default values are used. See Detailed Argument Descriptions under Details for more information. |
partial.residuals |
an optional list of specifications for plotting partial residuals for linear and generalized linear models; if not given, generally reasonable default values are used. See Detailed Argument Descriptions under Details for more information, along with the Effect Displays with Partial Residuals vignette. |
id |
an optional list of specifications for identifying points when partial residuals are plotted; if not specified, no points are labelled. See Detailed Argument Descriptions under Details for more information. |
lattice |
an optional list of specifications for various lattice properties, such as legend placement; if not given, generally reasonable default values are used. See Detailed Argument Descriptions under Details for more information. |
selection |
the optional index (number) or quoted name of the effect in an efflist object to be plotted; if not supplied, a menu of high-order terms is presented or all effects are plotted. |
rows, cols |
Number of rows and columns in the “meta-array” of plots produced for an |
ask |
if |
graphics |
if |
... |
arguments to be passed down. For |
multiline, z.var, rug, xlab, ylab, colors, cex, lty, lwd, ylim, xlim, factor.names, ci.style, band.transparency, band.colors, ticks, alternating, rotx, roty, grid, layout, rescale.axis, transform.x, ticks.x, show.strip.values, key.args, use.splines, type, residuals.color, residuals.pch, residuals.cex, smooth.residuals, residuals.smooth.color, show.fitted, span, style |
legacy arguments retained for backwards compatibility; if specified, these will take precedence over the newer list-style arguments described above. See |
Details
Effects plots and predictor effects plots are produced by these methods. The plots are highly customizable using the optional arguments described here. For example, effects in a GLM are plotted on the scale of the linear predictor, but the vertical axis is labelled on the response scale. This preserves the linear structure of the model while permitting interpretation on what is usually a more familiar scale. This approach may also be used with linear models, for example to display effects on the scale of the response even if the data are analyzed on a transformed scale, such as log or square-root. See the axes argument details below to change the scale to response scale, or to linear predictor scale with tick marks labeled in response scale.
When a factor is on the x-axis, the plot method for eff objects connects the points representing the effect by line segments, creating a response “profile.” If you wish to suppress these lines, add lty=0 to the lines argument to the call to plot (see below and the examples).
In a polytomous multinomial or proportional-odds logit model, by default effects are plotted on the probability scale; they may alternatively be plotted on the scale of the individual-level logits.
All of the arguments to plot objects created by Effect or allEffects can also be used with objects created by predictorEffect or predictorEffects.
Detailed Argument Descriptions
For more information about these arguments and many examples, see the Predictor Effects Graphics Gallery vignette.
Maximizing the flexibility of these plot commands requires inclusion of a myriad of options. In an attempt to simplify the use of these options, they have been organized into just a few arguments that each accept a list of specifications as an argument. In a few cases the named entries in the list are themselves lists.
Each of the following arguments takes an optional list of specifications; any specification absent from the list
assumes its default value. Some of the list elements are themselves lists, so in complex cases, the argument can take
the form of nested lists. All of these arguments can also be used on objects created with predictorEffects.
symbolsTRUE,FALSE, or a list of options that controls the plotting symbols and their sizes for use with factors; ifFALSEsymbols are suppressed; ifTRUEdefault values are used:pchploting symbols, a vector of plotting characters, with the default taken from
trellis.par.get("superpose.symbol")$pch, typically a vector of 1s (circles).cexplotting character sizes, a vector of values, with the default taken from
trellis.par.get("superpose.symbol")$cex, typically a vector of 0.8s.
linesTRUE,FALSE, or a list that controls the characteristics of lines drawn on a plot, and also whether or not multiple lines should be drawn in the same panel in the plot; ifFALSElines are suppressed; ifTRUEdefault values are used:multilinedisplay a multiline plot in each panel; the default is
TRUEif there are no standard errors in the"eff"object,FALSEotherwise. For an"effpoly"objectmultline=TRUEcauses all of the response levels to be shown in the same panel rather than in separate panels.- z.var
for linear, generalized linear or mixed models, the index (number) or quoted name of the covariate or factor for which individual lines are to be drawn in each panel of the effect plot. The default is the predictor with the smallest number of levels or values. This argument is only used for multipline plots.
ltyvector of line types, with the default taken from
trellis.par.get("superpose.line")$lty, typically a vector of 1s (solid lines).lwdvector of line widths, with the default taken from
trellis.par.get("superpose.line")$lwd, typically a vector with 2 in the first position followed by 1s.cola vector of line colors, with the default taken from from
trellis.par.get("superpose.line")$col, used both for lines and for areas in stacked area plots for"effpoly"objects; in the latter case, the default colors for an ordered response are instead generated bysequential_hclin the colorspace package.splinesuse splines to smooth plotted effect lines; the default is
TRUE.
axesa list with elements
x,y,alternating, andgridthat control axis limits, ticks, and labels. Thexandyelements may themselves be lists.The
xentry is a list with elements named for predictors, with each predictor element itself a list with the following elements:labaxis label, defaults to the name of the predictor; may either be a text string or a list with the text label (optionally named
label) as its first element and the named elementcexas its second element.lima two-element vector giving the axis limits, with the default determined from the data.
ticksa list with either element
at, a vector specifying locations for the ticks marks, orn, the number of tick marks.transformtransformations to be applied to the horizontal axis of a numeric predictor, in the form of a list of two functions, with element names
transandinverse. Thetransfunction is applied to the values of the predictor, andinverseis used for computing proper axis tick labels. The default is not to transform the predictor axis.
Two additional elements may appear in the
xlist, and apply to all predictors:rotateangle in degrees to rotate tick labels; the default is 0.
rugdisplay a rug plot showing the marginal distribution of a numeric predictor; the default is
TRUE.
The
ylist containslab,lim,ticks, androtateelements (similar to those specified for individual predictors in thexlist), along with the additionaltype,transform, andstyleelements:typefor plotting linear or generalized linear models,
"rescale"(the default) plots the vertical axis on the link scale (e.g., the logit scale for a logit model) but labels the axis on the response scale (e.g., the probability scale for a logit model);"response"plots and labels the vertical axis on the scale of the response (e.g., the probability scale for a logit model); and"link"plots and labels the vertical axis on the scale of the link (e.g., the logit scale for a logit model). For polytomous logit models, this element is either"probability"or"logit", with the former as the default.transformprimarily for linear or linear mixed models, this argument is used to apply an arbitrary transformation to the vertical axis. For example, if fitting a linear model with response
log(y), then settingtransform=expwould plotexp(log(y)) = yon the vertical axis. If the response were1/y, then usetransform=function(yt) 1/yt, since the reciprocal is its own inverse. Thetransformargument can also be a list of two functions. For example with a responselog(y), the specificationtransform=list(trans=log, inverse=log), type="rescale"will plot in log-scale, but will label tick marks in arithmetic scale; see the example below. The specificationtransform=list(trans=log, inverse=exp), type="response"is equivalent totransform=exp. Whentype="response"thelabargument will geneally be used to get a label for the axis that matches the untransformed response. If this argument is used with a generalized linear model or another model with a non-identity link function, the function is applied to the linear predictor, and will probably not be of interest.stylefor polytomous logit models, this element can take on the value
"lines"(the default) or"stacked"for line plots or stacked-area plots, respectively.
Other elements:
alternatingif
TRUE(the default), the tick labels alternate by panels in multi-panel displays from left to right and top to bottom; ifFALSE, tick labels appear at the bottom and on the left.gridif
TRUE(the default isFALSE), add grid lines to the plot.
confintspecifications to add/remove confidence intervals or regions from a plot, and to set the nominal confidence level.
styleone of
"auto","bars","lines","bands", and"none"; the default is"bars"for factors,"bands"for numeric predictors, and"none"for multiline plots;"auto"also produces"bars"for factors and"bands"for numeric predictors, even in multiline plots.alphatransparency of confidence bands; the default is 0.15.
colcolors; the default is taken from the line colors.
partial.residualsspecifications concerning the addition of partial residuals to the plot.
plotdisplay the partial residuals; the default is
TRUEif residuals are present in the"eff"object,FALSEotherwise.fittedshow fitted values as well as residuals; the default is
FALSE.colcolor for partial residuals; the default is the second line color.
pchplotting symbols for partial residuals; the default is 1, a circle.
cexsize of symbols for partial residuals; the default is 1.
smoothdraw a loess smooth of the partial residuals; the default is
TRUE.spanspan for the loess smooth; the default is 2/3.
smooth.colcolor for the loess smooth; the default is the second line color.
ltyline type for the loess smooth; the default is the first line type, normally 1 (a solid line).
lwdline width for the loess smooth; the default is the first line width, normally 2.
idspecifications for optional point identification when partial residuals are plotted.
nnumber of points to identify; default is
2ifid=TRUEand0ifid=FALSE. Points are selected based on the Mahalanobis distances of the pairs of x-values and partial residuals from their centroid.colcolor for the point labels; default is the same as the color of the partial residuals.
cexrelative size of text for point labels; default is
0.75.labelsvector of point labels; the default is the names of the residual vector, which is typically the row names of the data frame to which the model is fit.
latticethe plots are drawn with the lattice package, generally by the
xyplotfunction. These specifications are passed as arguments to the functions that actually draw the plots.layoutthe
layoutargument to the lattice functionxyplot(or, in some casesdensityplot), which is used to draw the effect display; if not specified, the plot will be formatted so that it appears on a single page.key.argsa key, or legend, is added to the plot if
multiline=TRUE. This argument is a list with components that determine the the placement and other characteristics of the key. The default if not set by the user iskey.args = list(space="top", columns=2, border=FALSE, fontfamily="serif", cex.title=.80, cex=0.75). If there are more than 6 groups in the plot,columnsis set to 3. For stacked-area plots, the default is a one-column key. In addition to the arguments shown explicitly below, any of the arguments listed in thexyplotdocumentation in thekeysection can be used.spacedetermines the placement of the key outside the plotting area, with default
space="above"for above the plot and below its title. Settingspace="right"uses space to the right of the plot for the key.x, y, cornerused to put the key on the graph itself. For example,
x=.05, y=.95, corner=c(0,1)will locate the upper-left corner of the key at (.05, .95), thinking of the graph as a unit square.columnsnumber of columns in the key. If
space="top", columns should be 2, 3 or 4; ifspace="right", setcolumns=1.borderif
TRUEdraw a border around the key; omit the border ifFALSE.fontfamilythe default is
"sans"for the sans-serif font used in the rest of the plot; the alternative is"serif"for a serif font.cex, cex.titlethe default relative size of the font for labels and the title, respectively. To save space set these to be smaller than 1.
stripa list with three elements:
factor.names, which ifTRUE, the default, shows conditioning variable names in the panel headers;values, which ifTRUE, the default unless partial residuals are plotted, displays conditioning variable values in the panel headers, andcex, the relative size of the text displayed in the strip.arraya list with elements
row,col,nrow,ncol, andmore, used to graph an effect as part of an array of plots;row,col,nrow, andncolare used to compose thesplitargument andmorethemoreargument toprint.trellis. Thearrayargument is automatically set byplot.efflistand will be ignored if used with that function.
Value
The summary method for "eff" objects returns a "summary.eff" object with the following components
(those pertaining to confidence limits need not be present):
header |
a character string to label the effect. |
effect |
an array containing the estimated effect. |
lower.header |
a character string to label the lower confidence limits. |
lower |
an array containing the lower confidence limits. |
upper.header |
a character string to label the upper confidence limits. |
upper |
an array containing the upper confidence limits. |
The plot method for "eff" objects returns a "plot.eff" object (an enhanced "trellis" object); the provided
print method plots the object.
The [ method for "efflist" objects is used to subset an "efflist" object and returns an object of the same class.
Author(s)
John Fox jfox@mcmaster.ca and Jangman Hong.
See Also
LegacyArguments, effect, allEffects, effectsTheme,
xyplot, densityplot, print.trellis, loess,
sequential_hcl,
and the Predictor Effects Graphics Gallery and Effect Displays with Partial Residuals vignettes.
Examples
# also see examples in ?effect
# plot predictorEffects
mod <- lm(prestige ~ education + log(income)*type + women, Prestige)
plot(predictorEffects(mod, ~ income), axes=list(grid=TRUE))
plot(predictorEffects(mod, ~ income), lines=list(multiline=TRUE),
axes=list(grid=TRUE))
plot(predictorEffects(mod, ~ type), lines=list(multiline=TRUE),
axes=list(grid=TRUE),
confint=list(style="bars"))
mod.cowles <- glm(volunteer ~ sex + neuroticism*extraversion,
data=Cowles, family=binomial)
eff.cowles <- allEffects(mod.cowles, xlevels=list(extraversion=seq(0, 24, 6)))
eff.cowles
as.data.frame(eff.cowles[[2]]) # neuroticism*extraversion interaction
plot(eff.cowles, 'sex', axes=list(grid=TRUE,
y=list(lab="Prob(Volunteer)"),
x=list(rotate=90)),
lines=list(lty=0))
plot(eff.cowles, 'neuroticism:extraversion',
axes=list(y=list(lab="Prob(Volunteer)",
ticks=list(at=c(.1,.25,.5,.75,.9)))))
plot(Effect(c("neuroticism", "extraversion"), mod.cowles,
se=list(type="Scheffe"),
xlevels=list(extraversion=seq(0, 24, 6))),
axes=list(y=list(lab="Prob(Volunteer)",
ticks=list(at=c(.1,.25,.5,.75,.9)))))
# change color of the confidence bands to 'black' with .15 transparency
plot(eff.cowles, 'neuroticism:extraversion',
axes=list(y=list(lab="Prob(Volunteer)",
ticks=list(at=c(.1,.25,.5,.75,.9)))),
confint=list(col="red", alpha=.3))
plot(eff.cowles, 'neuroticism:extraversion',
lines=list(multiline=TRUE),
axes=list(y=list(lab="Prob(Volunteer)")),
lattice=list(key.args = list(x = 0.65, y = 0.99, corner = c(0, 1))))
# use probability scale in place of logit scale, all lines are black.
plot(eff.cowles, 'neuroticism:extraversion',
lines=list(multiline=TRUE, lty=1:8, col="black"),
axes=list(y=list(type="response", lab="Prob(Volunteer)")),
lattice=list(key.args = list(x = 0.65, y = 0.99, corner = c(0, 1))),
confint=list(style="bands"))
plot(effect('sex:neuroticism:extraversion', mod.cowles,
xlevels=list(extraversion=seq(0, 24, 6))),
lines=list(multiline=TRUE))
plot(effect('sex:neuroticism:extraversion', mod.cowles,
xlevels=list(extraversion=seq(0, 24, 6))),
lines=list(multiline=TRUE),
axes=list(y=list(type="response")),
confint=list(style="bands"),
lattice=list(key.args = list(x=0.75, y=0.75, corner=c(0, 0))))
if (require(nnet)){
mod.beps <- multinom(vote ~ age + gender + economic.cond.national +
economic.cond.household + Blair + Hague + Kennedy +
Europe*political.knowledge, data=BEPS)
plot(effect("Europe*political.knowledge", mod.beps,
xlevels=list(political.knowledge=0:3)))
plot(effect("Europe*political.knowledge", mod.beps,
xlevels=list(political.knowledge=0:3),
fixed.predictors=list(given.values=c(gendermale=0.5))),
axes=list(y=list(style="stacked"), x=list(rug=FALSE), grid=TRUE),
lines=list(col=c("blue", "red", "orange")))
}
if (require(MASS)){
mod.wvs <- polr(poverty ~ gender + religion + degree + country*poly(age,3),
data=WVS)
plot(effect("country*poly(age, 3)", mod.wvs))
plot(effect("country*poly(age, 3)", mod.wvs), lines=list(multiline=TRUE))
plot(effect("country*poly(age, 3)", mod.wvs),
axes=list(y=list(style="stacked")),
lines=list(col=c("gray75", "gray50", "gray25")))
plot(effect("country*poly(age, 3)", latent=TRUE, mod.wvs))
}
mod.pres <- lm(prestige ~ log(income, 10) + poly(education, 3) + poly(women, 2),
data=Prestige)
eff.pres <- allEffects(mod.pres)
plot(eff.pres)
plot(eff.pres[1:2])
plot(eff.pres[1],
axes=list(x=list(income=list(transform=list(
trans=log10, inverse=function(x) 10^x),
ticks=list(at=c(1000, 2000, 5000, 10000, 20000))))))
mod <- lm(log(prestige) ~ income:type + education, data=Prestige)
p1 <- predictorEffects(mod, ~ income)
# log-scale for response
plot(p1, lines=list(multiline=TRUE))
# log-scale, with arithmetic tick marks
plot(p1, lines=list(multiline=TRUE),
axes=list(y=list(transform=list(trans=log, inverse = exp),
lab="prestige", type="rescale")))
# arithmetic scale and tick marks, with other arguments
plot(p1, lines=list(multiline=TRUE), grid=TRUE,
lattice=list(key.args=list(space="right", border=TRUE)),
axes=list(y=list(transform=exp, lab="prestige")))